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);
            }
        }
Ejemplo n.º 2
0
        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_NormalValues_ShouldDeleteTree()
        {
            // 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 DeleteClassifierTreeHandler(unitOfWorkFactory, dbContextFactory, classifierTypeService);

            using (var _ = unitOfWorkFactory.Create())
            {
                // arrange
                await generator.InsertType(HierarchyType.Groups, cancellationToken);

                var tree2 = await generator.InsertTree("tree2", cancellationToken);

                // assert - default and second trees exists
                var trees = await generator.GetTrees(cancellationToken);

                Assert.AreEqual(2, trees.TotalCount);

                // act
                var result = await handler.Handle(new DeleteClassifierTree
                {
                    UserUid  = generator.UserUid,
                    TypeCode = generator.TypeCode,
                    // ReSharper disable once PossibleInvalidOperationException
                    Uids = new [] { tree2.Uid.Value }
                }, cancellationToken);

                // assert - link deleted
                Assert.IsTrue(result.Success);
                Assert.AreEqual(1, result.AffectedRows);

                // assert - default hierarchy exists
                trees = await generator.GetTrees(cancellationToken);

                Assert.AreEqual(1, trees.TotalCount);
                Assert.AreEqual(ClassifierTree.DefaultCode, trees.Rows[0].Code);
            }
        }