From 526ca8df97c1080d068f78eb5033afae5b72df37 Mon Sep 17 00:00:00 2001 From: sgoo Date: Tue, 3 Dec 2024 16:54:25 +0900 Subject: [PATCH 1/2] Add Collection Test --- ...llection_Returns_CorrectValue.verified.txt | 11 ++++++ Mimir.Tests/QueryTests/CollectionTest.cs | 38 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 Mimir.Tests/QueryTests/CollectionTest.GraphQL_Query_Collection_Returns_CorrectValue.verified.txt create mode 100644 Mimir.Tests/QueryTests/CollectionTest.cs diff --git a/Mimir.Tests/QueryTests/CollectionTest.GraphQL_Query_Collection_Returns_CorrectValue.verified.txt b/Mimir.Tests/QueryTests/CollectionTest.GraphQL_Query_Collection_Returns_CorrectValue.verified.txt new file mode 100644 index 00000000..6ee4f7b0 --- /dev/null +++ b/Mimir.Tests/QueryTests/CollectionTest.GraphQL_Query_Collection_Returns_CorrectValue.verified.txt @@ -0,0 +1,11 @@ +{ + "data": { + "collection": { + "ids": [ + 1, + 2, + 3 + ] + } + } +} \ No newline at end of file diff --git a/Mimir.Tests/QueryTests/CollectionTest.cs b/Mimir.Tests/QueryTests/CollectionTest.cs new file mode 100644 index 00000000..166856e6 --- /dev/null +++ b/Mimir.Tests/QueryTests/CollectionTest.cs @@ -0,0 +1,38 @@ +using Mimir.MongoDB.Repositories; +using Moq; +using Lib9c.Models.States; +using Libplanet.Crypto; +using Mimir.MongoDB.Bson; + +namespace Mimir.Tests.QueryTests; + +public class CollectionTest +{ + [Fact] + public async Task GraphQL_Query_Collection_Returns_CorrectValue() + { + var address = new Address("0x0000000001000000000200000000030000000004"); + var state = new CollectionState + { + Ids = new SortedSet(new[] { 1, 2, 3 }) + }; + var mockRepo = new Mock(); + mockRepo + .Setup(repo => repo.GetByAddressAsync(It.IsAny
())) + .ReturnsAsync(new CollectionDocument(1, address, state)); + var serviceProvider = TestServices.Builder + .With(mockRepo.Object) + .Build(); + var query = $$""" + query { + collection(address: "{{address}}") { + ids + } + } + """; + + var result = await TestServices.ExecuteRequestAsync(serviceProvider, b => b.SetDocument(query)); + + await Verify(result); + } +} \ No newline at end of file From d49ca81e0b82492b0847033d968eb43aec5a4df3 Mon Sep 17 00:00:00 2001 From: sgoo Date: Tue, 3 Dec 2024 17:02:01 +0900 Subject: [PATCH 2/2] comment Solve --- Lib9c.Models/States/CollectionState.cs | 2 ++ Mimir.MongoDB/Repositories/CollectionRepository.cs | 7 ++++++- Mimir/GraphQL/Queries/Query.cs | 2 +- Mimir/Program.cs | 2 +- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Lib9c.Models/States/CollectionState.cs b/Lib9c.Models/States/CollectionState.cs index 32268100..3f56df41 100644 --- a/Lib9c.Models/States/CollectionState.cs +++ b/Lib9c.Models/States/CollectionState.cs @@ -18,6 +18,8 @@ public class CollectionState : IBencodable [BsonIgnore, GraphQLIgnore, JsonIgnore] public IValue Bencoded => List.Empty.Add(new List(Ids)); + public CollectionState() { } + public CollectionState(IValue bencoded) { if (bencoded is not List l) diff --git a/Mimir.MongoDB/Repositories/CollectionRepository.cs b/Mimir.MongoDB/Repositories/CollectionRepository.cs index e41c0408..2fa1dbbe 100644 --- a/Mimir.MongoDB/Repositories/CollectionRepository.cs +++ b/Mimir.MongoDB/Repositories/CollectionRepository.cs @@ -6,7 +6,12 @@ namespace Mimir.MongoDB.Repositories; -public class CollectionRepository(IMongoDbService dbService) +public interface ICollectionRepository +{ + Task GetByAddressAsync(Address address); +} + +public class CollectionRepository(IMongoDbService dbService) : ICollectionRepository { public async Task GetByAddressAsync(Address address) { diff --git a/Mimir/GraphQL/Queries/Query.cs b/Mimir/GraphQL/Queries/Query.cs index 57434cba..10ec70c4 100644 --- a/Mimir/GraphQL/Queries/Query.cs +++ b/Mimir/GraphQL/Queries/Query.cs @@ -80,7 +80,7 @@ public async Task GetBalanceAsync( /// /// The address of the avatar. /// The collection state for the specified avatar address. - public async Task GetCollectionAsync(Address address, [Service] CollectionRepository repo) => + public async Task GetCollectionAsync(Address address, [Service] ICollectionRepository repo) => (await repo.GetByAddressAsync(address)).Object; /// diff --git a/Mimir/Program.cs b/Mimir/Program.cs index 20e07bff..04358db9 100644 --- a/Mimir/Program.cs +++ b/Mimir/Program.cs @@ -43,7 +43,7 @@ builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); -builder.Services.AddSingleton(); +builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton();