Пример #1
0
        public async Task AppendRecordAsync_ShouldThrow_OnModelContainsInvalidTags()
        {
            //arrange
            var model = new RecordViewModel {Tags = new[] {new TagViewModel {Name = "<invalid tag>"}}};
            _recordValidatorMock.Setup(o => o.IsNameValid("<invalid tag>")).Returns(false);
            var target = GetService();

            //act & assert
            var actual = Assert.ThrowsAsync(typeof(ArgumentException), () => target.AppendRecordAsync(model));
            actual.Message.Should().Be("Invalid tag");
        }
Пример #2
0
        public async Task AppendRecordAsync_ShouldThrow_OnModelWithInvalidTitle()
        {
            //arrange
            var model = new RecordViewModel { Title = "<script>" };
            _recordValidatorMock.Setup(o => o.IsNameValid(It.IsAny<string>())).Returns(false);
            var target = GetService();

            //act & assert
            var actual = Assert.ThrowsAsync(typeof(ArgumentException), () => target.AppendRecordAsync(model));
            actual.Message.Should().Be("Invalid title: <script>");
        }
Пример #3
0
        public async Task AppendRecordAsync_ShouldSaveNewTag_OnModelContainsNewTagThatDoesntExist()
        {
            //arrange
            var NOT_EXISTING_TAG = "not existing tag";
            var model = new RecordViewModel {Tags = new[] {new TagViewModel {Name = NOT_EXISTING_TAG, Id = 0}}};
            var target = GetService();

            //act
            await target.AppendRecordAsync(model);

            //assert
            _tagRepositoryMock.Verify(o => o.SaveAsync(It.IsAny<IEnumerable<Tag>>()), Times.Once);
        }
Пример #4
0
        public async Task<RecordViewModel> AppendRecordAsync(RecordViewModel recordViewModel)
        {
            if (!_recordValidator.IsDescriptionValid(recordViewModel.Description))
                throw new ArgumentException(string.Format("Invalid description: {0}", recordViewModel.Description));

            if (!_recordValidator.IsNameValid(recordViewModel.Title))
                throw new ArgumentException(string.Format("Invalid title: {0}", recordViewModel.Title));

            if (recordViewModel.Tags.Any(tag => !_recordValidator.IsNameValid(tag.Name)))
                throw new ArgumentException("Invalid tag");

            if (recordViewModel.Tags.Any(tag => tag.Id == 0))
            {
                var tagsToSave = recordViewModel.Tags.Select(_converter.ConvertToTag);
                var savedTags = await _tagRepository.SaveAsync(tagsToSave);
                recordViewModel.Tags = savedTags.Select(_converter.ConvertToViewModel);
            }

            var savedRecord = await _recordsRepository.SaveAsync(_converter.ConvertToRecord(recordViewModel));

            return _converter.ConvertToViewModel(savedRecord);
        }
Пример #5
0
 public async Task<JsonResult> AppendRecord(RecordViewModel record)
 {
     return Json(await _service.AppendRecordAsync(record), JsonRequestBehavior.AllowGet);
 }
Пример #6
0
        public async Task AppendRecordAsync_ShouldSaveRecord()
        {
            //assert
            var target = GetService();
            var model = new RecordViewModel { Tags = new[] { new TagViewModel { Name = "some-tag" } } };
            _recordRepositoryMock.Setup(o => o.GetTagByNameAsync(It.IsAny<string>())).ReturnsAsync(new Tag());

            //act
            await target.AppendRecordAsync(model);

            //assert
            _recordRepositoryMock.Verify(o => o.SaveAsync(It.IsAny<Record>()), Times.Once);
        }