public virtual void OnAppStartup() { using (IDependencyResolver childDependencyResolver = _dependencyManager.CreateChildDependencyResolver()) { TestDbContext dbContext = childDependencyResolver.Resolve <TestDbContext>(); dbContext.Database.EnsureDeleted(); // after delete, scope and it's transaction are not much usable! } using (IDependencyResolver childDependencyResolver = _dependencyManager.CreateChildDependencyResolver()) { TestDbContext dbContext = childDependencyResolver.Resolve <TestDbContext>(); dbContext.Database.EnsureCreated(); dbContext.TestModels.Add(_testModel); dbContext.ParentEntities.AddRange(_parentEntities); TestCity city = new TestCity { Id = Guid.Parse("EF529174-C497-408B-BB4D-C31C205D46BB"), Name = "TestCity", Version = DateTimeOffset.UtcNow.Ticks }; TestCustomer customer = new TestCustomer { Id = Guid.Parse("28E1FF65-DA41-4FA3-8AEB-5196494B407D"), City = city, CityId = city.Id, Name = "TestCustomer", Version = DateTimeOffset.UtcNow.Ticks }; dbContext.TestCities.Add(city); dbContext.TestCustomers.Add(customer); dbContext.SaveChanges(); } }
public async Task CreateAndUpdateTest(NextApiTransport transport) { var uploadQueue = new List <UploadQueueDto>(); var newTestCity = new TestCity { Name = "MyNewTestCity", Population = 123456, Demonym = "MyTestCityDemonym" }; var createOp = new UploadQueueDto { Id = Guid.NewGuid(), OccuredAt = DateTimeOffset.Now, OperationType = OperationType.Create, EntityName = nameof(TestCity), NewValue = JsonConvert.SerializeObject(newTestCity), EntityRowGuid = newTestCity.Id }; await Task.Delay(2000); const string updatedName = "UpdatedName"; var updateOp = new UploadQueueDto { Id = Guid.NewGuid(), OccuredAt = DateTimeOffset.Now, OperationType = OperationType.Update, EntityName = nameof(TestCity), ColumnName = nameof(TestCity.Name), NewValue = updatedName, EntityRowGuid = newTestCity.Id }; uploadQueue.Add(createOp); uploadQueue.Add(updateOp); var service = ResolveQueueService(transport); var resultDict = await service.ProcessAsync(uploadQueue); foreach (var keyValuePair in resultDict) { Assert.Equal(UploadQueueError.NoError, keyValuePair.Value.Error); } using var scope = App.ServerServices.CreateScope(); var serviceProvider = scope.ServiceProvider; var testCityRepo = serviceProvider.GetService <IRepo <TestCity, Guid> >(); var newTestCityFromServer = await testCityRepo.GetAsync(city => city.Id == newTestCity.Id); Assert.NotNull(newTestCityFromServer); Assert.Equal(updatedName, newTestCityFromServer.Name); }
public virtual void OnAppStartup() { using (IDependencyResolver childDependencyResolver = _dependencyManager.CreateChildDependencyResolver()) { TestDbContext dbContext = null; if (Settings.Default.UseInMemoryProviderByDefault) { dbContext = childDependencyResolver.Resolve <TestDbContext>(); } else { DbContextOptionsBuilder dbContextOptionsBuilder = new DbContextOptionsBuilder(); SqlConnection dbConnection = new SqlConnection(DefaultAppEnvironmentProvider.Current.GetActiveAppEnvironment().GetConfig <string>("TestDbConnectionString")); dbContextOptionsBuilder.UseSqlServer(dbConnection); dbContext = new TestDbContext(dbContextOptionsBuilder.Options); } using (dbContext) { dbContext.Database.EnsureDeleted(); dbContext.Database.EnsureCreated(); dbContext.TestModels.Add(_testModel); dbContext.ParentEntities.AddRange(_parentEntities); TestCity city = new TestCity { Id = Guid.Parse("EF529174-C497-408B-BB4D-C31C205D46BB"), Name = "TestCity" }; TestCustomer customer = new TestCustomer { Id = Guid.Parse("28E1FF65-DA41-4FA3-8AEB-5196494B407D"), City = city, CityId = city.Id, Name = "TestCustomer" }; dbContext.TestCities.Add(city); dbContext.TestCustomers.Add(customer); dbContext.SaveChanges(); } } }
public async Task CreateTwiceInSameBatchTest(NextApiTransport transport) { var uploadQueue = new List <UploadQueueDto>(); var newTestCity = new TestCity { Name = "MyNewTestCity", Population = 123456, Demonym = "MyTestCityDemonym" }; var createOp1 = new UploadQueueDto { Id = Guid.NewGuid(), OccuredAt = DateTimeOffset.Now, OperationType = OperationType.Create, EntityName = nameof(TestCity), NewValue = JsonConvert.SerializeObject(newTestCity), EntityRowGuid = newTestCity.Id }; var createOp2 = new UploadQueueDto { Id = Guid.NewGuid(), OccuredAt = DateTimeOffset.Now, OperationType = OperationType.Create, EntityName = nameof(TestCity), NewValue = JsonConvert.SerializeObject(newTestCity), EntityRowGuid = newTestCity.Id }; uploadQueue.Add(createOp1); uploadQueue.Add(createOp2); var service = ResolveQueueService(transport); var resultDict = await service.ProcessAsync(uploadQueue); Assert.Equal(uploadQueue.Count, resultDict.Count); Assert.Contains(resultDict, pair => pair.Value.Error == UploadQueueError.NoError); Assert.Contains(resultDict, pair => pair.Value.Error == UploadQueueError.OnlyOneCreateOperationAllowed); }
public async Task ChangesHandlerTest(NextApiTransport transport) { var uploadQueue = new List <UploadQueueDto>(); var newTestCity = new TestCity { Name = "MyNewTestCity", Population = 123456, Demonym = "MyTestCityDemonym", Id = TestUploadQueueChangesHandler.RejectCreateGuid }; var createOp = new UploadQueueDto { Id = Guid.NewGuid(), OccuredAt = DateTimeOffset.Now, OperationType = OperationType.Create, EntityName = nameof(TestCity), NewValue = JsonConvert.SerializeObject(newTestCity), EntityRowGuid = TestUploadQueueChangesHandler.RejectCreateGuid }; var updateOp = new UploadQueueDto { Id = Guid.NewGuid(), OccuredAt = DateTimeOffset.Now, OperationType = OperationType.Update, EntityName = nameof(TestCity), ColumnName = nameof(TestCity.Demonym), NewValue = "SomeNewDemonym", EntityRowGuid = TestUploadQueueChangesHandler.RejectCreateGuid }; await Task.Delay(1000); uploadQueue.Add(createOp); uploadQueue.Add(updateOp); var service = ResolveQueueService(transport); { var resultDict = await service.ProcessAsync(uploadQueue); Assert.Equal(uploadQueue.Count, resultDict.Count); var createOpResult = resultDict[createOp.Id]; var updateOpResult = resultDict[updateOp.Id]; Assert.Equal(UploadQueueError.Exception, createOpResult.Error); Assert.Contains(TestUploadQueueChangesHandler.RejectCreateGuidMessage, createOpResult.Extra.ToString()); Assert.Equal(UploadQueueError.Exception, updateOpResult.Error); Assert.Contains(TestUploadQueueChangesHandler.RejectCreateGuidMessage, updateOpResult.Extra.ToString()); } { newTestCity.Id = TestUploadQueueChangesHandler.RejectUpdateGuid; createOp.NewValue = JsonConvert.SerializeObject(newTestCity); createOp.EntityRowGuid = newTestCity.Id; updateOp.EntityRowGuid = newTestCity.Id; var resultDict = await service.ProcessAsync(uploadQueue); Assert.Equal(uploadQueue.Count, resultDict.Count); var createOpResult = resultDict[createOp.Id]; var updateOpResult = resultDict[updateOp.Id]; Assert.Equal(UploadQueueError.NoError, createOpResult.Error); Assert.Null(createOpResult.Extra); Assert.Equal(UploadQueueError.Exception, updateOpResult.Error); Assert.Contains(TestUploadQueueChangesHandler.RejectUpdateGuidMessage, updateOpResult.Extra.ToString()); } { newTestCity.Id = TestUploadQueueChangesHandler.RejectDeleteGuid; createOp.NewValue = JsonConvert.SerializeObject(newTestCity); createOp.EntityRowGuid = newTestCity.Id; uploadQueue.Clear(); uploadQueue.Add(createOp); var resultDictCreate = await service.ProcessAsync(uploadQueue); Assert.Equal(uploadQueue.Count, resultDictCreate.Count); var createOpResult = resultDictCreate[createOp.Id]; Assert.Equal(UploadQueueError.NoError, createOpResult.Error); var deleteOp = new UploadQueueDto { Id = Guid.NewGuid(), OccuredAt = DateTimeOffset.Now, OperationType = OperationType.Delete, EntityName = nameof(TestCity), EntityRowGuid = createOp.EntityRowGuid }; uploadQueue.Clear(); uploadQueue.Add(deleteOp); var resultDict = await service.ProcessAsync(uploadQueue); Assert.Equal(uploadQueue.Count, resultDict.Count); var deleteOpResult = resultDict[deleteOp.Id]; Assert.Equal(UploadQueueError.Exception, deleteOpResult.Error); Assert.Contains(TestUploadQueueChangesHandler.RejectDeleteGuidMessage, deleteOpResult.Extra.ToString()); } }
public async Task CreateAndUpdateStressTest(bool createAndUpdateInSameBatch, NextApiTransport transport) { var createUploadQueue = new List <UploadQueueDto>(); var updateUploadQueue = new List <UploadQueueDto>(); var testCities = new List <TestCity>(); for (var i = 0; i < 500; i++) { var newTestCity = new TestCity { Name = "MyNewTestCity" + i, Population = i, Demonym = "MyTestCityDemonym" + i }; testCities.Add(newTestCity); var createOp = new UploadQueueDto { Id = Guid.NewGuid(), OccuredAt = DateTimeOffset.Now, OperationType = OperationType.Create, EntityName = nameof(TestCity), NewValue = JsonConvert.SerializeObject(newTestCity), EntityRowGuid = newTestCity.Id }; var updatedName = "UpdatedName" + i; newTestCity.Name = updatedName; var updateOp = new UploadQueueDto { Id = Guid.NewGuid(), OccuredAt = DateTimeOffset.Now, OperationType = OperationType.Update, EntityName = nameof(TestCity), ColumnName = nameof(TestCity.Name), NewValue = updatedName, EntityRowGuid = newTestCity.Id }; var updatedPopulation = i + 1; newTestCity.Population = updatedPopulation; var updateOp2 = new UploadQueueDto { Id = Guid.NewGuid(), OccuredAt = DateTimeOffset.Now, OperationType = OperationType.Update, EntityName = nameof(TestCity), ColumnName = nameof(TestCity.Population), NewValue = updatedPopulation, EntityRowGuid = newTestCity.Id }; if (createAndUpdateInSameBatch) { createUploadQueue.Add(createOp); createUploadQueue.Add(updateOp); createUploadQueue.Add(updateOp2); } else { createUploadQueue.Add(createOp); updateUploadQueue.Add(updateOp); updateUploadQueue.Add(updateOp2); } } var service = ResolveQueueService(transport); var sw = new Stopwatch(); sw.Start(); var resultDict1 = await service.ProcessAsync(createUploadQueue); _output.WriteLine( $"Uploading createUploadQueue took {sw.Elapsed}, {nameof(createAndUpdateInSameBatch)}: {createAndUpdateInSameBatch}"); foreach (var keyValuePair in resultDict1) { Assert.Equal(UploadQueueError.NoError, keyValuePair.Value.Error); } using (var scope = App.ServerServices.CreateScope()) { var serviceProvider = scope.ServiceProvider; var testCityRepo = serviceProvider.GetService <IRepo <TestCity, Guid> >(); foreach (var testCity in testCities) { var testCityFromServer = await testCityRepo.GetAsync(city => city.Id == testCity.Id); Assert.NotNull(testCityFromServer); if (!createAndUpdateInSameBatch) { continue; } Assert.Equal(testCity.Name, testCityFromServer.Name); Assert.Equal(testCity.Population, testCityFromServer.Population); } } if (createAndUpdateInSameBatch) { return; } sw.Restart(); var resultDict2 = await service.ProcessAsync(updateUploadQueue); _output.WriteLine($"Uploading updateUploadQueue took {sw.Elapsed}"); foreach (var keyValuePair in resultDict2) { Assert.Equal(UploadQueueError.NoError, keyValuePair.Value.Error); } using (var scope = App.ServerServices.CreateScope()) { var serviceProvider = scope.ServiceProvider; var testCityRepo = serviceProvider.GetService <IRepo <TestCity, Guid> >(); foreach (var newTestCity in testCities) { var newTestCityFromServer = await testCityRepo.GetAsync(city => city.Id == newTestCity.Id); Assert.NotNull(newTestCityFromServer); Assert.Equal(newTestCity.Name, newTestCityFromServer.Name); Assert.Equal(newTestCity.Population, newTestCityFromServer.Population); } } }
public async Task GuidPropTest(NextApiTransport transport) { var uploadQueue = new List <UploadQueueDto>(); var newTestCity = new TestCity { GuidProp = Guid.NewGuid(), NullableGuidProp = Guid.NewGuid() }; var createOp1 = new UploadQueueDto { Id = Guid.NewGuid(), OccuredAt = DateTimeOffset.Now, OperationType = OperationType.Create, EntityName = nameof(TestCity), NewValue = JsonConvert.SerializeObject(newTestCity), EntityRowGuid = newTestCity.Id }; uploadQueue.Add(createOp1); var service = ResolveQueueService(transport); await service.ProcessAsync(uploadQueue); // set to new Guid values uploadQueue.Clear(); var updateOp = new UploadQueueDto { Id = Guid.NewGuid(), OccuredAt = DateTimeOffset.Now, OperationType = OperationType.Update, EntityName = nameof(TestCity), ColumnName = nameof(TestCity.GuidProp), NewValue = Guid.NewGuid(), EntityRowGuid = newTestCity.Id }; var updateOp1 = new UploadQueueDto { Id = Guid.NewGuid(), OccuredAt = DateTimeOffset.Now, OperationType = OperationType.Update, EntityName = nameof(TestCity), ColumnName = nameof(TestCity.NullableGuidProp), NewValue = Guid.NewGuid(), EntityRowGuid = newTestCity.Id }; uploadQueue.Add(updateOp); uploadQueue.Add(updateOp1); var resultDict = await service.ProcessAsync(uploadQueue); Assert.Equal(uploadQueue.Count, resultDict.Count); Assert.Equal(UploadQueueError.NoError, resultDict[updateOp.Id].Error); Assert.Equal(UploadQueueError.NoError, resultDict[updateOp1.Id].Error); // set to null uploadQueue.Clear(); var updateOp2 = new UploadQueueDto { Id = Guid.NewGuid(), OccuredAt = DateTimeOffset.Now, OperationType = OperationType.Update, EntityName = nameof(TestCity), ColumnName = nameof(TestCity.NullableGuidProp), NewValue = null, EntityRowGuid = newTestCity.Id }; uploadQueue.Add(updateOp2); resultDict = await service.ProcessAsync(uploadQueue); Assert.Equal(uploadQueue.Count, resultDict.Count); Assert.Equal(UploadQueueError.NoError, resultDict[updateOp2.Id].Error); }
public async Task SilentCreateDeleteTest(NextApiTransport transport) { var uploadQueue = new List <UploadQueueDto>(); var newTestCity = new TestCity { Id = Guid.NewGuid(), Name = "MyNewTestCity1", Population = 123456, Demonym = "MyTestCityDemonym" }; var createOp = new UploadQueueDto { Id = Guid.NewGuid(), OccuredAt = DateTimeOffset.Now, OperationType = OperationType.Create, EntityName = nameof(TestCity), NewValue = JsonConvert.SerializeObject(newTestCity), EntityRowGuid = newTestCity.Id }; var updateOp = new UploadQueueDto { Id = Guid.NewGuid(), OccuredAt = DateTimeOffset.Now, OperationType = OperationType.Update, EntityName = nameof(TestCity), ColumnName = nameof(TestCity.Name), NewValue = "sadkf", EntityRowGuid = newTestCity.Id }; var deleteOp = new UploadQueueDto { Id = Guid.NewGuid(), OccuredAt = DateTimeOffset.Now, OperationType = OperationType.Delete, EntityName = nameof(TestCity), EntityRowGuid = newTestCity.Id }; uploadQueue.Add(createOp); uploadQueue.Add(updateOp); uploadQueue.Add(deleteOp); var service = ResolveQueueService(transport); var resultDict = await service.ProcessAsync(uploadQueue); foreach (var result in resultDict) { Assert.Equal(UploadQueueError.NoError, result.Value.Error); } // Check for default guids as well createOp.EntityRowGuid = Guid.Empty; updateOp.EntityRowGuid = Guid.Empty; deleteOp.EntityRowGuid = Guid.Empty; resultDict = await service.ProcessAsync(uploadQueue); foreach (var result in resultDict) { Assert.Equal(UploadQueueError.NoError, result.Value.Error); } }
public async Task CreateWithDefaultGuidTest(NextApiTransport transport) { var uploadQueue = new List <UploadQueueDto>(); var newTestCity1 = new TestCity { Id = Guid.Empty, Name = "MyNewTestCity1", Population = 123456, Demonym = "MyTestCityDemonym" }; var newTestCity2 = new TestCity { Id = Guid.Empty, Name = "MyNewTestCity2", Population = 123456, Demonym = "MyTestCityDemonym" }; var createOp1 = new UploadQueueDto { Id = Guid.NewGuid(), OccuredAt = DateTimeOffset.Now, OperationType = OperationType.Create, EntityName = nameof(TestCity), NewValue = JsonConvert.SerializeObject(newTestCity1), EntityRowGuid = newTestCity1.Id }; var createOp2 = new UploadQueueDto { Id = Guid.NewGuid(), OccuredAt = DateTimeOffset.Now, OperationType = OperationType.Create, EntityName = nameof(TestCity), NewValue = JsonConvert.SerializeObject(newTestCity2), EntityRowGuid = newTestCity2.Id }; var emptyGuidUpdateOp = new UploadQueueDto { Id = Guid.NewGuid(), OccuredAt = DateTimeOffset.Now, OperationType = OperationType.Update, EntityName = nameof(TestCity), ColumnName = nameof(TestCity.Name), NewValue = "sadkf", EntityRowGuid = Guid.Empty }; uploadQueue.Add(createOp1); uploadQueue.Add(createOp2); uploadQueue.Add(emptyGuidUpdateOp); var service = ResolveQueueService(transport); var resultDict = await service.ProcessAsync(uploadQueue); Assert.Equal(UploadQueueError.NoError, resultDict[createOp1.Id].Error); Assert.Equal(UploadQueueError.NoError, resultDict[createOp2.Id].Error); Assert.Equal(UploadQueueError.EntityDoesNotExist, resultDict[emptyGuidUpdateOp.Id].Error); }
public async Task UploadNullValues(NextApiTransport transport) { var uploadQueue = new List <UploadQueueDto>(); var newTestCity = new TestCity { Name = "MyNewTestCity", Population = 123456, Demonym = "MyTestCityDemonym", SomeNullableInt = 100 }; var createOp = new UploadQueueDto { Id = Guid.NewGuid(), OccuredAt = DateTimeOffset.Now, OperationType = OperationType.Create, EntityName = nameof(TestCity), NewValue = JsonConvert.SerializeObject(newTestCity), EntityRowGuid = newTestCity.Id }; uploadQueue.Add(createOp); var service = ResolveQueueService(transport); var resultDictCreate = await service.ProcessAsync(uploadQueue); foreach (var keyValuePair in resultDictCreate) { Assert.Equal(UploadQueueError.NoError, keyValuePair.Value.Error); } using (var scope = App.ServerServices.CreateScope()) { var serviceProvider = scope.ServiceProvider; var testCityRepo = serviceProvider.GetService <IRepo <TestCity, Guid> >(); var newTestCityFromServer = await testCityRepo.GetAsync(city => city.Id == newTestCity.Id); Assert.NotNull(newTestCityFromServer); Assert.Equal(100, newTestCityFromServer.SomeNullableInt); } var updateOp = new UploadQueueDto { Id = Guid.NewGuid(), OccuredAt = DateTimeOffset.Now, OperationType = OperationType.Update, EntityName = nameof(TestCity), ColumnName = nameof(TestCity.SomeNullableInt), NewValue = null, EntityRowGuid = newTestCity.Id }; uploadQueue.Clear(); uploadQueue.Add(updateOp); var resultDictUpdate = await service.ProcessAsync(uploadQueue); foreach (var keyValuePair in resultDictUpdate) { Assert.Equal(UploadQueueError.NoError, keyValuePair.Value.Error); } using (var scope = App.ServerServices.CreateScope()) { var serviceProvider = scope.ServiceProvider; var testCityRepo = serviceProvider.GetService <IRepo <TestCity, Guid> >(); var newTestCityFromServer = await testCityRepo.GetAsync(city => city.Id == newTestCity.Id); Assert.NotNull(newTestCityFromServer); Assert.Null(newTestCityFromServer.SomeNullableInt); } }