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

test/BalanceQueryTest #561

Merged
merged 2 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
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.",
Atralupus marked this conversation as resolved.
Show resolved Hide resolved
"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<IInventoryRepository, InventoryRepository>();
Expand Down
Loading