-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCacheKeyFactoryTests.cs
More file actions
84 lines (71 loc) · 3.24 KB
/
CacheKeyFactoryTests.cs
File metadata and controls
84 lines (71 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using ByteSync.ServerCommon.Business.Settings;
using ByteSync.ServerCommon.Entities;
using ByteSync.ServerCommon.Factories;
using FakeItEasy;
using FluentAssertions;
using Microsoft.Extensions.Options;
namespace ByteSync.ServerCommon.Tests.Factories;
public class CacheKeyFactoryTests
{
private CacheKeyFactory _cacheKeyFactory;
private IOptions<RedisSettings> _redisSettings;
private const string TestPrefix = "test-prefix";
[SetUp]
public void Setup()
{
_redisSettings = A.Fake<IOptions<RedisSettings>>();
A.CallTo(() => _redisSettings.Value).Returns(new RedisSettings { Prefix = TestPrefix });
_cacheKeyFactory = new CacheKeyFactory(_redisSettings);
}
[Test]
[TestCase(EntityType.Session, "session123", "Session")]
[TestCase(EntityType.Inventory, "inv456", "Inventory")]
[TestCase(EntityType.Synchronization, "sync789", "Synchronization")]
[TestCase(EntityType.SharedFile, "shared123", "SharedFile")]
[TestCase(EntityType.SessionSharedFiles, "sessionFiles123", "SessionSharedFiles")]
[TestCase(EntityType.TrackingAction, "action123", "TrackingAction")]
[TestCase(EntityType.Client, "client123", "Client")]
[TestCase(EntityType.ClientSoftwareVersionSettings, "version123", "ClientSoftwareVersionSettings")]
[TestCase(EntityType.CloudSessionProfile, "profile123", "CloudSessionProfile")]
[TestCase(EntityType.Lobby, "lobby123", "Lobby")]
[TestCase(EntityType.MessageDefinition, "msg123", "MessageDefinition")]
public void Create_ShouldGenerateCacheKey_WithCorrectFormat(EntityType entityType, string entityId, string expectedEntityTypeName)
{
// Arrange
var expectedCacheKeyValue = $"{TestPrefix}:{expectedEntityTypeName}:{entityId}";
// Act
var result = _cacheKeyFactory.Create(entityType, entityId);
// Assert
result.Should().NotBeNull();
result.EntityType.Should().Be(entityType);
result.EntityId.Should().Be(entityId);
result.Value.Should().Be(expectedCacheKeyValue);
}
[Test]
public void Create_WithDifferentPrefix_ShouldUseConfiguredPrefix()
{
// Arrange
const string customPrefix = "custom-prefix";
var customRedisSettings = A.Fake<IOptions<RedisSettings>>();
A.CallTo(() => customRedisSettings.Value).Returns(new RedisSettings { Prefix = customPrefix });
var factory = new CacheKeyFactory(customRedisSettings);
const string entityId = "123";
const EntityType entityType = EntityType.Session;
// Act
var result = factory.Create(entityType, entityId);
// Assert
result.Value.Should().StartWith(customPrefix);
result.Value.Should().Be($"{customPrefix}:Session:{entityId}");
}
[Test]
public void Create_WithInvalidEntityType_ShouldThrowArgumentOutOfRangeException()
{
// Arrange
const string entityId = "123";
const EntityType invalidEntityType = (EntityType)999;
// Act & Assert
FluentActions.Invoking(() => _cacheKeyFactory.Create(invalidEntityType, entityId))
.Should().Throw<ArgumentOutOfRangeException>()
.WithParameterName("entityType");
}
}