public async Task DeleteClassifier_NormalValues_DeleteItems() { // arrange var cancellationToken = new CancellationToken(); var unitOfWorkFactory = new TransactionScopeUnitOfWorkFactory(); var dbContextFactory = new DefaultDbContextFactory(); var generator = new MasterDataDbGenerator(unitOfWorkFactory, dbContextFactory); var classifierRepositoryFactory = CreateClassifierRepositoryFactory(dbContextFactory); var handler = new DeleteClassifierHandler(unitOfWorkFactory, classifierRepositoryFactory); using (var _ = unitOfWorkFactory.Create()) { // arrange await generator.InsertType(HierarchyType.None, cancellationToken); var insertedIds = new List <Guid>(); for (var i = 0; i < 5; i++) { var insertItem = await generator.InsertItem("00" + i, null, cancellationToken); // ReSharper disable once PossibleInvalidOperationException insertedIds.Add(insertItem.Uid.Value); } var searchResult = await classifierRepositoryFactory.GetNamedOrDefaultService(generator.TypeCode) .Search(new ClassifierSearchRequest { TypeCode = generator.TypeCode }, cancellationToken); // assert Assert.IsNotNull(searchResult); Assert.AreEqual(insertedIds.Count, searchResult.Rows.Count); // act var command = new DeleteClassifier { UserUid = generator.UserUid, TypeCode = generator.TypeCode, Uids = insertedIds.ToArray() }; var result = await handler.Handle(command, cancellationToken); // assert Assert.IsNotNull(result); Assert.IsTrue(result.Success); Assert.AreEqual(insertedIds.Count, result.AffectedRows); searchResult = await classifierRepositoryFactory.GetNamedOrDefaultService(generator.TypeCode) .Search(new ClassifierSearchRequest { TypeCode = generator.TypeCode }, cancellationToken); // assert Assert.IsNotNull(searchResult); Assert.AreEqual(0, searchResult.Rows.Count); } }
public async Task ManageRole_NormalValues_ManageItems() { // arrange var cancellationToken = new CancellationToken(); var unitOfWorkFactory = new TransactionScopeUnitOfWorkFactory(); var dbContextFactory = new DefaultDbContextFactory(); var generator = new MasterDataDbGenerator(unitOfWorkFactory, dbContextFactory); var classifierRepositoryFactoryBuilder = new ClassifierRepositoryFactoryBuilder(dbContextFactory); var classifierRepositoryFactory = classifierRepositoryFactoryBuilder.Build(); var insertHandler = new InsertClassifierHandler(unitOfWorkFactory, classifierRepositoryFactory); var updateHandler = new UpdateClassifierHandler(unitOfWorkFactory, classifierRepositoryFactory); var deleteHandler = new DeleteClassifierHandler(unitOfWorkFactory, classifierRepositoryFactory); using (var _ = unitOfWorkFactory.Create()) { // arrange generator.TypeCode = classifierRepositoryFactoryBuilder.RoleTypeCode; await generator.InsertType(HierarchyType.None, cancellationToken); // act - insert var insertedIds = new List <Guid>(); for (var i = 0; i < 3; i++) { var insertResult = await insertHandler.Handle(new InsertClassifier { UserUid = generator.UserUid, Item = new Role { Type = generator.TypeCode, Code = "00" + i, Name = "00" + i + " - Test Role" } }, cancellationToken); Assert.IsNotNull(insertResult); Assert.IsTrue(insertResult.Success, string.Join(",", insertResult.Errors.SelectMany(x => x.Messages))); // ReSharper disable once PossibleInvalidOperationException insertedIds.Add(insertResult.Uid.Value); } var searchResult = await classifierRepositoryFactory.GetNamedOrDefaultService(generator.TypeCode) .Search(new ClassifierSearchRequest { TypeCode = generator.TypeCode }, cancellationToken); // assert Assert.IsNotNull(searchResult); Assert.AreEqual(insertedIds.Count, searchResult.Rows.Count); // act - update foreach (var item in searchResult.Rows.Cast <Role>()) { item.Name = item.Name.Replace("Test", "Updated"); var updateCommand = new UpdateClassifier { UserUid = generator.UserUid, Item = item }; var updateResult = await updateHandler.Handle(updateCommand, cancellationToken); Assert.IsNotNull(updateResult); Assert.IsTrue(updateResult.Success, string.Join(",", updateResult.Errors.SelectMany(x => x.Messages))); } searchResult = await classifierRepositoryFactory.GetNamedOrDefaultService(generator.TypeCode) .Search(new ClassifierSearchRequest { TypeCode = generator.TypeCode }, cancellationToken); // assert Assert.IsNotNull(searchResult); Assert.AreEqual(insertedIds.Count, searchResult.Rows.Count); Assert.AreEqual(0, searchResult.Rows.Count(x => x.Name.Contains("Test"))); Assert.AreEqual(insertedIds.Count, searchResult.Rows.Count(x => x.Name.Contains("Updated"))); // act - delete var command = new DeleteClassifier { UserUid = generator.UserUid, TypeCode = generator.TypeCode, Uids = insertedIds.ToArray() }; var deleteResult = await deleteHandler.Handle(command, cancellationToken); // assert Assert.IsNotNull(deleteResult); Assert.IsTrue(deleteResult.Success, string.Join(",", deleteResult.Errors?.SelectMany(x => x.Messages) ?? Array.Empty <string>())); Assert.AreEqual(insertedIds.Count, deleteResult.AffectedRows); searchResult = await classifierRepositoryFactory.GetNamedOrDefaultService(generator.TypeCode) .Search(new ClassifierSearchRequest { TypeCode = generator.TypeCode }, cancellationToken); // assert Assert.IsNotNull(searchResult); Assert.AreEqual(0, searchResult.Rows.Count); } }
public async Task ManageNumerator_NormalValues_ManageItems() { // arrange var cancellationToken = new CancellationToken(); var unitOfWorkFactory = new TransactionScopeUnitOfWorkFactory(); var dbContextFactory = new DefaultDbContextFactory(); var generator = new MasterDataDbGenerator(unitOfWorkFactory, dbContextFactory); var classifierRepositoryFactory = CreateClassifierRepositoryFactory(dbContextFactory); var insertHandler = new InsertClassifierHandler(unitOfWorkFactory, classifierRepositoryFactory); var updateHandler = new UpdateClassifierHandler(unitOfWorkFactory, classifierRepositoryFactory); var deleteHandler = new DeleteClassifierHandler(unitOfWorkFactory, classifierRepositoryFactory); using (var _ = unitOfWorkFactory.Create()) { // arrange generator.TypeCode = NumeratorTypeCode; await generator.InsertType(HierarchyType.None, cancellationToken); // act - insert var insertedIds = new List <Guid>(); for (var i = 0; i < 5; i++) { var insertResult = await insertHandler.Handle(new InsertClassifier { UserUid = generator.UserUid, Item = new Numerator { Type = generator.TypeCode, Code = "00" + i, Name = "00" + i + " - Test Numerator", EntityTypeCode = "DocumentType", Pattern = "{Number}" } }, cancellationToken); Assert.IsNotNull(insertResult); Assert.AreEqual(true, insertResult.Success); // ReSharper disable once PossibleInvalidOperationException insertedIds.Add(insertResult.Uid.Value); } var searchResult = await classifierRepositoryFactory.GetNamedOrDefaultService(generator.TypeCode) .Search(new ClassifierSearchRequest { TypeCode = generator.TypeCode }, cancellationToken); // assert Assert.IsNotNull(searchResult); Assert.AreEqual(insertedIds.Count, searchResult.Rows.Count); // act - update foreach (var classifier in searchResult.Rows.Cast <Numerator>()) { classifier.Name = classifier.Name.Replace("Test", "Updated"); classifier.Pattern = "No. " + classifier.Pattern; var updateCommand = new UpdateClassifier { UserUid = generator.UserUid, Item = classifier }; var updateResult = await updateHandler.Handle(updateCommand, cancellationToken); Assert.IsNotNull(updateResult); Assert.AreEqual(true, updateResult.Success); } searchResult = await classifierRepositoryFactory.GetNamedOrDefaultService(generator.TypeCode) .Search(new ClassifierSearchRequest { TypeCode = generator.TypeCode }, cancellationToken); // assert Assert.IsNotNull(searchResult); Assert.AreEqual(insertedIds.Count, searchResult.Rows.Count); Assert.AreEqual(0, searchResult.Rows.Count(x => x.Name.Contains("Test"))); Assert.AreEqual(insertedIds.Count, searchResult.Rows.Count(x => x.Name.Contains("Updated"))); Assert.AreEqual(insertedIds.Count, searchResult.Rows.Cast <Numerator>().Count(x => x.Pattern.Contains("No."))); // act - delete var command = new DeleteClassifier { UserUid = generator.UserUid, TypeCode = generator.TypeCode, Uids = insertedIds.ToArray() }; var result = await deleteHandler.Handle(command, cancellationToken); // assert Assert.IsNotNull(result); Assert.IsTrue(result.Success); Assert.AreEqual(insertedIds.Count, result.AffectedRows); searchResult = await classifierRepositoryFactory.GetNamedOrDefaultService(generator.TypeCode) .Search(new ClassifierSearchRequest { TypeCode = generator.TypeCode }, cancellationToken); // assert Assert.IsNotNull(searchResult); Assert.AreEqual(0, searchResult.Rows.Count); } }