Files
Prowlarr/src/NzbDrone.Core.Test/Framework/TestDatabase.cs
Robin Dadswell cac2729230 Running Integration Tests against Postgres Database (#838)
* Allow configuring postgres with environment variables

(cherry picked from commit 8439df78fea25656a9a1275d2a2fe3f0df0528c7)

* Fix process provider when environment variables alread exist

[common]

(cherry picked from commit 66e5b4025974e081c1406f01a860b1ac52949c22)

* First bash at running integration tests on postgres

(cherry picked from commit f950e80c7e4f9b088ec6a149386160eab83b61c3)

* Postgres integration tests running as part of the build pipeline

(cherry picked from commit 9ca8616f5098778e9b5e6ce09d2aa11224018fab)

* Fixed: Register PostgresOptions when running in utility mode

* fixup!

* fixup!

* fixup!

* fixup!

* fixup!

Co-authored-by: ta264 <ta264@users.noreply.github.com>
Co-authored-by: Qstick <qstick@gmail.com>
2022-07-02 17:48:10 -05:00

89 lines
2.5 KiB
C#

using System.Collections.Generic;
using System.Data;
using System.Linq;
using Moq;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Messaging.Events;
namespace NzbDrone.Core.Test.Framework
{
public interface ITestDatabase
{
void InsertMany<T>(IEnumerable<T> items)
where T : ModelBase, new();
T Insert<T>(T item)
where T : ModelBase, new();
List<T> All<T>()
where T : ModelBase, new();
T Single<T>()
where T : ModelBase, new();
void Update<T>(T childModel)
where T : ModelBase, new();
void Delete<T>(T childModel)
where T : ModelBase, new();
IDirectDataMapper GetDirectDataMapper();
IDbConnection OpenConnection();
DatabaseType DatabaseType { get; }
}
public class TestDatabase : ITestDatabase
{
private readonly IDatabase _dbConnection;
private readonly IEventAggregator _eventAggregator;
public DatabaseType DatabaseType => _dbConnection.DatabaseType;
public TestDatabase(IDatabase dbConnection)
{
_eventAggregator = new Mock<IEventAggregator>().Object;
_dbConnection = dbConnection;
}
public void InsertMany<T>(IEnumerable<T> items)
where T : ModelBase, new()
{
new BasicRepository<T>(_dbConnection, _eventAggregator).InsertMany(items.ToList());
}
public T Insert<T>(T item)
where T : ModelBase, new()
{
return new BasicRepository<T>(_dbConnection, _eventAggregator).Insert(item);
}
public List<T> All<T>()
where T : ModelBase, new()
{
return new BasicRepository<T>(_dbConnection, _eventAggregator).All().ToList();
}
public T Single<T>()
where T : ModelBase, new()
{
return All<T>().SingleOrDefault();
}
public void Update<T>(T childModel)
where T : ModelBase, new()
{
new BasicRepository<T>(_dbConnection, _eventAggregator).Update(childModel);
}
public void Delete<T>(T childModel)
where T : ModelBase, new()
{
new BasicRepository<T>(_dbConnection, _eventAggregator).Delete(childModel);
}
public IDirectDataMapper GetDirectDataMapper()
{
return new DirectDataMapper(_dbConnection);
}
public IDbConnection OpenConnection()
{
return _dbConnection.OpenConnection();
}
}
}