-
Notifications
You must be signed in to change notification settings - Fork 0
/
CachedUserServiceTests,cs
104 lines (87 loc) · 4.07 KB
/
CachedUserServiceTests,cs
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using NUnit.Framework;
using Moq;
using InMemoryCachingSample.Services;
using InMemoryCachingSample.Infrastructure;
using InMemoryCachingSample.Models;
using InMemoryCachingSample.Utils;
using System.Collections.Generic;
using System.Threading.Tasks;
using System;
namespace InMemoryCachingSample.Tests
{
[TestFixture]
public class CachedUserServiceTests
{
private CachedUserService _cachedUserService;
private Mock<UsersService> _usersServiceMock;
private Mock<ICacheProvider> _cacheProviderMock;
[SetUp]
public void Setup()
{
// Create mock objects
_usersServiceMock = new Mock<UsersService>();
_cacheProviderMock = new Mock<ICacheProvider>();
// Instantiate the CachedUserService with mock dependencies
_cachedUserService = new CachedUserService(_usersServiceMock.Object, _cacheProviderMock.Object);
}
[Test]
public async Task GetUsers_ReturnsCachedUsers_WhenUsersAreCached()
{
// Arrange
var cachedUsers = new List<User> { new User { Id = 1, Name = "John Doe" } };
_cacheProviderMock.Setup(cp => cp.GetFromCache<IEnumerable<User>>(CacheKeys.Users)).Returns(cachedUsers);
// Act
var result = await _cachedUserService.GetUsers();
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(cachedUsers, result);
_usersServiceMock.Verify(us => us.GetUsersAsync(), Times.Never);
}
[Test]
public async Task GetUsers_CallsUsersServiceAndCachesResult_WhenUsersAreNotCached()
{
// Arrange
List<User> cachedUsers = null;
var usersFromService = new List<User> { new User { Id = 1, Name = "Jane Doe" } };
_cacheProviderMock.Setup(cp => cp.GetFromCache<IEnumerable<User>>(CacheKeys.Users)).Returns(cachedUsers);
_usersServiceMock.Setup(us => us.GetUsersAsync()).ReturnsAsync(usersFromService);
// Act
var result = await _cachedUserService.GetUsers();
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(usersFromService, result);
_usersServiceMock.Verify(us => us.GetUsersAsync(), Times.Once);
_cacheProviderMock.Verify(cp => cp.SetCache(CacheKeys.Users, usersFromService, It.IsAny<DateTimeOffset>()), Times.Once);
}
[Test]
public async Task GetUsersAsync_UsesSemaphoreAndCachesResult_WhenCacheIsEmpty()
{
// Arrange
List<User> cachedUsers = null;
var usersFromService = new List<User> { new User { Id = 2, Name = "Alice Doe" } };
_cacheProviderMock.Setup(cp => cp.GetFromCache<IEnumerable<User>>(CacheKeys.Users)).Returns(cachedUsers);
_usersServiceMock.Setup(us => us.GetUsersAsync()).ReturnsAsync(usersFromService);
// Act
var result = await _cachedUserService.GetUsersAsync();
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(usersFromService, result);
_usersServiceMock.Verify(us => us.GetUsersAsync(), Times.Once);
_cacheProviderMock.Verify(cp => cp.SetCache(CacheKeys.Users, usersFromService, It.IsAny<DateTimeOffset>()), Times.Once);
}
[Test]
public async Task GetUsersAsync_ReturnsCachedUsersWithoutEnteringSemaphore_WhenUsersAreCached()
{
// Arrange
var cachedUsers = new List<User> { new User { Id = 3, Name = "Bob Doe" } };
_cacheProviderMock.Setup(cp => cp.GetFromCache<IEnumerable<User>>(CacheKeys.Users)).Returns(cachedUsers);
// Act
var result = await _cachedUserService.GetUsersAsync();
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(cachedUsers, result);
_usersServiceMock.Verify(us => us.GetUsersAsync(), Times.Never);
_cacheProviderMock.Verify(cp => cp.SetCache(It.IsAny<string>(), It.IsAny<IEnumerable<User>>(), It.IsAny<DateTimeOffset>()), Times.Never);
}
}
}