Skip to content

Commit

Permalink
Merge pull request #560 from Jangho614/ProductTestIssue
Browse files Browse the repository at this point in the history
add product query test
  • Loading branch information
Atralupus authored Dec 4, 2024
2 parents 53582d3 + b65141d commit b12d64f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
7 changes: 6 additions & 1 deletion Mimir.MongoDB/Repositories/ProductRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@

namespace Mimir.MongoDB.Repositories;

public class ProductRepository
public interface IProductRepository
{
Task<ProductDocument> GetByProductIdAsync(Guid productId);
}

public class ProductRepository : IProductRepository
{
private static readonly Codec Codec = new();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"data": {
"product": {
"sellerAgentAddress": "0x0000000000000000000000000000000000000000",
"sellerAvatarAddress": "0x0000000000000000000000000000000000000000"
}
}
}
49 changes: 49 additions & 0 deletions Mimir.Tests/QueryTests/ProductTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Lib9c.Models.Market;
using Lib9c.Models.States;
using Libplanet.Crypto;
using Mimir.MongoDB.Bson;
using Mimir.MongoDB.Repositories;
using Mimir.Tests;
using Moq;

namespace Mimir;

public class ProductTest
{
[Fact]
public async Task GraphQL_Query_Product_Returns_CorrectValue()
{

var address = new Address("0x0000000001000000000200000000030000000004");
var state = new CollectionState
{
Ids = new SortedSet<int>(new[] { 1, 2, 3 })
};
var mockRepo = new Mock<IProductRepository>();
mockRepo
.Setup(repo => repo.GetByProductIdAsync(It.IsAny<Guid>()))
.ReturnsAsync(new ProductDocument(0, default, default, default, new Product() {
ProductId = default,
RegisteredBlockIndex = 0,
SellerAvatarAddress = default,
SellerAgentAddress = default,
}));

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

var query = $$"""
query {
product(productId: "0b9567ea-d3c0-4074-8c74-56375721c042") {
sellerAgentAddress
sellerAvatarAddress
}
}
""";

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 @@ -136,7 +136,7 @@ public async Task<PledgeDocument> GetPledgeAsync(Address agentAddress, [Service]
/// </summary>
/// <param name="productId">The product ID</param>
/// <returns>The product.</returns>
public async Task<Product> GetProductAsync(Guid productId, [Service] ProductRepository repo) =>
public async Task<Product> GetProductAsync(Guid productId, [Service] IProductRepository repo) =>
(await repo.GetByProductIdAsync(productId)).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 @@ -49,8 +49,8 @@
builder.Services.AddSingleton<ItemSlotRepository>();
builder.Services.AddSingleton<IMetadataRepository,MetadataRepository>();
builder.Services.AddSingleton<PetRepository>();
builder.Services.AddSingleton<IProductRepository, ProductRepository>();
builder.Services.AddSingleton<IPledgeRepository, PledgeRepository>();
builder.Services.AddSingleton<ProductRepository>();
builder.Services.AddSingleton<IProductsRepository, ProductsRepository>();
builder.Services.AddSingleton<IStakeRepository, StakeRepository>();
builder.Services.AddSingleton<TableSheetsRepository>();
Expand Down

0 comments on commit b12d64f

Please sign in to comment.