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

Add integration Test for Pet Metadata Query #548 #555

Merged
merged 5 commits into from
Dec 4, 2024
Merged
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
1 change: 1 addition & 0 deletions Lib9c.Models/States/PetState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class PetState : IBencodable
public int Level { get; init; }
public long UnlockedBlockIndex { get; init; }

public PetState(){}
[BsonIgnore, GraphQLIgnore, JsonIgnore]
public IValue Bencoded => List.Empty
.Add(PetId.Serialize())
Expand Down
5 changes: 4 additions & 1 deletion Mimir.MongoDB/Repositories/PetRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

namespace Mimir.MongoDB.Repositories;

public class PetRepository(IMongoDbService dbService)
public interface IPetRepository{
Task<PetStateDocument> GetByAvatarAddressAsync(Address avatarAddress);
}
public class PetRepository(IMongoDbService dbService) : IPetRepository
{
public async Task<PetStateDocument> GetByAvatarAddressAsync(Address avatarAddress)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"data": {
"pet": {
"level": 1,
"petId": 1,
"unlockedBlockIndex": 0
}
}
}
50 changes: 50 additions & 0 deletions Mimir.Tests/QueryTests/PetTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Lib9c.Models.States;
using Libplanet.Crypto;
using Mimir.MongoDB.Bson;
using Mimir.MongoDB.Repositories;
using Mimir.Tests;
using Moq;

public class PetTest
{
[Fact]
public async Task GraphQL_Query_Pet_Returns_CorrectValue()
{

// process mokking
var address = new Address();
var mockRepo = new Mock<IPetRepository>();
mockRepo
.Setup(repo => repo.GetByAvatarAddressAsync(It.IsAny<Address>()))
.ReturnsAsync(new PetStateDocument(
1,
new Address(),
new Address(),
new PetState() {
PetId = 1,
Level = 1,
UnlockedBlockIndex = default
}
));

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

var query = $$"""
query {
pet(avatarAddress: "{{address}}") {
level
petId
unlockedBlockIndex
}
}
""";

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

await Verify(result);


}
}
2 changes: 1 addition & 1 deletion Mimir/GraphQL/Queries/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public async Task<MetadataDocument> GetMetadataAsync(string collectionName, [Ser
/// </summary>
/// <param name="avatarAddress">The address of the avatar.</param>
/// <returns>The agent state</returns>
public async Task<PetState> GetPetAsync(Address avatarAddress, [Service] PetRepository repo) =>
public async Task<PetState> GetPetAsync(Address avatarAddress, [Service] IPetRepository repo) =>
(await repo.GetByAvatarAddressAsync(avatarAddress)).Object;

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Mimir/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
builder.Services.AddSingleton<IDailyRewardRepository, DailyRewardRepository>();
builder.Services.AddSingleton<IInventoryRepository, InventoryRepository>();
builder.Services.AddSingleton<ItemSlotRepository>();
builder.Services.AddSingleton<IPetRepository, PetRepository>();
builder.Services.AddSingleton<IMetadataRepository,MetadataRepository>();
builder.Services.AddSingleton<PetRepository>();
builder.Services.AddSingleton<IProductRepository, ProductRepository>();
builder.Services.AddSingleton<IPledgeRepository, PledgeRepository>();
builder.Services.AddSingleton<IProductsRepository, ProductsRepository>();
Expand Down
Loading