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

#553 #562

Merged
merged 3 commits into from
Dec 4, 2024
Merged

#553 #562

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
2 changes: 2 additions & 0 deletions Lib9c.Models/States/WorldInformationState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
.Add(kv.Key.Serialize())
.Add(kv.Value.Bencoded)));

public WorldInformationState(){}

Check warning on line 28 in Lib9c.Models/States/WorldInformationState.cs

View workflow job for this annotation

GitHub Actions / Test (Mimir.Worker.Tests)

Non-nullable property 'WorldDictionary' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 28 in Lib9c.Models/States/WorldInformationState.cs

View workflow job for this annotation

GitHub Actions / Test (Mimir.MongoDB.Tests)

Non-nullable property 'WorldDictionary' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

public WorldInformationState(IValue bencoded)
{
if (bencoded is not Dictionary d)
Expand Down
10 changes: 8 additions & 2 deletions Mimir.MongoDB/Repositories/WorldInformationRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
using Mimir.MongoDB.Exceptions;
using Mimir.MongoDB.Services;
using MongoDB.Driver;
using Lib9c.Models.States;

namespace Mimir.MongoDB.Repositories;

public class WorldInformationRepository(IMongoDbService dbService)
public interface IWorldInformationRepository
{
Task<WorldInformationDocument> GetByAddressAsync(Address address);
}

public class WorldInformationRepository(IMongoDbService dbService) : IWorldInformationRepository
{
public async Task<WorldInformationDocument> GetByAddressAsync(Address address)
{
Expand All @@ -27,4 +33,4 @@ public async Task<WorldInformationDocument> GetByAddressAsync(Address address)

return document;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"data": {
"worldInformation": {
"worldDictionary": [
{
"key": 1,
"value": {
"id": 1,
"isStageCleared": true,
"isUnlocked": true,
"name": "kim",
"stageBegin": 2,
"stageClearedBlockIndex": 1,
"stageClearedId": 2,
"stageEnd": 3,
"unlockedBlockIndex": 2
}
}
]
}
}
}
74 changes: 74 additions & 0 deletions Mimir.Tests/QueryTests/WorldInformationTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Bencodex.Types;
using Lib9c.Models.States;
using Lib9c.Models.WorldInformation;
using Libplanet.Crypto;
using Mimir.MongoDB.Bson;
using Mimir.MongoDB.Repositories;
using Mimir.Tests;
using Moq;
using Nekoyume.Model.State;

public class WorldInformationTest
{
[Fact]
public async Task GraphQL_Query_WorldInformation_Returns_CorrectValue()
{
var address = new Address("0x0000000000000000000000000000000000000000");

var mockRepo = new Mock<IWorldInformationRepository>();
mockRepo
.Setup(repo => repo.GetByAddressAsync(It.IsAny<Address>()))
.ReturnsAsync(new WorldInformationDocument(
0,
default,
new WorldInformationState()
{
WorldDictionary = new Dictionary<int, World>()
{
{
1,
new World(new Dictionary(new Dictionary<IKey, IValue>
{
[(Text)"Id"] = 1.Serialize(),
[(Text)"Name"] = "kim".Serialize(),
[(Text)"StageBegin"] = 2.Serialize(),
[(Text)"StageEnd"] = 3.Serialize(),
[(Text)"UnlockedBlockIndex"] = 2.Serialize(),
[(Text)"StageClearedBlockIndex"] = 1.Serialize(),
[(Text)"StageClearedId"] = 2.Serialize(),
}))
}
}
}
));

var serviceProvider = TestServices.Builder
.With(mockRepo.Object)
.Build();

var query = $$"""
query {
worldInformation(address: "{{address}}") {
worldDictionary {
key
value {
id
isStageCleared
isUnlocked
name
stageBegin
stageClearedBlockIndex
stageClearedId
stageEnd
unlockedBlockIndex
}
}
}
}
""";

var result = await TestServices.ExecuteRequestAsync(serviceProvider, b => b.SetDocument(query));

await Verify(result);
}
}
5 changes: 3 additions & 2 deletions Mimir/GraphQL/Queries/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Nekoyume;
using Nekoyume.Action;
using Nekoyume.Extensions;
using Lib9c.Models.States;
using Nekoyume.TableData;

namespace Mimir.GraphQL.Queries;
Expand Down Expand Up @@ -253,6 +254,6 @@ public async Task<RaiderState> GetWorldBossRaiderAsync(
/// <returns>The world information state.</returns>
public async Task<WorldInformationState> GetWorldInformationAsync(
Address address,
[Service] WorldInformationRepository repo) =>
[Service] IWorldInformationRepository repo) =>
(await repo.GetByAddressAsync(address)).Object;
}
}
3 changes: 2 additions & 1 deletion Mimir/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
builder.Services.AddSingleton<WorldBossKillRewardRecordRepository>();
builder.Services.AddSingleton<WorldBossRaiderRepository>();
builder.Services.AddSingleton<WorldBossRepository>();
builder.Services.AddSingleton<WorldInformationRepository>();
builder.Services.AddSingleton<IWorldInformationRepository, WorldInformationRepository>();

// ~MongoDB repositories.
builder.Services.AddCors();
builder.Services.AddHttpClient();
Expand Down
Loading