public async Task TestUpdateAsync_Succes()
        {
            // Arrange
            var random = new Random(0);
            var number = random.Next(5);
            var entity = GetList()[number];
            await _service.InsertAsync(entity);

            _dbContext.Entry(entity).State = EntityState.Detached;

            entity = new TypeVictim()
            {
                Id        = entity.Id,
                Name      = "Тип № Тест",
                CreatedAt = DateTime.Now.AddMinutes(60),
                UpdatedAt = DateTime.Now.AddMinutes(60)
            };
            await _service.UpdateAsync(entity);

            _dbContext.Entry(entity).State = EntityState.Detached;

            // Act
            var result = await _service.GetAsync(entity.Id);

            _dbContext.Entry(result).State = EntityState.Detached;

            // Assert
            Assert.Multiple(() =>
            {
                Assert.IsTrue(entity.Name == result.Name);
                Assert.IsTrue(entity.CreatedAt == result.CreatedAt);
                Assert.IsTrue(entity.UpdatedAt == result.UpdatedAt);
            });
        }
Exemplo n.º 2
0
        public async Task UpdateValidationAsync_Null_UnSucces()
        {
            // Arrange
            MockData(
                isExistTypeVictim: true,
                isExistTypeVictimById: true
                );
            TypeVictim entity    = null;
            var        listError = new Dictionary <string, string>()
            {
                { nameof(TypeVictim), string.Format(BaseValidation.ObjectNotCanBeNull, nameof(TypeVictim)) }
            };

            // Act
            var result = await _validation.UpdateValidationAsync(entity);

            // Assert
            Assert.Multiple(() =>
            {
                Assert.NotNull(result.Errors);
                Assert.IsNotEmpty(result.Errors);
                Assert.IsFalse(result.IsValid);
                foreach (var error in result.Errors)
                {
                    Assert.IsTrue(listError.ContainsKey(error.Key));
                    Assert.AreEqual(listError[error.Key], error.Value);
                }
            });
        }
Exemplo n.º 3
0
        public async Task <TypeVictim> UpdateAsync(TypeVictim entity, CancellationToken cancellationToken)
        {
            db.TypeVictims.Update(entity);
            await db.SaveChangesAsync(cancellationToken);

            return(entity);
        }
        public async Task <ValidationModel <TypeVictim> > UpdateValidationAsync(TypeVictim newEntity, CancellationToken cancellationToken = default)
        {
            if (newEntity is not null)
            {
                if (!await dataTypeVictim.ExistByIdAsync(newEntity.Id, cancellationToken))
                {
                    validation.Errors.Add(nameof(TypeVictim),
                                          string.Format(BaseValidation.ObjectNotExistById, nameof(TypeVictim), newEntity.Id));
                }
                else if (await dataTypeVictim.ExistAsync(newEntity, cancellationToken))
                {
                    validation.Errors.Add(nameof(TypeVictim),
                                          string.Format(BaseValidation.ObjectExistWithThisData, nameof(TypeVictim)));
                }

                if (newEntity.Name is null)
                {
                    validation.Errors.Add(nameof(TypeVictim.Name),
                                          string.Format(BaseValidation.FieldNotCanBeNull, nameof(TypeVictim.Name)));
                }
                else if (newEntity.Name.Length < 3 || newEntity.Name.Length > 128)
                {
                    validation.Errors.Add(nameof(TypeVictim.Name),
                                          string.Format(BaseValidation.FieldInvalidLength, nameof(TypeVictim.Name), 3, 128));
                }
            }
            else
            {
                validation.Errors.Add(nameof(TypeVictim),
                                      string.Format(BaseValidation.ObjectNotCanBeNull, nameof(TypeVictim)));
            }

            return(validation);
        }
Exemplo n.º 5
0
        public async Task <TypeVictim> InsertAsync(TypeVictim entity, CancellationToken cancellationToken)
        {
            await db.TypeVictims.AddAsync(entity, cancellationToken);

            await db.SaveChangesAsync(cancellationToken);

            return(entity);
        }
Exemplo n.º 6
0
        public async Task <ValidationModel <TypeVictim> > InsertAsync(TypeVictim newEntity, CancellationToken cancellationToken = default)
        {
            var validation = await typeVictimValidation.InsertValidationAsync(newEntity, cancellationToken);

            if (!validation.IsValid)
            {
                return(validation);
            }

            newEntity.CreatedAt = DateTime.Now;
            newEntity.UpdatedAt = DateTime.Now;

            validation.Result = await dataTypeVictim.InsertAsync(newEntity, cancellationToken);

            return(validation);
        }
Exemplo n.º 7
0
        private void MockData(
            TypeVictim typeVictim = default,
            IEnumerable <TypeVictim> typeVictimList           = default,
            ValidationModel <TypeVictim> validationTypeVictim = default
            )
        {
            _dataTypeVictim.Setup(x => x.GetAsync(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(typeVictim);
            _dataTypeVictim.Setup(x => x.GetAllAsync(It.IsAny <CancellationToken>())).ReturnsAsync(typeVictimList);
            _dataTypeVictim.Setup(x => x.InsertAsync(It.IsAny <TypeVictim>(), It.IsAny <CancellationToken>())).ReturnsAsync(typeVictim);
            _dataTypeVictim.Setup(x => x.UpdateAsync(It.IsAny <TypeVictim>(), It.IsAny <CancellationToken>())).ReturnsAsync(typeVictim);
            _dataTypeVictim.Setup(x => x.DeleteAsync(It.IsAny <Guid>(), It.IsAny <CancellationToken>()));

            _typeVictimValidation.Setup(x => x.GetValidationAsync(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(validationTypeVictim);
            _typeVictimValidation.Setup(x => x.InsertValidationAsync(It.IsAny <TypeVictim>(), It.IsAny <CancellationToken>())).ReturnsAsync(validationTypeVictim);
            _typeVictimValidation.Setup(x => x.UpdateValidationAsync(It.IsAny <TypeVictim>(), It.IsAny <CancellationToken>())).ReturnsAsync(validationTypeVictim);
            _typeVictimValidation.Setup(x => x.DeleteValidationAsync(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(validationTypeVictim);

            _service = new TypeVictimService(_dataTypeVictim.Object, _typeVictimValidation.Object);
        }
Exemplo n.º 8
0
 public async Task <bool> ExistAsync(TypeVictim entity, CancellationToken cancellationToken)
 {
     return(await db.TypeVictims.AnyAsync(db =>
                                          db.Name == entity.Name,
                                          cancellationToken));
 }