public async Task InsertClassifier_DuplicateCode_ReturnError()
        {
            // 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 InsertClassifierHandler(unitOfWorkFactory, classifierRepositoryFactory);

            using (var _ = unitOfWorkFactory.Create())
            {
                // arrange - 1st item
                await generator.InsertType(HierarchyType.None, cancellationToken);

                var command = new InsertClassifier
                {
                    UserUid = generator.UserUid,
                    Item    = new Classifier
                    {
                        Type = generator.TypeCode,
                        Code = "001",
                        Name = "Test Classifier"
                    }
                };

                // act
                var result = await handler.Handle(command, cancellationToken);

                // assert
                Assert.IsNotNull(result);
                Assert.IsTrue(result.Success);
                Assert.IsNotNull(result.Uid);
                Assert.AreNotEqual(Guid.Empty, result.Uid);

                // arrange - 2nd item
                command.Item.Uid = Guid.NewGuid();

                // act
                result = await handler.Handle(command, cancellationToken);

                // assert
                Assert.IsNotNull(result);
                Assert.IsFalse(result.Success);
                Assert.IsNull(result.Uid);
                Assert.AreEqual(1, result.Errors.Count);
                Assert.AreEqual("code", result.Errors[0].Key);
                // todo: use error codes?
                Assert.AreEqual("Code «001» is already used in item \"Test Classifier\"", result.Errors[0].Messages[0]);
            }
        }
Exemple #2
0
        public async Task <ApiResult> Insert(InsertClassifier request)
        {
            request.UserUid = _currentUserProvider.GetUserUid();

            return(await _mediator.Send(request));
        }
        public async Task InsertClassifier_NormalValues_InsertItems()
        {
            // 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 InsertClassifierHandler(unitOfWorkFactory, classifierRepositoryFactory);

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

                // act
                var command = new InsertClassifier
                {
                    UserUid = generator.UserUid,
                    Item    = new Classifier
                    {
                        Type   = generator.TypeCode,
                        Code   = "001",
                        Name   = "Test Classifier",
                        Fields = new FieldData
                        {
                            { "test1", "value1" },
                            { "test2", "value2" },
                            { "test3", "value3" }
                        }
                    }
                };

                var result = await handler.Handle(command, cancellationToken);

                // assert
                Assert.IsNotNull(result);
                Assert.IsTrue(result.Success);
                Assert.IsNotNull(result.Uid);
                Assert.AreNotEqual(Guid.Empty, result.Uid);

                var classifierResult = await classifierRepositoryFactory.GetNamedOrDefaultService(generator.TypeCode)
                                       .Search(new ClassifierSearchRequest {
                    TypeCode = generator.TypeCode, Uid = result.Uid, IncludeFields = true
                }, cancellationToken);

                Assert.AreEqual(1, classifierResult.Rows.Count);
                var inserted = classifierResult.Rows[0];
                // todo: figure out why insert.Fields is null
                Assert.AreEqual(command.Item.Code, inserted.Code);
                Assert.AreEqual(command.Item.Name, inserted.Name);

                // assert field data inserted
                IList <DbFieldData> fieldData;
                using (var db = dbContextFactory.Create())
                {
                    fieldData = await db.GetTable <DbFieldData>()
                                .Where(x => x.EntityTypeCode == Classifier.TypeCode && x.EntityUid == result.Uid)
                                .ToListAsync(cancellationToken);
                }

                Assert.AreEqual(command.Item.Fields.Count, fieldData.Count);
                Assert.AreEqual(command.Item.Fields["test1"], fieldData.Single(x => x.Key == "test1").Value);
                Assert.AreEqual(command.Item.Fields["test2"], fieldData.Single(x => x.Key == "test2").Value);
                Assert.AreEqual(command.Item.Fields["test3"], fieldData.Single(x => x.Key == "test3").Value);
            }
        }