40 lines
1.2 KiB
C#
40 lines
1.2 KiB
C#
using MongoDB.Bson;
|
|||
|
|
using MongoDB.Bson.Serialization;
|
||
|
|
using MongoDB.Bson.Serialization.Serializers;
|
||
|
|
using MongoDB.Driver;
|
||
|
|
using MongoDB.Entities;
|
||
|
|
|
||
|
|
[assembly: AssemblyFixture(typeof(PrivaPub.Tests.Support.MongoFixture))]
|
||
|
|
|
||
|
|
namespace PrivaPub.Tests.Support
|
||
|
|
{
|
||
|
|
public sealed class MongoFixture : IAsyncLifetime
|
||
|
|
{
|
||
|
|
public const string Skip = "set PRIVAPUB_TEST_MONGOD=1 (and optionally PRIVAPUB_TEST_MONGO) to run the tests that need a mongod";
|
||
|
|
|
||
|
|
public static bool Enabled => Environment.GetEnvironmentVariable("PRIVAPUB_TEST_MONGOD") == "1";
|
||
|
|
|
||
|
|
public string Database { get; } = $"PrivaPubTests_{Guid.NewGuid():N}";
|
||
|
|
|
||
|
|
public async ValueTask InitializeAsync()
|
||
|
|
{
|
||
|
|
if (!Enabled)
|
||
|
|
return;
|
||
|
|
try
|
||
|
|
{
|
||
|
|
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
|
||
|
|
}
|
||
|
|
catch (BsonSerializationException)
|
||
|
|
{
|
||
|
|
}
|
||
|
|
var connection = Environment.GetEnvironmentVariable("PRIVAPUB_TEST_MONGO") ?? "mongodb://127.0.0.1:27017";
|
||
|
|
await DB.InitAsync(Database, MongoClientSettings.FromConnectionString(connection));
|
||
|
|
}
|
||
|
|
|
||
|
|
public async ValueTask DisposeAsync()
|
||
|
|
{
|
||
|
|
if (Enabled)
|
||
|
|
await DB.Default.Database().Client.DropDatabaseAsync(Database);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|