diff --git a/Domain/RDD.Domain.Tests/AppControllerTests.cs b/Domain/RDD.Domain.Tests/AppControllerTests.cs index 1492c281..c0ba7d3d 100644 --- a/Domain/RDD.Domain.Tests/AppControllerTests.cs +++ b/Domain/RDD.Domain.Tests/AppControllerTests.cs @@ -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; @@ -11,66 +10,65 @@ namespace RDD.Domain.Tests { - public class AppControllerTests : SingleContextTests + public class AppControllerTests : IClassFixture { + 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(storage, _rightsService); - var users = new UsersCollectionWithHardcodedGetById(repo, _patcherProvider, Instanciator); - var controller = new UsersAppController(storage, users); - var query = new Query(); - query.Options.CheckRights = false; - var candidate = Candidate.Parse(@"{ ""id"": 3 }"); + var users = new UsersCollectionWithHardcodedGetById(_fixture.UsersRepo, _fixture.PatcherProvider, _fixture.Instanciator); + var controller = new UsersAppController(_fixture.InMemoryStorage, users); + var query = new Query(); + query.Options.CheckRights = false; + var id = Guid.NewGuid(); + var candidate = Candidate.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(storage, _rightsService); - var users = new UsersCollectionWithHardcodedGetById(repo, _patcherProvider, Instanciator); - var controller = new UsersAppController(storage, users); - var query = new Query(); - query.Options.CheckRights = false; - var candidate1 = Candidate.Parse(@"{ ""id"": 3 }"); - var candidate2 = Candidate.Parse(@"{ ""id"": 4 }"); + var users = new UsersCollectionWithHardcodedGetById(_fixture.UsersRepo, _fixture.PatcherProvider, _fixture.Instanciator); + var controller = new UsersAppController(_fixture.InMemoryStorage, users); + var query = new Query(); + query.Options.CheckRights = false; + var id1 = Guid.NewGuid(); + var id2 = Guid.NewGuid(); + var candidate1 = Candidate.Parse($@"{{ ""id"": ""{id1}"" }}"); + var candidate2 = Candidate.Parse($@"{{ ""id"": ""{id2}"" }}"); - var result = (await controller.CreateAsync(new List> { candidate1, candidate2 }, query)).ToList(); + var result = (await controller.CreateAsync(new List> { 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(storage, _rightsService); - var users = new UsersCollectionWithHardcodedGetById(repo, _patcherProvider, Instanciator); - var controller = new UsersAppController(storage, users); - var query = new Query(); - query.Options.CheckRights = false; - var candidate = Candidate.Parse(@"{ ""id"": 3 }"); + var users = new UsersCollectionWithHardcodedGetById(_fixture.UsersRepo, _fixture.PatcherProvider, _fixture.Instanciator); + var controller = new UsersAppController(_fixture.InMemoryStorage, users); + var query = new Query(); + query.Options.CheckRights = false; + var id = Guid.NewGuid(); + var candidate = Candidate.Parse($@"{{ ""id"": ""{id}"" }}"); - await controller.CreateAsync(candidate, query); + await controller.CreateAsync(candidate, query); - candidate = Candidate.Parse(@"{ ""name"": ""newName"" }"); + candidate = Candidate.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); } } } diff --git a/Domain/RDD.Domain.Tests/CollectionMethodsTests.cs b/Domain/RDD.Domain.Tests/CollectionMethodsTests.cs index 4894dedf..bfcb697e 100644 --- a/Domain/RDD.Domain.Tests/CollectionMethodsTests.cs +++ b/Domain/RDD.Domain.Tests/CollectionMethodsTests.cs @@ -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; @@ -16,54 +14,52 @@ using System.Linq.Expressions; using System.Threading.Tasks; using Xunit; + namespace RDD.Domain.Tests { - public class CollectionMethodsTests : SingleContextTests + public class CollectionMethodsTests : IClassFixture { + 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(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())); - } + Assert.Null(await users.GetByIdAsync(Guid.NewGuid(), new Query())); } [Fact] public async Task Put_SHOULD_NOT_throw_notfound_exception_WHEN_unexisting_entity_() { - using (var storage = _newStorage(Guid.NewGuid().ToString())) - { - Expression> trueFilter = t => true; - var rightService = new Mock>(); - rightService.Setup(s => s.GetFilter(It.IsAny>())).Returns(trueFilter); + Expression> trueFilter = t => true; + var rightService = new Mock>(); + rightService.Setup(s => s.GetFilter(It.IsAny>())).Returns(trueFilter); - var user = new User { Id = 3 }; - var repo = new Repository(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(_fixture.InMemoryStorage, rightService.Object); + var users = new UsersCollection(repo, _fixture.PatcherProvider, _fixture.Instanciator); + var app = new UsersAppController(_fixture.InMemoryStorage, users); - await app.CreateAsync(Candidate.Parse(@"{ ""id"": 3 }"), new Query()); + await app.CreateAsync(Candidate.Parse($@"{{ ""id"": ""{id}"" }}"), new Query()); - await app.UpdateByIdAsync(0, Candidate.Parse(@"{ ""name"": ""new name"" }"), new Query()); - } + await app.UpdateByIdAsync(Guid.NewGuid(), Candidate.Parse(@"{ ""name"": ""new name"" }"), new Query()); } [Fact] public async Task Post_SHOULD_work_WHEN_InstantiateEntityIsNotOverridenAndEntityHasAParameterlessConstructor() { - using (var storage = _newStorage(Guid.NewGuid().ToString())) - { - var repo = new Repository(storage, _rightsService); - var users = new UsersCollection(repo, _patcherProvider, Instanciator); - var query = new Query(); - query.Options.CheckRights = false; + var users = new UsersCollection(_fixture.UsersRepo, _fixture.PatcherProvider, _fixture.Instanciator); + var query = new Query(); + query.Options.CheckRights = false; - await users.CreateAsync(Candidate.Parse(@"{ ""id"": 3 }"), query); - } + await users.CreateAsync(Candidate.Parse($@"{{ ""id"": ""{Guid.NewGuid()}"" }}"), query); } class InstanciatorImplementation : IInstanciator @@ -80,126 +76,107 @@ public UserWithParameters InstanciateNew(ICandidate candidat [Fact] public async Task Post_SHOULD_work_WHEN_InstantiateEntityIsOverridenAndEntityHasParametersInConstructor() { - using (var storage = _newStorage(Guid.NewGuid().ToString())) - { - var repo = new Repository(storage, new RightsServiceMock()); - var users = new UsersCollectionWithParameters(repo, _patcherProvider, new InstanciatorImplementation()); - var query = new Query(); - query.Options.CheckRights = false; + var repo = new Repository(_fixture.InMemoryStorage, new RightsServiceMock()); + var users = new UsersCollectionWithParameters(repo, _fixture.PatcherProvider, new InstanciatorImplementation()); + var query = new Query(); + query.Options.CheckRights = false; - var result = await users.CreateAsync(Candidate.Parse(@"{ ""id"": 3, ""name"": ""John"" }"), query); + var result = await users.CreateAsync(Candidate.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(storage, _rightsService); - var users = new UsersCollection(repo, _patcherProvider, Instanciator); - var query = new Query(); - 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(); + 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(storage, _rightsService); - var users = new UsersCollection(repo, _patcherProvider, Instanciator); - var query = new Query(); - query.Options.CheckRights = false; - - storage.Add(user); - await storage.SaveChangesAsync(); - - await users.UpdateByIdAsync(2, Candidate.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(); + query.Options.CheckRights = false; + + _fixture.InMemoryStorage.Add(user); + await _fixture.InMemoryStorage.SaveChangesAsync(); + + await users.UpdateByIdAsync(id, Candidate.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(storage, _rightsService); - var users = new UsersCollection(repo, _patcherProvider, Instanciator); - var query = new Query(); - 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(); + query.Options.CheckRights = false; - storage.Add(user); - await storage.SaveChangesAsync(); + _fixture.InMemoryStorage.Add(user); + await _fixture.InMemoryStorage.SaveChangesAsync(); - query = new Query(query, u => u.TwitterUri == new Uri("https://twitter.com")); + query = new Query(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(async () => - { - using (var storage = _newStorage(Guid.NewGuid().ToString())) - { - var repo = new Repository(storage, new Mock>().Object); - var instanciator = new BaseClassInstanciator(new InheritanceConfiguration()); - var collection = new RestCollection(repo, new ObjectPatcher(_patcherProvider), instanciator); - - JsonConvert.DefaultSettings = () => new JsonSerializerSettings - { - Converters = new List - { - new BaseClassJsonConverter(new InheritanceConfiguration()) - } - }; - var candidate = Candidate.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(storage, new Mock>().Object); + var repo = new Repository(_fixture.InMemoryStorage, new Mock>().Object); var instanciator = new BaseClassInstanciator(new InheritanceConfiguration()); - var collection = new RestCollection(repo, new BaseClassPatcher(_patcherProvider, new InheritanceConfiguration()), instanciator); + var collection = new RestCollection(repo, new ObjectPatcher(_fixture.PatcherProvider), instanciator); JsonConvert.DefaultSettings = () => new JsonSerializerSettings { Converters = new List - { + { new BaseClassJsonConverter(new InheritanceConfiguration()) - } + } }; var candidate = Candidate.Parse(@"{ ""type"":""super"", ""superProperty"": ""lol"" }"); await collection.CreateAsync(candidate); - } + }); } + [Fact] + public async Task BaseClass_Collection_on_hierarchy_works() + { + var repo = new Repository(_fixture.InMemoryStorage, new Mock>().Object); + var instanciator = new BaseClassInstanciator(new InheritanceConfiguration()); + var collection = new RestCollection(repo, new BaseClassPatcher(_fixture.PatcherProvider, new InheritanceConfiguration()), instanciator); + + JsonConvert.DefaultSettings = () => new JsonSerializerSettings + { + Converters = new List + { + new BaseClassJsonConverter(new InheritanceConfiguration()) + } + }; + var candidate = Candidate.Parse(@"{ ""type"":""super"", ""superProperty"": ""lol"" }"); + await collection.CreateAsync(candidate); + } } } \ No newline at end of file diff --git a/Domain/RDD.Domain.Tests/DefaultFixture.cs b/Domain/RDD.Domain.Tests/DefaultFixture.cs new file mode 100644 index 00000000..f1102a82 --- /dev/null +++ b/Domain/RDD.Domain.Tests/DefaultFixture.cs @@ -0,0 +1,31 @@ +using RDD.Application; +using RDD.Domain.Mocks; +using RDD.Domain.Models; +using RDD.Domain.Patchers; +using RDD.Domain.Rights; +using RDD.Domain.Tests.Models; +using RDD.Infra.Storage; +using System; + +namespace RDD.Domain.Tests +{ + public class DefaultFixture : IDisposable + { + public IRightExpressionsHelper RightsService { get; private set; } + public IPatcherProvider PatcherProvider { get; private set; } + public IInstanciator Instanciator { get; private set; } + public IStorageService InMemoryStorage { get; private set; } + public IRepository UsersRepo { get; private set; } + + public DefaultFixture() + { + RightsService = new RightsServiceMock(); + PatcherProvider = new PatcherProvider(); + Instanciator = new DefaultInstanciator(); + InMemoryStorage = new InMemoryStorageService(); + UsersRepo = new Repository(InMemoryStorage, RightsService); + } + + public void Dispose() { } + } +} diff --git a/Domain/RDD.Domain.Tests/Models/User.cs b/Domain/RDD.Domain.Tests/Models/User.cs index f1493901..dcc5d797 100644 --- a/Domain/RDD.Domain.Tests/Models/User.cs +++ b/Domain/RDD.Domain.Tests/Models/User.cs @@ -5,9 +5,9 @@ namespace RDD.Domain.Tests.Models { - public class User : EntityBase + public class User : EntityBase { - public override int Id { get; set; } + public override Guid Id { get; set; } public override string Name { get; set; } public MailAddress Mail { get; set; } public Uri TwitterUri { get; set; } @@ -22,8 +22,9 @@ public static IEnumerable GetManyRandomUsers(int howMuch) for (var i = 1; i <= howMuch; i++) { var name = $"John Doe {i}"; + var id = Guid.NewGuid(); - result.Add(new User { Id = i, Name = name }); + result.Add(new User { Id = id, Name = name }); } return result; diff --git a/Domain/RDD.Domain.Tests/Models/UserWithParameters.cs b/Domain/RDD.Domain.Tests/Models/UserWithParameters.cs index 2dcc0605..8d1cc90b 100644 --- a/Domain/RDD.Domain.Tests/Models/UserWithParameters.cs +++ b/Domain/RDD.Domain.Tests/Models/UserWithParameters.cs @@ -26,8 +26,9 @@ public static IEnumerable GetManyRandomUsers(int howMuch) for (var i = 1; i <= howMuch; i++) { var name = $"John Doe {i}"; + var id = Guid.NewGuid(); - result.Add(new User { Id = i, Name = name }); + result.Add(new User { Id = id, Name = name }); } return result; diff --git a/Domain/RDD.Domain.Tests/Models/UsersAppController.cs b/Domain/RDD.Domain.Tests/Models/UsersAppController.cs index efc4586c..192eeebc 100644 --- a/Domain/RDD.Domain.Tests/Models/UsersAppController.cs +++ b/Domain/RDD.Domain.Tests/Models/UsersAppController.cs @@ -1,9 +1,10 @@ -using RDD.Application; +using RDD.Application; using RDD.Application.Controllers; +using System; namespace RDD.Domain.Tests.Models { - public class UsersAppController : AppController + public class UsersAppController : AppController { public UsersAppController(IStorageService storage, UsersCollection collection) : base(storage, collection) { } diff --git a/Domain/RDD.Domain.Tests/Models/UsersCollection.cs b/Domain/RDD.Domain.Tests/Models/UsersCollection.cs index eb4a14bf..23d668a8 100644 --- a/Domain/RDD.Domain.Tests/Models/UsersCollection.cs +++ b/Domain/RDD.Domain.Tests/Models/UsersCollection.cs @@ -1,9 +1,10 @@ using RDD.Domain.Models; using RDD.Domain.Patchers; +using System; namespace RDD.Domain.Tests.Models { - public class UsersCollection : RestCollection + public class UsersCollection : RestCollection { public UsersCollection(IRepository repository, IPatcherProvider patcherProvider, IInstanciator instanciator) : base(repository, new ObjectPatcher(patcherProvider), instanciator) { } diff --git a/Domain/RDD.Domain.Tests/Models/UsersCollectionWithHardcodedGetById.cs b/Domain/RDD.Domain.Tests/Models/UsersCollectionWithHardcodedGetById.cs index 233c7de7..c986fc0a 100644 --- a/Domain/RDD.Domain.Tests/Models/UsersCollectionWithHardcodedGetById.cs +++ b/Domain/RDD.Domain.Tests/Models/UsersCollectionWithHardcodedGetById.cs @@ -1,6 +1,7 @@ using RDD.Domain.Models; using RDD.Domain.Models.Querying; using RDD.Domain.Patchers; +using System; using System.Threading.Tasks; namespace RDD.Domain.Tests.Models @@ -10,14 +11,14 @@ public class UsersCollectionWithHardcodedGetById : UsersCollection public UsersCollectionWithHardcodedGetById(IRepository repository, IPatcherProvider patcherProvider, IInstanciator instanciator) : base(repository, patcherProvider, instanciator) { } - public override Task GetByIdAsync(int id, Query query) + public override Task GetByIdAsync(Guid id, Query query) { //Only for get, because PUT will always make a GetById( ) to retrieve the entity to update if (query.Verb == Helpers.HttpVerbs.Get) { return Task.FromResult(new User { - Id = 4 + Id = Guid.NewGuid() }); } diff --git a/Domain/RDD.Domain.Tests/OrderByConverterTests.cs b/Domain/RDD.Domain.Tests/OrderByConverterTests.cs index 2245260f..f61dbbc7 100644 --- a/Domain/RDD.Domain.Tests/OrderByConverterTests.cs +++ b/Domain/RDD.Domain.Tests/OrderByConverterTests.cs @@ -1,5 +1,6 @@ using RDD.Domain.Models.Querying; using RDD.Domain.Tests.Models; +using System; using System.Collections.Generic; using System.Linq; using Xunit; @@ -23,20 +24,23 @@ public void SimpleOrderByShouldWork() public void SimpleOrdersByShouldWork() { var orderby1 = OrderBy.Ascending(u => u.Name); - var orderby2 = OrderBy.Ascending(u => u.Id); + var orderby2 = OrderBy.Ascending(u => u.Salary); + var id30 = Guid.NewGuid(); + var id0 = Guid.NewGuid(); + var id35 = Guid.NewGuid(); var users = new List { - new User { Name = "bName", Id = 30}, - new User { Name = "bName", Id = 0}, - new User { Name = "aName", Id = 35 } + new User { Name = "bName", Id = id30, Salary = 30}, + new User { Name = "bName", Id = id0, Salary = 0}, + new User { Name = "aName", Id = id35, Salary = 35 } }.AsQueryable(); var results = orderby1.ApplyOrderBy(users); results = orderby2.ApplyOrderBy(results); - Assert.Equal(35, results.ElementAt(0).Id); - Assert.Equal(0, results.ElementAt(1).Id); - Assert.Equal(30, results.ElementAt(2).Id); + Assert.Equal(id35, results.ElementAt(0).Id); + Assert.Equal(id0, results.ElementAt(1).Id); + Assert.Equal(id30, results.ElementAt(2).Id); } [Fact] @@ -46,24 +50,30 @@ public void FullOrdersByShouldWork() var orderby2 = OrderBy.Ascending(u => u.Salary); var orderby3 = OrderBy.Descending(u => u.Id); + var id26 = new Guid("26000000-0000-0000-0000-000000000000"); + var id27 = new Guid("27000000-0000-0000-0000-000000000000"); + var id28 = new Guid("28000000-0000-0000-0000-000000000000"); + var id29 = new Guid("29000000-0000-0000-0000-000000000000"); + var id30 = new Guid("30000000-0000-0000-0000-000000000000"); + var users = new List { - new User { Name = "b", Id = 26, Salary = 30}, - new User { Name = "b", Id = 27, Salary = 0}, - new User { Name = "b", Id = 28, Salary = 30}, - new User { Name = "a", Id = 29}, - new User { Name = "a", Id = 30}, + new User { Name = "b", Id = id26, Salary = 30}, + new User { Name = "b", Id = id27, Salary = 0}, + new User { Name = "b", Id = id28, Salary = 30}, + new User { Name = "a", Id = id29}, + new User { Name = "a", Id = id30}, }.AsQueryable(); var results = orderby1.ApplyOrderBy(users); results = orderby2.ApplyOrderBy(results); results = orderby3.ApplyOrderBy(results); - Assert.Equal(30, results.ElementAt(0).Id); - Assert.Equal(29, results.ElementAt(1).Id); - Assert.Equal(27, results.ElementAt(2).Id); - Assert.Equal(28, results.ElementAt(3).Id); - Assert.Equal(26, results.ElementAt(4).Id); + Assert.Equal(id30, results.ElementAt(0).Id); + Assert.Equal(id29, results.ElementAt(1).Id); + Assert.Equal(id27, results.ElementAt(2).Id); + Assert.Equal(id28, results.ElementAt(3).Id); + Assert.Equal(id26, results.ElementAt(4).Id); } } } diff --git a/Domain/RDD.Domain.Tests/PropertySelectorCollectionTests.cs b/Domain/RDD.Domain.Tests/PropertySelectorCollectionTests.cs index f65286f2..0c85094b 100644 --- a/Domain/RDD.Domain.Tests/PropertySelectorCollectionTests.cs +++ b/Domain/RDD.Domain.Tests/PropertySelectorCollectionTests.cs @@ -1,5 +1,4 @@ -using RDD.Domain.Helpers; -using RDD.Domain.Helpers.Expressions; +using RDD.Domain.Helpers.Expressions; using RDD.Domain.Tests.Models; using System.Linq; using System.Text.RegularExpressions; diff --git a/Domain/RDD.Domain.Tests/QueryTests.cs b/Domain/RDD.Domain.Tests/QueryTests.cs index 4f4d2b01..1a6e46ea 100644 --- a/Domain/RDD.Domain.Tests/QueryTests.cs +++ b/Domain/RDD.Domain.Tests/QueryTests.cs @@ -3,25 +3,13 @@ using RDD.Domain.Models.Querying; using RDD.Domain.Tests.Models; using RDD.Domain.Tests.Templates; -using RDD.Infra.Storage; using System; using Xunit; namespace RDD.Domain.Tests { - public class QueryTests : SingleContextTests + public class QueryTests { - private readonly IRepository _repo; - private IReadOnlyRestCollection _collection; - private readonly IStorageService _storage; - - public QueryTests() - { - _storage = _newStorage(Guid.NewGuid().ToString()); - _repo = new Repository(_storage, _rightsService); - _collection = new UsersCollection(_repo, _patcherProvider, Instanciator); - } - [Fact] public void Cloning_query_should_clone_verb() { diff --git a/Infra/RDD.Infra.Tests/CollectionTests.cs b/Infra/RDD.Infra.Tests/CollectionTests.cs new file mode 100644 index 00000000..af049b29 --- /dev/null +++ b/Infra/RDD.Infra.Tests/CollectionTests.cs @@ -0,0 +1,45 @@ +using RDD.Domain.Models.Querying; +using RDD.Domain.Tests; +using RDD.Domain.Tests.Models; +using RDD.Infra.Storage; +using System.Linq; +using Xunit; + +namespace RDD.Infra.Tests +{ + public class CollectionTests : DatabaseTest, IClassFixture + { + private DefaultFixture _fixture; + + public CollectionTests(DefaultFixture fixture) + { + _fixture = fixture; + } + + [Fact] + public async void Guids_ordering_in_sql_should_work_properly() + { + await RunCodeInsideIsolatedDatabaseAsync(async (context) => + { + var storage = new EFStorageService(context); + var repo = new UsersRepository(storage, _fixture.RightsService); + var collection = new UsersCollection(repo, _fixture.PatcherProvider, _fixture.Instanciator); + var users = User.GetManyRandomUsers(20).OrderBy(u => u.Id).ToList(); + var firstUser = users.ElementAt(0); + var tenthUser = users.ElementAt(9); + var lastUser = users.ElementAt(19); + repo.AddRange(users); + await storage.SaveChangesAsync(); + var result = (await collection.GetAsync(new Query())).Items.ToList(); + + var firstItem = result.ElementAt(0); + var tenthItem = result.ElementAt(9); + var lastItem = result.ElementAt(19); + + Assert.Equal(firstUser, firstItem); + Assert.Equal(tenthUser, tenthItem); + Assert.Equal(lastUser, lastItem); + }); + } + } +} diff --git a/Infra/RDD.Infra.Tests/DatabaseTest.cs b/Infra/RDD.Infra.Tests/DatabaseTest.cs new file mode 100644 index 00000000..85228789 --- /dev/null +++ b/Infra/RDD.Infra.Tests/DatabaseTest.cs @@ -0,0 +1,40 @@ +using Microsoft.EntityFrameworkCore; +using RDD.Domain.Tests.Models; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace RDD.Infra.Tests +{ + public class DatabaseTest + { + protected virtual DbContext GetContext(string dbName) + { + return new DataContext(GetOptions(dbName)); + } + protected virtual DbContextOptions GetOptions(string dbName) + { + return new DbContextOptionsBuilder() + .UseSqlServer($@"Server=(localdb)\mssqllocaldb;Database={dbName};Trusted_Connection=True;ConnectRetryCount=0") + .Options; + } + + public async Task RunCodeInsideIsolatedDatabaseAsync(Func code) + { + using (var context = GetContext(Guid.NewGuid().ToString())) + { + try + { + await context.Database.EnsureCreatedAsync(); + + await code(context); + } + finally + { + await context.Database.EnsureDeletedAsync(); + } + } + } + } +} diff --git a/Infra/RDD.Infra.Tests/RDD.Infra.Tests.csproj b/Infra/RDD.Infra.Tests/RDD.Infra.Tests.csproj index 4bfdc576..fce697a6 100644 --- a/Infra/RDD.Infra.Tests/RDD.Infra.Tests.csproj +++ b/Infra/RDD.Infra.Tests/RDD.Infra.Tests.csproj @@ -14,6 +14,7 @@ + diff --git a/Infra/RDD.Infra.Tests/UsersRepository.cs b/Infra/RDD.Infra.Tests/UsersRepository.cs new file mode 100644 index 00000000..c8899cf4 --- /dev/null +++ b/Infra/RDD.Infra.Tests/UsersRepository.cs @@ -0,0 +1,24 @@ +using RDD.Application; +using RDD.Domain.Models.Querying; +using RDD.Domain.Rights; +using RDD.Domain.Tests.Models; +using System.Linq; + +namespace RDD.Infra.Tests +{ + public class UsersRepository : OpenRepository + { + public UsersRepository(IStorageService storageService, IRightExpressionsHelper rightsService) + : base(storageService, rightsService) { } + + protected override IQueryable ApplyOrderBys(IQueryable entities, Query query) + { + if (!query.OrderBys.Any()) + { + return entities.OrderBy(u => u.Id.ToString()); + } + + return base.ApplyOrderBys(entities, query); + } + } +} diff --git a/Web/RDD.Web.Tests/CollectionPropertiesTests.cs b/Web/RDD.Web.Tests/CollectionPropertiesTests.cs index be95141c..a0141920 100644 --- a/Web/RDD.Web.Tests/CollectionPropertiesTests.cs +++ b/Web/RDD.Web.Tests/CollectionPropertiesTests.cs @@ -1,25 +1,27 @@ -using RDD.Application; -using RDD.Domain; +using RDD.Domain.Tests; using RDD.Domain.Tests.Models; -using RDD.Domain.Tests.Templates; +using RDD.Infra.Storage; using RDD.Web.Querying; -using System; using System.Linq; using Xunit; namespace RDD.Web.Tests { - public class CollectionPropertiesTests : SingleContextTests + public class CollectionPropertiesTests : IClassFixture { - private readonly IRepository _repo; - private readonly IReadOnlyRestCollection _collection; - private readonly IStorageService _storage; - public CollectionPropertiesTests() + private DefaultFixture _fixture; + private InMemoryStorageService _storage; + private OpenRepository _repo; + private UsersCollection _collection; + + public CollectionPropertiesTests(DefaultFixture fixture) { - _storage = _newStorage(Guid.NewGuid().ToString()); - _repo = new OpenRepository(_storage, _rightsService); - _collection = new UsersCollection(_repo, _patcherProvider, Instanciator); + _fixture = fixture; + _storage = new InMemoryStorageService(); + _repo = new OpenRepository(_storage, _fixture.RightsService); + _collection = new UsersCollection(_repo, _fixture.PatcherProvider, _fixture.Instanciator); } + [Fact] public async void Count_of_collection_should_tell_10_when_10_entities() { @@ -29,6 +31,7 @@ public async void Count_of_collection_should_tell_10_when_10_entities() var result = await _collection.GetAsync(new WebQuery()); Assert.Equal(10, result.Count); } + [Fact] public async void Count_of_collection_should_tell_100_when_100_entities() { @@ -38,6 +41,7 @@ public async void Count_of_collection_should_tell_100_when_100_entities() var result = await _collection.GetAsync(new WebQuery()); Assert.Equal(100, result.Count); } + [Fact] public async void Count_of_collection_should_tell_10000_when_10000_entities() { diff --git a/Web/RDD.Web.Tests/WebControllerTests.cs b/Web/RDD.Web.Tests/WebControllerTests.cs index cc3b4304..803f33bd 100644 --- a/Web/RDD.Web.Tests/WebControllerTests.cs +++ b/Web/RDD.Web.Tests/WebControllerTests.cs @@ -5,10 +5,7 @@ using RDD.Web.Helpers; using RDD.Web.Serialization; using RDD.Web.Tests.Models; -using System; -using System.Collections.Generic; using System.Linq; -using System.Text; using System.Threading.Tasks; using Xunit; diff --git a/Web/RDD.Web.Tests/WebPagingTests.cs b/Web/RDD.Web.Tests/WebPagingTests.cs index 4b43eae3..55e3e9ab 100644 --- a/Web/RDD.Web.Tests/WebPagingTests.cs +++ b/Web/RDD.Web.Tests/WebPagingTests.cs @@ -1,11 +1,10 @@ -using RDD.Application; -using RDD.Domain; +using RDD.Domain; using RDD.Domain.Exceptions; using RDD.Domain.Models.Querying; +using RDD.Domain.Tests; using RDD.Domain.Tests.Models; -using RDD.Domain.Tests.Templates; +using RDD.Infra.Storage; using RDD.Web.Querying; -using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -13,19 +12,21 @@ namespace RDD.Web.Tests { - public class WebPaginggTests : SingleContextTests + public class WebPaginggTests : IClassFixture { - public WebPaginggTests() + private DefaultFixture _fixture; + private InMemoryStorageService _storage; + private OpenRepository _repo; + private UsersCollection _collection; + + public WebPaginggTests(DefaultFixture fixture) { - _storage = _newStorage(Guid.NewGuid().ToString()); - _repo = new OpenRepository(_storage, _rightsService); - _collection = new UsersCollection(_repo, _patcherProvider, Instanciator); + _fixture = fixture; + _storage = new InMemoryStorageService(); + _repo = new OpenRepository(_storage, _fixture.RightsService); + _collection = new UsersCollection(_repo, _fixture.PatcherProvider, _fixture.Instanciator); } - private readonly IRepository _repo; - private readonly IReadOnlyRestCollection _collection; - private readonly IStorageService _storage; - [Fact] public void Changing_query_page_count_should_not_affect_another_query() {