Esempio n. 1
0
        public void CreateDateTimeValidatorValidateDateTimeSuccessfullyWithinRangeSupplied()
        {
            var validator = new DateTimeValidator(new DateTime(1978, 1, 1), new DateTime(1980, 1, 1));
            var result    = validator.Validate(new DateTime(1979, 05, 06));

            Assert.IsTrue(result.Success);
        }
Esempio n. 2
0
        public void CreateDateTimeValidatorValidateDateTimeSuccessfullyNoValidationRangeSupplied()
        {
            var validator = new DateTimeValidator();
            var result    = validator.Validate(new DateTime(1979, 05, 06));

            Assert.IsTrue(result.Success);
        }
Esempio n. 3
0
        public void CreateDateTimeValidatorValidateBadNumberValueExpectValidationError()
        {
            var validator = new DateTimeValidator();
            var result    = validator.Validate("23");

            Assert.IsFalse(result.Success);
        }
Esempio n. 4
0
        public void Validate_DateMustBe2000OrLater(ValidationType type, int year, int month, int day)
        {
            //Arrange
            var model = new DateTime(year, month, day);

            //Act
            var result = _sut.Validate(model);

            //Assert
            result.Should(type, "8d7af18b-1a13-4b7e-963d-68c6cd6636ca");
        }
        public void ErrorMessage_BasicFormat_AreEqual()
        {
            string column = "測試欄位";

            DateTimeValidator validator = new DateTimeValidator(column, "error");

            validator.Validate();

            Assert.AreEqual(
                string.Format(validator.DefaultErrorMessageFormat, column),
                validator.ErrorMessage
                );
        }
        public void ErrorMessage_CustomFormat_AreEqual()
        {
            string column = "測試欄位";

            DateTimeValidator validator = new DateTimeValidator(column, "error", "{0}DateTime");

            validator.Validate();

            Assert.AreEqual(
                string.Format(validator.CustomErrorMessageFormat, column),
                validator.ErrorMessage
                );
        }
        public void ErrorMessage_MinFormat_AreEqual()
        {
            string   column      = "測試欄位";
            DateTime minDateTime = new DateTime(1912, 3, 5);

            DateTimeValidator validator = DateTimeValidator.CreateMinDateTime(column, "1912/03/04", minDateTime);

            validator.Validate();

            Assert.AreEqual(
                string.Format(validator.MinValueErrorMessageFormat, column, minDateTime),
                validator.ErrorMessage
                );
        }
        public void ErrorMessage_MaxCustomFormat_AreEqual()
        {
            string   column      = "測試欄位";
            DateTime maxDateTime = new DateTime(1912, 3, 3);

            DateTimeValidator validator = DateTimeValidator.CreateMaxDateTime(column, "1912/03/04", maxDateTime, customRangeMessageFormat: "{0}_{1}DateTime");

            validator.Validate();

            Assert.AreEqual(
                string.Format(validator.CustomRangeMessageFormat, column, maxDateTime),
                validator.ErrorMessage
                );
        }
        public void ErrorMessage_RangeFormat_AreEqual()
        {
            string   column      = "測試欄位";
            DateTime minDateTime = new DateTime(1912, 3, 5);
            DateTime maxDateTime = new DateTime(1912, 3, 3);

            DateTimeValidator validator = new DateTimeValidator(column, "1912/03/04", minDateTime, maxDateTime);

            validator.Validate();

            Assert.AreEqual(
                string.Format(validator.RangeErrorMessageFormat, column, minDateTime, maxDateTime),
                validator.ErrorMessage
                );
        }
Esempio n. 10
0
        public async Task <IActionResult> SaveCustomer([FromBody] CreateCustomerRequest request)
        {
            var errorInfo = new ErrorInfo();

            // Validate Fullname
            errorInfo = FullNameValidator.Validate(request.FullName, out string fullName);
            if (errorInfo.ErrorCode != ErrorTypes.OK)
            {
                throw new BadInputException(errorInfo);
            }

            // Validate Date of Birth
            errorInfo = DateTimeValidator.Validate(request.DateOfBirth, out DateTime? validDate);
            if (errorInfo.ErrorCode != ErrorTypes.OK)
            {
                throw new BadInputException(errorInfo);
            }

            // Check if user already exists
            var user = await _customerRepository.GetCustomerByFullNameAsync(fullName, validDate);

            if (user?.Id != null)
            {
                errorInfo.ErrorCode    = ErrorTypes.InvalidFullName;
                errorInfo.ErrorMessage = $"This name '{fullName}' is already exists.";
                throw new BadInputException(errorInfo);
            }

            // Validate ICollection Address
            foreach (var item in request.Address)
            {
                errorInfo = AddressValidator.Validate(item);
                if (errorInfo.ErrorCode != ErrorTypes.OK)
                {
                    throw new BadInputException(errorInfo);
                }
            }

            // Map request into model
            var customerModel = _mapper.Map <CustomerModel>(request);

            customerModel.DateOfBirth = validDate;
            customerModel.FullName    = fullName;

            await _customerRepository.CreateCustomerAsync(customerModel);

            return(Ok(customerModel));
        }
Esempio n. 11
0
        /// <summary>
        /// Calculate Method
        /// </summary>
        /// <param name="theDateTime"></param>
        /// <returns></returns>
        public static int Calculate(string theDateTime)
        {
            var errorInfo = new ErrorInfo();

            // Validate Date of Birth
            errorInfo = DateTimeValidator.Validate(theDateTime, out DateTime? validDate);
            if (errorInfo.ErrorCode != ErrorTypes.OK)
            {
                throw new BadInputException(errorInfo);
            }

            int age = DateTime.Today.Year - validDate.Value.Year;

            if (validDate.Value.AddYears(age) > DateTime.Today)
            {
                age--;
            }

            return(age);
        }
 public TimeReservationContentViewModel(IInteractionRequestAware viewModel, ReservationItem item)
 {
     _reservationService = ServiceLocator.Current.GetInstance <ReservationService>();
     StartAt             = new ReactiveProperty <string>(item?.StartAt?.ToString() ?? "").AddTo(this);
     RepetitionType      = new ReactiveProperty <EnumWrap <Repetition> >(new EnumWrap <Repetition>(Repetition.None)).AddTo(this);
     StartAt.SetValidateNotifyError(w => _dtValidator.Validate(w)).AddTo(this);
     RegisterCommand = StartAt.ObserveHasErrors.Select(w => !w).ToReactiveCommand().AddTo(this);
     RegisterCommand.Subscribe(w =>
     {
         if (item == null)
         {
             _reservationService.InsertTimeReservaion(DateTime.Parse(StartAt.Value),
                                                      RepetitionType.Value.EnumValue);
         }
         else
         {
             item.TimeReservation.StartAt = DateTime.Parse(StartAt.Value);
             item.Update();
         }
         viewModel.FinishInteraction.Invoke();
     }).AddTo(this);
 }
Esempio n. 13
0
        public void DateTime_Validator_Test()
        {
            var sut = new DateTimeValidator();

            var col1 = new ColumnTypeInfo()
            {
                AllowNull = true
            };
            var col2 = new ColumnTypeInfo()
            {
                AllowNull = false
            };

            Assert_Valid(sut.Validate(col1, "1/1/2016"));
            Assert_Valid(sut.Validate(col1, new DateTime(2016, 1, 1)));

            Assert_Invalid(sut.Validate(col1, "Foo"));
            Assert_Invalid(sut.Validate(col1, "2/29/2015"));

            Assert_Invalid_Replaced(sut.Validate(col2, "Foo"), SqlDateTime.MinValue);
            Assert_Invalid_Replaced(sut.Validate(col2, "2/29/2015"), SqlDateTime.MinValue);
            Assert_Invalid_Replaced(sut.Validate(col2, DBNull.Value), SqlDateTime.MinValue);
        }
        public void Validate_Range_AreEqual(string value, bool isValid)
        {
            DateTimeValidator validator = new DateTimeValidator("", value, new DateTime(1912, 3, 2), new DateTime(1912, 3, 4));

            Assert.AreEqual(validator.Validate(), isValid);
        }
Esempio n. 15
0
        public async Task <IActionResult> UpdateCustomer(int id, UpdateCustomerRequest request)
        {
            var errorInfo = new ErrorInfo();

            // Verify customer identifier from route and request body
            errorInfo = IdentifierValidator.Validate(id, request.CustomerIdentifier);
            if (errorInfo.ErrorCode != ErrorTypes.OK)
            {
                throw new BadInputException(errorInfo);
            }

            // Find customer to update
            var currentCustomer = await _customerRepository.GetCustomerByIdentifierAsync(id);

            // Customer
            errorInfo = CustomerObjectValidator.Validate(currentCustomer, id);
            if (errorInfo.ErrorCode != ErrorTypes.OK)
            {
                throw new BadInputException(errorInfo);
            }

            bool isModified = false;

            // Fullname
            if (request.FullName != null && currentCustomer.FullName != request.FullName)
            {
                errorInfo = FullNameValidator.Validate(request.FullName, out string fullName);
                if (errorInfo.ErrorCode != ErrorTypes.OK)
                {
                    throw new BadInputException(errorInfo);
                }

                isModified = true;
                currentCustomer.FullName = fullName;
            }

            // Date of Birth
            if (request.DateOfBirth != null && currentCustomer.DateOfBirth.ToString() != request.DateOfBirth)
            {
                errorInfo = DateTimeValidator.Validate(request.DateOfBirth, out DateTime? validDate);
                if (errorInfo.ErrorCode != ErrorTypes.OK)
                {
                    throw new BadInputException(errorInfo);
                }

                isModified = true;
                currentCustomer.DateOfBirth = validDate;
                currentCustomer.Age         = CalculateAge.Calculate(request.DateOfBirth);
            }

            // Validate ICollection Address
            if (request?.Address != null)
            {
                foreach (var item in request.Address)
                {
                    errorInfo = AddressValidator.Validate(item);
                    if (errorInfo.ErrorCode != ErrorTypes.OK)
                    {
                        throw new BadInputException(errorInfo);
                    }
                }

                isModified = true;
                currentCustomer.Address = _mapper.Map <List <AddressModel> >(request.Address);
            }

            if (isModified)
            {
                // TODO: To implement the updated and created date in Model
                // newCustomer.UpdatedDate = DateTime.UtcNow;
                await _customerRepository.UpdateCustomerAsync(currentCustomer);
            }

            // Map Journal Model to Journal Dto
            var resultDto = _mapper.Map <CustomerDto>(currentCustomer);

            return(Ok(resultDto));
        }
        public void Validate_Max_AreEqual(string value, bool isValid)
        {
            DateTimeValidator validator = DateTimeValidator.CreateMaxDateTime("", value, new DateTime(1912, 3, 2));

            Assert.AreEqual(validator.Validate(), isValid);
        }
        public void Validate_Format_AreEqual(string value, bool isValid)
        {
            DateTimeValidator validator = new DateTimeValidator("", value);

            Assert.AreEqual(validator.Validate(), isValid);
        }