Skip to content

Commit

Permalink
Merge branch 'pfh59:main' into lupus
Browse files Browse the repository at this point in the history
  • Loading branch information
rdymade authored Jul 11, 2024
2 parents beb94fe + f2ac65e commit c54729c
Show file tree
Hide file tree
Showing 20 changed files with 697 additions and 898 deletions.
134 changes: 134 additions & 0 deletions src/WHMapper.Tests/Services/EveMapper/EveMapperAccessHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using AutoFixture.Xunit2;
using Moq;
using WHMapper.Models.Db;
using WHMapper.Models.DTO.EveAPI.Character;
using WHMapper.Repositories.WHAccesses;
using WHMapper.Repositories.WHAdmins;
using WHMapper.Services.EveAPI.Characters;
using WHMapper.Services.EveMapper;

namespace WHMapper.Tests.Services.EveMapper
{
public class EveMapperAccessHelperTests
{
#region IsEveMapperUserAccessAuthorized()
[Theory]
[InlineAutoMoqData(1)]
[InlineAutoMoqData(10000)]
[InlineAutoMoqData(int.MinValue)]
[InlineAutoMoqData(int.MaxValue)]
public async Task IfAccessRepositoryIsEmpty_WhenGettingState_ReturnsTrue(
int characterId,
[Frozen] Mock<IWHAccessRepository> accessRepository,
EveMapperAccessHelper sut
)
{
var accessRepositoryReturn = Task.FromResult(new List<WHAccess>() as IEnumerable<WHAccess>);
accessRepository.Setup(x => x.GetAll()).Returns(accessRepositoryReturn!);

Assert.True(await sut.IsEveMapperUserAccessAuthorized(characterId));
}

[Theory, AutoDomainData]
public async Task IfWHAccessExistsAndCharacterIsNull_WhenGettingAccess_ReturnsFalse(
WHAccess wHAccess,
[Frozen] Mock<IWHAccessRepository> accessRepository,
[Frozen] Mock<ICharacterServices> characterServices,
EveMapperAccessHelper sut
)
{
var accessRepositoryReturn = Task.FromResult(new List<WHAccess>() { wHAccess } as IEnumerable<WHAccess>);
accessRepository.Setup(x => x.GetAll()).Returns(accessRepositoryReturn!);

Character? characterReturn = null!;
characterServices.Setup(x => x.GetCharacter(It.IsAny<int>())).Returns(Task.FromResult<Character?>(characterReturn));

Assert.False(await sut.IsEveMapperUserAccessAuthorized(1));
}

[Theory, AutoDomainData]
public async Task IfWHAccessExistsAndCharacterNotExists_WhenGettingAccess_ReturnsFalse(
WHAccess wHAccess,
[Frozen] Mock<IWHAccessRepository> accessRepository,
EveMapperAccessHelper sut
)
{
var accessRepositoryReturn = Task.FromResult(new List<WHAccess>() { wHAccess } as IEnumerable<WHAccess>);
accessRepository.Setup(x => x.GetAll()).Returns(accessRepositoryReturn!);

Assert.False(await sut.IsEveMapperUserAccessAuthorized(1));
}

[Theory, AutoDomainData]
public async Task IfWHAccessExistsAndCharacterExists_WhenGettingAccess_ReturnsTrue(
WHAccess wHAccess,
[Frozen] Mock<IWHAccessRepository> accessRepository,
EveMapperAccessHelper sut
)
{
wHAccess.EveEntityId = 2;
var accessRepositoryReturn = Task.FromResult(new List<WHAccess>() { wHAccess } as IEnumerable<WHAccess>);
accessRepository.Setup(x => x.GetAll()).Returns(accessRepositoryReturn!);

Assert.True(await sut.IsEveMapperUserAccessAuthorized(2));
}
#endregion

#region IsEveMapperAdminAccessAuthorized()
[Theory]
[InlineAutoMoqData(1)]
[InlineAutoMoqData(10000)]
[InlineAutoMoqData(int.MinValue)]
[InlineAutoMoqData(int.MaxValue)]
public async Task IfAccessRepositoryIsEmpty_WhenGettingAdminState_ReturnsTrue(
int characterId,
[Frozen] Mock<IWHAdminRepository> accessRepository,
EveMapperAccessHelper sut
)
{
var accessRepositoryReturn = Task.FromResult(new List<WHAdmin>() as IEnumerable<WHAdmin>);
accessRepository.Setup(x => x.GetAll()).Returns(accessRepositoryReturn!);

Assert.True(await sut.IsEveMapperUserAccessAuthorized(characterId));
}

[Theory]
[InlineAutoMoqData(1)]
[InlineAutoMoqData(10000)]
[InlineAutoMoqData(int.MinValue)]
[InlineAutoMoqData(int.MaxValue)]
public async Task IfAccessRepositoryIsPopulatedGettingUnknownCharacterId_WhenGettingAdminState_ReturnsFalse(
int characterId,
WHAdmin wHAdmin,
[Frozen] Mock<IWHAdminRepository> accessRepository,
EveMapperAccessHelper sut
)
{
wHAdmin.EveCharacterId = 2;
var accessRepositoryReturn = Task.FromResult(new List<WHAdmin>() { wHAdmin } as IEnumerable<WHAdmin>);
accessRepository.Setup(x => x.GetAll()).Returns(accessRepositoryReturn!);

Assert.True(await sut.IsEveMapperUserAccessAuthorized(characterId));
}

[Theory]
[InlineAutoMoqData(1)]
[InlineAutoMoqData(10000)]
[InlineAutoMoqData(int.MinValue)]
[InlineAutoMoqData(int.MaxValue)]
public async Task IfAccessRepositoryIsPopulatedGettingKnownCharacterId_WhenGettingAdminState_ReturnsFalse(
int characterId,
WHAdmin wHAdmin,
[Frozen] Mock<IWHAdminRepository> accessRepository,
EveMapperAccessHelper sut
)
{
wHAdmin.EveCharacterId = characterId;
var accessRepositoryReturn = Task.FromResult(new List<WHAdmin>() { wHAdmin } as IEnumerable<WHAdmin>);
accessRepository.Setup(x => x.GetAll()).Returns(accessRepositoryReturn!);

Assert.True(await sut.IsEveMapperUserAccessAuthorized(characterId));
}
#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using AutoFixture.Xunit2;
using Moq;
using WHMapper.Models.DTO.EveMapper.EveEntity;
using WHMapper.Services.Cache;
using WHMapper.Services.EveMapper;

namespace WHMapper.Tests.Services.EveMapper
{
public class EveMapperCacheServiceTests
{
[Theory]
[InlineAutoMoqData(typeof(AllianceEntity), IEveMapperCacheService.REDIS_ALLIANCE_KEY)]
[InlineAutoMoqData(typeof(CorporationEntity), IEveMapperCacheService.REDIS_COORPORATION_KEY)]
[InlineAutoMoqData(typeof(CharactereEntity), IEveMapperCacheService.REDIS_CHARACTER_KEY)]
[InlineAutoMoqData(typeof(ShipEntity), IEveMapperCacheService.REDIS_SHIP_KEY)]
[InlineAutoMoqData(typeof(SystemEntity), IEveMapperCacheService.REDIS_SYSTEM_KEY)]
[InlineAutoMoqData(typeof(ConstellationEntity), IEveMapperCacheService.REDIS_CONSTELLATION_KEY)]
[InlineAutoMoqData(typeof(RegionEntity), IEveMapperCacheService.REDIS_REGION_KEY)]
[InlineAutoMoqData(typeof(StargateEntity), IEveMapperCacheService.REDIS_STARTGATE_KEY)]
[InlineAutoMoqData(typeof(GroupEntity), IEveMapperCacheService.REDIS_GROUP_KEY)]
[InlineAutoMoqData(typeof(WHEntity), IEveMapperCacheService.REDIS_WORMHOLE_KEY)]
[InlineAutoMoqData(typeof(SunEntity), IEveMapperCacheService.REDIS_SUN_KEY)]
public async Task IfEntityIsMapped_WhenClearingCache_CallsRemoveForCorrectEntity(
Type entityType,
string redisKey,
[Frozen] Mock<ICacheService> cacheService,
EveMapperCacheService sut
)
{
cacheService.Setup(x => x.Remove(It.Is<string>(x => x.Contains(redisKey))))
.Returns(Task.FromResult(true))
.Verifiable();

var sutMethod = sut.GetType().GetMethod("ClearCacheAsync")!.MakeGenericMethod(entityType);

var result = await (Task<bool>)sutMethod.Invoke(sut, null)!;

Assert.True(result);
cacheService.Verify(x => x.Remove(It.Is<string>(x => x.Contains(redisKey))), Times.Once);
cacheService.VerifyNoOtherCalls();
}

[Theory]
[InlineAutoMoqData(typeof(AllianceEntity), IEveMapperCacheService.REDIS_ALLIANCE_KEY)]
[InlineAutoMoqData(typeof(CorporationEntity), IEveMapperCacheService.REDIS_COORPORATION_KEY)]
[InlineAutoMoqData(typeof(CharactereEntity), IEveMapperCacheService.REDIS_CHARACTER_KEY)]
[InlineAutoMoqData(typeof(ShipEntity), IEveMapperCacheService.REDIS_SHIP_KEY)]
[InlineAutoMoqData(typeof(SystemEntity), IEveMapperCacheService.REDIS_SYSTEM_KEY)]
[InlineAutoMoqData(typeof(ConstellationEntity), IEveMapperCacheService.REDIS_CONSTELLATION_KEY)]
[InlineAutoMoqData(typeof(RegionEntity), IEveMapperCacheService.REDIS_REGION_KEY)]
[InlineAutoMoqData(typeof(StargateEntity), IEveMapperCacheService.REDIS_STARTGATE_KEY)]
[InlineAutoMoqData(typeof(GroupEntity), IEveMapperCacheService.REDIS_GROUP_KEY)]
[InlineAutoMoqData(typeof(WHEntity), IEveMapperCacheService.REDIS_WORMHOLE_KEY)]
[InlineAutoMoqData(typeof(SunEntity), IEveMapperCacheService.REDIS_SUN_KEY)]
public async Task IfCacheThrowsException_WhenClearingCache_ReturnsFalse(
Type entityType,
string redisKey,
[Frozen] Mock<ICacheService> cacheService,
EveMapperCacheService sut
)
{
cacheService.Setup(x => x.Remove(It.Is<string>(x => x.Contains(redisKey))))
.Throws<Exception>()
.Verifiable();

var sutMethod = sut.GetType().GetMethod("ClearCacheAsync")!.MakeGenericMethod(entityType);
var result = await (Task<bool>)sutMethod.Invoke(sut, null)!;

Assert.False(result);
cacheService.Verify(x => x.Remove(It.Is<string>(x => x.Contains(redisKey))), Times.Once);
cacheService.VerifyNoOtherCalls();
}

[Theory]
[InlineAutoMoqData(typeof(AEveEntity))]
public async Task IfEntityIsNotMapped_WhenClearingCache_ReturnsFalse(
Type entityType,
[Frozen] Mock<ICacheService> cacheService,
EveMapperCacheService sut
)
{
cacheService.Setup(x => x.Remove(It.IsAny<string>()))
.Returns(Task.FromResult(true))
.Verifiable();

var sutMethod = sut.GetType().GetMethod("ClearCacheAsync")!.MakeGenericMethod(entityType);
var result = await (Task<bool>)sutMethod.Invoke(sut, null)!;

Assert.False(result);
cacheService.VerifyNoOtherCalls();
}
}
}
60 changes: 60 additions & 0 deletions src/WHMapper.Tests/Services/EveMapper/EveMapperServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using AutoFixture.Xunit2;
using Moq;
using WHMapper.Models.DTO.EveMapper.EveEntity;
using WHMapper.Services.EveAPI;
using WHMapper.Services.EveMapper;

namespace WHMapper.Tests.Services.EveMapper
{
public class EveMapperServiceTests
{
[Theory]
[AutoMoqData]
public async Task CacheContainsGroup_WhenGettingGroup_ReturnsItemFromCache(
[Frozen] Mock<IEveMapperCacheService> cacheService,
[Frozen] Mock<IEveAPIServices> apiService,
EveMapperService sut
)
{
var groupEntity = new GroupEntity(1, new WHMapper.Models.DTO.EveAPI.Universe.Group(1, "1", true, 1, []));
cacheService.Setup(x => x.GetAsync<GroupEntity>(1))
.ReturnsAsync(groupEntity)
.Verifiable();

var result = await sut.GetGroup(1);

Assert.NotNull(result);
Assert.Equal(groupEntity, result);
cacheService.Verify(x => x.GetAsync<GroupEntity>(1), Times.Once);
cacheService.VerifyNoOtherCalls();
apiService.VerifyNoOtherCalls();
}

[Theory]
[AutoMoqData]
public async Task ApiReturnsGroup_WhenGettingGroup_ReturnsItemFromCache(
[Frozen] Mock<IEveMapperCacheService> cacheService,
[Frozen] Mock<IEveAPIServices> apiService,
EveMapperService sut
)
{
GroupEntity groupEntity = null!;
cacheService.Setup(x => x.GetAsync<GroupEntity>(1))
.ReturnsAsync(groupEntity)
.Verifiable();

var group = new WHMapper.Models.DTO.EveAPI.Universe.Group(1, "hi", true, 1, []);
apiService.Setup(x => x.UniverseServices.GetGroup(1)).ReturnsAsync(group);

var result = await sut.GetGroup(1);

Assert.NotNull(result);
cacheService.Verify(x => x.GetAsync<GroupEntity>(1), Times.Once);
cacheService.Verify(x => x.AddAsync<GroupEntity>(It.IsAny<GroupEntity>()), Times.Once);
cacheService.VerifyNoOtherCalls();

apiService.Verify(x => x.UniverseServices.GetGroup(1), Times.Once);
apiService.VerifyNoOtherCalls();
}
}
}
Loading

0 comments on commit c54729c

Please sign in to comment.