Skip to content

Commit

Permalink
Merge pull request #545 from sguoo/test/collection-test
Browse files Browse the repository at this point in the history
Add Collection Test
  • Loading branch information
Atralupus authored Dec 3, 2024
2 parents 230df86 + d49ca81 commit 1b31134
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Lib9c.Models/States/CollectionState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class CollectionState : IBencodable
[BsonIgnore, GraphQLIgnore, JsonIgnore]
public IValue Bencoded => List.Empty.Add(new List(Ids));

public CollectionState() { }

public CollectionState(IValue bencoded)
{
if (bencoded is not List l)
Expand Down
7 changes: 6 additions & 1 deletion Mimir.MongoDB/Repositories/CollectionRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

namespace Mimir.MongoDB.Repositories;

public class CollectionRepository(IMongoDbService dbService)
public interface ICollectionRepository
{
Task<CollectionDocument> GetByAddressAsync(Address address);
}

public class CollectionRepository(IMongoDbService dbService) : ICollectionRepository
{
public async Task<CollectionDocument> GetByAddressAsync(Address address)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"data": {
"collection": {
"ids": [
1,
2,
3
]
}
}
}
38 changes: 38 additions & 0 deletions Mimir.Tests/QueryTests/CollectionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Mimir.MongoDB.Repositories;
using Moq;
using Lib9c.Models.States;
using Libplanet.Crypto;
using Mimir.MongoDB.Bson;

namespace Mimir.Tests.QueryTests;

public class CollectionTest
{
[Fact]
public async Task GraphQL_Query_Collection_Returns_CorrectValue()
{
var address = new Address("0x0000000001000000000200000000030000000004");
var state = new CollectionState
{
Ids = new SortedSet<int>(new[] { 1, 2, 3 })
};
var mockRepo = new Mock<ICollectionRepository>();
mockRepo
.Setup(repo => repo.GetByAddressAsync(It.IsAny<Address>()))
.ReturnsAsync(new CollectionDocument(1, address, state));
var serviceProvider = TestServices.Builder
.With(mockRepo.Object)
.Build();
var query = $$"""
query {
collection(address: "{{address}}") {
ids
}
}
""";

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 @@ -80,7 +80,7 @@ public async Task<string> GetBalanceAsync(
/// </summary>
/// <param name="address">The address of the avatar.</param>
/// <returns>The collection state for the specified avatar address.</returns>
public async Task<CollectionState> GetCollectionAsync(Address address, [Service] CollectionRepository repo) =>
public async Task<CollectionState> GetCollectionAsync(Address address, [Service] ICollectionRepository repo) =>
(await repo.GetByAddressAsync(address)).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 @@ -43,7 +43,7 @@
builder.Services.AddSingleton<ArenaParticipantRepository>();
builder.Services.AddSingleton<IAvatarRepository, AvatarRepository>();
builder.Services.AddSingleton<BalanceRepository>();
builder.Services.AddSingleton<CollectionRepository>();
builder.Services.AddSingleton<ICollectionRepository,CollectionRepository>();
builder.Services.AddSingleton<IDailyRewardRepository, DailyRewardRepository>();
builder.Services.AddSingleton<InventoryRepository>();
builder.Services.AddSingleton<ItemSlotRepository>();
Expand Down

0 comments on commit 1b31134

Please sign in to comment.