Skip to content

Commit

Permalink
REF Tests
Browse files Browse the repository at this point in the history
Tests run under a disposable localdb database, with a GUID name. This ensure that SQL constraints will be respected.
Tests run in 24 sec now.
User now has a Guid identity column in order to avoir IDENTITY_INSERT ON/OFF on localdb database (cf dotnet/efcore#703)
I have used a TestFixture to encapsulate the creation/deletion of the database
NB : Domain tests could be converted to use InMemoryStorage for faster run
  • Loading branch information
nfaugout-lucca committed Oct 3, 2018
1 parent 3dd0c7b commit 1cb5cbf
Show file tree
Hide file tree
Showing 14 changed files with 294 additions and 207 deletions.
63 changes: 38 additions & 25 deletions Domain/RDD.Domain.Tests/AppControllerTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using RDD.Domain.Models.Querying;
using RDD.Domain.Tests.Models;
using RDD.Domain.Tests.Templates;
using RDD.Infra.Storage;
using RDD.Web.Models;
using System;
Expand All @@ -11,66 +10,80 @@

namespace RDD.Domain.Tests
{
public class AppControllerTests : SingleContextTests
public class AppControllerTests : IClassFixture<DefaultFixture>
{
private DefaultFixture _fixture;

public AppControllerTests(DefaultFixture fixture)
{
_fixture = fixture;
}

[Fact]
public async Task PostShouldNotCallGetByIdOnTheCollection()
{
using (var storage = _newStorage(Guid.NewGuid().ToString()))
await _fixture.RunCodeInsideIsolatedDatabaseAsync(async (context) =>
{
var repo = new Repository<User>(storage, _rightsService);
var users = new UsersCollectionWithHardcodedGetById(repo, _patcherProvider, Instanciator);
var storage = new EFStorageService(context);
var repo = new Repository<User>(storage, _fixture.RightsService);
var users = new UsersCollectionWithHardcodedGetById(repo, _fixture.PatcherProvider, _fixture.Instanciator);
var controller = new UsersAppController(storage, users);
var query = new Query<User>();
query.Options.CheckRights = false;
var candidate = Candidate<User, int>.Parse(@"{ ""id"": 3 }");
var id = Guid.NewGuid();
var candidate = Candidate<User, Guid>.Parse($@"{{ ""id"": ""{id}"" }}");
var user = await controller.CreateAsync(candidate, query);
Assert.Equal(3, user.Id);
}
Assert.Equal(id, user.Id);
});
}

[Fact]
public async Task PostShouldNotCallGetByIdsOnTheCollection()
{
using (var storage = _newStorage(Guid.NewGuid().ToString()))
await _fixture.RunCodeInsideIsolatedDatabaseAsync(async (context) =>
{
var repo = new Repository<User>(storage, _rightsService);
var users = new UsersCollectionWithHardcodedGetById(repo, _patcherProvider, Instanciator);
var storage = new EFStorageService(context);
var repo = new Repository<User>(storage, _fixture.RightsService);
var users = new UsersCollectionWithHardcodedGetById(repo, _fixture.PatcherProvider, _fixture.Instanciator);
var controller = new UsersAppController(storage, users);
var query = new Query<User>();
query.Options.CheckRights = false;
var candidate1 = Candidate<User, int>.Parse(@"{ ""id"": 3 }");
var candidate2 = Candidate<User, int>.Parse(@"{ ""id"": 4 }");
var id1 = Guid.NewGuid();
var id2 = Guid.NewGuid();
var candidate1 = Candidate<User, Guid>.Parse($@"{{ ""id"": ""{id1}"" }}");
var candidate2 = Candidate<User, Guid>.Parse($@"{{ ""id"": ""{id2}"" }}");
var result = (await controller.CreateAsync(new List<Candidate<User, int>> { candidate1, candidate2 }, query)).ToList();
var result = (await controller.CreateAsync(new List<Candidate<User, Guid>> { candidate1, candidate2 }, query)).ToList();
Assert.Equal(3, result[0].Id);
Assert.Equal(4, result[1].Id);
}
Assert.Equal(id1, result[0].Id);
Assert.Equal(id2, result[1].Id);
});
}

[Fact]
public async Task PutShouldNotCallGetByIdOnTheCollection()
{
using (var storage = _newStorage(Guid.NewGuid().ToString()))
await _fixture.RunCodeInsideIsolatedDatabaseAsync(async (context) =>
{
var repo = new Repository<User>(storage, _rightsService);
var users = new UsersCollectionWithHardcodedGetById(repo, _patcherProvider, Instanciator);
var storage = new EFStorageService(context);
var repo = new Repository<User>(storage, _fixture.RightsService);
var users = new UsersCollectionWithHardcodedGetById(repo, _fixture.PatcherProvider, _fixture.Instanciator);
var controller = new UsersAppController(storage, users);
var query = new Query<User>();
query.Options.CheckRights = false;
var candidate = Candidate<User, int>.Parse(@"{ ""id"": 3 }");
var id = Guid.NewGuid();
var candidate = Candidate<User, Guid>.Parse($@"{{ ""id"": ""{id}"" }}");
await controller.CreateAsync(candidate, query);
candidate = Candidate<User, int>.Parse(@"{ ""name"": ""newName"" }");
candidate = Candidate<User, Guid>.Parse($@"{{ ""name"": ""newName"" }}");
var user = await controller.UpdateByIdAsync(3, candidate, query);
var user = await controller.UpdateByIdAsync(id, candidate, query);
Assert.Equal(3, user.Id);
}
Assert.Equal(id, user.Id);
});
}
}
}
119 changes: 68 additions & 51 deletions Domain/RDD.Domain.Tests/CollectionMethodsTests.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using Moq;
using Newtonsoft.Json;
using RDD.Domain.Exceptions;
using RDD.Domain.Helpers;
using RDD.Domain.Mocks;
using RDD.Domain.Models;
using RDD.Domain.Models.Querying;
using RDD.Domain.Patchers;
using RDD.Domain.Rights;
using RDD.Domain.Tests.Models;
using RDD.Domain.Tests.Templates;
using RDD.Infra.Storage;
using RDD.Web.Models;
using System;
Expand All @@ -18,52 +16,63 @@
using Xunit;
namespace RDD.Domain.Tests
{
public class CollectionMethodsTests : SingleContextTests
public class CollectionMethodsTests : IClassFixture<DefaultFixture>
{
private DefaultFixture _fixture;

public CollectionMethodsTests(DefaultFixture fixture)
{
_fixture = fixture;
}

[Fact]
public async Task GetById_SHOULD_not_throw_exception_and_return_null_WHEN_id_does_not_exist()
{
using (var storage = _newStorage(Guid.NewGuid().ToString()))
await _fixture.RunCodeInsideIsolatedDatabaseAsync(async (context) =>
{
var repo = new Repository<User>(storage, _rightsService);
var users = new UsersCollection(repo, _patcherProvider, Instanciator);
var storage = new EFStorageService(context);
var repo = new Repository<User>(storage, _fixture.RightsService);
var users = new UsersCollection(repo, _fixture.PatcherProvider, _fixture.Instanciator);
Assert.Null(await users.GetByIdAsync(0, new Query<User>()));
}
Assert.Null(await users.GetByIdAsync(Guid.NewGuid(), new Query<User>()));
});
}

[Fact]
public async Task Put_SHOULD_NOT_throw_notfound_exception_WHEN_unexisting_entity_()
{
using (var storage = _newStorage(Guid.NewGuid().ToString()))
await _fixture.RunCodeInsideIsolatedDatabaseAsync(async (context) =>
{
var storage = new EFStorageService(context);
Expression<Func<User, bool>> trueFilter = t => true;
var rightService = new Mock<IRightExpressionsHelper<User>>();
rightService.Setup(s => s.GetFilter(It.IsAny<Query<User>>())).Returns(trueFilter);
var user = new User { Id = 3 };
var id = Guid.NewGuid();
var user = new User { Id = id };
var repo = new Repository<User>(storage, rightService.Object);
var users = new UsersCollection(repo, _patcherProvider, Instanciator);
var users = new UsersCollection(repo, _fixture.PatcherProvider, _fixture.Instanciator);
var app = new UsersAppController(storage, users);
await app.CreateAsync(Candidate<User, int>.Parse(@"{ ""id"": 3 }"), new Query<User>());
await app.CreateAsync(Candidate<User, Guid>.Parse($@"{{ ""id"": ""{id}"" }}"), new Query<User>());
await app.UpdateByIdAsync(0, Candidate<User, int>.Parse(@"{ ""name"": ""new name"" }"), new Query<User>());
}
await app.UpdateByIdAsync(Guid.NewGuid(), Candidate<User, Guid>.Parse(@"{ ""name"": ""new name"" }"), new Query<User>());
});
}

[Fact]
public async Task Post_SHOULD_work_WHEN_InstantiateEntityIsNotOverridenAndEntityHasAParameterlessConstructor()
{
using (var storage = _newStorage(Guid.NewGuid().ToString()))
await _fixture.RunCodeInsideIsolatedDatabaseAsync(async (context) =>
{
var repo = new Repository<User>(storage, _rightsService);
var users = new UsersCollection(repo, _patcherProvider, Instanciator);
var storage = new EFStorageService(context);
var repo = new Repository<User>(storage, _fixture.RightsService);
var users = new UsersCollection(repo, _fixture.PatcherProvider, _fixture.Instanciator);
var query = new Query<User>();
query.Options.CheckRights = false;
await users.CreateAsync(Candidate<User, int>.Parse(@"{ ""id"": 3 }"), query);
}
await users.CreateAsync(Candidate<User, Guid>.Parse($@"{{ ""id"": ""{Guid.NewGuid()}"" }}"), query);
});
}

class InstanciatorImplementation : IInstanciator<UserWithParameters>
Expand All @@ -80,68 +89,75 @@ public UserWithParameters InstanciateNew(ICandidate<UserWithParameters> candidat
[Fact]
public async Task Post_SHOULD_work_WHEN_InstantiateEntityIsOverridenAndEntityHasParametersInConstructor()
{
using (var storage = _newStorage(Guid.NewGuid().ToString()))
await _fixture.RunCodeInsideIsolatedDatabaseAsync(async (context) =>
{
var storage = new EFStorageService(context);
var repo = new Repository<UserWithParameters>(storage, new RightsServiceMock<UserWithParameters>());
var users = new UsersCollectionWithParameters(repo, _patcherProvider, new InstanciatorImplementation());
var users = new UsersCollectionWithParameters(repo, _fixture.PatcherProvider, new InstanciatorImplementation());
var query = new Query<UserWithParameters>();
query.Options.CheckRights = false;
var result = await users.CreateAsync(Candidate<UserWithParameters, int>.Parse(@"{ ""id"": 3, ""name"": ""John"" }"), query);
var result = await users.CreateAsync(Candidate<UserWithParameters, int>.Parse($@"{{ ""id"": 3, ""name"": ""John"" }}"), query);
Assert.Equal(3, result.Id);
Assert.Equal("John", result.Name);
}
});
}

[Fact]
public async Task AnyAsync_should_work()
{
using (var storage = _newStorage(Guid.NewGuid().ToString()))
await _fixture.RunCodeInsideIsolatedDatabaseAsync(async (context) =>
{
var user = new User { Id = 2 };
var repo = new Repository<User>(storage, _rightsService);
var users = new UsersCollection(repo, _patcherProvider, Instanciator);
var storage = new EFStorageService(context);
var id = Guid.NewGuid();
var user = new User { Id = id };
var repo = new Repository<User>(storage, _fixture.RightsService);
var users = new UsersCollection(repo, _fixture.PatcherProvider, _fixture.Instanciator);
var query = new Query<User>();
query.Options.CheckRights = false;
storage.Add(user);
await storage.SaveChangesAsync();
var any = await users.AnyAsync(query);
Assert.True(any);
}
});
}

[Fact]
public async Task Put_serializedEntity_should_work()
{
using (var storage = _newStorage(Guid.NewGuid().ToString()))
await _fixture.RunCodeInsideIsolatedDatabaseAsync(async (context) =>
{
var user = new User { Id = 2, Name = "Name", Salary = 1, TwitterUri = new Uri("https://twitter.com") };
var repo = new Repository<User>(storage, _rightsService);
var users = new UsersCollection(repo, _patcherProvider, Instanciator);
var storage = new EFStorageService(context);
var id = Guid.NewGuid();
var user = new User { Id = id, Name = "Name", Salary = 1, TwitterUri = new Uri("https://twitter.com") };
var repo = new Repository<User>(storage, _fixture.RightsService);
var users = new UsersCollection(repo, _fixture.PatcherProvider, _fixture.Instanciator);
var query = new Query<User>();
query.Options.CheckRights = false;
storage.Add(user);
await storage.SaveChangesAsync();
await users.UpdateByIdAsync(2, Candidate<User, int>.Parse(JsonConvert.SerializeObject(user)), query);
await users.UpdateByIdAsync(id, Candidate<User, Guid>.Parse(JsonConvert.SerializeObject(user)), query);
Assert.True(true);
}
});
}

[Fact]
public async Task Get_withFilters_shouldWork()
{
using (var storage = _newStorage(Guid.NewGuid().ToString()))
await _fixture.RunCodeInsideIsolatedDatabaseAsync(async (context) =>
{
var user = new User { Id = 2, Name = "Name", Salary = 1, TwitterUri = new Uri("https://twitter.com") };
var repo = new Repository<User>(storage, _rightsService);
var users = new UsersCollection(repo, _patcherProvider, Instanciator);
var storage = new EFStorageService(context);
var id = Guid.NewGuid();
var user = new User { Id = id, Name = "Name", Salary = 1, TwitterUri = new Uri("https://twitter.com") };
var repo = new Repository<User>(storage, _fixture.RightsService);
var users = new UsersCollection(repo, _fixture.PatcherProvider, _fixture.Instanciator);
var query = new Query<User>();
query.Options.CheckRights = false;
Expand All @@ -153,19 +169,20 @@ public async Task Get_withFilters_shouldWork()
var results = await users.GetAsync(query);
Assert.Equal(1, results.Count);
}
});
}

[Fact]
public void Collection_on_hierarchy_fails()
{
Assert.ThrowsAsync<BadRequestException>(async () =>
Assert.ThrowsAsync<BadRequestException>(async () =>
{
using (var storage = _newStorage(Guid.NewGuid().ToString()))
await _fixture.RunCodeInsideIsolatedDatabaseAsync(async (context) =>
{
var storage = new EFStorageService(context);
var repo = new Repository<Hierarchy>(storage, new Mock<IRightExpressionsHelper<Hierarchy>>().Object);
var instanciator = new BaseClassInstanciator<Hierarchy>(new InheritanceConfiguration());
var collection = new RestCollection<Hierarchy, int>(repo, new ObjectPatcher<Hierarchy>(_patcherProvider), instanciator);
var collection = new RestCollection<Hierarchy, int>(repo, new ObjectPatcher<Hierarchy>(_fixture.PatcherProvider), instanciator);
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
Expand All @@ -176,30 +193,30 @@ public void Collection_on_hierarchy_fails()
};
var candidate = Candidate<Hierarchy, int>.Parse(@"{ ""type"":""super"", ""superProperty"": ""lol"" }");
await collection.CreateAsync(candidate);
}
});
});
}

[Fact]
public async Task BaseClass_Collection_on_hierarchy_works()
{
using (var storage = _newStorage(Guid.NewGuid().ToString()))
await _fixture.RunCodeInsideIsolatedDatabaseAsync(async (context) =>
{
var storage = new EFStorageService(context);
var repo = new Repository<Hierarchy>(storage, new Mock<IRightExpressionsHelper<Hierarchy>>().Object);
var instanciator = new BaseClassInstanciator<Hierarchy>(new InheritanceConfiguration());
var collection = new RestCollection<Hierarchy, int>(repo, new BaseClassPatcher<Hierarchy>(_patcherProvider, new InheritanceConfiguration()), instanciator);
var collection = new RestCollection<Hierarchy, int>(repo, new BaseClassPatcher<Hierarchy>(_fixture.PatcherProvider, new InheritanceConfiguration()), instanciator);
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
Converters = new List<JsonConverter>
{
{
new BaseClassJsonConverter<Hierarchy>(new InheritanceConfiguration())
}
}
};
var candidate = Candidate<Hierarchy, int>.Parse(@"{ ""type"":""super"", ""superProperty"": ""lol"" }");
await collection.CreateAsync(candidate);
}
});
}

}
}
Loading

0 comments on commit 1cb5cbf

Please sign in to comment.