public void GetDate_WithFormAnswers_ReturnCorrectDate() { var formAnswers = new FormAnswers { Pages = new List <PageAnswers> { new PageAnswers { Answers = new List <Answers> { new Answers { QuestionId = $"test-date{DateInput.YEAR_EXTENSION}", Response = "2020" }, new Answers { QuestionId = $"test-date{DateInput.MONTH_EXTENSION}", Response = "01" }, new Answers { QuestionId = $"test-date{DateInput.DAY_EXTENSION}", Response = "01" } } } } }; DateTime?result = DateInput.GetDate(formAnswers, "test-date"); Assert.True(result.HasValue); Assert.True(result.Value == new DateTime(2020, 1, 1)); }
public void GetDate_ReturnNull_IfKeyNotPresent() { var viewModel = new Dictionary <string, dynamic>(); DateTime?result = DateInput.GetDate(viewModel, "test-date"); Assert.False(result.HasValue); }
public ValidationResult Validate(Element element, Dictionary <string, dynamic> viewModel, FormSchema baseForm) { if ((!element.Type.Equals(EElementType.DatePicker) && !element.Type.Equals(EElementType.DateInput)) || string.IsNullOrEmpty(element.Properties.IsDateBeforeAbsolute)) { return(new ValidationResult { IsValid = true }); } DateTime?dateValue = new DateTime(); if (element.Type.Equals(EElementType.DatePicker)) { dateValue = DatePicker.GetDate(viewModel, element.Properties.QuestionId); } if (element.Type.Equals(EElementType.DateInput)) { dateValue = DateInput.GetDate(viewModel, element.Properties.QuestionId); } if (!dateValue.HasValue) { return new ValidationResult { IsValid = true } } ; if (!DateTime.TryParse(element.Properties.IsDateBeforeAbsolute, out DateTime comparisonDateValue)) { throw new FormatException("IsDateBeforeAbsoluteValidator: The comparison date format was incorrect"); } if (dateValue < comparisonDateValue) { return new ValidationResult { IsValid = true } } ; return(new ValidationResult { IsValid = false, Message = !string.IsNullOrEmpty(element.Properties.IsDateBeforeValidationMessage) ? element.Properties.IsDateBeforeValidationMessage : string.Format(ValidationConstants.IS_DATE_BEFORE_VALIDATOR_DEFAULT, element.Properties.IsDateBeforeAbsolute) }); } } }
public void GetDate_ReturnNull_IfOnlyPartialCorrectDate() { var viewModel = new Dictionary <string, dynamic> { { $"test-date{DateInput.YEAR_EXTENSION}", "2020" }, { $"test-date{DateInput.MONTH_EXTENSION}", "01" }, }; var result = DateInput.GetDate(viewModel, "test-date"); Assert.False(result.HasValue); }
public void GetDate_ReturnCorrectDate() { var viewModel = new Dictionary <string, dynamic> { { $"test-date{DateInput.YEAR_EXTENSION}", "2020" }, { $"test-date{DateInput.MONTH_EXTENSION}", "01" }, { $"test-date{DateInput.DAY_EXTENSION}", "01" } }; DateTime?result = DateInput.GetDate(viewModel, "test-date"); Assert.True(result.HasValue); Assert.True(result.Value == new DateTime(2020, 1, 1)); }