public async Task Handle_WithoutExistingLink_ShouldInsertNewLink() { // arrange var cancellationToken = new CancellationToken(); var unitOfWorkFactory = new TransactionScopeUnitOfWorkFactory(); var dbContextFactory = new DefaultDbContextFactory(); var classifierTypeRepository = new DbClassifierTypeRepository(dbContextFactory); var classifierTypeService = new DbClassifierTypeService(dbContextFactory, classifierTypeRepository); var generator = new MasterDataDbGenerator(unitOfWorkFactory, dbContextFactory); var handler = new InsertClassifierLinkHandler(unitOfWorkFactory, dbContextFactory, classifierTypeService); using (var _ = unitOfWorkFactory.Create()) { await generator.InsertType(HierarchyType.Groups, cancellationToken); var root = await generator.FindTree(ClassifierTree.DefaultCode, cancellationToken); var group1 = await generator.InsertGroup(root.Uid, "001", null, cancellationToken); var root2 = await generator.InsertTree("root2", cancellationToken); // ReSharper disable once PossibleInvalidOperationException var group2 = await generator.InsertGroup(root2.Uid.Value, "002", null, cancellationToken); var item1 = await generator.InsertItem("001", null, cancellationToken); await generator.InsertLink(group1.Uid, item1.Uid, cancellationToken); // assert - initially new items belongs to default group var links = await generator.GetLinks(null, item1.Uid, cancellationToken); Assert.AreEqual(1, links.TotalCount); Assert.AreEqual(group1.Uid, links.Rows[0].Group.Uid); Assert.AreEqual(item1.Uid, links.Rows[0].Item.Uid); // act - link with new group in same hierarchy var result = await handler.Handle(new InsertClassifierLink { UserUid = generator.UserUid, TypeCode = generator.TypeCode, // ReSharper disable once PossibleInvalidOperationException GroupUid = group2.Uid.Value, // ReSharper disable once PossibleInvalidOperationException ItemUid = item1.Uid.Value }, cancellationToken); // assert - new link inserted Assert.IsTrue(result.Success); // assert - item linked to new group, link with default root still exists links = await generator.GetLinks(null, item1.Uid, cancellationToken); Assert.AreEqual(2, links.TotalCount); var groups = links.Rows.Select(x => x.Group.Uid).ToList(); CollectionAssert.Contains(groups, group1.Uid); CollectionAssert.Contains(groups, group2.Uid); } }
public async Task Handle_InSecondaryHierarchy_ShouldDeleteExistingLink() { // arrange var cancellationToken = new CancellationToken(); var unitOfWorkFactory = new TransactionScopeUnitOfWorkFactory(); var dbContextFactory = new DefaultDbContextFactory(); var classifierTypeRepository = new DbClassifierTypeRepository(dbContextFactory); var classifierTypeService = new DbClassifierTypeService(dbContextFactory, classifierTypeRepository); var dbHelper = new MasterDataDbGenerator(unitOfWorkFactory, dbContextFactory); var handler = new DeleteClassifierLinkHandler(unitOfWorkFactory, dbContextFactory, classifierTypeService); using (var _ = unitOfWorkFactory.Create()) { // arrange await dbHelper.InsertType(HierarchyType.Groups, cancellationToken); var root = await dbHelper.FindTree(ClassifierTree.DefaultCode, cancellationToken); var root2 = await dbHelper.InsertTree("root2", cancellationToken); // ReSharper disable once PossibleInvalidOperationException var group2 = await dbHelper.InsertGroup(root2.Uid.Value, "002", null, cancellationToken); var item1 = await dbHelper.InsertItem("001", null, cancellationToken); await dbHelper.InsertLink(group2.Uid, item1.Uid, cancellationToken); // assert - links to --default and-- secondary hierarchy exists var links = await dbHelper.GetLinks(null, item1.Uid, cancellationToken); Assert.AreEqual(1, links.TotalCount); var groups = links.Rows.Select(x => x.Group.Uid).ToList(); // CollectionAssert.Contains(groups, root.Uid); CollectionAssert.Contains(groups, group2.Uid); // act var result = await handler.Handle(new DeleteClassifierLink { UserUid = dbHelper.UserUid, TypeCode = dbHelper.TypeCode, // ReSharper disable once PossibleInvalidOperationException GroupUid = group2.Uid.Value, // ReSharper disable once PossibleInvalidOperationException ItemUid = item1.Uid.Value }, cancellationToken); // assert - link deleted Assert.IsTrue(result.Success); // assert - NO link to default hierarchy exists links = await dbHelper.GetLinks(null, item1.Uid, cancellationToken); Assert.AreEqual(0, links.TotalCount); // Assert.AreEqual(root.Uid, links.Rows[0].Group.Uid); // Assert.AreEqual(item1.Uid, links.Rows[0].Item.Uid); } }
public async Task Handle_LastLinkInDefaultHierarchy_ShouldThrow() { // arrange var cancellationToken = new CancellationToken(); var unitOfWorkFactory = new TransactionScopeUnitOfWorkFactory(); var dbContextFactory = new DefaultDbContextFactory(); var classifierTypeRepository = new DbClassifierTypeRepository(dbContextFactory); var classifierTypeService = new DbClassifierTypeService(dbContextFactory, classifierTypeRepository); var generator = new MasterDataDbGenerator(unitOfWorkFactory, dbContextFactory); var handler = new DeleteClassifierLinkHandler(unitOfWorkFactory, dbContextFactory, classifierTypeService); using (var _ = unitOfWorkFactory.Create()) { // arrange await generator.InsertType(HierarchyType.Groups, cancellationToken); var root = await generator.FindTree(ClassifierTree.DefaultCode, cancellationToken); var group1 = await generator.InsertGroup(root.Uid, "001", null, cancellationToken); var item1 = await generator.InsertItem("001", null, cancellationToken); await generator.InsertLink(group1.Uid, item1.Uid, cancellationToken); // assert - links in default hierarchy exists var links = await generator.GetLinks(null, item1.Uid, cancellationToken); Assert.AreEqual(1, links.TotalCount); Assert.AreEqual(group1.Uid, links.Rows[0].Group.Uid); Assert.AreEqual(item1.Uid, links.Rows[0].Item.Uid); // act var result = await handler.Handle(new DeleteClassifierLink { UserUid = generator.UserUid, TypeCode = generator.TypeCode, // ReSharper disable once PossibleInvalidOperationException GroupUid = group1.Uid.Value, // ReSharper disable once PossibleInvalidOperationException ItemUid = item1.Uid.Value }, cancellationToken); // assert - link not deleted Assert.IsTrue(result.Success); Assert.AreEqual(1, result.Errors.Count); // assert - link to default hierarchy root exists links = await generator.GetLinks(null, item1.Uid, cancellationToken); Assert.AreEqual(1, links.TotalCount); Assert.AreEqual(root.Uid, links.Rows[0].Group.Uid); Assert.AreEqual(item1.Uid, links.Rows[0].Item.Uid); } }
public async Task GetGroups_ForNotNullParent_ReturnItems() { // arrange var cancellationToken = CancellationToken.None; var unitOfWorkFactory = new TransactionScopeUnitOfWorkFactory(); var dbContextFactory = new DefaultDbContextFactory(); var classifierTypeRepository = new DbClassifierTypeRepository(dbContextFactory); var classifierTreeRepository = new DbClassifierTreeRepository(dbContextFactory); var classifierTypeService = new DbClassifierTypeService(dbContextFactory, classifierTypeRepository); var classifierTreeService = new DefaultClassifierTreeService(classifierTreeRepository); var generator = new MasterDataDbGenerator(unitOfWorkFactory, dbContextFactory); var handler = new GetClassifierGroupListHandler(dbContextFactory, classifierTypeService, classifierTreeService); using (var _ = unitOfWorkFactory.Create()) { // arrange await generator.InsertType(HierarchyType.Groups, cancellationToken); var tree = await generator.FindTree(ClassifierTree.DefaultCode, cancellationToken); var parentGroup = await generator.InsertGroup(tree.Uid, "001", null, cancellationToken); await generator.InsertGroup(tree.Uid, "001.001", parentGroup.Uid, cancellationToken); await generator.InsertGroup(tree.Uid, "001.002", parentGroup.Uid, cancellationToken); await generator.InsertGroup(tree.Uid, "001.003", parentGroup.Uid, cancellationToken); await generator.InsertGroup(tree.Uid, "001.004", parentGroup.Uid, cancellationToken); await generator.InsertGroup(tree.Uid, "001.005", parentGroup.Uid, cancellationToken); await generator.InsertGroup(tree.Uid, "001.006", parentGroup.Uid, cancellationToken); await generator.InsertGroup(tree.Uid, "001.007", parentGroup.Uid, cancellationToken); // act var command = new GetClassifierGroupList { TypeCode = generator.TypeCode, TreeUid = tree.Uid, ParentUid = parentGroup.Uid, }; var result = await handler.Handle(command, cancellationToken); // assert Assert.IsNotNull(result); Assert.AreEqual(7, result.Rows.Count); } }
public async Task GetItems_ForBigGroups_ReturnNoMoreThan1000Items() { // arrange var cancellationToken = CancellationToken.None; var unitOfWorkFactory = new TransactionScopeUnitOfWorkFactory(); var dbContextFactory = new DefaultDbContextFactory(); var classifierTypeRepository = new DbClassifierTypeRepository(dbContextFactory); var classifierTreeRepository = new DbClassifierTreeRepository(dbContextFactory); var classifierTypeService = new DbClassifierTypeService(dbContextFactory, classifierTypeRepository); var classifierTreeService = new DefaultClassifierTreeService(classifierTreeRepository); var generator = new MasterDataDbGenerator(unitOfWorkFactory, dbContextFactory); var handler = new GetClassifierGroupListHandler(dbContextFactory, classifierTypeService, classifierTreeService); using (var _ = unitOfWorkFactory.Create()) { // arrange await generator.InsertType(HierarchyType.Groups, cancellationToken); var tree = await generator.FindTree(ClassifierTree.DefaultCode, cancellationToken); // todo: generate a lot of groups // act var command = new GetClassifierGroupList { TypeCode = generator.TypeCode, TreeUid = tree.Uid }; var result = await handler.Handle(command, cancellationToken); // assert Assert.IsNotNull(result); Assert.IsTrue(result.Rows.Count <= 1000); } }
public async Task GetClassifierLinkList_Should_ReturnList() { // arrange var cancellationToken = new CancellationToken(); var unitOfWorkFactory = new TransactionScopeUnitOfWorkFactory(); var dbContextFactory = new DefaultDbContextFactory(); var classifierTypeRepository = new DbClassifierTypeRepository(dbContextFactory); var classifierTypeService = new DbClassifierTypeService(dbContextFactory, classifierTypeRepository); var generator = new MasterDataDbGenerator(unitOfWorkFactory, dbContextFactory); var handler = new GetClassifierLinkListHandler(dbContextFactory, classifierTypeService); using (var _ = unitOfWorkFactory.Create()) { await generator.InsertType(HierarchyType.Groups, cancellationToken); var tree = await generator.FindTree(ClassifierTree.DefaultCode, cancellationToken); var group1 = await generator.InsertGroup(tree.Uid, "001", null, cancellationToken); var item1 = await generator.InsertItem("001", null, cancellationToken); var item2 = await generator.InsertItem("002", null, cancellationToken); await generator.InsertLink(group1.Uid, item1.Uid, cancellationToken); await generator.InsertLink(group1.Uid, item2.Uid, cancellationToken); // act - search by group code var result = await handler.Handle(new GetClassifierLinkList { TypeCode = generator.TypeCode, GroupUid = group1.Uid }, cancellationToken); // assert Assert.IsNotNull(result); Assert.AreEqual(2, result.TotalCount); Assert.AreEqual(tree.Uid, result.Rows[0].Tree.Uid); Assert.AreEqual(group1.Uid, result.Rows[0].Group.Uid); Assert.AreEqual(tree.Uid, result.Rows[1].Tree.Uid); Assert.AreEqual(group1.Uid, result.Rows[1].Group.Uid); var items = result.Rows.Select(x => x.Item.Uid).ToList(); CollectionAssert.Contains(items, item1.Uid); CollectionAssert.Contains(items, item2.Uid); // act - search by item code result = await handler.Handle(new GetClassifierLinkList { TypeCode = generator.TypeCode, ItemUid = item1.Uid }, cancellationToken); // assert Assert.IsNotNull(result); Assert.AreEqual(1, result.TotalCount); Assert.AreEqual(tree.Uid, result.Rows[0].Tree.Uid); Assert.AreEqual(group1.Uid, result.Rows[0].Group.Uid); Assert.AreEqual(item1.Uid, result.Rows[0].Item.Uid); // act - search by both group and item codes result = await handler.Handle(new GetClassifierLinkList { TypeCode = generator.TypeCode, GroupUid = group1.Uid, ItemUid = item2.Uid }, cancellationToken); // assert Assert.IsNotNull(result); Assert.AreEqual(1, result.TotalCount); Assert.AreEqual(tree.Uid, result.Rows[0].Tree.Uid); Assert.AreEqual(group1.Uid, result.Rows[0].Group.Uid); Assert.AreEqual(item2.Uid, result.Rows[0].Item.Uid); } }
public async Task GetGroups_WithFocusUid_ReturnChildrenOfEachParentGroups() { // arrange var cancellationToken = CancellationToken.None; var unitOfWorkFactory = new TransactionScopeUnitOfWorkFactory(); var dbContextFactory = new DefaultDbContextFactory(); var classifierTypeRepository = new DbClassifierTypeRepository(dbContextFactory); var classifierTreeRepository = new DbClassifierTreeRepository(dbContextFactory); var classifierTypeService = new DbClassifierTypeService(dbContextFactory, classifierTypeRepository); var classifierTreeService = new DefaultClassifierTreeService(classifierTreeRepository); var generator = new MasterDataDbGenerator(unitOfWorkFactory, dbContextFactory); var handler = new GetClassifierGroupListHandler(dbContextFactory, classifierTypeService, classifierTreeService); using (var _ = unitOfWorkFactory.Create()) { // arrange await generator.InsertType(HierarchyType.Groups, cancellationToken); var tree = await generator.FindTree(ClassifierTree.DefaultCode, cancellationToken); // 2 roots without parent for (var i = 0; i < 2; i++) { await generator.InsertGroup(tree.Uid, $"1-{i:D4}", null, cancellationToken); } // 1 root with 4 deep children Guid?parentUid = null; for (var i = 0; i <= 5; i++) { var childGroup = await generator.InsertGroup(tree.Uid, $"2-{i:D4}", parentUid, cancellationToken); parentUid = childGroup.Uid; } // 3 roots without parent for (var i = 0; i < 3; i++) { await generator.InsertGroup(tree.Uid, $"3-{i:D4}", null, cancellationToken); } var focusUid = parentUid; // act & assert - focus in root scope var command = new GetClassifierGroupList { TypeCode = generator.TypeCode, TreeUid = tree.Uid, ParentUid = null, FocusUid = focusUid // PageSize = 100 // todo: common limit for all levels is not ok }; var result = await handler.Handle(command, cancellationToken); // todo: pretty print and compare by focus.txt Assert.IsNotNull(result); Assert.AreEqual(6, result.Rows.Count); Assert.IsNull(result.Rows[0].Children); Assert.IsNull(result.Rows[1].Children); Assert.IsNull(result.Rows[3].Children); Assert.IsNull(result.Rows[4].Children); Assert.IsNull(result.Rows[5].Children); Assert.IsNotNull(result.Rows[2].Children); Assert.AreEqual(result.Rows[2].Children.Count, 1); // 0 lvl Assert.AreEqual(result.Rows[2].Children[0].Children.Count, 1); // 1 lvl Assert.AreEqual(result.Rows[2].Children[0].Children[0].Children.Count, 1); // 2 lvl Assert.AreEqual(result.Rows[2].Children[0].Children[0].Children[0].Children.Count, 1); // 3 lvl Assert.AreEqual(result.Rows[2].Children[0].Children[0].Children[0].Children[0].Children.Count, 1); // 4 lvl Assert.IsNull(result.Rows[2].Children[0].Children[0].Children[0].Children[0].Children[0].Children); // act & assert - focus in scope of selected parent // todo: what is the difference with previous asserts - ParentUid should be set? command = new GetClassifierGroupList { TypeCode = generator.TypeCode, TreeUid = tree.Uid, FocusUid = focusUid }; result = await handler.Handle(command, cancellationToken); // todo: pretty print and compare by focus.txt Assert.IsNotNull(result); Assert.AreEqual(6, result.Rows.Count); Assert.IsNull(result.Rows[0].Children); Assert.IsNull(result.Rows[1].Children); Assert.IsNull(result.Rows[3].Children); Assert.IsNull(result.Rows[4].Children); Assert.IsNull(result.Rows[5].Children); Assert.IsNotNull(result.Rows[2].Children); Assert.AreEqual(result.Rows[2].Children.Count, 1); // 0 lvl Assert.AreEqual(result.Rows[2].Children[0].Children.Count, 1); // 1 lvl Assert.AreEqual(result.Rows[2].Children[0].Children[0].Children.Count, 1); // 2 lvl Assert.AreEqual(result.Rows[2].Children[0].Children[0].Children[0].Children.Count, 1); // 3 lvl Assert.AreEqual(result.Rows[2].Children[0].Children[0].Children[0].Children[0].Children.Count, 1); // 4 lvl Assert.IsNull(result.Rows[2].Children[0].Children[0].Children[0].Children[0].Children[0].Children); } }