Skip to content

Commit

Permalink
Merge pull request #555 from pty0735/feat/my-work
Browse files Browse the repository at this point in the history
Add integration Test for Pet Metadata Query #548
  • Loading branch information
Atralupus authored Dec 4, 2024
2 parents fddb0ba + d25abd5 commit 8721635
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 3 deletions.
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

0 comments on commit 8721635

Please sign in to comment.