コード例 #1
0
        public async Task EditAsync(EditRoadObjectServiceModel editRoadObjectServiceModel)
        {
            var roadObject = await this.context
                             .RoadObjects
                             .FindAsync(editRoadObjectServiceModel.Id);

            if (roadObject == null)
            {
                throw new ArgumentNullException(string.Format(InvalidRoadObjectIdErrorMessage, editRoadObjectServiceModel.Id));
            }

            if (string.IsNullOrWhiteSpace(editRoadObjectServiceModel.Name))
            {
                throw new ArgumentNullException(EmptyRoadObjectErrorMessage);
            }

            if (await this.context.RoadObjects.AnyAsync(ro => ro.Name == editRoadObjectServiceModel.Name))
            {
                throw new InvalidOperationException(RoadObjectExistErrorMessage);
            }

            if (editRoadObjectServiceModel.Name.Length > AttributesConstraints.RoadObjectNameMaxLength)
            {
                throw new InvalidOperationException(string.Format(RoadObjectNameMaxLengthErrorMessage, AttributesConstraints.RoadObjectNameMaxLength));
            }

            roadObject.Name = editRoadObjectServiceModel.Name;

            await this.context.SaveChangesAsync();
        }
コード例 #2
0
        public async Task EditAsync_ShouldSuccessfullyEdit()
        {
            MapperInitializer.InitializeMapper();
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            await this.SeedDataAsync(context);

            var roadObjectService          = new RoadObjectService(context);
            var editRoadObjectServiceModel = new EditRoadObjectServiceModel();
            var roadObjectId   = 1;
            var roadObjectName = "FN 3";

            editRoadObjectServiceModel.Id   = roadObjectId;
            editRoadObjectServiceModel.Name = roadObjectName;

            await roadObjectService.EditAsync(editRoadObjectServiceModel);

            var expectedResult = roadObjectName;
            var actualResult   = roadObjectService
                                 .All()
                                 .First()
                                 .Name;

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

            await this.SeedDataAsync(context);

            var roadObjectService          = new RoadObjectService(context);
            var editRoadObjectServiceModel = new EditRoadObjectServiceModel();

            editRoadObjectServiceModel.Id = 3;

            await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                await roadObjectService.EditAsync(editRoadObjectServiceModel);
            });
        }
コード例 #4
0
        public async Task EditAsync_WithOverMaxNameLengthShouldThrowInvalidOperationException()
        {
            MapperInitializer.InitializeMapper();
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            await this.SeedDataAsync(context);

            var roadObjectService          = new RoadObjectService(context);
            var editRoadObjectServiceModel = new EditRoadObjectServiceModel();
            var roadObjectName             = "qwertyuiop qwertyuiop qwertyuiop qwertyuiop qwertyuiop";

            editRoadObjectServiceModel.Name = roadObjectName;
            editRoadObjectServiceModel.Id   = 1;
            var message = "Road object's name cannot be more than 50 characters.";

            var exception = await Assert.ThrowsAsync <InvalidOperationException>(async() =>
            {
                await roadObjectService.EditAsync(editRoadObjectServiceModel);
            });

            Assert.Equal(message, exception.Message);
        }
コード例 #5
0
        public async Task EditAsync_WithExistingNameShouldThrowInvalidOperationException()
        {
            MapperInitializer.InitializeMapper();
            var context = ApplicationDbContextInMemoryFactory.InitializeContext();

            await this.SeedDataAsync(context);

            var roadObjectService          = new RoadObjectService(context);
            var editRoadObjectServiceModel = new EditRoadObjectServiceModel();
            var roadObjectName             = "RON 2";

            editRoadObjectServiceModel.Name = roadObjectName;
            editRoadObjectServiceModel.Id   = 1;
            var message = "Road object's name already exists.";

            var exception = await Assert.ThrowsAsync <InvalidOperationException>(async() =>
            {
                await roadObjectService.EditAsync(editRoadObjectServiceModel);
            });

            Assert.Equal(message, exception.Message);
        }