From e427b5008e394c6a93c769c958ea340886343f77 Mon Sep 17 00:00:00 2001 From: suk07 Date: Wed, 4 Dec 2024 20:28:42 +0900 Subject: [PATCH 1/7] add metadata query test --- .../Repositories/MetadataRepository.cs | 9 ++++- ...Metadata_Returns_CorrectValue.verified.txt | 9 +++++ Mimir.Tests/QueryTests/MetadataTest.cs | 38 +++++++++++++++++++ Mimir/GraphQL/Queries/Query.cs | 2 +- Mimir/Program.cs | 2 +- 5 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 Mimir.Tests/QueryTests/MetadataTest.GraphQL_Query_Metadata_Returns_CorrectValue.verified.txt create mode 100644 Mimir.Tests/QueryTests/MetadataTest.cs diff --git a/Mimir.MongoDB/Repositories/MetadataRepository.cs b/Mimir.MongoDB/Repositories/MetadataRepository.cs index 6c327737..afecb76a 100644 --- a/Mimir.MongoDB/Repositories/MetadataRepository.cs +++ b/Mimir.MongoDB/Repositories/MetadataRepository.cs @@ -4,8 +4,13 @@ using MongoDB.Driver; namespace Mimir.MongoDB.Repositories; - -public class MetadataRepository(IMongoDbService dbService) +public interface IMetadataRepository{ + Task GetByCollectionAsync(string collectionName); + Task GetByCollectionAndTypeAsync( + string pollerType, + string collectionName); +} +public class MetadataRepository(IMongoDbService dbService):IMetadataRepository { public async Task GetByCollectionAsync(string collectionName) { diff --git a/Mimir.Tests/QueryTests/MetadataTest.GraphQL_Query_Metadata_Returns_CorrectValue.verified.txt b/Mimir.Tests/QueryTests/MetadataTest.GraphQL_Query_Metadata_Returns_CorrectValue.verified.txt new file mode 100644 index 00000000..3a0bbd14 --- /dev/null +++ b/Mimir.Tests/QueryTests/MetadataTest.GraphQL_Query_Metadata_Returns_CorrectValue.verified.txt @@ -0,0 +1,9 @@ +{ + "data": { + "metadata": { + "collectionName": "collection", + "latestBlockIndex": 0, + "pollerType": "poller-type" + } + } +} \ No newline at end of file diff --git a/Mimir.Tests/QueryTests/MetadataTest.cs b/Mimir.Tests/QueryTests/MetadataTest.cs new file mode 100644 index 00000000..1aebe468 --- /dev/null +++ b/Mimir.Tests/QueryTests/MetadataTest.cs @@ -0,0 +1,38 @@ +using Libplanet.Crypto; +using Mimir.MongoDB.Bson; +using Mimir.MongoDB.Repositories; +using Mimir.Tests; +using Moq; + +public class MetadataTest +{ + [Fact] + public async Task GraphQL_Query_Metadata_Returns_CorrectValue() + { + var mockRepo = new Mock(); + mockRepo + .Setup(repo => repo.GetByCollectionAsync(It.IsAny())) + .ReturnsAsync(new MetadataDocument{ + PollerType="poller-type", + CollectionName="collection" + }); + + var serviceProvider = TestServices.Builder + .With(mockRepo.Object) + .Build(); + + var query = $$""" + query { + metadata(collectionName: "collection") { + collectionName + latestBlockIndex + pollerType + } + } + """; + + var result = await TestServices.ExecuteRequestAsync(serviceProvider, b => b.SetDocument(query)); + + await Verify(result); + } +} \ No newline at end of file diff --git a/Mimir/GraphQL/Queries/Query.cs b/Mimir/GraphQL/Queries/Query.cs index 10ec70c4..b33b6c00 100644 --- a/Mimir/GraphQL/Queries/Query.cs +++ b/Mimir/GraphQL/Queries/Query.cs @@ -114,7 +114,7 @@ public async Task GetInventoryAsync(Address address, [Service] Invent /// /// The name of the collection. /// The metadata - public async Task GetMetadataAsync(string collectionName, [Service] MetadataRepository repo) => + public async Task GetMetadataAsync(string collectionName, [Service] IMetadataRepository repo) => await repo.GetByCollectionAsync(collectionName); /// diff --git a/Mimir/Program.cs b/Mimir/Program.cs index 04358db9..effcdbad 100644 --- a/Mimir/Program.cs +++ b/Mimir/Program.cs @@ -47,7 +47,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(); From 89b9100acca5878a8acf45d81104ea0bc4994fcc Mon Sep 17 00:00:00 2001 From: jusiwoo Date: Wed, 4 Dec 2024 20:29:09 +0900 Subject: [PATCH 2/7] kimdongkyun --- Lib9c.Models/Stake/Contract.cs | 2 + Lib9c.Models/States/StakeState.cs | 2 + Mimir.MongoDB/Repositories/StakeRepository.cs | 7 ++- ...ry_Stake_Returns_CorrectValue.verified.txt | 14 +++++ Mimir.Tests/QueryTests/StakeTest.cs | 61 +++++++++++++++++++ Mimir/GraphQL/Queries/Query.cs | 2 +- Mimir/Program.cs | 2 +- 7 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 Mimir.Tests/QueryTests/StakeTest.GraphQL_Query_Stake_Returns_CorrectValue.verified.txt create mode 100644 Mimir.Tests/QueryTests/StakeTest.cs diff --git a/Lib9c.Models/Stake/Contract.cs b/Lib9c.Models/Stake/Contract.cs index 67e508f5..8c1c857c 100644 --- a/Lib9c.Models/Stake/Contract.cs +++ b/Lib9c.Models/Stake/Contract.cs @@ -25,6 +25,8 @@ public record Contract : IBencodable public long RewardInterval { get; init; } public long LockupInterval { get; init; } + public Contract(){} + public Contract(IValue bencoded) { if (bencoded is not List l) diff --git a/Lib9c.Models/States/StakeState.cs b/Lib9c.Models/States/StakeState.cs index 58eddb06..0e075d63 100644 --- a/Lib9c.Models/States/StakeState.cs +++ b/Lib9c.Models/States/StakeState.cs @@ -19,6 +19,8 @@ public record StakeState : IBencodable public Contract Contract { get; init; } public long StartedBlockIndex { get; init; } public long ReceivedBlockIndex { get; init; } + + public StakeState() {} [BsonIgnore, GraphQLIgnore, JsonIgnore] public IValue Bencoded => diff --git a/Mimir.MongoDB/Repositories/StakeRepository.cs b/Mimir.MongoDB/Repositories/StakeRepository.cs index 251c3595..d9ea6829 100644 --- a/Mimir.MongoDB/Repositories/StakeRepository.cs +++ b/Mimir.MongoDB/Repositories/StakeRepository.cs @@ -6,7 +6,12 @@ namespace Mimir.MongoDB.Repositories; -public class StakeRepository(IMongoDbService dbService) +public interface IStakeRepository +{ + Task GetByAgentAddressAsync(Address agentAddress); +} + +public class StakeRepository(IMongoDbService dbService) : IStakeRepository { public async Task GetByAgentAddressAsync(Address agentAddress) { diff --git a/Mimir.Tests/QueryTests/StakeTest.GraphQL_Query_Stake_Returns_CorrectValue.verified.txt b/Mimir.Tests/QueryTests/StakeTest.GraphQL_Query_Stake_Returns_CorrectValue.verified.txt new file mode 100644 index 00000000..8366203c --- /dev/null +++ b/Mimir.Tests/QueryTests/StakeTest.GraphQL_Query_Stake_Returns_CorrectValue.verified.txt @@ -0,0 +1,14 @@ +{ + "data": { + "stake": { + "receivedBlockIndex": 1, + "startedBlockIndex": 2, + "contract": { + "lockupInterval": 1, + "rewardInterval": 1, + "stakeRegularFixedRewardSheetTableName": "a", + "stakeRegularRewardSheetTableName": "a" + } + } + } +} \ No newline at end of file diff --git a/Mimir.Tests/QueryTests/StakeTest.cs b/Mimir.Tests/QueryTests/StakeTest.cs new file mode 100644 index 00000000..c961db41 --- /dev/null +++ b/Mimir.Tests/QueryTests/StakeTest.cs @@ -0,0 +1,61 @@ +using DnsClient.Protocol; +using Lib9c.Models.Stake; +using Lib9c.Models.States; +using Libplanet.Crypto; +using Mimir.MongoDB.Bson; +using Mimir.MongoDB.Repositories; +using Mimir.Tests; +using Moq; + +public class StakeTest +{ + [Fact] + public async Task GraphQL_Query_Stake_Returns_CorrectValue() + { + var address = new Address(); + // 모킹 처리 + var mockRepo = new Mock(); + mockRepo + .Setup(repo => repo.GetByAgentAddressAsync(It.IsAny
())) + .ReturnsAsync( + new StakeDocument( + 1, + new Address(), + new Address(), + new StakeState() { + Contract = new Contract(){ + StakeRegularFixedRewardSheetTableName = "a", + StakeRegularRewardSheetTableName = "a", + RewardInterval = 1, + LockupInterval = 1 + }, + StartedBlockIndex = 2, + ReceivedBlockIndex = 1 + }, + 1 + )); + + var serviceProvider = TestServices.Builder + .With(mockRepo.Object) + .Build(); + + var query = $$""" + query { + stake(address: "{{address}}") { + receivedBlockIndex + startedBlockIndex + contract { + lockupInterval + rewardInterval + stakeRegularFixedRewardSheetTableName + stakeRegularRewardSheetTableName + } + } + } + """; + + var result = await TestServices.ExecuteRequestAsync(serviceProvider, b => b.SetDocument(query)); + + await Verify(result); + } +} \ No newline at end of file diff --git a/Mimir/GraphQL/Queries/Query.cs b/Mimir/GraphQL/Queries/Query.cs index 10ec70c4..feaf0ad0 100644 --- a/Mimir/GraphQL/Queries/Query.cs +++ b/Mimir/GraphQL/Queries/Query.cs @@ -160,7 +160,7 @@ public async Task GetRunesAsync(Address avatarAddress, [Service] Al ///
/// The address of the agent. /// The stake state. - public async Task GetStakeAsync(Address address, [Service] StakeRepository repo) => + public async Task GetStakeAsync(Address address, [Service] IStakeRepository repo) => (await repo.GetByAgentAddressAsync(address)).Object; /// diff --git a/Mimir/Program.cs b/Mimir/Program.cs index 04358db9..101058d3 100644 --- a/Mimir/Program.cs +++ b/Mimir/Program.cs @@ -52,7 +52,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(); From 271081a42b0c3e0d762d16636df8579103a108ed Mon Sep 17 00:00:00 2001 From: pybsh Date: Wed, 4 Dec 2024 20:31:37 +0900 Subject: [PATCH 3/7] Add integration test for Inventory Query --- Lib9c.Models/Items/Inventory.cs | 4 +- Lib9c.Models/Items/InventoryItem.cs | 4 +- .../Repositories/InventoryRepository.cs | 7 +- Mimir.Tests/Mimir.Tests.csproj | 6 + ...nventory_Returns_CorrectValue.verified.txt | 28 ++ Mimir.Tests/QueryTests/InventoryTest.cs | 266 ++++++++++++++++++ Mimir/GraphQL/Queries/Query.cs | 2 +- Mimir/Program.cs | 2 +- 8 files changed, 314 insertions(+), 5 deletions(-) create mode 100644 Mimir.Tests/QueryTests/InventoryTest.GraphQL_Query_Inventory_Returns_CorrectValue.verified.txt create mode 100644 Mimir.Tests/QueryTests/InventoryTest.cs diff --git a/Lib9c.Models/Items/Inventory.cs b/Lib9c.Models/Items/Inventory.cs index b90a5d8a..12407ca1 100644 --- a/Lib9c.Models/Items/Inventory.cs +++ b/Lib9c.Models/Items/Inventory.cs @@ -14,7 +14,9 @@ namespace Lib9c.Models.Items; public record Inventory : IBencodable { public List Items { get; init; } - + + public Inventory() { } + [BsonIgnore, GraphQLIgnore, JsonIgnore] public IValue Bencoded => new List(Items .OrderBy(i => i.Item.Id) diff --git a/Lib9c.Models/Items/InventoryItem.cs b/Lib9c.Models/Items/InventoryItem.cs index 3b35d1ad..9a272b24 100644 --- a/Lib9c.Models/Items/InventoryItem.cs +++ b/Lib9c.Models/Items/InventoryItem.cs @@ -19,7 +19,9 @@ public record InventoryItem : IBencodable public ItemBase Item { get; init; } public int Count { get; init; } public ILock? Lock { get; init; } - + + public InventoryItem() { } + [BsonIgnore, GraphQLIgnore, JsonIgnore] public IValue Bencoded { diff --git a/Mimir.MongoDB/Repositories/InventoryRepository.cs b/Mimir.MongoDB/Repositories/InventoryRepository.cs index 096931e1..340bb183 100644 --- a/Mimir.MongoDB/Repositories/InventoryRepository.cs +++ b/Mimir.MongoDB/Repositories/InventoryRepository.cs @@ -6,7 +6,12 @@ namespace Mimir.MongoDB.Repositories; -public class InventoryRepository(IMongoDbService dbService) +public interface IInventoryRepository +{ + public Task GetByAddressAsync(Address address); +} + +public class InventoryRepository(IMongoDbService dbService) : IInventoryRepository { public async Task GetByAddressAsync(Address address) { diff --git a/Mimir.Tests/Mimir.Tests.csproj b/Mimir.Tests/Mimir.Tests.csproj index 9b42677e..884a7ef6 100644 --- a/Mimir.Tests/Mimir.Tests.csproj +++ b/Mimir.Tests/Mimir.Tests.csproj @@ -26,5 +26,11 @@ + + + + InventoryTest.cs + + diff --git a/Mimir.Tests/QueryTests/InventoryTest.GraphQL_Query_Inventory_Returns_CorrectValue.verified.txt b/Mimir.Tests/QueryTests/InventoryTest.GraphQL_Query_Inventory_Returns_CorrectValue.verified.txt new file mode 100644 index 00000000..aea629b2 --- /dev/null +++ b/Mimir.Tests/QueryTests/InventoryTest.GraphQL_Query_Inventory_Returns_CorrectValue.verified.txt @@ -0,0 +1,28 @@ +{ + "data": { + "inventory": { + "items": [ + { + "count": 1, + "item": { + "elementalType": "NORMAL", + "grade": 1, + "id": 1, + "itemSubType": "ARMOR", + "itemType": "EQUIPMENT" + } + }, + { + "count": 2, + "item": { + "elementalType": "FIRE", + "grade": 2, + "id": 2, + "itemSubType": "FOOD", + "itemType": "MATERIAL" + } + } + ] + } + } +} \ No newline at end of file diff --git a/Mimir.Tests/QueryTests/InventoryTest.cs b/Mimir.Tests/QueryTests/InventoryTest.cs new file mode 100644 index 00000000..7bd5d188 --- /dev/null +++ b/Mimir.Tests/QueryTests/InventoryTest.cs @@ -0,0 +1,266 @@ +using Lib9c.Models.Items; +using Libplanet.Crypto; +using Mimir.MongoDB.Bson; +using Mimir.MongoDB.Repositories; +using Moq; +using Nekoyume.Model.Elemental; +using Nekoyume.Model.Item; +using Inventory = Lib9c.Models.Items.Inventory; +using ItemBase = Lib9c.Models.Items.ItemBase; + +namespace Mimir.Tests.QueryTests; + +public class InventoryTest +{ + [Fact] + public async Task GraphQL_Query_Inventory_Returns_CorrectValue() + { + var address = new Address(); + var inventory = new Inventory + { + Items = new List + { + new() + { + Item = new ItemBase + { + Id = 1, + Grade = 1, + ItemType = ItemType.Equipment, + ItemSubType = ItemSubType.Armor, + ElementalType = ElementalType.Normal + }, + Count = 1, + Lock = new OrderLock() + }, + new() + { + Item = new ItemBase + { + Id = 2, + Grade = 2, + ItemType = ItemType.Material, + ItemSubType = ItemSubType.Food, + ElementalType = ElementalType.Fire + }, + Count = 2, + Lock = null + } + } + }; + + // process mocking + var mockRepo = new Mock(); + mockRepo + .Setup(repo => repo.GetByAddressAsync(It.IsAny
())) + .ReturnsAsync(new InventoryDocument(0, address, inventory)); + + var serviceProvider = TestServices.Builder + .With(mockRepo.Object) + .Build(); + + // Issue: There is issue with the merge in relation to itemId. + // So, Temporary fix is to remove the itemId from the query. + var query = $$""" + query { + inventory(address: "{{address}}") { + items { + count + item { + elementalType + grade + id + itemSubType + itemType + ... on Armor { + elementalType + equipped + exp + grade + id + + itemSubType + itemType + level + madeWithMimisbrunnrRecipe + optionCountFromCombination + requiredBlockIndex + setId + spineResourcePath + } + ... on Aura { + elementalType + equipped + exp + grade + id + + itemSubType + itemType + level + madeWithMimisbrunnrRecipe + optionCountFromCombination + requiredBlockIndex + setId + spineResourcePath + } + ... on Belt { + elementalType + equipped + exp + grade + id + + itemSubType + itemType + level + madeWithMimisbrunnrRecipe + optionCountFromCombination + requiredBlockIndex + setId + spineResourcePath + } + ... on Consumable { + elementalType + grade + id + + itemSubType + itemType + requiredBlockIndex + } + ... on Costume { + elementalType + equipped + grade + id + + itemSubType + itemType + requiredBlockIndex + spineResourcePath + } + ... on Equipment { + elementalType + equipped + exp + grade + id + + itemSubType + itemType + level + madeWithMimisbrunnrRecipe + optionCountFromCombination + requiredBlockIndex + setId + spineResourcePath + } + ... on Grimoire { + elementalType + equipped + exp + grade + id + + itemSubType + itemType + level + madeWithMimisbrunnrRecipe + optionCountFromCombination + requiredBlockIndex + setId + spineResourcePath + } + ... on ItemBase { + elementalType + grade + id + itemSubType + itemType + } + ... on ItemUsable { + elementalType + grade + id + + itemSubType + itemType + requiredBlockIndex + } + ... on Material { + elementalType + grade + id + + itemSubType + itemType + } + ... on Necklace { + elementalType + equipped + exp + grade + id + + itemSubType + itemType + level + madeWithMimisbrunnrRecipe + optionCountFromCombination + requiredBlockIndex + setId + spineResourcePath + } + ... on Ring { + elementalType + equipped + exp + grade + id + + itemSubType + itemType + level + madeWithMimisbrunnrRecipe + optionCountFromCombination + requiredBlockIndex + setId + spineResourcePath + } + ... on TradableMaterial { + elementalType + grade + id + + itemSubType + itemType + requiredBlockIndex + } + ... on Weapon { + elementalType + equipped + exp + grade + id + + itemSubType + itemType + level + madeWithMimisbrunnrRecipe + optionCountFromCombination + requiredBlockIndex + setId + spineResourcePath + } + } + } + } + } + """; + + var result = await TestServices.ExecuteRequestAsync(serviceProvider, b => b.SetDocument(query)); + + await Verify(result); + } +} \ No newline at end of file diff --git a/Mimir/GraphQL/Queries/Query.cs b/Mimir/GraphQL/Queries/Query.cs index 10ec70c4..b2dfba23 100644 --- a/Mimir/GraphQL/Queries/Query.cs +++ b/Mimir/GraphQL/Queries/Query.cs @@ -106,7 +106,7 @@ public async Task GetDailyRewardReceivedBlockIndexAsync(Address address, [ ///
/// The address of the avatar. /// The inventory state for the specified avatar address. - public async Task GetInventoryAsync(Address address, [Service] InventoryRepository repo) => + public async Task GetInventoryAsync(Address address, [Service] IInventoryRepository repo) => (await repo.GetByAddressAsync(address)).Object; /// diff --git a/Mimir/Program.cs b/Mimir/Program.cs index 04358db9..5034c018 100644 --- a/Mimir/Program.cs +++ b/Mimir/Program.cs @@ -45,7 +45,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(); From b1c93ae5cea5fd191d5584fcf7dd1bbc0daa93fd Mon Sep 17 00:00:00 2001 From: wjdghksgml Date: Wed, 4 Dec 2024 20:35:30 +0900 Subject: [PATCH 4/7] add product ids query test --- .../Repositories/ProductsRepository.cs | 6 +++- ...oductIds_Returns_CorrectValue.verified.txt | 5 +++ Mimir.Tests/QueryTests/ProductIdsTest.cs | 33 +++++++++++++++++++ Mimir/GraphQL/Queries/Query.cs | 2 +- Mimir/Program.cs | 2 +- 5 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 Mimir.Tests/QueryTests/ProductIdsTest.GraphQL_Query_ProductIds_Returns_CorrectValue.verified.txt create mode 100644 Mimir.Tests/QueryTests/ProductIdsTest.cs diff --git a/Mimir.MongoDB/Repositories/ProductsRepository.cs b/Mimir.MongoDB/Repositories/ProductsRepository.cs index 1202ca8d..6f7f7481 100644 --- a/Mimir.MongoDB/Repositories/ProductsRepository.cs +++ b/Mimir.MongoDB/Repositories/ProductsRepository.cs @@ -6,7 +6,11 @@ namespace Mimir.MongoDB.Repositories; -public class ProductsRepository(IMongoDbService dbService) +public interface IProductsRepository{ + Task GetByAvatarAddressAsync(Address avatarAddress); +} + +public class ProductsRepository(IMongoDbService dbService):IProductsRepository { public async Task GetByAvatarAddressAsync(Address avatarAddress) { diff --git a/Mimir.Tests/QueryTests/ProductIdsTest.GraphQL_Query_ProductIds_Returns_CorrectValue.verified.txt b/Mimir.Tests/QueryTests/ProductIdsTest.GraphQL_Query_ProductIds_Returns_CorrectValue.verified.txt new file mode 100644 index 00000000..7dd7209e --- /dev/null +++ b/Mimir.Tests/QueryTests/ProductIdsTest.GraphQL_Query_ProductIds_Returns_CorrectValue.verified.txt @@ -0,0 +1,5 @@ +{ + "data": { + "productIds": [] + } +} \ No newline at end of file diff --git a/Mimir.Tests/QueryTests/ProductIdsTest.cs b/Mimir.Tests/QueryTests/ProductIdsTest.cs new file mode 100644 index 00000000..7dc57508 --- /dev/null +++ b/Mimir.Tests/QueryTests/ProductIdsTest.cs @@ -0,0 +1,33 @@ +using Lib9c.Models.Market; +using Libplanet.Crypto; +using Mimir.MongoDB.Bson; +using Mimir.MongoDB.Repositories; +using Mimir.Tests; +using Moq; + +public class ProductIdsTest +{ + [Fact] + public async Task GraphQL_Query_ProductIds_Returns_CorrectValue() + { + // process mokking + var mockRepo = new Mock(); + mockRepo + .Setup(repo => repo.GetByAvatarAddressAsync(It.IsAny
())) + .ReturnsAsync(new ProductsStateDocument(0, default, new ProductsState(), default)); + + var serviceProvider = TestServices.Builder + .With(mockRepo.Object) + .Build(); + + var query = $$""" + query { + productIds(avatarAddress: "0x14f4ec204c3154768f96109fc969904e29ff69b0") + } + """; + + var result = await TestServices.ExecuteRequestAsync(serviceProvider, b => b.SetDocument(query)); + + await Verify(result); + } +} \ No newline at end of file diff --git a/Mimir/GraphQL/Queries/Query.cs b/Mimir/GraphQL/Queries/Query.cs index 10ec70c4..c6142378 100644 --- a/Mimir/GraphQL/Queries/Query.cs +++ b/Mimir/GraphQL/Queries/Query.cs @@ -144,7 +144,7 @@ public async Task GetProductAsync(Guid productId, [Service] ProductRepo ///
/// The address of the avatar. /// The product ids that contained in the products state for the specified avatar address. - public async Task> GetProductIdsAsync(Address avatarAddress, [Service] ProductsRepository repo) => + public async Task> GetProductIdsAsync(Address avatarAddress, [Service] IProductsRepository repo) => (await repo.GetByAvatarAddressAsync(avatarAddress)).Object.ProductIds; /// diff --git a/Mimir/Program.cs b/Mimir/Program.cs index 04358db9..6bf1ca75 100644 --- a/Mimir/Program.cs +++ b/Mimir/Program.cs @@ -51,7 +51,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(); From 087e304d0df964f79c61e6075f62c5b43179d9d1 Mon Sep 17 00:00:00 2001 From: sskskfskfj Date: Wed, 4 Dec 2024 20:38:32 +0900 Subject: [PATCH 5/7] jusiwoo --- Mimir.MongoDB/Bson/PledgeDocument.cs | 3 +- .../Repositories/ActionPointRepository.cs | 1 - .../Repositories/PledgeRepository.cs | 7 ++- ...y_Pledge_Returns_CorrectValue.verified.txt | 16 +++++++ Mimir.Tests/QueryTests/PledgeTest.cs | 43 +++++++++++++++++++ Mimir/GraphQL/Queries/Query.cs | 2 +- Mimir/Program.cs | 2 +- 7 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 Mimir.Tests/QueryTests/PledgeTest.GraphQL_Query_Pledge_Returns_CorrectValue.verified.txt create mode 100644 Mimir.Tests/QueryTests/PledgeTest.cs diff --git a/Mimir.MongoDB/Bson/PledgeDocument.cs b/Mimir.MongoDB/Bson/PledgeDocument.cs index 5b4e41cb..7f3523b0 100644 --- a/Mimir.MongoDB/Bson/PledgeDocument.cs +++ b/Mimir.MongoDB/Bson/PledgeDocument.cs @@ -11,4 +11,5 @@ public record PledgeDocument( Address ContractAddress, bool Contracted, int RefillMead -) : MimirBsonDocument(Address.ToHex(), new DocumentMetadata(1, StoredBlockIndex)) { } +) : MimirBsonDocument(Address.ToHex(), new DocumentMetadata(1, StoredBlockIndex)); + diff --git a/Mimir.MongoDB/Repositories/ActionPointRepository.cs b/Mimir.MongoDB/Repositories/ActionPointRepository.cs index f909024b..8bc47994 100644 --- a/Mimir.MongoDB/Repositories/ActionPointRepository.cs +++ b/Mimir.MongoDB/Repositories/ActionPointRepository.cs @@ -10,7 +10,6 @@ public interface IActionPointRepository { Task GetByAddressAsync(Address address); } - public class ActionPointRepository(IMongoDbService dbService) : IActionPointRepository { public virtual async Task GetByAddressAsync(Address address) diff --git a/Mimir.MongoDB/Repositories/PledgeRepository.cs b/Mimir.MongoDB/Repositories/PledgeRepository.cs index c28032a3..6f3626e3 100644 --- a/Mimir.MongoDB/Repositories/PledgeRepository.cs +++ b/Mimir.MongoDB/Repositories/PledgeRepository.cs @@ -5,8 +5,13 @@ using MongoDB.Driver; namespace Mimir.MongoDB.Repositories; +public interface IPledgeRepository +{ + Task GetByAddressAsync(Address address); +} + -public class PledgeRepository(IMongoDbService dbService) +public class PledgeRepository(IMongoDbService dbService) : IPledgeRepository { public async Task GetByAddressAsync(Address address) { diff --git a/Mimir.Tests/QueryTests/PledgeTest.GraphQL_Query_Pledge_Returns_CorrectValue.verified.txt b/Mimir.Tests/QueryTests/PledgeTest.GraphQL_Query_Pledge_Returns_CorrectValue.verified.txt new file mode 100644 index 00000000..97626671 --- /dev/null +++ b/Mimir.Tests/QueryTests/PledgeTest.GraphQL_Query_Pledge_Returns_CorrectValue.verified.txt @@ -0,0 +1,16 @@ +{ + "data": { + "pledge": { + "address": "0x0000000000000000000000000000000000000000", + "contractAddress": "0x0000000000000000000000000000000000000000", + "contracted": true, + "id": "0000000000000000000000000000000000000000", + "refillMead": 10, + "storedBlockIndex": 10000, + "metadata": { + "schemaVersion": 1, + "storedBlockIndex": 10000 + } + } + } +} \ No newline at end of file diff --git a/Mimir.Tests/QueryTests/PledgeTest.cs b/Mimir.Tests/QueryTests/PledgeTest.cs new file mode 100644 index 00000000..e9d5a777 --- /dev/null +++ b/Mimir.Tests/QueryTests/PledgeTest.cs @@ -0,0 +1,43 @@ +using Libplanet.Crypto; +using Mimir.MongoDB.Bson; +using Mimir.MongoDB.Repositories; +using Mimir.Tests; +using Moq; + +public class PledgeTest +{ + [Fact] + public async Task GraphQL_Query_Pledge_Returns_CorrectValue() + { + var mockRepo = new Mock(); + mockRepo + .Setup(repo => repo.GetByAddressAsync(It.IsAny
())) + .ReturnsAsync(new PledgeDocument(10000, default, default, true, 10)); + + var serviceProvider = TestServices.Builder + .With(mockRepo.Object) + .Build(); + + var address = "0x0000008001000000000200000000030000000004"; + var query = $$""" + query { + pledge(agentAddress: "{{address}}") { + address + contractAddress + contracted + id + refillMead + storedBlockIndex + metadata { + schemaVersion + storedBlockIndex + } + } + } + """; + + var result = await TestServices.ExecuteRequestAsync(serviceProvider, b => b.SetDocument(query)); + + await Verify(result); + } +} \ No newline at end of file diff --git a/Mimir/GraphQL/Queries/Query.cs b/Mimir/GraphQL/Queries/Query.cs index 10ec70c4..71bf6279 100644 --- a/Mimir/GraphQL/Queries/Query.cs +++ b/Mimir/GraphQL/Queries/Query.cs @@ -128,7 +128,7 @@ public async Task GetPetAsync(Address avatarAddress, [Service] PetRepo /// /// Get the pledge state for a given agent address. /// - public async Task GetPledgeAsync(Address agentAddress, [Service] PledgeRepository repo) => + public async Task GetPledgeAsync(Address agentAddress, [Service] IPledgeRepository repo) => await repo.GetByAddressAsync(agentAddress.GetPledgeAddress()); /// diff --git a/Mimir/Program.cs b/Mimir/Program.cs index 04358db9..3b783c3e 100644 --- a/Mimir/Program.cs +++ b/Mimir/Program.cs @@ -49,7 +49,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(); From ffb42944e5ef9ecbd141081f2fba5b0f3c14116c Mon Sep 17 00:00:00 2001 From: wjdghksgml Date: Wed, 4 Dec 2024 20:55:14 +0900 Subject: [PATCH 6/7] add product default list --- ...QL_Query_ProductIds_Returns_CorrectValue.verified.txt | 4 +++- Mimir.Tests/QueryTests/ProductIdsTest.cs | 9 ++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Mimir.Tests/QueryTests/ProductIdsTest.GraphQL_Query_ProductIds_Returns_CorrectValue.verified.txt b/Mimir.Tests/QueryTests/ProductIdsTest.GraphQL_Query_ProductIds_Returns_CorrectValue.verified.txt index 7dd7209e..11fba0c8 100644 --- a/Mimir.Tests/QueryTests/ProductIdsTest.GraphQL_Query_ProductIds_Returns_CorrectValue.verified.txt +++ b/Mimir.Tests/QueryTests/ProductIdsTest.GraphQL_Query_ProductIds_Returns_CorrectValue.verified.txt @@ -1,5 +1,7 @@ { "data": { - "productIds": [] + "productIds": [ + "00000000-0000-0000-0000-000000000000" + ] } } \ No newline at end of file diff --git a/Mimir.Tests/QueryTests/ProductIdsTest.cs b/Mimir.Tests/QueryTests/ProductIdsTest.cs index 7dc57508..448c1eeb 100644 --- a/Mimir.Tests/QueryTests/ProductIdsTest.cs +++ b/Mimir.Tests/QueryTests/ProductIdsTest.cs @@ -14,7 +14,14 @@ public async Task GraphQL_Query_ProductIds_Returns_CorrectValue() var mockRepo = new Mock(); mockRepo .Setup(repo => repo.GetByAvatarAddressAsync(It.IsAny
())) - .ReturnsAsync(new ProductsStateDocument(0, default, new ProductsState(), default)); + .ReturnsAsync(new ProductsStateDocument( + 0, + default, + new ProductsState() + { + ProductIds = new List() {default} + }, + default)); var serviceProvider = TestServices.Builder .With(mockRepo.Object) From 8e186ba6db50a44d0c6d852819070b9600fb93dd Mon Sep 17 00:00:00 2001 From: Jangho614 Date: Wed, 4 Dec 2024 20:59:11 +0900 Subject: [PATCH 7/7] add product query test --- .../Repositories/ProductRepository.cs | 7 ++- ..._Product_Returns_CorrectValue.verified.txt | 8 +++ Mimir.Tests/QueryTests/ProductTest.cs | 49 +++++++++++++++++++ Mimir/GraphQL/Queries/Query.cs | 2 +- Mimir/Program.cs | 2 +- 5 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 Mimir.Tests/QueryTests/ProductTest.GraphQL_Query_Product_Returns_CorrectValue.verified.txt create mode 100644 Mimir.Tests/QueryTests/ProductTest.cs diff --git a/Mimir.MongoDB/Repositories/ProductRepository.cs b/Mimir.MongoDB/Repositories/ProductRepository.cs index 81eacbc9..3bbe110e 100644 --- a/Mimir.MongoDB/Repositories/ProductRepository.cs +++ b/Mimir.MongoDB/Repositories/ProductRepository.cs @@ -15,7 +15,12 @@ namespace Mimir.MongoDB.Repositories; -public class ProductRepository +public interface IProductRepository +{ + Task GetByProductIdAsync(Guid productId); +} + +public class ProductRepository : IProductRepository { private static readonly Codec Codec = new(); diff --git a/Mimir.Tests/QueryTests/ProductTest.GraphQL_Query_Product_Returns_CorrectValue.verified.txt b/Mimir.Tests/QueryTests/ProductTest.GraphQL_Query_Product_Returns_CorrectValue.verified.txt new file mode 100644 index 00000000..71265234 --- /dev/null +++ b/Mimir.Tests/QueryTests/ProductTest.GraphQL_Query_Product_Returns_CorrectValue.verified.txt @@ -0,0 +1,8 @@ +{ + "data": { + "product": { + "sellerAgentAddress": "0x0000000000000000000000000000000000000000", + "sellerAvatarAddress": "0x0000000000000000000000000000000000000000" + } + } +} \ No newline at end of file diff --git a/Mimir.Tests/QueryTests/ProductTest.cs b/Mimir.Tests/QueryTests/ProductTest.cs new file mode 100644 index 00000000..67e5ab80 --- /dev/null +++ b/Mimir.Tests/QueryTests/ProductTest.cs @@ -0,0 +1,49 @@ +using Lib9c.Models.Market; +using Lib9c.Models.States; +using Libplanet.Crypto; +using Mimir.MongoDB.Bson; +using Mimir.MongoDB.Repositories; +using Mimir.Tests; +using Moq; + +namespace Mimir; + +public class ProductTest +{ + [Fact] + public async Task GraphQL_Query_Product_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.GetByProductIdAsync(It.IsAny())) + .ReturnsAsync(new ProductDocument(0, default, default, default, new Product() { + ProductId = default, + RegisteredBlockIndex = 0, + SellerAvatarAddress = default, + SellerAgentAddress = default, + })); + + var serviceProvider = TestServices.Builder + .With(mockRepo.Object) + .Build(); + + var query = $$""" + query { + product(productId: "0b9567ea-d3c0-4074-8c74-56375721c042") { + sellerAgentAddress + sellerAvatarAddress + } + } + """; + + var result = await TestServices.ExecuteRequestAsync(serviceProvider, b => b.SetDocument(query)); + + await Verify(result); + } +} \ No newline at end of file diff --git a/Mimir/GraphQL/Queries/Query.cs b/Mimir/GraphQL/Queries/Query.cs index 1e59be93..b8785f84 100644 --- a/Mimir/GraphQL/Queries/Query.cs +++ b/Mimir/GraphQL/Queries/Query.cs @@ -136,7 +136,7 @@ public async Task GetPledgeAsync(Address agentAddress, [Service] ///
/// The product ID /// The product. - public async Task GetProductAsync(Guid productId, [Service] ProductRepository repo) => + public async Task GetProductAsync(Guid productId, [Service] IProductRepository repo) => (await repo.GetByProductIdAsync(productId)).Object; /// diff --git a/Mimir/Program.cs b/Mimir/Program.cs index b2d3e4c2..c494d79b 100644 --- a/Mimir/Program.cs +++ b/Mimir/Program.cs @@ -50,7 +50,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();