School Commit Init

This commit is contained in:
2024-08-31 12:07:21 +03:00
commit 0b130ee18c
2801 changed files with 4720552 additions and 0 deletions
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CacheModule\CacheModule.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,134 @@
using Xunit;
using CacheModule;
using Newtonsoft.Json;
namespace CacheModuleTest{
public class CacheServiceIntegratedTest
{
private class MockIntegration{
private CacheService _cacheService;
public Dictionary<string, List<int>> keyValuePairs = new Dictionary<string, List<int>>(){
{"first", new List<int>(){1,2,3}},
{"second", new List<int>(){4,5,6}},
{"third", new List<int>(){7,8,9}}
};
public MockIntegration(){
_cacheService = CacheService.Instance;
}
public int Get(string key, int index){
return keyValuePairs[key][index];
}
public int GetSum(string key){
return keyValuePairs[key].Sum();
}
public int CachedGet(string key, int index){
var json = JsonConvert.SerializeObject(new List<object>(){key, index}) ?? string.Empty;
var cachedObject = _cacheService.Get(nameof(Get), json);
if(cachedObject != null){
return (int)cachedObject;
}
var result = Get(key, index);
_cacheService.Add(nameof(Get), json, result, Duration.OneMinute);
return result;
}
public int CachedGetSum(string key){
var json = JsonConvert.SerializeObject(new List<object>(){key}) ?? string.Empty;
var cachedObject = _cacheService.Get(nameof(GetSum), json);
if(cachedObject != null){
return (int)cachedObject;
}
var result = GetSum(key);
_cacheService.Add(nameof(GetSum), json, result, Duration.OneMinute);
return result;
}
public void Clear(){
_cacheService.Clear();
}
}
[Fact]
public void CachedGet_WhenCalledWithinDuration_ReturnsCachedValue()
{
// Arrange
var mockIntegration = new MockIntegration();
mockIntegration.Clear();
var key = "first";
var index = 1;
var expected = mockIntegration.Get(key, index);
var cachedExpected = mockIntegration.CachedGet(key, index);
mockIntegration.keyValuePairs[key][index] = 100;
// Act
var cachedResult = mockIntegration.CachedGet(key, index);
// Assert
Assert.Equal(expected, cachedResult);
Assert.Equal(cachedExpected, cachedResult);
}
[Fact]
public void CachedGet_WhenCalledAfterDuration_ReturnsNewValue()
{
// Arrange
var mockIntegration = new MockIntegration();
mockIntegration.Clear();
var key = "first";
var index = 1;
var expected = mockIntegration.Get(key, index);
var cachedExpected = mockIntegration.CachedGet(key, index);
mockIntegration.keyValuePairs[key][index] = 100;
// Act
System.Threading.Thread.Sleep(61000);
var cachedResult = mockIntegration.CachedGet(key, index);
// Assert
Assert.Equal(100, cachedResult);
Assert.NotEqual(cachedExpected, cachedResult);
}
[Fact]
public void CachedGetSum_WhenCalledWithinDuration_ReturnsCachedValue()
{
// Arrange
var mockIntegration = new MockIntegration();
mockIntegration.Clear();
var key = "first";
var expected = mockIntegration.GetSum(key);
var cachedExpected = mockIntegration.CachedGetSum(key);
mockIntegration.keyValuePairs[key] = new List<int>(){100, 100, 100};
// Act
var cachedResult = mockIntegration.CachedGetSum(key);
// Assert
Assert.Equal(expected, cachedResult);
Assert.Equal(cachedExpected, cachedResult);
}
[Fact]
public void CachedGetSum_WhenCalledAfterDuration_ReturnsNewValue()
{
// Arrange
var mockIntegration = new MockIntegration();
mockIntegration.Clear();
var key = "first";
var expected = mockIntegration.GetSum(key);
var cachedExpected = mockIntegration.CachedGetSum(key);
mockIntegration.keyValuePairs[key] = new List<int>(){100, 100, 100};
// Act
System.Threading.Thread.Sleep(61000);
var cachedResult = mockIntegration.CachedGetSum(key);
// Assert
Assert.Equal(300, cachedResult);
Assert.NotEqual(cachedExpected, cachedResult);
}
}
}
@@ -0,0 +1,190 @@
using Xunit;
using CacheModule;
namespace CacheModuleTest{
public class CacheServiceUnitTest
{
[Fact]
public void Instance_WhenCalled_ReturnsSameInstance()
{
// Arrange
var cacheService1 = CacheService.Instance;
var cacheService2 = CacheService.Instance;
// Act
// Assert
Assert.Same(cacheService1, cacheService2);
}
[Fact]
public void Add_WhenCalledWithinDuration_AddsToCache()
{
// Arrange
var cacheService = CacheService.Instance;
cacheService.Clear();
var functionName = "functionName";
var functionParams = "functionParams";
var value = new object();
// Act
cacheService.Add(functionName, functionParams, value, Duration.OneMinute);
// Assert
Assert.Equal(value, cacheService.Get(functionName, functionParams));
}
[Fact]
public void Add_WhenCalledAfterDuration_RemovesFromCache()
{
// Arrange
var cacheService = CacheService.Instance;
cacheService.Clear();
var functionName = "functionName";
var functionParams = "functionParams";
var value = new object();
// Act
cacheService.Add(functionName, functionParams, value, Duration.OneSecond);
Thread.Sleep(2000);
// Assert
Assert.Null(cacheService.Get(functionName, functionParams));
}
[Fact]
public void Add_WhenCalledWithSameFunctionNameAndFunctionParams_OverwritesCache()
{
// Arrange
var cacheService = CacheService.Instance;
cacheService.Clear();
var functionName = "functionName";
var functionParams = "functionParams";
var value1 = new object();
var value2 = new object();
// Act
cacheService.Add(functionName, functionParams, value1, Duration.OneMinute);
cacheService.Add(functionName, functionParams, value2, Duration.OneMinute);
// Assert
Assert.Equal(value2, cacheService.Get(functionName, functionParams));
}
[Fact]
public void Get_WhenCalledWithNonExistingFunctionName_ReturnsNull()
{
// Arrange
var cacheService = CacheService.Instance;
cacheService.Clear();
var functionName = "functionName";
var functionParams = "functionParams";
// Act
// Assert
Assert.Null(cacheService.Get(functionName, functionParams));
}
[Fact]
public void Get_WhenCalledWithNonExistingFunctionParams_ReturnsNull()
{
// Arrange
var cacheService = CacheService.Instance;
cacheService.Clear();
var functionName = "functionName";
var functionParams = "functionParams";
var value = new object();
// Act
cacheService.Add(functionName, functionParams, value, Duration.OneMinute);
// Assert
Assert.Null(cacheService.Get(functionName, "nonExistingFunctionParams"));
}
[Fact]
public void Clear_WhenCalledWithParams_ClearsCache()
{
// Arrange
var cacheService = CacheService.Instance;
var functionName = "functionName";
var functionParams = "functionParams";
var value = new object();
cacheService.Add(functionName, functionParams, value, Duration.OneMinute);
// Act
cacheService.Clear();
// Assert
Assert.Null(cacheService.Get(functionName, functionParams));
}
[Fact]
public void Clear_WhenCalledWithFunctionName_ClearsCache()
{
// Arrange
var cacheService = CacheService.Instance;
var functionName = "functionName";
var functionParams = "functionParams";
var functionName2 = "functionName2";
var functionParams2 = "functionParams2";
var value = new object();
var value2 = new object();
cacheService.Add(functionName, functionParams, value, Duration.OneMinute);
cacheService.Add(functionName2, functionParams2, value2, Duration.OneMinute);
// Act
cacheService.Clear(functionName);
// Assert
Assert.Null(cacheService.Get(functionName, functionParams));
Assert.Equal(value2, cacheService.Get(functionName2, functionParams2));
}
[Fact]
public void Clear_WhenCalledWithFunctionNameAndFunctionParams_ClearsCache()
{
// Arrange
var cacheService = CacheService.Instance;
var functionName = "functionName";
var functionParams = "functionParams";
var functionParams2 = "functionParams2";
var value = new object();
var value2 = new object();
cacheService.Add(functionName, functionParams, value, Duration.OneMinute);
cacheService.Add(functionName, functionParams2, value2, Duration.OneMinute);
// Act
cacheService.Clear(functionName, functionParams);
// Assert
Assert.Null(cacheService.Get(functionName, functionParams));
Assert.Equal(value2, cacheService.Get(functionName, functionParams2));
}
[Fact]
public void ClearExpired_WhenCalled_ClearsExpiredCache()
{
// Arrange
var cacheService = CacheService.Instance;
var functionName = "functionName";
var functionParams = "functionParams";
var functionName2 = "functionName2";
var functionParams2 = "functionParams2";
var value = new object();
cacheService.Add(functionName, functionParams, value, Duration.OneSecond);
cacheService.Add(functionName2, functionParams2, value, Duration.OneMinute);
Thread.Sleep(2000);
// Act
cacheService.ClearExpired();
// Assert
Assert.Null(cacheService.Get(functionName, functionParams));
Assert.Equal(value, cacheService.Get(functionName2, functionParams2));
}
}
}