Skip to content

Commit

Permalink
Share
Browse files Browse the repository at this point in the history
  • Loading branch information
Atralupus committed Nov 22, 2024
1 parent b46d133 commit 8c0cd5e
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 37 deletions.
14 changes: 10 additions & 4 deletions Mimir.MongoDB/Repositories/ActionPointRepository.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
using Libplanet.Crypto;
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 ActionPointRepository(MongoDbService dbService)
public interface IActionPointRepository
{
Task<ActionPointDocument> GetByAddressAsync(Address address);
}

public class ActionPointRepository(IMongoDbService dbService)
{
public async Task<ActionPointDocument> GetByAddressAsync(Address address)
public virtual async Task<ActionPointDocument> GetByAddressAsync(Address address)
{
var collectionName = CollectionNames.GetCollectionName<ActionPointDocument>();
var collection = dbService.GetCollection<ActionPointDocument>(collectionName);
Expand All @@ -18,7 +23,8 @@ public async Task<ActionPointDocument> GetByAddressAsync(Address address)
{
throw new DocumentNotFoundInMongoCollectionException(
collection.CollectionNamespace.CollectionName,
$"'Address' equals to '{address.ToHex()}'");
$"'Address' equals to '{address.ToHex()}'"
);
}

return document;
Expand Down
13 changes: 13 additions & 0 deletions Mimir.MongoDB/Services/IMongoDbService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.GridFS;

namespace Mimir.MongoDB.Services;

public interface IMongoDbService
{
IMongoCollection<T> GetCollection<T>(string collectionName);
IMongoDatabase GetDatabase();
GridFSBucket GetGridFs();
Task<byte[]> RetrieveFromGridFs(ObjectId fileId);
}
2 changes: 1 addition & 1 deletion Mimir.MongoDB/Services/MongoDbService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Mimir.MongoDB.Services;

public class MongoDbService
public class MongoDbService : IMongoDbService
{
private readonly IMongoDatabase _database;
private readonly GridFSBucket _gridFs;
Expand Down
25 changes: 9 additions & 16 deletions Mimir.Tests/Mimir.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,23 @@
<OutputType>Exe</OutputType>
<RootNamespace>Mimir.Tests</RootNamespace>
<TargetFramework>net8.0</TargetFramework>
<!--
To enable the Microsoft Testing Platform 'dotnet test' experience, add property:
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
To enable the Microsoft Testing Platform native command line experience, add property:
<UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner>
For more information on Microsoft Testing Platform support in xUnit.net, please visit:
https://xunit.net/docs/getting-started/v3/microsoft-testing-platform
-->
</PropertyGroup>

<ItemGroup>
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2"/>
<PackageReference Include="Verify" Version="26.6.0" />
<PackageReference Include="Verify.Xunit" Version="26.6.0" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
<Using Include="Xunit"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit.v3" Version="0.6.0-pre.7" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0-pre.49" />
<ProjectReference Include="..\Mimir\Mimir.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"data": {
"actionPoint": 120
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"data": {
"actionPoint": 120
}
}
20 changes: 20 additions & 0 deletions Mimir.Tests/QueryTests/ActionPointTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Libplanet.Crypto;

namespace Mimir.Tests.QueryTests;

public class ActionPointTests
{
[Fact]
public async Task GetActionPoint_Returns_CorrectValue()
{
// var query =
// @"
// query($address: Address!) {
// actionPoint(address: $address)
// }";
var query = "query { actionPoint(address: \"0x0000000000000000000000000000000000000000\")}";
var result = await TestServices.ExecuteRequestAsync(b => b.SetQuery(query));

await Verify(result);
}
}
68 changes: 68 additions & 0 deletions Mimir.Tests/TestServices.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Numerics;
using System.Security.Cryptography;
using HotChocolate;
using HotChocolate.Execution;
using Lib9c.GraphQL.Types;
using Libplanet.Common;
using Libplanet.Crypto;
using Microsoft.Extensions.DependencyInjection;
using Mimir.MongoDB.Bson;
using Mimir.MongoDB.Repositories;
using Mimir.MongoDB.Services;
using MongoDB.Driver;
using Moq;

namespace Mimir.Tests;

public static class TestServices
{
public static readonly IServiceProvider Services;
public static readonly RequestExecutorProxy Executor;

static TestServices()
{
var serviceCollection = new ServiceCollection();

serviceCollection
.AddGraphQLServer()
.AddLib9cGraphQLTypes()
.AddMimirGraphQLTypes()
.BindRuntimeType(typeof(Address), typeof(AddressType))
.BindRuntimeType(typeof(BigInteger), typeof(BigIntegerType))
.BindRuntimeType(typeof(HashDigest<SHA256>), typeof(HashDigestSHA256Type))
.Services.AddSingleton(sp => new RequestExecutorProxy(
sp.GetRequiredService<IRequestExecutorResolver>(),
Schema.DefaultName
));

var mockMongoDbService = new Mock<IMongoDbService>();
serviceCollection.AddSingleton(mockMongoDbService.Object);
serviceCollection.AddSingleton<ActionPointRepository>();

var mockRepo = new Mock<IActionPointRepository>();
mockRepo
.Setup(repo => repo.GetByAddressAsync(It.IsAny<Address>()))
.ReturnsAsync(new ActionPointDocument(1, new Address(), 120));
serviceCollection.AddSingleton(mockRepo.Object);

Services = serviceCollection.BuildServiceProvider();
Executor = Services.GetRequiredService<RequestExecutorProxy>();
}

public static async Task<string> ExecuteRequestAsync(
Action<IQueryRequestBuilder> configureRequest,
CancellationToken cancellationToken = default
)
{
await using var scope = Services.CreateAsyncScope();

var requestBuilder = new QueryRequestBuilder();
requestBuilder.SetServices(scope.ServiceProvider);
configureRequest(requestBuilder);
var request = requestBuilder.Create();

await using var result = await Executor.ExecuteAsync(request, cancellationToken);
result.ExpectQueryResult();
return result.ToJson();
}
}
10 changes: 0 additions & 10 deletions Mimir.Tests/UnitTest1.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Mimir.Tests/xunit.runner.json

This file was deleted.

2 changes: 1 addition & 1 deletion Mimir/GraphQL/Queries/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Query
/// </summary>
/// <param name="address">The address of the avatar.</param>
/// <returns>The action point.</returns>
public async Task<int> GetActionPointAsync(Address address, [Service] ActionPointRepository repo) =>
public async Task<int> GetActionPointAsync(Address address, [Service] IActionPointRepository repo) =>
(await repo.GetByAddressAsync(address)).Object;

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions Mimir/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@

builder.Services.AddAuthorization();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSingleton<MongoDbService>();
builder.Services.AddSingleton<IMongoDbService>();
// NOTE: MongoDB repositories. Sort in alphabetical order.
builder.Services.AddSingleton<ActionPointRepository>();
builder.Services.AddSingleton<IActionPointRepository>();
builder.Services.AddSingleton<AgentRepository>();
builder.Services.AddSingleton<AllCombinationSlotStateRepository>();
builder.Services.AddSingleton<AllRuneRepository>();
Expand Down

0 comments on commit 8c0cd5e

Please sign in to comment.