Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalTichy committed Oct 10, 2024
1 parent 72f0886 commit 359725f
Showing 1 changed file with 63 additions and 122 deletions.
185 changes: 63 additions & 122 deletions src/Tests/ViewModel/ViewModelValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using DotVVM.Framework.Testing;
using Microsoft.Extensions.DependencyInjection;
using DotVVM.Framework.Hosting;
using Microsoft.Extensions.Logging;

namespace DotVVM.Framework.Tests.ViewModel
{
Expand Down Expand Up @@ -41,15 +42,8 @@ public void ViewModelValidator_SimpleObject()
[TestMethod]
public void ViewModelValidator_ObjectWithCollection()
{
var testViewModel = new TestViewModel()
{
Children = new List<TestViewModel2>()
{
new TestViewModel2() { Code = "012" },
new TestViewModel2() { Code = "ABC", Id = 13 },
new TestViewModel2() { Code = "345", Id = 15 }
},
Child = new TestViewModel2() { Code = "123" }
var testViewModel = new TestViewModel {
Children = new List<TestViewModel2> { new() { Code = "012" }, new() { Code = "ABC", Id = 13 }, new() { Code = "345", Id = 15 } }, Child = new TestViewModel2 { Code = "123" }
};
var validator = CreateValidator();
var expander = CreateErrorPathExpander();
Expand Down Expand Up @@ -105,15 +99,7 @@ public void ViewModelValidator_WithValidationTarget_Property()
[TestMethod]
public void ViewModelValidator_WithValidationTarget_ArrayElement()
{
var testViewModel = new TestViewModel()
{
Children = new List<TestViewModel2>()
{
new TestViewModel2() { Code = "5" },
new TestViewModel2() { Code = "6" },
new TestViewModel2() { Code = "7" },
}
};
var testViewModel = new TestViewModel { Children = new List<TestViewModel2> { new() { Code = "5" }, new() { Code = "6" }, new() { Code = "7" } } };
var validator = CreateValidator();
var expander = CreateErrorPathExpander();
var modelState = new ModelState() { ValidationTarget = testViewModel.Children[1] };
Expand Down Expand Up @@ -184,10 +170,7 @@ public void ViewModelValidator_Child_IValidatableObject()
[TestMethod]
public void ViewModelValidator_CollectionOfIValidatableObjects()
{
var testViewModel = new TestViewModel6()
{
Children = new List<TestViewModel5Child>() {new TestViewModel5Child() {IsChecked = true}}
};
var testViewModel = new TestViewModel6 { Children = new List<TestViewModel5Child> { new() { IsChecked = true } } };
var validator = CreateValidator();
var expander = CreateErrorPathExpander();
var modelState = new ModelState() { ValidationTarget = testViewModel };
Expand Down Expand Up @@ -221,19 +204,9 @@ public void ViewModelValidator_ValidationErrorFactoryWithValidationContextSuppli
[TestMethod]
public void ViewModelValidator_CustomModelStateErrors()
{
var testViewModel = new TestViewModel()
{
Context = new TestDotvvmRequestContext
{
Configuration = DotvvmTestHelper.DefaultConfig,
ModelState = new ModelState(),
RequestType = DotvvmRequestType.Command
},
Child = new TestViewModel2()
{
Id = 11,
Code = "Code",
},
var testViewModel = new TestViewModel {
Context = new TestDotvvmRequestContext { Configuration = DotvvmTestHelper.DefaultConfig, ModelState = new ModelState(), RequestType = DotvvmRequestType.Command },
Child = new TestViewModel2 { Id = 11, Code = "Code" }
};
var validator = CreateValidator();
var expander = CreateErrorPathExpander();
Expand All @@ -257,19 +230,9 @@ public void ViewModelValidator_CustomModelStateErrors()
[ExpectedException(typeof(ArgumentException))]
public void ViewModelValidator_CustomModelStateErrors_OldFormatThrows()
{
var testViewModel = new TestViewModel()
{
Context = new TestDotvvmRequestContext
{
Configuration = DotvvmTestHelper.DefaultConfig,
ModelState = new ModelState(),
RequestType = DotvvmRequestType.Command
},
Child = new TestViewModel2()
{
Id = 11,
Code = "Code",
},
var testViewModel = new TestViewModel {
Context = new TestDotvvmRequestContext { Configuration = DotvvmTestHelper.DefaultConfig, ModelState = new ModelState(), RequestType = DotvvmRequestType.Command },
Child = new TestViewModel2 { Id = 11, Code = "Code" }
};
var validator = CreateValidator();
var expander = CreateErrorPathExpander();
Expand All @@ -286,25 +249,10 @@ public void ViewModelValidator_CustomModelStateErrors_OldFormatThrows()
[TestMethod]
public void ViewModelValidator_CustomModelStateErrors_OutsideValidationTarget()
{
var testViewModel = new TestViewModel()
{
Context = new TestDotvvmRequestContext
{
Configuration = DotvvmTestHelper.DefaultConfig,
ModelState = new ModelState(),
RequestType = DotvvmRequestType.Command
},
Child = new TestViewModel2()
{
Id = 11,
Code = "Code",
},
Children = new List<TestViewModel2>()
{
new TestViewModel2() { Code = "5" },
new TestViewModel2() { Code = "6" },
new TestViewModel2() { Code = "7" },
}
var testViewModel = new TestViewModel {
Context = new TestDotvvmRequestContext { Configuration = DotvvmTestHelper.DefaultConfig, ModelState = new ModelState(), RequestType = DotvvmRequestType.Command },
Child = new TestViewModel2 { Id = 11, Code = "Code" },
Children = new List<TestViewModel2> { new() { Code = "5" }, new() { Code = "6" }, new() { Code = "7" } }
};
var validator = CreateValidator();
var expander = CreateErrorPathExpander();
Expand All @@ -320,37 +268,16 @@ public void ViewModelValidator_CustomModelStateErrors_OutsideValidationTarget()
modelState.ErrorsInternal.AddRange(errors);
expander.Expand(modelState, testViewModel);

XAssert.Equal(new string [] {
"/",
"/Child",
"/Children/0/Code",
"/Children/0/Id",
"/Children/2"
}, modelState.Errors.Select(e => e.PropertyPath).OrderBy(p => p));
XAssert.Equal(new[] { "/", "/Child", "/Children/0/Code", "/Children/0/Id", "/Children/2" }, modelState.Errors.Select(e => e.PropertyPath).OrderBy(p => p));
}

[TestMethod]
public void ViewModelValidator_CustomModelStateErrors_ArbitraryTargetObjectAndLambda()
{
var testViewModel = new TestViewModel()
{
Context = new TestDotvvmRequestContext
{
Configuration = DotvvmTestHelper.DefaultConfig,
ModelState = new ModelState(),
RequestType = DotvvmRequestType.Command
},
Child = new TestViewModel2()
{
Id = 11,
Code = "Code",
},
Children = new List<TestViewModel2>()
{
new TestViewModel2() { Code = "5" },
new TestViewModel2() { Code = "6" },
new TestViewModel2() { Code = "7" },
}
var testViewModel = new TestViewModel {
Context = new TestDotvvmRequestContext { Configuration = DotvvmTestHelper.DefaultConfig, ModelState = new ModelState(), RequestType = DotvvmRequestType.Command },
Child = new TestViewModel2 { Id = 11, Code = "Code" },
Children = new List<TestViewModel2> { new() { Code = "5" }, new() { Code = "6" }, new() { Code = "7" } }
};
var context = testViewModel.Context;
var validator = CreateValidator();
Expand Down Expand Up @@ -382,25 +309,10 @@ public void ViewModelValidator_CustomModelStateErrors_ArbitraryTargetObjectAndLa
public void ViewModelValidator_ObjectWithAttachedErrorReferencedMultipleTimes()
{
var innerViewModel = new TestViewModel2() { Code = "123" };
var testViewModel = new TestViewModel()
{
Context = new TestDotvvmRequestContext
{
Configuration = DotvvmTestHelper.DefaultConfig,
ModelState = new ModelState(),
RequestType = DotvvmRequestType.Command
},
Child = new TestViewModel2()
{
Id = 11,
Code = "Code",
},
Children = new List<TestViewModel2>()
{
innerViewModel,
new TestViewModel2() { Code = "6" },
innerViewModel,
}
var testViewModel = new TestViewModel {
Context = new TestDotvvmRequestContext { Configuration = DotvvmTestHelper.DefaultConfig, ModelState = new ModelState(), RequestType = DotvvmRequestType.Command },
Child = new TestViewModel2 { Id = 11, Code = "Code" },
Children = new List<TestViewModel2> { innerViewModel, new() { Code = "6" }, innerViewModel }
};
var validator = CreateValidator();
var expander = CreateErrorPathExpander();
Expand All @@ -422,14 +334,8 @@ public void ViewModelValidator_ObjectWithAttachedErrorReferencedMultipleTimes()
[ExpectedException(typeof(ArgumentException))]
public void ViewModelValidator_AttemptToPassOldPaths(string path)
{
var testViewModel = new TestViewModel()
{
Context = new TestDotvvmRequestContext
{
Configuration = DotvvmTestHelper.DefaultConfig,
ModelState = new ModelState(),
RequestType = DotvvmRequestType.Command
},
var testViewModel = new TestViewModel {
Context = new TestDotvvmRequestContext { Configuration = DotvvmTestHelper.DefaultConfig, ModelState = new ModelState(), RequestType = DotvvmRequestType.Command }
};
var validator = CreateValidator();
var expander = CreateErrorPathExpander();
Expand All @@ -446,6 +352,22 @@ public void ViewModelValidator_AttemptToPassOldPaths(string path)
}


[TestMethod]
public void ViewModelValidator_ServiceProvider()
{
var testViewModel = new TestViewModel8();
var validator = CreateValidator();
var expander = CreateErrorPathExpander();
var modelState = new ModelState { ValidationTarget = testViewModel };

var errors = validator.ValidateViewModel(testViewModel).OrderBy(n => n.PropertyPath);
modelState.ErrorsInternal.AddRange(errors);
expander.Expand(modelState, testViewModel);
var results = modelState.Errors.OrderBy(n => n.PropertyPath).ToList();

Assert.AreEqual(0, results.Count);
}

public class TestViewModel : DotvvmViewModelBase
{
[Required]
Expand Down Expand Up @@ -497,7 +419,7 @@ protected override ValidationResult IsValid(object value, ValidationContext vali
var entity = (TestViewModel4Child)validationContext.ObjectInstance;
if (entity.IsChecked && string.IsNullOrEmpty(entity.ConditionalRequired))
{
return new ValidationResult("Value is required when the field is checked!", new[] { validationContext.MemberName });
return new ValidationResult("Value is required when the field is checked!", new[] { validationContext.MemberName });
}

return base.IsValid(value, validationContext);
Expand Down Expand Up @@ -542,6 +464,25 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex
}
}
}
}


public class TestViewModel8
{
[ServiceProviderTest]
public int? Id { get; set; }

public class ServiceProviderTestAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (validationContext.GetService<IViewModelValidator>() == null)
{
return new ValidationResult("Service provider is not available");
}

return ValidationResult.Success;
}
}
}
}
}

0 comments on commit 359725f

Please sign in to comment.