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 1cd7fa9
Show file tree
Hide file tree
Showing 19 changed files with 336 additions and 257 deletions.
76 changes: 37 additions & 39 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,65 @@

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()))
{
var repo = new Repository<User>(storage, _rightsService);
var users = new UsersCollectionWithHardcodedGetById(repo, _patcherProvider, 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 users = new UsersCollectionWithHardcodedGetById(_fixture.UsersRepo, _fixture.PatcherProvider, _fixture.Instanciator);
var controller = new UsersAppController(_fixture.InMemoryStorage, users);
var query = new Query<User>();
query.Options.CheckRights = false;
var id = Guid.NewGuid();
var candidate = Candidate<User, Guid>.Parse($@"{{ ""id"": ""{id}"" }}");

var user = await controller.CreateAsync(candidate, query);
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()))
{
var repo = new Repository<User>(storage, _rightsService);
var users = new UsersCollectionWithHardcodedGetById(repo, _patcherProvider, 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 users = new UsersCollectionWithHardcodedGetById(_fixture.UsersRepo, _fixture.PatcherProvider, _fixture.Instanciator);
var controller = new UsersAppController(_fixture.InMemoryStorage, users);
var query = new Query<User>();
query.Options.CheckRights = false;
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()))
{
var repo = new Repository<User>(storage, _rightsService);
var users = new UsersCollectionWithHardcodedGetById(repo, _patcherProvider, 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 users = new UsersCollectionWithHardcodedGetById(_fixture.UsersRepo, _fixture.PatcherProvider, _fixture.Instanciator);
var controller = new UsersAppController(_fixture.InMemoryStorage, users);
var query = new Query<User>();
query.Options.CheckRights = false;
var id = Guid.NewGuid();
var candidate = Candidate<User, Guid>.Parse($@"{{ ""id"": ""{id}"" }}");

await controller.CreateAsync(candidate, query);
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);
}
}
}
199 changes: 88 additions & 111 deletions Domain/RDD.Domain.Tests/CollectionMethodsTests.cs
Original file line number Diff line number Diff line change
@@ -1,69 +1,65 @@
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;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
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()))
{
var repo = new Repository<User>(storage, _rightsService);
var users = new UsersCollection(repo, _patcherProvider, Instanciator);
var users = new UsersCollection(_fixture.UsersRepo, _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()))
{
Expression<Func<User, bool>> trueFilter = t => true;
var rightService = new Mock<IRightExpressionsHelper<User>>();
rightService.Setup(s => s.GetFilter(It.IsAny<Query<User>>())).Returns(trueFilter);
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 repo = new Repository<User>(storage, rightService.Object);
var users = new UsersCollection(repo, _patcherProvider, Instanciator);
var app = new UsersAppController(storage, users);
var id = Guid.NewGuid();
var user = new User { Id = id };
var repo = new Repository<User>(_fixture.InMemoryStorage, rightService.Object);
var users = new UsersCollection(repo, _fixture.PatcherProvider, _fixture.Instanciator);
var app = new UsersAppController(_fixture.InMemoryStorage, 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()))
{
var repo = new Repository<User>(storage, _rightsService);
var users = new UsersCollection(repo, _patcherProvider, Instanciator);
var query = new Query<User>();
query.Options.CheckRights = false;
var users = new UsersCollection(_fixture.UsersRepo, _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,126 +76,107 @@ public UserWithParameters InstanciateNew(ICandidate<UserWithParameters> candidat
[Fact]
public async Task Post_SHOULD_work_WHEN_InstantiateEntityIsOverridenAndEntityHasParametersInConstructor()
{
using (var storage = _newStorage(Guid.NewGuid().ToString()))
{
var repo = new Repository<UserWithParameters>(storage, new RightsServiceMock<UserWithParameters>());
var users = new UsersCollectionWithParameters(repo, _patcherProvider, new InstanciatorImplementation());
var query = new Query<UserWithParameters>();
query.Options.CheckRights = false;
var repo = new Repository<UserWithParameters>(_fixture.InMemoryStorage, new RightsServiceMock<UserWithParameters>());
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);
}
Assert.Equal(3, result.Id);
Assert.Equal("John", result.Name);
}

[Fact]
public async Task AnyAsync_should_work()
{
using (var storage = _newStorage(Guid.NewGuid().ToString()))
{
var user = new User { Id = 2 };
var repo = new Repository<User>(storage, _rightsService);
var users = new UsersCollection(repo, _patcherProvider, 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);
}
var id = Guid.NewGuid();
var user = new User { Id = id };
var users = new UsersCollection(_fixture.UsersRepo, _fixture.PatcherProvider, _fixture.Instanciator);
var query = new Query<User>();
query.Options.CheckRights = false;

_fixture.InMemoryStorage.Add(user);
await _fixture.InMemoryStorage.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()))
{
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 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);

Assert.True(true);
}
var id = Guid.NewGuid();
var user = new User { Id = id, Name = "Name", Salary = 1, TwitterUri = new Uri("https://twitter.com") };
var users = new UsersCollection(_fixture.UsersRepo, _fixture.PatcherProvider, _fixture.Instanciator);
var query = new Query<User>();
query.Options.CheckRights = false;

_fixture.InMemoryStorage.Add(user);
await _fixture.InMemoryStorage.SaveChangesAsync();

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()))
{
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 query = new Query<User>();
query.Options.CheckRights = false;
var id = Guid.NewGuid();
var user = new User { Id = id, Name = "Name", Salary = 1, TwitterUri = new Uri("https://twitter.com") };
var users = new UsersCollection(_fixture.UsersRepo, _fixture.PatcherProvider, _fixture.Instanciator);
var query = new Query<User>();
query.Options.CheckRights = false;

storage.Add(user);
await storage.SaveChangesAsync();
_fixture.InMemoryStorage.Add(user);
await _fixture.InMemoryStorage.SaveChangesAsync();

query = new Query<User>(query, u => u.TwitterUri == new Uri("https://twitter.com"));
query = new Query<User>(query, u => u.TwitterUri == new Uri("https://twitter.com"));

var results = await users.GetAsync(query);
var results = await users.GetAsync(query);

Assert.Equal(1, results.Count);
}
Assert.Equal(1, results.Count);
}

[Fact]
public void Collection_on_hierarchy_fails()
{
Assert.ThrowsAsync<BadRequestException>(async () =>
{
using (var storage = _newStorage(Guid.NewGuid().ToString()))
{
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);
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);
}
});
}

[Fact]
public async Task BaseClass_Collection_on_hierarchy_works()
{
using (var storage = _newStorage(Guid.NewGuid().ToString()))
{
var repo = new Repository<Hierarchy>(storage, new Mock<IRightExpressionsHelper<Hierarchy>>().Object);
var repo = new Repository<Hierarchy>(_fixture.InMemoryStorage, 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 ObjectPatcher<Hierarchy>(_fixture.PatcherProvider), 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);
}
});
}

[Fact]
public async Task BaseClass_Collection_on_hierarchy_works()
{
var repo = new Repository<Hierarchy>(_fixture.InMemoryStorage, new Mock<IRightExpressionsHelper<Hierarchy>>().Object);
var instanciator = new BaseClassInstanciator<Hierarchy>(new InheritanceConfiguration());
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 1cd7fa9

Please sign in to comment.