Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize Mercurial Gauntlet on story skip #893

Merged
merged 2 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public FortRepositoryTest(DbTestFixture fixture)
{
this.fixture = fixture;
this.mockPlayerIdentityService = new(MockBehavior.Strict);
this.mockPlayerIdentityService.SetupGet(x => x.ViewerId).Returns(DbTestFixture.ViewerId);

this.fixture.ApiContext.Database.EnsureDeleted();
this.fixture.ApiContext.Database.EnsureCreated();
this.fixture.ApiContext.ChangeTracker.Clear();

this.fortRepository = new FortRepository(
this.fixture.ApiContext,
Expand All @@ -29,64 +34,28 @@ public FortRepositoryTest(DbTestFixture fixture)
CommonAssertionOptions.ApplyIgnoreOwnerOptions();
}

[Fact]
public async Task Builds_FiltersByAccountId()
{
this.mockPlayerIdentityService.SetupGet(x => x.ViewerId).Returns(1);

await this.fixture.AddRangeToDatabase(
new List<DbFortBuild>()
{
new() { ViewerId = 1, PlantId = FortPlants.TheHungerdome, },
new() { ViewerId = 1, PlantId = FortPlants.CircusTent, },
new() { ViewerId = 2, PlantId = FortPlants.JackOLantern, },
new() { ViewerId = 3, PlantId = FortPlants.WaterAltar, },
}
);

(await this.fortRepository.Builds.ToListAsync())
.Should()
.AllSatisfy(x => x.ViewerId.Should().Be(1))
.And.ContainEquivalentOf(
new DbFortBuild() { ViewerId = 1, PlantId = FortPlants.TheHungerdome, },
opts => opts.Excluding(x => x.Owner).Excluding(x => x.BuildId)
)
.And.ContainEquivalentOf(
new DbFortBuild() { ViewerId = 1, PlantId = FortPlants.CircusTent },
opts => opts.Excluding(x => x.Owner).Excluding(x => x.BuildId)
);

this.mockPlayerIdentityService.VerifyAll();
}

[Fact]
public async Task CheckPlantLevel_Success_ReturnsTrue()
{
this.mockPlayerIdentityService.SetupGet(x => x.ViewerId).Returns(1);

await this.fixture.AddToDatabase(
new DbFortBuild()
{
ViewerId = 1,
ViewerId = DbTestFixture.ViewerId,
PlantId = FortPlants.Dragonata,
Level = 10
}
);

(await this.fortRepository.CheckPlantLevel(FortPlants.Dragonata, 10)).Should().BeTrue();

this.mockPlayerIdentityService.VerifyAll();
}

[Fact]
public async Task CheckPlantLevel_Fail_ReturnsFalse()
{
this.mockPlayerIdentityService.SetupGet(x => x.ViewerId).Returns(1);

await this.fixture.AddToDatabase(
new DbFortBuild()
{
ViewerId = 1,
ViewerId = DbTestFixture.ViewerId,
PlantId = FortPlants.BroadleafTree,
Level = 3
}
Expand All @@ -95,16 +64,16 @@ await this.fixture.AddToDatabase(
(await this.fortRepository.CheckPlantLevel(FortPlants.BroadleafTree, 10))
.Should()
.BeFalse();

this.mockPlayerIdentityService.VerifyAll();
}

[Fact]
public async Task GetFortDetail_ReturnsFortDetail()
{
this.mockPlayerIdentityService.SetupGet(x => x.ViewerId).Returns(1);

DbFortDetail detail = new DbFortDetail() { ViewerId = 1, CarpenterNum = 2, };
DbFortDetail detail = new DbFortDetail()
{
ViewerId = DbTestFixture.ViewerId,
CarpenterNum = 2,
};
await this.fixture.AddToDatabase(detail);

(await this.fortRepository.GetFortDetail()).Should().BeEquivalentTo(detail);
Expand All @@ -115,25 +84,25 @@ public async Task GetFortDetail_ReturnsFortDetail()
[Fact]
public async Task GetFortDetail_NotFound_CreatesNew()
{
this.mockPlayerIdentityService.SetupGet(x => x.ViewerId).Returns(4);

(await this.fortRepository.GetFortDetail())
.Should()
.BeEquivalentTo(new DbFortDetail() { ViewerId = 4, CarpenterNum = 2 });
.BeEquivalentTo(
new DbFortDetail() { ViewerId = DbTestFixture.ViewerId, CarpenterNum = 2 }
);

this.mockPlayerIdentityService.VerifyAll();
}

[Fact]
public async Task UpdateFortMaximumCarpenter_UpdatesCarpenterNum()
{
this.mockPlayerIdentityService.SetupGet(x => x.ViewerId).Returns(5);

await this.fixture.AddToDatabase(new DbFortDetail() { ViewerId = 5, CarpenterNum = 2 });
await this.fixture.AddToDatabase(
new DbFortDetail() { ViewerId = DbTestFixture.ViewerId, CarpenterNum = 2 }
);

await this.fortRepository.UpdateFortMaximumCarpenter(4);

(await this.fixture.ApiContext.PlayerFortDetails.FindAsync(5L))!
(await this.fixture.ApiContext.PlayerFortDetails.FindAsync(DbTestFixture.ViewerId))!
.CarpenterNum.Should()
.Be(4);

Expand All @@ -143,12 +112,10 @@ public async Task UpdateFortMaximumCarpenter_UpdatesCarpenterNum()
[Fact]
public async Task GetBuilding_GetsBuilding()
{
this.mockPlayerIdentityService.SetupGet(x => x.ViewerId).Returns(1);

DbFortBuild build =
new()
{
ViewerId = 1,
ViewerId = DbTestFixture.ViewerId,
PlantId = FortPlants.DaggerDojo,
Level = 1,
BuildId = 8,
Expand All @@ -163,18 +130,15 @@ public async Task GetBuilding_GetsBuilding()
await this.fixture.AddToDatabase(build);

(await this.fortRepository.GetBuilding(8)).Should().BeEquivalentTo(build);
this.mockPlayerIdentityService.VerifyAll();
}

[Fact]
public async Task GetBuilding_NotFound_Throws()
{
this.mockPlayerIdentityService.SetupGet(x => x.ViewerId).Returns(1);

DbFortBuild build =
new()
{
ViewerId = 2,
ViewerId = DbTestFixture.ViewerId + 1,
PlantId = FortPlants.DaggerDojo,
Level = 1,
BuildId = 9,
Expand All @@ -192,13 +156,14 @@ await this
.fortRepository.Invoking(x => x.GetBuilding(9))
.Should()
.ThrowAsync<InvalidOperationException>();
this.mockPlayerIdentityService.VerifyAll();
}

[Fact]
public async Task AddBuild_Adds()
{
await this.fortRepository.AddBuild(new() { ViewerId = 12, BuildId = 12 });
await this.fortRepository.AddBuild(
new() { ViewerId = DbTestFixture.ViewerId, BuildId = 12 }
);

(await this.fixture.ApiContext.PlayerFortBuilds.FindAsync(12L)).Should().NotBeNull();
}
Expand All @@ -209,7 +174,7 @@ public async Task DeleteBuild_Deletes()
DbFortBuild build =
new()
{
ViewerId = 44,
ViewerId = DbTestFixture.ViewerId,
PlantId = FortPlants.DaggerDojo,
Level = 1,
BuildId = 15,
Expand All @@ -226,42 +191,40 @@ public async Task DeleteBuild_Deletes()
[Fact]
public async Task GetActiveCarpenters_ReturnsActiveCarpenters()
{
this.mockPlayerIdentityService.SetupGet(x => x.ViewerId).Returns(12);

await this.fixture.AddRangeToDatabase(
new List<DbFortBuild>()
{
new()
{
ViewerId = 12,
ViewerId = DbTestFixture.ViewerId,
PlantId = FortPlants.PalmTree,
BuildStartDate = DateTimeOffset.MinValue,
BuildEndDate = DateTimeOffset.MaxValue
},
new()
{
ViewerId = 12,
ViewerId = DbTestFixture.ViewerId,
PlantId = FortPlants.Lectern,
BuildStartDate = DateTimeOffset.MinValue,
BuildEndDate = DateTimeOffset.MaxValue
},
new()
{
ViewerId = 12,
ViewerId = DbTestFixture.ViewerId,
PlantId = FortPlants.Snowdrake,
BuildStartDate = DateTimeOffset.MinValue,
BuildEndDate = DateTimeOffset.UtcNow - TimeSpan.FromSeconds(22)
},
new()
{
ViewerId = 12,
ViewerId = DbTestFixture.ViewerId,
PlantId = FortPlants.Wishmill,
BuildStartDate = DateTimeOffset.UnixEpoch,
BuildEndDate = DateTimeOffset.UnixEpoch
},
new()
{
ViewerId = 13,
ViewerId = DbTestFixture.ViewerId + 1,
PlantId = FortPlants.FafnirStatueFlame,
BuildStartDate = DateTimeOffset.UnixEpoch,
BuildEndDate = DateTimeOffset.MaxValue
Expand All @@ -270,32 +233,30 @@ await this.fixture.AddRangeToDatabase(
);

(await this.fortRepository.GetActiveCarpenters()).Should().Be(3);

this.mockPlayerIdentityService.VerifyAll();
}

[Fact]
public async Task InitializeFort_InitializesFort()
{
this.mockPlayerIdentityService.SetupGet(x => x.ViewerId).Returns(13);

await this.fortRepository.InitializeFort();
await this.fixture.ApiContext.SaveChangesAsync();

this.fixture.ApiContext.PlayerFortDetails.Should()
.ContainEquivalentOf(new DbFortDetail() { ViewerId = 13, CarpenterNum = 2 });
.ContainEquivalentOf(
new DbFortDetail() { ViewerId = DbTestFixture.ViewerId, CarpenterNum = 2 }
);

this.fixture.ApiContext.PlayerFortBuilds.Should()
.Contain(x => x.PlantId == FortPlants.TheHalidom && x.ViewerId == 13);
.Contain(x =>
x.PlantId == FortPlants.TheHalidom && x.ViewerId == DbTestFixture.ViewerId
);

this.mockPlayerIdentityService.VerifyAll();
}

[Fact]
public async Task InitializeFort_DataExists_DoesNotThrow()
{
this.mockPlayerIdentityService.SetupGet(x => x.ViewerId).Returns(1);

await this.fortRepository.InitializeFort();
await this.fixture.ApiContext.SaveChangesAsync();

Expand All @@ -307,8 +268,6 @@ public async Task InitializeFort_DataExists_DoesNotThrow()
[Fact]
public async Task AddDojos_AddsDojos()
{
this.mockPlayerIdentityService.SetupGet(x => x.ViewerId).Returns(80);

await this.fortRepository.AddDojos();
await this.fixture.ApiContext.SaveChangesAsync();

Expand All @@ -330,7 +289,7 @@ public async Task AddDojos_AddsDojos()
this.fixture.ApiContext.PlayerFortBuilds.Should()
.Contain(x =>
x.PlantId == plant
&& x.ViewerId == 80
&& x.ViewerId == DbTestFixture.ViewerId
&& x.PositionX == -1
&& x.PositionZ == -1
&& x.Level == 1
Expand All @@ -343,12 +302,14 @@ public async Task AddDojos_AddsDojos()
[Fact]
public async Task AddToStorage_IsTotalQuantity_AddsBuilds()
{
this.mockPlayerIdentityService.SetupGet(x => x.ViewerId).Returns(2);

this.fixture.ApiContext.PlayerFortBuilds.AddRange(
new List<DbFortBuild>()
{
new() { ViewerId = 3, PlantId = FortPlants.FlameDracolith, }
new()
{
ViewerId = DbTestFixture.ViewerId + 1,
PlantId = FortPlants.FlameDracolith,
}
}
);
await this.fixture.ApiContext.SaveChangesAsync();
Expand All @@ -363,19 +324,20 @@ await this.fortRepository.AddToStorage(

this.fixture.ApiContext.PlayerFortBuilds.Should()
.Contain(x =>
x.ViewerId == 2 && x.Level == 4 && x.PositionX == -1 && x.PositionZ == -1
x.ViewerId == DbTestFixture.ViewerId
&& x.Level == 4
&& x.PositionX == -1
&& x.PositionZ == -1
);
}

[Fact]
public async Task AddToStorage_IsTotalQuantity_AlreadyOwned_DoesNotAddBuilds()
{
this.mockPlayerIdentityService.SetupGet(x => x.ViewerId).Returns(4);

this.fixture.ApiContext.PlayerFortBuilds.AddRange(
new List<DbFortBuild>()
{
new() { ViewerId = 4, PlantId = FortPlants.WindDracolith, }
new() { ViewerId = DbTestFixture.ViewerId, PlantId = FortPlants.WindDracolith, }
}
);
await this.fixture.ApiContext.SaveChangesAsync();
Expand Down
8 changes: 8 additions & 0 deletions DragaliaAPI/DragaliaAPI.Database/ApiContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,13 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder
.Entity<DbPlayerPresentHistory>()
.HasQueryFilter(x => x.ViewerId == this.playerIdentityService.ViewerId);

modelBuilder
.Entity<DbQuest>()
.HasQueryFilter(x => x.ViewerId == this.playerIdentityService.ViewerId);

modelBuilder
.Entity<DbFortBuild>()
.HasQueryFilter(x => x.ViewerId == this.playerIdentityService.ViewerId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
this.playerIdentityService = playerIdentityService;
}

public IQueryable<DbQuest> Quests =>
this.apiContext.PlayerQuests.Where(x => x.ViewerId == this.playerIdentityService.ViewerId);
[Obsolete("This entity has a global query filter, use ApiContext.PlayerQuests instead.")]
public IQueryable<DbQuest> Quests => this.apiContext.PlayerQuests;

public IQueryable<DbQuestEvent> QuestEvents =>
this.apiContext.QuestEvents.Where(x => x.ViewerId == this.playerIdentityService.ViewerId);
Expand Down Expand Up @@ -63,7 +63,7 @@

public async Task DeleteQuests(IEnumerable<int> questIds)
{
List<DbQuest> questEntities = await this

Check warning on line 66 in DragaliaAPI/DragaliaAPI.Database/Repositories/QuestRepository.cs

View workflow job for this annotation

GitHub Actions / Integration test / Integration test

'QuestRepository.Quests' is obsolete: 'This entity has a global query filter, use ApiContext.PlayerQuests instead.'

Check warning on line 66 in DragaliaAPI/DragaliaAPI.Database/Repositories/QuestRepository.cs

View workflow job for this annotation

GitHub Actions / Unit test (DragaliaAPI/DragaliaAPI.Shared.Test) / Test

'QuestRepository.Quests' is obsolete: 'This entity has a global query filter, use ApiContext.PlayerQuests instead.'

Check warning on line 66 in DragaliaAPI/DragaliaAPI.Database/Repositories/QuestRepository.cs

View workflow job for this annotation

GitHub Actions / Unit test (DragaliaAPI/DragaliaAPI.Database.Test) / Test

'QuestRepository.Quests' is obsolete: 'This entity has a global query filter, use ApiContext.PlayerQuests instead.'

Check warning on line 66 in DragaliaAPI/DragaliaAPI.Database/Repositories/QuestRepository.cs

View workflow job for this annotation

GitHub Actions / Unit test (DragaliaAPI/DragaliaAPI.Test) / Test

'QuestRepository.Quests' is obsolete: 'This entity has a global query filter, use ApiContext.PlayerQuests instead.'
.Quests.Where(x => questIds.Contains(x.QuestId))
.ToListAsync();
this.apiContext.PlayerQuests.RemoveRange(questEntities);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,7 @@ await this.Client.PostMsgpack<StorySkipSkipResponse>("story_skip/skip")
.Contain(x => x.Id == upgradeHalidomToLv3Mission)
.Which.State.Should()
.Be(MissionState.Completed);

this.ApiContext.PlayerQuestWalls.Should().Contain(x => x.ViewerId == this.ViewerId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ public async Task DbPlayerPresentHistory_HasGlobalQueryFilter()
.Be(this.ViewerId);
}

[Fact]
public async Task DbQuest_HasGlobalQueryFilter() => await TestGlobalQueryFilter<DbQuest>();

[Fact]
public async Task DbFortBuild_HasGlobalQueryFilter() =>
await TestGlobalQueryFilter<DbFortBuild>();

private async Task TestGlobalQueryFilter<TEntity>()
where TEntity : class, IDbPlayerData
{
Expand Down
Loading
Loading