コード例 #1
0
        public async Task CreateAsync_WithEmptyNameShouldThrowArgumentNullException()
        {
            MapperInitializer.InitializeMapper();
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();
            var asphaltMixtureService            = new AsphaltMixtureService(context);
            var createAsphaltMixtureServiceModel = new CreateAsphaltMixtureServiceModel();
            var asphaltMixtureType = "  ";

            createAsphaltMixtureServiceModel.Type = asphaltMixtureType;

            await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                await asphaltMixtureService.CreateAsync(createAsphaltMixtureServiceModel);
            });
        }
コード例 #2
0
        public async Task CreateAsync_WithOverMaxNameLengthShouldThrowInvalidOperationException()
        {
            MapperInitializer.InitializeMapper();
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();
            var asphaltMixtureService            = new AsphaltMixtureService(context);
            var createAsphaltMixtureServiceModel = new CreateAsphaltMixtureServiceModel();
            var asphaltMixtureType = "qwertyuiop qwertyuiop qwertyuiop qwertyuiop qwertyuiop";

            createAsphaltMixtureServiceModel.Type = asphaltMixtureType;
            var message = "Asphalt mixture's type cannot be more than 30 characters.";

            var exception = await Assert.ThrowsAsync <InvalidOperationException>(async() =>
            {
                await asphaltMixtureService.CreateAsync(createAsphaltMixtureServiceModel);
            });

            Assert.Equal(message, exception.Message);
        }
コード例 #3
0
        public async Task CreateAsync_ShouldSuccessfullyCreate()
        {
            MapperInitializer.InitializeMapper();
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();
            var asphaltMixtureService            = new AsphaltMixtureService(context);
            var createAsphaltMixtureServiceModel = new CreateAsphaltMixtureServiceModel();
            var asphaltMixtureType = "AMT 1";

            createAsphaltMixtureServiceModel.Type = asphaltMixtureType;

            await asphaltMixtureService.CreateAsync(createAsphaltMixtureServiceModel);

            var expectedResult = asphaltMixtureType;
            var actualResult   = asphaltMixtureService
                                 .All()
                                 .First()
                                 .Type;

            Assert.True(expectedResult == actualResult);
        }
コード例 #4
0
        public async Task CreateAsync_WithExistingNameShouldThrowInvalidOperationException()
        {
            MapperInitializer.InitializeMapper();
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            await this.SeedDataAsync(context);

            var asphaltMixtureService            = new AsphaltMixtureService(context);
            var createAsphaltMixtureServiceModel = new CreateAsphaltMixtureServiceModel();
            var asphaltMixtureType = "AMT 1";

            createAsphaltMixtureServiceModel.Type = asphaltMixtureType;
            var message = "Asphalt mixture's type already exists.";

            var exception = await Assert.ThrowsAsync <InvalidOperationException>(async() =>
            {
                await asphaltMixtureService.CreateAsync(createAsphaltMixtureServiceModel);
            });

            Assert.Equal(message, exception.Message);
        }
コード例 #5
0
        public async Task CreateAsync(CreateAsphaltMixtureServiceModel createAsphaltMixtureServiceModel)
        {
            var asphaltMixture = AutoMapperConfig.MapperInstance.Map <AsphaltMixture>(createAsphaltMixtureServiceModel);

            if (string.IsNullOrWhiteSpace(asphaltMixture.Type))
            {
                throw new ArgumentNullException(EmptyAsphaltMixtureErrorMessage);
            }

            if (await this.context.AsphaltMixtures.AnyAsync(am => am.Type == asphaltMixture.Type))
            {
                throw new InvalidOperationException(AsphaltMixtureExistErrorMessage);
            }

            if (asphaltMixture.Type.Length > AttributesConstraints.AsphaltMixtureTypeMaxLength)
            {
                throw new InvalidOperationException(string.Format(AsphaltMixtureTypeMaxLengthErrorMessage, AttributesConstraints.AsphaltMixtureTypeMaxLength));
            }

            await this.context.AsphaltMixtures.AddAsync(asphaltMixture);

            await this.context.SaveChangesAsync();
        }