Odd NHibernate Sqllite.InMemory behaviour - unit-testing

I am trying to setup database unit tests using the Sqllite.InMemory functionality.
If I run an unit test, everything is working fine. If I run the same test a second time I get an System.Data.SQLite.SQLiteException: no such table: Person
After waiting for a while and/or (?) restarting Visual studio I can run the unit test once again.
Is there something wrong with the configuration or the session handling?
public abstract class InMemoryDatabaseFixture : IDisposable
{
private const string ConnectionString
= "Data Source=:memory:;Version=3;New=True;Pooling=True;Max Pool Size=1;";
private readonly ISessionFactory _sessionFactory;
private readonly ISession _session;
protected InMemoryDatabaseFixture()
{
var config = SQLiteConfiguration.Standard.InMemory().ShowSql().ConnectionString(ConnectionString);
_sessionFactory = Fluently.Configure()
.Database(config)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<SessionContainer>())
.ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(true, true))
.BuildSessionFactory();
_session = _sessionFactory.OpenSession();
SessionContainer = MockRepository.GenerateMock<ISessionContainer>();
SessionContainer.Stub(sc => sc.SessionFactory).Return(_sessionFactory);
SessionContainer.Stub(sc => sc.Session).Return(_session);
}
protected ISessionContainer SessionContainer { get; private set; }
public void Dispose()
{
_sessionFactory.Dispose();
_session.Dispose();
}
}
Here a simple usage of the base class:
[TestFixture]
public class FinderFixture : InMemoryDatabaseFixture
{
[Test]
public void Test()
{
var finder = new Finder(SessionContainer);
var result = finder.Find();
Assert.That(result, Is.Not.Null);
}
}
Update: After some trials here is finally my working configuration. Exporting the schema after building the SessionFactory is doing the magic.
Configuration configuration = null;
_sessionFactory = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory().ShowSql())
.ExposeConfiguration(cfg => configuration = cfg)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<SessionContainer>())
.BuildSessionFactory();
_session = _sessionFactory.OpenSession();
var export = new SchemaExport(configuration);
export.Execute(true, true, false, _session.Connection, null);

You have requested that connection pooling in the ADO.NET provider be enabled. This will keep the underlying connection active even after NHibernate has closed it.
In my own unit tests, I have simply (based on your original code):
_sessionFactory = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<SessionContainer>())
.ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(true, true))
.BuildSessionFactory();
Also, since the session is generated from the session factory, it would be prudent to dispose all sessions before disposing the session factory.

Related

How do I get my NancyFX unit tests to run my validation?

I'm using NancyFX with FluentValidation, as documented at https://github.com/NancyFx/Nancy/wiki/Nancy-and-Validation. My web app is running fine and validation is working perfectly, but when I try to unit test any of the modules that use validation, I'm getting an error
Nancy.Validation.ModelValidationException : No model validator factory could be located.
Please ensure that you have an appropriate validation package installed, such as
one of the Nancy.Validation packages.
I've verified that my unit test project has references to the Nancy.Validation.FluentValidation and FluentValidation assemblies.
My test code looks like this:
public class ArticleModuleTests {
private Browser browser;
private IDatabase db;
const int USER_ID = 123;
const int ARTICLE_ID = 456;
[SetUp]
public void SetUp() {
var user = new User { Username = "test", Id = USER_ID };
db = A.Fake<IDatabase>();
browser = new Browser(with => {
with.Module<ArticleModule>();
with.RequestStartup((container, pipelines, context) => context.CurrentUser = user);
with.Dependency(db);
});
}
[Test]
public void User_Can_Publish_Article() {
var article = new { title = "Test" };
var result = browser.Post($"/users/{USER_ID}/articles", with => {
with.HttpRequest();
with.Body(JsonConvert.SerializeObject(article));
});
result.StatusCode.ShouldBe(HttpStatusCode.BadRequest);
}
}
My module code is:
public class ArticlesModule : NancyModule {
private IDatabase database;
public ArticlesModule(IDatabase db) {
this.database = db;
Post["/users/{id:int}/articles"] = args => PostArticle(args.id);
}
private dynamic PostArticle(int userId) {
var article = this.Bind<Article>();
var validation = this.Validate(article);
if (!validation.IsValid) return Negotiate.WithModel(validation).WithStatusCode(HttpStatusCode.BadRequest);
database.CreateArticle(userId, article);
return NegotiatorExtensions.WithModel(Negotiate, result)
.WithStatusCode(HttpStatusCode.Created)
.WithHeader("Location", $"http://whatever/users/{userId}/articles/{article.Id}");
}
}
and my validation class is:
public class ArticleValidator : AbstractValidator<Article> {
public ArticleValidator() {
RuleFor(article => article.Title)
.NotEmpty()
.WithMessage("The \"title\" property is required");
RuleFor(article => article.Title)
.Length(2, 50)
.WithMessage("The \"title\" property must be between 2 and 50 characters");
}
}
The NancyFX docs say "Create a validation class... There is no need to register it anywhere as it is automatically detected." - but I'm guessing whatever automatic detection is wired up isn't firing for a unit test project. I'm building on .NET 4.5.2 and using NCrunch as my test runner; what do I need to do to get my test code to pick up the same validation classes as my application modules?
OK, turns out that because NancyFX detects and instantiates validation classes automatically, there's no explicit references in my code to Nancy.Validation.FluentValidation, and so NCrunch is omitting this assembly when building my test project. Setting "Copy referenced assemblies to workspace" in the NCrunch project settings fixed it.

following constructor parameters did not have matching fixture data : DbContextOptions'1options [duplicate]

I have build a WebAPI and apart from my tests running on Postman I would like to implement some Integration/Unit tests.
Now my business logic is very thin, most of the time its more of CRUD actions, therefore I wanted to start with testing my Controllers.
I have a basic setup. Repository pattern (interfaces), Services (business logic) and Controllers.
The flow goes Controller (DI Service) -> Service (DI Repo) -> Repo Action!
So what I did was override my Startup file to change into a in memory database and the rest should be fine (I would assume) Services are added, repos are added and now I am pointing into a in memory DB which is fine for my basic testing.
namespace API.UnitTests
{
public class TestStartup : Startup
{
public TestStartup(IHostingEnvironment env)
: base(env)
{
}
public void ConfigureTestServices(IServiceCollection services)
{
base.ConfigureServices(services);
//services.Replace<IService, IMockedService>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
base.Configure(app, env, loggerFactory);
}
public override void SetUpDataBase(IServiceCollection services)
{
var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = ":memory:" };
var connectionString = connectionStringBuilder.ToString();
var connection = new SqliteConnection(connectionString);
services
.AddEntityFrameworkSqlite()
.AddDbContext<ApplicationDbContext>(
options => options.UseSqlite(connection)
);
}
}
}
I wrote my first test, but the DatasourceService is not there:
The following constructor parameters did not have matching fixture data: DatasourceService datasourceService
namespace API.UnitTests
{
public class DatasourceControllerTest
{
private readonly DatasourceService _datasourceService;
public DatasourceControllerTest(DatasourceService datasourceService)
{
_datasourceService = datasourceService;
}
[Xunit.Theory,
InlineData(1)]
public void GetAll(int companyFk) {
Assert.NotEmpty(_datasourceService.GetAll(companyFk));
}
}
}
What am I missing?
You can't use dependency injection on test classes. You can only let xunit inject special fixtures via constructor (see docs).
For Integration Testing you want to use the TestServer class from Microsoft.AspNetCore.TestHost package and a separate Startup.cs class (easier to setup configuration than inheritance imho).
public class TestStartup : Startup
{
public TestStartup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
public void ConfigureTestServices(IServiceCollection services)
{
services.Replace(ServiceDescriptor.Scoped<IService, MockedService>());
services.AddEntityFrameworkSqlite()
.AddDbContext<ApplicationDbContext>(
options => options.UseSqlite(connection)
);
}
public void Configure(IApplicationBuilder app)
{
// your usual registrations there
}
}
In your unit test project, you need to create an instance of the TestServer and perform the test.
public class DatasourceControllerTest
{
private readonly TestServer _server;
private readonly HttpClient _client;
public DatasourceControllerTest()
{
// Arrange
_server = new TestServer(new WebHostBuilder()
.UseStartup<TestStartup>());
_client = _server.CreateClient();
}
[Xunit.Theory,
InlineData(1)]
public async Task GetAll(int companyFk) {
// Act
var response = await _client.GetAsync($"/api/datasource/{companyFk}");
// expected result from rest service
var expected = #"[{""data"":""value1"", ""data2"":""value2""}]";
// Assert
// This makes sure, you return a success http code back in case of 4xx status codes
// or exceptions (5xx codes) it throws an exception
response.EnsureSuccessStatusCode();
var resultString = await response.Content.ReadAsStringAsync();
Assert.Equals(resultString, expectedString);
}
}
Now, when you call operations which write to the database, you can also check if the data is really written to the database:
[Xunit.Theory,
InlineData(1)]
public async Task GetAll(int companyFk) {
// Act
var response = await _client.DeleteAsync($"/api/datasource/{companyFk}");
// expected result from rest service
// Assert
response.EnsureSuccessStatusCode();
// now check if its really gone in the database. For this you need an instance
// of the in memory Sqlite DB. TestServer has a property Host, which is an IWebHost
// and it has a property Services which is the IoC container
var provider = _server.Host.Services;
var dbContext = provider.GetRequiredService<ApplicationDbContext>();
var result = await dbContext.YourTable.Where(entity => entity.Id == companyFk).Any();
// if it was deleted, the query should result in false
Assert.False(result);
}
Now you can use Xunit.DependencyInjection in your tests.
namespace Your.Test.Project
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IDependency, DependencyClass>();
}
}
}
your DI-classes:
public interface IDependency
{
int Value { get; }
}
internal class DependencyClass : IDependency
{
public int Value => 1;
}
and XUnit-test:
public class MyAwesomeTests
{
private readonly IDependency _d;
public MyAwesomeTests(IDependency d) => _d = d;
[Fact]
public void AssertThatWeDoStuff()
{
Assert.Equal(1, _d.Value);
}
}

Unit testing when Database is involved

I'm quite new to unit testing and I need an hand to understand if I'm doing things in the correct way. My major problem is regarding the DB testing... Here's my code then I'll expose my perplexities
Consider this class that's an item of a pipeline I've to perform
public class RetrieveApplicationUsernamePipelineStep : IPipelineStep
{
public const string RetrieveApplicationUsernameKey = "RetrieveApplicationUsername";
private readonly IRetrieveApplicationUserRepository repository;
public int Order => 3;
public string Name => RetrieveApplicationUsernameKey;
public RetrieveApplicationUsernamePipelineStep(IRetrieveApplicationUserRepository repository)
{
this.repository = repository;
}
public async Task<IDictionary<string, object>> Action(IDictionary<string, object> context)
{
string res = await repository.GetApplicationUser(context);
context[Resources.ApplicationUser] = res;
return context;
}
}
I wrote the following tests
[TestFixture]
public class RetrieveApplicationUsernamePipelineStepTests
{
private IRetrieveApplicationUserRepository retrieveApplicationUserRepository;
[OneTimeSetUp]
public void Start()
{
var configuration = new ConfigurationFromConfigFile();
retrieveApplicationUserRepository = new RetrieveApplicationUserRepository(configuration);
}
[Test]
public async Task ActionSuccessfullyCompleted()
{
var context = new Dictionary<string, object>();
var repository = Substitute.For<IRetrieveApplicationUserRepository>();
repository.GetApplicationUser(context).Returns("user1");
var pipeline = new RetrieveApplicationUsernamePipelineStep(repository);
var res = await pipeline.Action(context);
Assert.IsNotNull(res[Resources.ApplicationUser]);
Assert.IsNotEmpty((string)res[Resources.ApplicationUser]);
}
[Test]
public void ActionFailingCompleted()
{
var context = new Dictionary<string, object>();
var repository = Substitute.For<IRetrieveApplicationUserRepository>();
repository.GetApplicationUser(context).Throws(new UserMappingNotFoundException());
var pipeline = new RetrieveApplicationUsernamePipelineStep(repository);
Assert.ThrowsAsync<UserMappingNotFoundException>(async () => await pipeline.Action(context));
}
[Test]
public void NameTest()
{
var pipeline = new RetrieveApplicationUsernamePipelineStep(retrieveApplicationUserRepository);
Assert.IsTrue(pipeline.Name == RetrieveApplicationUsernamePipelineStep.RetrieveApplicationUsernameKey);
}
[Test]
public void OrderTest()
{
var pipeline = new RetrieveApplicationUsernamePipelineStep(retrieveApplicationUserRepository);
Assert.IsTrue(pipeline.Order == 3);
}
}
And those test works fine since for ActionSuccessfullyCompleted and ActionFailingCompleted I substitute the IRetrieveApplicationUserRepository's result with my expected one.
The real implementation of ther repository is
public class RetrieveApplicationUserRepository : IRetrieveApplicationUserRepository
{
#region Variables
private readonly IConfiguration configuration;
#endregion
#region Ctor
public RetrieveApplicationUserRepository(IConfiguration configuration)
{
this.configuration = configuration;
}
#endregion
#region IRetrieveApplicationUserRepository
public async Task<string> GetApplicationUser(IDictionary<string, object> context)
{
if (configuration.AppSettings[Resources.ApplicationUserFromDomainUserKey] == null)
throw new KeyNotFoundException(Resources.ApplicationUserFromDomainUserKey);
if (string.IsNullOrEmpty(configuration.ConnectionString))
throw new NullReferenceException();
string storedProcedure = configuration.AppSettings.Get(Resources.ApplicationUserFromDomainUserKey);
string result;
using (var sqlConnection = new SqlConnection(configuration.ConnectionString))
{
using (var sqlCommand = new SqlCommand(storedProcedure, sqlConnection))
{
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.AddWithValue("#DOMAINUSER", context[Resources.DomainUser]);
sqlCommand.Parameters.AddWithValue("#DOMAIN", context[Resources.DomainName]);
sqlCommand.Parameters.AddWithValue("#APPID", context[Resources.ApplicationId]);
sqlConnection.Open();
result = (string)await sqlCommand.ExecuteScalarAsync();
}
}
if (result == null)
throw new UserMappingNotFoundException();
return result;
}
#endregion
}
Here're the questions :
Are the test I wrote correct?
I've seen using Resharper's Code Coverage that it wants me to test-cover the properties...is there a way I can avoid this? is this test meaningful?
What's your approach when you've to unit test component that're related to DB? Have you got a real-db that's used for test? Consider that the real DB is about 10Gb so I don't want to have a copy as mdf (condider I can have this) just to test a small portion of the DB
Talking with my colleagues they told me to use test just for TDD while I wish to use them to avoid regressions
Going back to the DB question, I don't want to have a test where I write if username is "John" and maybe tomorrow John user won't be present in the DB anymore,so that the test I expect to pass wont' pass anoymore
The usual approach is to isolate the database side with an abstraction, so you can provide a test dummy (mock, fake, etc) of that abstraction. Only test an actual database when you do the integration testing.
For the tests of database stored procedures, you may well want a different test harness, creating a new test database in memory (equivalently, in a RAM-backed filesystem). You only need to populate enough data for the individual test (we're doing functional testing here, not performance testing), and you may be able to retain table structure across tests with judicious use of rollback.
I have done this, but it's some time ago, so I'll refrain from giving examples that may be no longer state-of-the-art (even if the code does still exist, and if I could find it).

How to create a test for Fluent nHibernate conventions?

I am creating a set of conventions for Fluent nHibernate. I create a few conventions, like primary key, foreign key and many-to-many table.
I would like to be able to test out these conventions with an in memory database to see if I coded these conventions correctly.
Currently, I am setting up nHibernate using an SQlite in-memory database like this:
Configuration configuration = null;
var factory = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory().ShowSql())
.Mappings(m =>
{
m.FluentMappings.AddFromAssemblyOf<DataLayer>();
m.FluentMappings.Conventions.AddFromAssemblyOf<DataLayer>();
})
.ExposeConfiguration((c) => configuration = c)
.BuildSessionFactory();
var session = factory.OpenSession();
var export = new SchemaExport(configuration);
export.Execute(true, true, false, session.Connection, null);
I create a test case, but I don't know test a naming convention. How can I achieve some unit tests on a conventions using a Visual Studio test project?
What I did is not testing against the database, but just test the generated mapping. So for example I have a convention that says that all foreign keys are written like ID and I test it like (I use xunit and not mstest, but hopefully you can get the concept...):
[Fact]
public void AddDefaultConventions_ShouldNameMappingToForeinKeyCorrectly()
{
var configuration = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008)
.Mappings(m =>
{
m.FluentMappings.Conventions.Add(new CustomForeignKeyConvention());
m.FluentMappings.Add<TestClassMap>();
m.FluentMappings.Add<TestClass2Map>();
})
.BuildConfiguration();
var typeMapping = configuration.GetClassMapping(typeof(TestClass2));
var property = typeMapping.GetProperty("Parent");
Assert.Equal("ParentID", property.ColumnIterator.First().Text);
}
private class TestClass
{
public virtual int ID { get; set; }
}
private class TestClass2
{
public virtual int ID { get; set; }
public virtual TestClass Parent { get; set; }
}
private class TestClassMap : ClassMap<TestClass>
{
public TestClassMap()
{
Id(x => x.ID);
}
}
private class TestClass2Map : ClassMap<TestClass2>
{
public TestClass2Map()
{
Id(x => x.ID);
References(x => x.Parent);
}
}
Btw. it wouldn't be too difficult to test against a DB, just try to select something from TestClass2 and make sure no exception is thrown... but I think the way I showed is easier and I trust that when FluentNhibernate can generate the correct NHibernate mapping, NHibernate can generate the correct queries for me.

nhibernate error recovery

I downloaded Rhino Security today and started going through some of the tests. Several that run perfectly in isolation start getting errors after one that purposely raises an exception runs though. Here is that test:
[Test]
public void EntitiesGroup_IfDuplicateName_Error() {
_authorizationRepository.CreateEntitiesGroup("Admininstrators");
_session.Flush();
var ex = Assert.Throws<GenericADOException>(
() =>
{
_authorizationRepository.CreateEntitiesGroup("Admininstrators");
_session.Flush();
}).InnerException;
Assert.That(ex.Message, Is.StringContaining("unique"));
}
And here are the tests and error messages that fail:
[Test]
public void User_CanSave() {
var ayende = new User {Name = "ayende"};
_session.Save(ayende);
_session.Flush();
_session.Evict(ayende);
var fromDb = _session.Get<User>(ayende.Id);
Assert.That(fromDb, Is.Not.Null);
Assert.That(ayende.Name, Is.EqualTo(fromDb.Name));
}
----> System.Data.SQLite.SQLiteException : Abort due to constraint violation column Name is not unique
[Test]
public void UsersGroup_CanCreate()
{
var group = _authorizationRepository.CreateUsersGroup("Admininstrators");
_session.Flush();
_session.Evict(group);
var fromDb = _session.Get<UsersGroup>(group.Id);
Assert.NotNull(fromDb);
Assert.That(fromDb.Name, Is.EqualTo(group.Name));
}
failed: NHibernate.AssertionFailure : null id in Rhino.Security.Tests.User entry (don't flush the Session after an exception occurs)
Does anyone see how I can reset the state of the in memory SQLite db after the first test?
I changed the code to use nunit instead of xunit so maybe that is part of the problem here as well.
Cheers,
Berryl
This is the base class that instantiates the session
public abstract class DatabaseFixture : IDisposable
{
protected Account _account;
protected IAuthorizationRepository _authorizationRepository;
protected IAuthorizationService _authorizationService;
protected IPermissionsBuilderService _permissionsBuilderService;
protected IPermissionsService _permissionService;
protected User _user;
protected ISession _session;
protected readonly ISessionFactory _factory;
protected DatabaseFixture()
{
BeforeSetup();
SillyContainer.SessionProvider = (() => _session);
var sillyContainer = new SillyContainer();
ServiceLocator.SetLocatorProvider(() => sillyContainer);
Assert.NotNull(typeof(System.Data.SQLite.SQLiteConnection));
var cfg = new Configuration()
.SetProperty(Environment.ConnectionDriver, typeof(SQLite20Driver).AssemblyQualifiedName)
.SetProperty(Environment.Dialect, typeof(SQLiteDialect).AssemblyQualifiedName)
.SetProperty(Environment.ConnectionString, ConnectionString)
.SetProperty(Environment.ProxyFactoryFactoryClass, typeof(ProxyFactoryFactory).AssemblyQualifiedName)
.SetProperty(Environment.ReleaseConnections, "on_close")
.SetProperty(Environment.UseSecondLevelCache, "true")
.SetProperty(Environment.UseQueryCache, "true")
.SetProperty(Environment.CacheProvider,typeof(HashtableCacheProvider).AssemblyQualifiedName)
.AddAssembly(typeof (User).Assembly);
Security.Configure<User>(cfg, SecurityTableStructure.Prefix);
_factory = cfg.BuildSessionFactory();
_session = _factory.OpenSession();
new SchemaExport(cfg).Execute(false, true, false, _session.Connection, null);
_session.BeginTransaction();
SetupEntities();
_session.Flush();
}
protected virtual void BeforeSetup() { }
public virtual string ConnectionString { get { return "Data Source=:memory:"; } }
public void Dispose()
{
if (_session.Transaction.IsActive)
_session.Transaction.Rollback();
_session.Dispose();
}
private void SetupEntities()
{
_user = new User {Name = "Ayende"};
_account = new Account {Name = "south sand"};
_session.Save(_user);
_session.Save(_account);
_authorizationService = ServiceLocator.Current.GetInstance<IAuthorizationService>();
_permissionService = ServiceLocator.Current.GetInstance<IPermissionsService>();
_permissionsBuilderService = ServiceLocator.Current.GetInstance<IPermissionsBuilderService>();
_authorizationRepository = ServiceLocator.Current.GetInstance<IAuthorizationRepository>();
_authorizationRepository.CreateUsersGroup("Administrators");
_authorizationRepository.CreateEntitiesGroup("Important Accounts");
_authorizationRepository.CreateOperation("/Account/Edit");
_authorizationRepository.AssociateUserWith(_user, "Administrators");
_authorizationRepository.AssociateEntityWith(_account, "Important Accounts");
}
}
How are you instantiating the session?
Whenever there's an exception, the session must be discarded. That also means you should almost never share the session between test methods.