retroactiune/Retroactiune.IntegrationTests/Retroactiune.WebAPI/Fixtures/MongoDbFixture.cs

49 lines
1.7 KiB
C#
Raw Normal View History

2021-05-30 15:29:30 +00:00
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
2021-05-30 17:39:04 +00:00
using Microsoft.Extensions.Options;
2021-05-30 15:29:30 +00:00
using MongoDB.Driver;
using Retroactiune.Core.Entities;
using Retroactiune.Core.Interfaces;
using Retroactiune.Infrastructure;
2021-05-30 15:29:30 +00:00
namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Fixtures
{
2021-06-07 20:13:21 +00:00
public class MongoDbFixture : IAsyncDisposable
2021-05-30 15:29:30 +00:00
{
private readonly IDatabaseSettings _settings;
private IMongoDatabase Database { get; }
2021-05-30 15:29:30 +00:00
public IMongoCollection<FeedbackReceiver> FeedbackReceiverCollection =>
Database.GetCollection<FeedbackReceiver>(_settings.FeedbackReceiversCollectionName);
2021-05-30 15:29:30 +00:00
public IMongoCollection<Token> TokensCollection =>
Database.GetCollection<Token>(_settings.TokensCollectionName);
public IMongoCollection<Feedback> FeedbacksCollection =>
Database.GetCollection<Feedback>(_settings.FeedbacksCollectionName);
public MongoDbFixture(IOptions<DatabaseSettings> options)
2021-05-30 15:29:30 +00:00
{
2021-05-30 17:39:04 +00:00
_settings = options.Value;
2021-05-30 15:29:30 +00:00
var client = new MongoClient(_settings.ConnectionString);
Database = client.GetDatabase(_settings.DatabaseName);
}
public async Task DropAsync()
{
await Task.WhenAll(
new List<Task>()
{
Database.DropCollectionAsync(_settings.FeedbacksCollectionName),
Database.DropCollectionAsync(_settings.FeedbackReceiversCollectionName),
2021-05-30 15:29:30 +00:00
Database.DropCollectionAsync(_settings.TokensCollectionName)
});
}
2021-06-07 20:13:21 +00:00
public async ValueTask DisposeAsync()
2021-05-30 15:29:30 +00:00
{
2021-06-07 20:13:21 +00:00
await DropAsync();
2021-05-30 15:29:30 +00:00
}
}
}