Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SapiensAnatis committed Aug 7, 2023
1 parent 41e827a commit 6de9b13
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using DragaliaAPI.Database.Entities;
using DragaliaAPI.Features.Dungeon;
using DragaliaAPI.Features.Dungeon.Record;
using DragaliaAPI.Features.Event;
using DragaliaAPI.Features.Reward;
using DragaliaAPI.Models;
using DragaliaAPI.Models.Generated;
using DragaliaAPI.Shared.Definitions.Enums;
using DragaliaAPI.Shared.MasterAsset;
using Microsoft.Extensions.Logging;

namespace DragaliaAPI.Test.Features.Dungeon.Record;

public class DungeonRecordRewardServiceTest
{
private readonly Mock<IQuestCompletionService> mockQuestCompletionService;
private readonly Mock<IRewardService> mockRewardService;
private readonly Mock<IAbilityCrestMultiplierService> mockAbilityCrestMultiplierService;
private readonly Mock<IEventDropService> mockEventDropService;
private readonly Mock<ILogger<DungeonRecordRewardService>> mockLogger;

private readonly IDungeonRecordRewardService dungeonRecordRewardService;

public DungeonRecordRewardServiceTest()
{
this.mockQuestCompletionService = new(MockBehavior.Strict);
this.mockRewardService = new(MockBehavior.Strict);
this.mockAbilityCrestMultiplierService = new(MockBehavior.Strict);
this.mockEventDropService = new(MockBehavior.Strict);
this.mockLogger = new(MockBehavior.Loose);

this.dungeonRecordRewardService = new DungeonRecordRewardService(
this.mockQuestCompletionService.Object,
this.mockRewardService.Object,
this.mockAbilityCrestMultiplierService.Object,
this.mockEventDropService.Object,
this.mockLogger.Object
);
}

[Fact]
public async Task ProcessQuestMissionCompletion_SetsEntityProperties()
{
int questId = 225021101;
DbQuest questEntity =
new()
{
DeviceAccountId = "id",
QuestId = questId,
PlayCount = 0,
IsMissionClear1 = false,
IsMissionClear2 = false,
IsMissionClear3 = false,
};

List<AtgenFirstClearSet> firstClearRewards =
new()
{
new()
{
id = 0,
quantity = 2,
type = EntityTypes.Wyrmite
}
};

PlayRecord playRecord = new();
DungeonSession session =
new() { QuestData = MasterAsset.QuestData[questId], Party = null! };
QuestMissionStatus status =
new(
new[] { true, true, true },
new List<AtgenMissionsClearSet>(),
new List<AtgenFirstClearSet>()
);

this.mockQuestCompletionService
.Setup(x => x.CompleteQuestMissions(session, new[] { false, false, false }, playRecord))
.ReturnsAsync(status);
this.mockQuestCompletionService
.Setup(x => x.GrantFirstClearRewards(questId))
.ReturnsAsync(firstClearRewards);

(
await this.dungeonRecordRewardService.ProcessQuestMissionCompletion(
playRecord,
session,
questEntity
)
)
.Should()
.Be(status);

questEntity.IsMissionClear1.Should().BeTrue();
questEntity.IsMissionClear2.Should().BeTrue();
questEntity.IsMissionClear3.Should().BeTrue();
}
}
204 changes: 204 additions & 0 deletions DragaliaAPI.Test/Features/Dungeon/Record/DungeonRecordServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
using Castle.Core.Logging;
using DragaliaAPI.Database.Entities;
using DragaliaAPI.Database.Repositories;
using DragaliaAPI.Features.Dungeon.Record;
using DragaliaAPI.Features.Missions;
using DragaliaAPI.Features.Player;
using DragaliaAPI.Models;
using DragaliaAPI.Models.Generated;
using DragaliaAPI.Services;
using DragaliaAPI.Shared.Definitions.Enums;
using DragaliaAPI.Shared.MasterAsset;
using DragaliaAPI.Test.Utils;
using Humanizer;
using Microsoft.Extensions.Logging;
using StackExchange.Redis;

namespace DragaliaAPI.Test.Features.Dungeon.Record;

public class DungeonRecordServiceTest
{
private readonly Mock<IDungeonRecordRewardService> mockDungeonRewardService;
private readonly Mock<IQuestRepository> mockQuestRepository;
private readonly Mock<IMissionProgressionService> mockMissionProgressionService;
private readonly Mock<IUserService> mockUserService;
private readonly Mock<ITutorialService> mockTutorialService;
private readonly Mock<ILogger<DungeonRecordService>> mockLogger;

private readonly IDungeonRecordService dungeonRecordService;

public DungeonRecordServiceTest()
{
this.mockDungeonRewardService = new(MockBehavior.Strict);
this.mockQuestRepository = new(MockBehavior.Strict);
this.mockMissionProgressionService = new(MockBehavior.Strict);
this.mockUserService = new(MockBehavior.Strict);
this.mockTutorialService = new(MockBehavior.Strict);
this.mockLogger = new(MockBehavior.Loose);

this.dungeonRecordService = new DungeonRecordService(
this.mockDungeonRewardService.Object,
this.mockQuestRepository.Object,
this.mockMissionProgressionService.Object,
this.mockUserService.Object,
this.mockTutorialService.Object,
this.mockLogger.Object
);

this.mockTutorialService.Setup(x => x.AddTutorialFlag(1022)).ReturnsAsync(new List<int>());

CommonAssertionOptions.ApplyTimeOptions();
}

[Fact]
public async Task GenerateIngameResultData_CallsExpectedMethods()
{
int lSurtrSoloId = 232031101;

DungeonSession session =
new()
{
QuestData = MasterAsset.QuestData[lSurtrSoloId],
Party = new List<PartySettingList>(),
StartTime = DateTimeOffset.UtcNow
};
PlayRecord playRecord = new() { time = 10, };

DbQuest mockQuest =
new()
{
DeviceAccountId = "id",
QuestId = lSurtrSoloId,
State = 0,
BestClearTime = 999
};

List<AtgenDropAll> dropList =
new()
{
new()
{
id = (int)Materials.FirestormRuby,
quantity = 10,
type = EntityTypes.Material
}
};

List<AtgenDropAll> eventDrops =
new()
{
new()
{
id = (int)Materials.WoodlandHerbs,
quantity = 20,
type = EntityTypes.Material
}
};

List<AtgenScoreMissionSuccessList> scoreMissionSuccessLists =
new()
{
new()
{
score_mission_complete_type = QuestCompleteType.LimitFall,
score_target_value = 100,
}
};

List<AtgenEventPassiveUpList> passiveUpLists =
new()
{
new() { passive_id = 1, progress = 2 }
};

int takeCoin = 10;
int takeMana = 20;
int takeAccumulatePoint = 30;
int takeBoostAccumulatePoint = 40;

this.mockQuestRepository
.Setup(x => x.GetQuestDataAsync(lSurtrSoloId))
.ReturnsAsync(mockQuest);

this.mockMissionProgressionService.Setup(x => x.OnQuestCleared(lSurtrSoloId));

this.mockUserService
.Setup(x => x.RemoveStamina(StaminaType.Single, 40))
.Returns(Task.CompletedTask);
this.mockUserService
.Setup(x => x.AddExperience(400))
.ReturnsAsync(new PlayerLevelResult(true, 100, 50));

this.mockDungeonRewardService
.Setup(x => x.ProcessEnemyDrops(playRecord, session))
.ReturnsAsync((dropList, takeMana, takeCoin));
this.mockDungeonRewardService
.Setup(x => x.ProcessEventRewards(playRecord, session))
.ReturnsAsync(
new DungeonRecordRewardService.EventRewardData(
scoreMissionSuccessLists,
takeAccumulatePoint,
takeBoostAccumulatePoint,
passiveUpLists,
eventDrops
)
);

IngameResultData ingameResultData =
await this.dungeonRecordService.GenerateIngameResultData(
"dungeonKey",
playRecord,
session
);

ingameResultData
.Should()
.BeEquivalentTo(
new IngameResultData()
{
dungeon_key = "dungeonKey",
play_type = QuestPlayType.Default,
quest_id = lSurtrSoloId,
is_host = true,
quest_party_setting_list = session.Party,
start_time = session.StartTime,
end_time = DateTimeOffset.UtcNow,
reborn_count = playRecord.reborn_count,
total_play_damage = playRecord.total_play_damage,
is_clear = true,
current_play_count = 1,
reward_record = new()
{
drop_all = dropList.Concat(eventDrops).ToList(),
take_boost_accumulate_point = takeBoostAccumulatePoint,
take_accumulate_point = takeAccumulatePoint,
take_coin = takeCoin,
take_astral_item_quantity = 0,
player_level_up_fstone = 50,
},
grow_record = new()
{
take_mana = takeMana,
take_player_exp = 400,
take_chara_exp = 1,
bonus_factor = 1,
mana_bonus_factor = 1,
chara_grow_record = new List<AtgenCharaGrowRecord>()
},
event_passive_up_list = passiveUpLists,
score_mission_success_list = scoreMissionSuccessLists,
is_best_clear_time = true,
clear_time = playRecord.time,
}
);

mockQuest.State.Should().Be(3);

this.mockDungeonRewardService.VerifyAll();
this.mockQuestRepository.VerifyAll();
this.mockMissionProgressionService.VerifyAll();
this.mockUserService.VerifyAll();
this.mockTutorialService.VerifyAll();
this.mockLogger.VerifyAll();
}
}
22 changes: 21 additions & 1 deletion DragaliaAPI.Test/Services/HelperServiceTest.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
using AutoMapper;
using DragaliaAPI.Database.Repositories;
using DragaliaAPI.Features.Dungeon;
using DragaliaAPI.Features.Dungeon.Record;
using DragaliaAPI.Models.Generated;
using DragaliaAPI.Services;
using DragaliaAPI.Services.Game;
using Microsoft.Extensions.Logging;

namespace DragaliaAPI.Test.Services;

public class HelperServiceTest
{
private readonly Mock<IPartyRepository> mockPartyRepository;
private readonly Mock<IDungeonRepository> mockDungeonRepository;
private readonly Mock<IUserDataRepository> mockUserDataRepository;
private readonly Mock<ILogger<HelperService>> mockLogger;

private readonly IHelperService helperService;
private readonly IMapper mapper;

Expand All @@ -16,7 +25,18 @@ public HelperServiceTest()
cfg => cfg.AddMaps(typeof(Program).Assembly)
).CreateMapper();

this.helperService = new HelperService(this.mapper);
this.mockPartyRepository = new(MockBehavior.Strict);
this.mockDungeonRepository = new(MockBehavior.Strict);
this.mockUserDataRepository = new(MockBehavior.Strict);
this.mockLogger = new(MockBehavior.Loose);

this.helperService = new HelperService(
this.mockPartyRepository.Object,
this.mockDungeonRepository.Object,
this.mockUserDataRepository.Object,
this.mapper,
this.mockLogger.Object
);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ EnemyDropList enemyDropList in enemyList

drops.Add(reward.ToDropAll());

await rewardService.GrantReward(reward, log: false);
await rewardService.GrantReward(reward);
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions DragaliaAPI/Features/Dungeon/Record/DungeonRecordService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ DungeonSession session
this.ProcessMissionProgression(session);
await this.ProcessGrowth(ingameResultData.grow_record, session);
await this.ProcessStaminaConsumption(session);
await this.ProcessPlayerLevel(ingameResultData.reward_record, session);
await this.ProcessPlayerLevel(
ingameResultData.grow_record,
ingameResultData.reward_record,
session
);

(IEnumerable<AtgenDropAll> dropList, int manaDrop, int coinDrop) =
await dungeonRecordRewardService.ProcessEnemyDrops(playRecord, session);
Expand Down Expand Up @@ -143,7 +147,11 @@ private async Task ProcessStaminaConsumption(DungeonSession session)
await userService.RemoveStamina(type, amount);
}

private async Task ProcessPlayerLevel(RewardRecord rewardRecord, DungeonSession session)
private async Task ProcessPlayerLevel(
GrowRecord growRecord,
RewardRecord rewardRecord,
DungeonSession session
)
{
// Constant for quests with no stamina usage, wip?
int experience =
Expand All @@ -156,5 +164,6 @@ private async Task ProcessPlayerLevel(RewardRecord rewardRecord, DungeonSession
PlayerLevelResult playerLevelResult = await userService.AddExperience(experience); // TODO: Exp boost

rewardRecord.player_level_up_fstone = playerLevelResult.RewardedWyrmite;
growRecord.take_player_exp = experience;
}
}
Loading

0 comments on commit 6de9b13

Please sign in to comment.