Skip to content

Commit

Permalink
test/BalanceQueryTest
Browse files Browse the repository at this point in the history
  • Loading branch information
sinjaeu committed Dec 4, 2024
1 parent 1b31134 commit 734b456
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 4 deletions.
9 changes: 7 additions & 2 deletions Mimir.MongoDB/Repositories/BalanceRepository.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
using Libplanet.Crypto;
using Libplanet.Types.Assets;
using Mimir.MongoDB.Exceptions;
using Mimir.MongoDB.Bson;
using Mimir.MongoDB.Exceptions;
using Mimir.MongoDB.Services;
using MongoDB.Driver;

namespace Mimir.MongoDB.Repositories;

public class BalanceRepository(IMongoDbService dbService)
public interface IBalanceRepository
{
Task<BalanceDocument> GetByAddressAsync(Currency currency, Address address);
}

public class BalanceRepository(IMongoDbService dbService) : IBalanceRepository
{
public async Task<BalanceDocument> GetByAddressAsync(Currency currency, Address address)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"data": {
"balance": "fgfgf"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"data": {
"balance": "fgfgf"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"errors": [
{
"message": "Either currency or currencyTicker must be provided.",
"locations": [
{
"line": 2,
"column": 7
}
],
"path": [
"balance"
]
}
],
"data": null
}
80 changes: 80 additions & 0 deletions Mimir.Tests/QueryTests/BalanceQueryTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using HotChocolate.AspNetCore;
using Lib9c.GraphQL.Extensions;
using Libplanet.Crypto;
using Mimir.MongoDB.Bson;
using Mimir.MongoDB.Repositories;
using Moq;

namespace Mimir.Tests.QueryTests;

public class BalanceQueryTest
{
[Fact]
public async Task GraphQL_Query_Balance_CRYSTAL_Returns_CorrectValue()
{
var mockAddress = new Address("0x0000000000000000000000000000000000000000");
var mockRepo = new Mock<IBalanceRepository>();

mockRepo
.Setup(repo => repo.GetByAddressAsync("CRYSTAL".ToCurrency(), mockAddress))
.ReturnsAsync(new BalanceDocument(1, new Address(), "fgfgf"));

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

var query = $$"""
query {
balance(currencyTicker: "CRYSTAL", address: "{{mockAddress}}")
}
""";

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

await Verify(result);
}

[Fact]
public async Task GraphQL_Query_Balance_NCG_Returns_CorrectValue()
{
var mockAddress = new Address("0x0000000000000000000000000000000000000000");
var mockRepo = new Mock<IBalanceRepository>();

mockRepo
.Setup(repo => repo.GetByAddressAsync("NCG".ToCurrency(), mockAddress))
.ReturnsAsync(new BalanceDocument(1, new Address(), "fgfgf"));

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

var query = $$"""
query {
balance(currencyTicker: "NCG", address: "{{mockAddress}}")
}
""";

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

await Verify(result);
}

[Fact]
public async Task GraphQL_Query_Balance_Throws_When_No_Inputs_Provided()
{
var mockRepo = new Mock<IBalanceRepository>();

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

var query = $$"""
query {
balance(address: "0x0000000000000000000000000000000000000000")
}
""";

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 @@ -60,7 +60,7 @@ public async Task<string> GetBalanceAsync(
CurrencyInput? currency,
string? currencyTicker,
Address address,
[Service] BalanceRepository repo)
[Service] IBalanceRepository repo)
{
if (currency is not null)
{
Expand Down
3 changes: 2 additions & 1 deletion Mimir/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Mimir.MongoDB.Repositories;
using Mimir.Options;
using Mimir.MongoDB.Services;
using BalanceRepository = Mimir.MongoDB.Repositories.BalanceRepository;

var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -42,7 +43,7 @@
builder.Services.AddSingleton<ArenaRepository>();
builder.Services.AddSingleton<ArenaParticipantRepository>();
builder.Services.AddSingleton<IAvatarRepository, AvatarRepository>();
builder.Services.AddSingleton<BalanceRepository>();
builder.Services.AddSingleton<IBalanceRepository, BalanceRepository>();
builder.Services.AddSingleton<ICollectionRepository,CollectionRepository>();
builder.Services.AddSingleton<IDailyRewardRepository, DailyRewardRepository>();
builder.Services.AddSingleton<InventoryRepository>();
Expand Down

0 comments on commit 734b456

Please sign in to comment.