using adas_core.Domain.Models.AppSettings;
using adas_core.Domain.Models.MongoModels;
using adas_core.Infrastructure.Repositories;
using Microsoft.Extensions.Options;
using MongoDB.Bson;
using Options = Microsoft.Extensions.Options.Options;
namespace adas_core.Test.Repositories;
[TestFixture]
[Category("Integration")]
public class ServiceConfigRepositoryTest
{
///
/// Performs one-time initialization for integration tests by creating a entry in the test database and preparing the repository used by the test fixture.
///
[OneTimeSetUp]
public async Task Init()
{
_optionsApiSettings = Options.Create(_apiSettings);
var serviceConfig = new ServiceConfig
{
Id = _id,
StrId = Id.ToString()
};
await IntegrationDb.Database.DropCollectionAsync("service_config");
await IntegrationDb.Database.CreateCollectionAsync("service_config");
_repository = new ServiceConfigRepository(_optionsApiSettings, IntegrationDb.Database);
await _repository.InsertOneAsync(serviceConfig);
}
private ServiceConfigRepository _repository;
private readonly ApiSettings _apiSettings = new()
{
ServiceConfig = "service_config"
};
private IOptions _optionsApiSettings;
private readonly ObjectId _id = ObjectId.GenerateNewId();
private static readonly ObjectId Id = ObjectId.GenerateNewId();
///
/// Verifies that .FindById returns a non-null entity when queried by its string identifier,
/// and that the returned entity's StrId matches the supplied id.
///
[Test]
public async Task FindById_Find_string()
{
var result = await _repository.FindById(Id.ToString());
Assert.That(result, Is.Not.Null);
Assert.That(result!.StrId, Is.EqualTo(Id.ToString()));
}
///
/// Verifies that the repository's FindById method successfully retrieves an object by its identifier,
/// returning a non-null result whose Id matches the requested identifier.
///
[Test]
public async Task FindById_Find_ObjectId()
{
var result = await _repository.FindById(_id);
Assert.That(result, Is.Not.Null);
Assert.That(result!.Id, Is.EqualTo(_id));
}
}