From e476608ad24a1bd7c423ea7de9fefee8af8e9e70 Mon Sep 17 00:00:00 2001 From: JH-ummm <138287752+JH-ummm@users.noreply.github.com> Date: Mon, 25 Nov 2024 07:28:38 +0900 Subject: [PATCH] add snapshot test for avatar query (#525) Co-authored-by: moreal --- Lib9c.Models/Mails/Mail.cs | 5 + Lib9c.Models/Mails/MailBox.cs | 5 + Lib9c.Models/States/AvatarState.cs | 5 + .../Repositories/AvatarRepository.cs | 4 +- .../Repositories/IAvatarRepository.cs | 10 ++ ...y_Avatar_Returns_CorrectValue.verified.txt | 46 +++++++ Mimir.Tests/QueryTests/AvatarQueryTest.cs | 118 ++++++++++++++++++ Mimir.Tests/TestServices.cs | 6 + Mimir/GraphQL/Queries/Query.cs | 2 +- Mimir/Program.cs | 1 + 10 files changed, 199 insertions(+), 3 deletions(-) create mode 100644 Mimir.MongoDB/Repositories/IAvatarRepository.cs create mode 100644 Mimir.Tests/QueryTests/AvatarQueryTest.GraphQL_Query_Avatar_Returns_CorrectValue.verified.txt create mode 100644 Mimir.Tests/QueryTests/AvatarQueryTest.cs diff --git a/Lib9c.Models/Mails/Mail.cs b/Lib9c.Models/Mails/Mail.cs index a6bb0e61..f74a9111 100644 --- a/Lib9c.Models/Mails/Mail.cs +++ b/Lib9c.Models/Mails/Mail.cs @@ -41,4 +41,9 @@ public Mail(IValue bencoded) BlockIndex = d["blockIndex"].ToLong(); RequiredBlockIndex = d["requiredBlockIndex"].ToLong(); } + + public Mail() + { + + } } diff --git a/Lib9c.Models/Mails/MailBox.cs b/Lib9c.Models/Mails/MailBox.cs index b4c6c75b..d79e572b 100644 --- a/Lib9c.Models/Mails/MailBox.cs +++ b/Lib9c.Models/Mails/MailBox.cs @@ -35,4 +35,9 @@ public MailBox(IValue bencoded) .Select(MailFactory.Create) .ToList(); } + + public MailBox() + { + + } } diff --git a/Lib9c.Models/States/AvatarState.cs b/Lib9c.Models/States/AvatarState.cs index 2232e4a4..2e9c4474 100644 --- a/Lib9c.Models/States/AvatarState.cs +++ b/Lib9c.Models/States/AvatarState.cs @@ -101,4 +101,9 @@ public AvatarState(IValue bencoded) : base(((List)bencoded)[0]) new[] { ValueKind.List }, bencoded.Kind); } + + public AvatarState() + { + + } } diff --git a/Mimir.MongoDB/Repositories/AvatarRepository.cs b/Mimir.MongoDB/Repositories/AvatarRepository.cs index d21a3a69..aa98a017 100644 --- a/Mimir.MongoDB/Repositories/AvatarRepository.cs +++ b/Mimir.MongoDB/Repositories/AvatarRepository.cs @@ -6,9 +6,9 @@ namespace Mimir.MongoDB.Repositories; -public class AvatarRepository(MongoDbService dbService) +public class AvatarRepository(MongoDbService dbService) : IAvatarRepository { - public async Task GetByAddressAsync(Address address) + public virtual async Task GetByAddressAsync(Address address) { var collectionName = CollectionNames.GetCollectionName(); var collection = dbService.GetCollection(collectionName); diff --git a/Mimir.MongoDB/Repositories/IAvatarRepository.cs b/Mimir.MongoDB/Repositories/IAvatarRepository.cs new file mode 100644 index 00000000..bfd81224 --- /dev/null +++ b/Mimir.MongoDB/Repositories/IAvatarRepository.cs @@ -0,0 +1,10 @@ +using Libplanet.Crypto; +using Mimir.MongoDB.Bson; + +namespace Mimir.MongoDB.Repositories; + +public interface IAvatarRepository +{ + public Task GetByAddressAsync(Address address); + +} \ No newline at end of file diff --git a/Mimir.Tests/QueryTests/AvatarQueryTest.GraphQL_Query_Avatar_Returns_CorrectValue.verified.txt b/Mimir.Tests/QueryTests/AvatarQueryTest.GraphQL_Query_Avatar_Returns_CorrectValue.verified.txt new file mode 100644 index 00000000..5e6387a9 --- /dev/null +++ b/Mimir.Tests/QueryTests/AvatarQueryTest.GraphQL_Query_Avatar_Returns_CorrectValue.verified.txt @@ -0,0 +1,46 @@ +{ + "data": { + "avatar": { + "address": "0x0000000000000000000000000000000000000000", + "agentAddress": "0x0000000000000000000000000000000000000001", + "blockIndex": 0, + "characterId": 1, + "name": "TestAvatar", + "level": 0, + "eventMap": [ + { + "key": 1, + "value": 1 + } + ], + "itemMap": [ + { + "key": 1, + "value": 1 + } + ], + "mailBox": { + "mails": [ + { + "blockIndex": 0, + "id": "a9320f02-7794-4c3b-8071-701d501f9529", + "requiredBlockIndex": 0, + "typeId": "A" + } + ] + }, + "monsterMap": [ + { + "key": 1, + "value": 1 + } + ], + "stageMap": [ + { + "key": 1, + "value": 1 + } + ] + } + } +} \ No newline at end of file diff --git a/Mimir.Tests/QueryTests/AvatarQueryTest.cs b/Mimir.Tests/QueryTests/AvatarQueryTest.cs new file mode 100644 index 00000000..a8fee94d --- /dev/null +++ b/Mimir.Tests/QueryTests/AvatarQueryTest.cs @@ -0,0 +1,118 @@ +using Lib9c.Models.Mails; +using Lib9c.Models.States; +using Libplanet.Crypto; +using Mimir.MongoDB.Bson; +using Mimir.MongoDB.Repositories; +using Moq; +using Nekoyume.Model; + +namespace Mimir.Tests.QueryTests; + +public class AvatarQueryTest +{ + [Fact] + public async Task GraphQL_Query_Avatar_Returns_CorrectValue() + { + var mockAddress = new Address("0x0000000000000000000000000000000000000000"); + + var stageMap = new CollectionMap(); + var monsterMap = new CollectionMap(); + var itemMap = new CollectionMap(); + var eventMap = new CollectionMap(); + Mail mail = new Mail() + { + BlockIndex = 0, + Id = Guid.Parse("a9320f02-7794-4c3b-8071-701d501f9529"), + RequiredBlockIndex = 0, + TypeId = "A" + }; + + List mails = new(); + mails.Add(mail); + MailBox mailBox = new MailBox() {Mails = mails}; + + stageMap.Add(1, 1); + monsterMap.Add(1, 1); + itemMap.Add(1, 1); + eventMap.Add(1, 1); + + var avatar = new AvatarState + { + Address = default, + Version = 1, + AgentAddress = new Address("0x0000000000000000000000000000000000000001"), + MailBox = mailBox, + BlockIndex = 0, + DailyRewardReceivedIndex = 0, + ActionPoint = 0, + StageMap = stageMap, + MonsterMap = monsterMap, + ItemMap = itemMap, + EventMap = eventMap, + Hair = 0, + Lens = 0, + Ear = 0, + Tail = 0, + CombinationSlotAddresses = new List
(){new Address()}, + RankingMapAddress = default, + Name = "TestAvatar", + CharacterId = 1, + Level = 0, + Exp = 0, + UpdatedAt = 0, + + + + // Add other mock data as needed + }; + + var mockRepo = new Mock(); + mockRepo + .Setup(repo => repo.GetByAddressAsync(It.IsAny
())) + .ReturnsAsync(new AvatarDocument(0, new Address(), avatar)); + + var serviceProvider = TestServices.CreateServices(avatarRepositoryMock: mockRepo); + + var query = """ + query { + avatar(address: "0x0000000000000000000000000000000000000000") { + address + agentAddress + blockIndex + characterId + name + level + eventMap { + key + value + } + itemMap { + key + value + } + mailBox { + mails { + blockIndex + id + requiredBlockIndex + typeId + } + } + monsterMap { + key + value + } + stageMap { + key + value + } + } + } + """; + + var result = await TestServices.ExecuteRequestAsync(serviceProvider, b => b.SetQuery(query)); + + await Verify(result); + + } +} \ No newline at end of file diff --git a/Mimir.Tests/TestServices.cs b/Mimir.Tests/TestServices.cs index f2a5623d..d5cd0103 100644 --- a/Mimir.Tests/TestServices.cs +++ b/Mimir.Tests/TestServices.cs @@ -22,6 +22,7 @@ public static IServiceProvider CreateServices( Mock? mongoDbServiceMock = null, Mock? actionPointRepositoryMock = null, Mock? agentRepositoryMock = null, + Mock? avatarRepositoryMock = null, Mock? allCombinationSlotStateRepositoryMock = null, Mock? dailyRewardRepositoryMock = null ) @@ -60,6 +61,11 @@ public static IServiceProvider CreateServices( serviceCollection.AddSingleton(); + if (avatarRepositoryMock is not null) + { + serviceCollection.AddSingleton(avatarRepositoryMock.Object); + } + configure?.Invoke(serviceCollection); return serviceCollection.BuildServiceProvider(); diff --git a/Mimir/GraphQL/Queries/Query.cs b/Mimir/GraphQL/Queries/Query.cs index 39fec058..57434cba 100644 --- a/Mimir/GraphQL/Queries/Query.cs +++ b/Mimir/GraphQL/Queries/Query.cs @@ -45,7 +45,7 @@ public async Task GetAgentAsync(Address address, [Service] IAgentRep /// /// The address of the avatar. /// The avatar state - public async Task GetAvatarAsync(Address address, [Service] AvatarRepository repo) => + public async Task GetAvatarAsync(Address address, [Service] IAvatarRepository repo) => (await repo.GetByAddressAsync(address)).Object; /// diff --git a/Mimir/Program.cs b/Mimir/Program.cs index e85f61b2..2e237d4e 100644 --- a/Mimir/Program.cs +++ b/Mimir/Program.cs @@ -41,6 +41,7 @@ builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton();