Skip to content

Commit

Permalink
add snapshot test for avatar query (#525)
Browse files Browse the repository at this point in the history
Co-authored-by: moreal <dev.moreal@gmail.com>
  • Loading branch information
JH-ummm and moreal authored Nov 24, 2024
1 parent eaa6b5e commit e476608
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Lib9c.Models/Mails/Mail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ public Mail(IValue bencoded)
BlockIndex = d["blockIndex"].ToLong();
RequiredBlockIndex = d["requiredBlockIndex"].ToLong();
}

public Mail()
{

}
}
5 changes: 5 additions & 0 deletions Lib9c.Models/Mails/MailBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ public MailBox(IValue bencoded)
.Select(MailFactory.Create)
.ToList();
}

public MailBox()
{

}
}
5 changes: 5 additions & 0 deletions Lib9c.Models/States/AvatarState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,9 @@ public AvatarState(IValue bencoded) : base(((List)bencoded)[0])
new[] { ValueKind.List },
bencoded.Kind);
}

public AvatarState()
{

}
}
4 changes: 2 additions & 2 deletions Mimir.MongoDB/Repositories/AvatarRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

namespace Mimir.MongoDB.Repositories;

public class AvatarRepository(MongoDbService dbService)
public class AvatarRepository(MongoDbService dbService) : IAvatarRepository
{
public async Task<AvatarDocument> GetByAddressAsync(Address address)
public virtual async Task<AvatarDocument> GetByAddressAsync(Address address)
{
var collectionName = CollectionNames.GetCollectionName<AvatarDocument>();
var collection = dbService.GetCollection<AvatarDocument>(collectionName);
Expand Down
10 changes: 10 additions & 0 deletions Mimir.MongoDB/Repositories/IAvatarRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Libplanet.Crypto;
using Mimir.MongoDB.Bson;

namespace Mimir.MongoDB.Repositories;

public interface IAvatarRepository
{
public Task<AvatarDocument> GetByAddressAsync(Address address);

}
Original file line number Diff line number Diff line change
@@ -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
}
]
}
}
}
118 changes: 118 additions & 0 deletions Mimir.Tests/QueryTests/AvatarQueryTest.cs
Original file line number Diff line number Diff line change
@@ -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<Mail> 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<Address>(){new Address()},
RankingMapAddress = default,
Name = "TestAvatar",
CharacterId = 1,
Level = 0,
Exp = 0,
UpdatedAt = 0,



// Add other mock data as needed
};

var mockRepo = new Mock<IAvatarRepository>();
mockRepo
.Setup(repo => repo.GetByAddressAsync(It.IsAny<Address>()))
.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);

}
}
6 changes: 6 additions & 0 deletions Mimir.Tests/TestServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static IServiceProvider CreateServices(
Mock<IMongoDbService>? mongoDbServiceMock = null,
Mock<IActionPointRepository>? actionPointRepositoryMock = null,
Mock<IAgentRepository>? agentRepositoryMock = null,
Mock<IAvatarRepository>? avatarRepositoryMock = null,
Mock<IAllCombinationSlotStateRepository>? allCombinationSlotStateRepositoryMock = null,
Mock<IDailyRewardRepository>? dailyRewardRepositoryMock = null
)
Expand Down Expand Up @@ -60,6 +61,11 @@ public static IServiceProvider CreateServices(

serviceCollection.AddSingleton<DailyRewardDocument>();

if (avatarRepositoryMock is not null)
{
serviceCollection.AddSingleton<IAvatarRepository>(avatarRepositoryMock.Object);
}

configure?.Invoke(serviceCollection);

return serviceCollection.BuildServiceProvider();
Expand Down
2 changes: 1 addition & 1 deletion Mimir/GraphQL/Queries/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task<AgentState> GetAgentAsync(Address address, [Service] IAgentRep
/// </summary>
/// <param name="address">The address of the avatar.</param>
/// <returns>The avatar state</returns>
public async Task<AvatarState> GetAvatarAsync(Address address, [Service] AvatarRepository repo) =>
public async Task<AvatarState> GetAvatarAsync(Address address, [Service] IAvatarRepository repo) =>
(await repo.GetByAddressAsync(address)).Object;

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions Mimir/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
builder.Services.AddSingleton<AllRuneRepository>();
builder.Services.AddSingleton<ArenaRepository>();
builder.Services.AddSingleton<AvatarRepository>();
builder.Services.AddSingleton<IAvatarRepository, AvatarRepository>();
builder.Services.AddSingleton<BalanceRepository>();
builder.Services.AddSingleton<CollectionRepository>();
builder.Services.AddSingleton<DailyRewardRepository>();
Expand Down

0 comments on commit e476608

Please sign in to comment.