Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to populate the remote graph for remote rules in nested members ... #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 118 additions & 5 deletions src/FubuMVC.Validation.Tests/RemoteRuleGraphActivatorTester.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Bottles;
using Bottles.Diagnostics;
Expand Down Expand Up @@ -46,11 +47,38 @@ public void SetUp()
[Test]
public void fills_the_rules_in_the_rule_graph()
{
var rule = theRuleGraph.RulesFor(ReflectionHelper.GetAccessor<ActivatorTargetWithRemotes>(x => x.Username)).Single();
var rule = remotesFor<ActivatorTargetWithRemotes>(x => x.Username).Single();
rule.Type.ShouldEqual(typeof(UniqueUsernameRule));

theRuleGraph.RulesFor(ReflectionHelper.GetAccessor<ActivatorTargetWithRemotes>(x => x.Name)).ShouldHaveCount(0);
theRuleGraph.RulesFor(ReflectionHelper.GetAccessor<ActivatorTargetNoRemotes>(x => x.Name)).ShouldHaveCount(0);
remotesFor<ActivatorTargetWithRemotes>(x => x.Name).ShouldHaveCount(0);
remotesFor<ActivatorTargetNoRemotes>(x => x.Name).ShouldHaveCount(0);
}

[Test]
public void register_remote_rules_for_nested_properties()
{
remotesFor<ActivatorTargetWithNestedRemotes>(x => x.ModelNestedProperty.NestedDateProperty)
.ShouldHaveCount(1).All(x => x.Rule is RemoteDateRule).ShouldBeTrue();
remotesFor<ActivatorTargetWithNestedRemotes>(x => x.ModelNestedProperty.NestedIntProperty)
.ShouldHaveCount(1).All(x => x.Rule is RemoteIntRule).ShouldBeTrue();
remotesFor<ActivatorTargetWithNestedRemotes>(x => x.ModelNestedProperty.NestedStringProperty)
.ShouldHaveCount(1).All(x => x.Rule is RemoteStringRule).ShouldBeTrue();
}

[Test]
public void register_remote_rules_for_collection_closing_types()
{
remotesFor<ActivatorTargetWithNestedRemotes>(x => x.ModelNestedProperty.Items[0].ItemDateProperty)
.ShouldHaveCount(1).All(x => x.Rule is RemoteDateRule).ShouldBeTrue();
remotesFor<ActivatorTargetWithNestedRemotes>(x => x.ModelNestedProperty.Items[1].ItemIntProperty)
.ShouldHaveCount(1).All(x => x.Rule is RemoteIntRule).ShouldBeTrue();
remotesFor<ActivatorTargetWithNestedRemotes>(x => x.ModelNestedProperty.Items[2].ItemStringProperty)
.ShouldHaveCount(1).All(x => x.Rule is RemoteStringRule).ShouldBeTrue();
}

private IEnumerable<RemoteFieldRule> remotesFor<T>(Expression<Func<T, object>> expression)
{
return theRuleGraph.RulesFor(expression.ToAccessor());
}

public class RemoteRuleGraphEndpoint
Expand All @@ -64,6 +92,10 @@ public AjaxContinuation get_no_remotes(ActivatorTargetNoRemotes request)
{
throw new NotImplementedException();
}
public AjaxContinuation get_nested_remotes(ActivatorTargetWithNestedRemotes input)
{
throw new NotImplementedException();
}
}

public class ActivatorTargetWithRemotes
Expand All @@ -80,14 +112,44 @@ public class ActivatorTargetNoRemotes
public string Name { get; set; }
}

public class ActivatorTargetWithNestedRemotes
{
public string ModelStringProperty { get; set; }
public int ModelIntProperty { get; set; }
public DateTime ModelDateProperty { get; set; }
public Nested ModelNestedProperty { get; set; }
}

public class Nested
{
public List<Item> Items { get; set; }
[RemoteStringRule]
[MaximumStringLength(10)]
public string NestedStringProperty { get; set; }
[RemoteIntRule]
public int NestedIntProperty { get; set; }
[RemoteDateRule]
public DateTime NestedDateProperty { get; set; }
public Nested NestedNestedProperty { get; set; }
}

public class Item
{
[RemoteStringRule]
public string ItemStringProperty { get; set; }
[RemoteIntRule]
public int ItemIntProperty { get; set; }
[RemoteDateRule]
public DateTime ItemDateProperty { get; set; }
}

}

public class UniqueUsernameRule : IRemoteFieldValidationRule
{
public StringToken Token { get; set; }
public StringToken Token { get; set; }

public void Validate(Accessor accessor, ValidationContext context)
public void Validate(Accessor accessor, ValidationContext context)
{
throw new System.NotImplementedException();
}
Expand All @@ -100,4 +162,55 @@ public override IEnumerable<IFieldValidationRule> RulesFor(PropertyInfo property
yield return new UniqueUsernameRule();
}
}


public class RemoteStringRuleAttribute : FieldValidationAttribute
{
public override IEnumerable<IFieldValidationRule> RulesFor(PropertyInfo property)
{
yield return new RemoteStringRule();
}
}


public class RemoteStringRule : IRemoteFieldValidationRule
{
public void Validate(Accessor accessor, ValidationContext context)
{
}

public StringToken Token { get; set; }
}

public class RemoteIntRule : IRemoteFieldValidationRule
{
public void Validate(Accessor accessor, ValidationContext context)
{
}

public StringToken Token { get; set; }
}
public class RemoteIntRuleAttribute : FieldValidationAttribute
{
public override IEnumerable<IFieldValidationRule> RulesFor(PropertyInfo property)
{
yield return new RemoteIntRule();
}
}

public class RemoteDateRule : IRemoteFieldValidationRule
{
public void Validate(Accessor accessor, ValidationContext context)
{
}

public StringToken Token { get; set; }
}
public class RemoteDateRuleAttribute : FieldValidationAttribute
{
public override IEnumerable<IFieldValidationRule> RulesFor(PropertyInfo property)
{
yield return new RemoteDateRule();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using FubuCore;
using System;
using System.Collections.Generic;
using FubuCore;
using FubuCore.Reflection;
using FubuMVC.Core.UI.Elements;
using FubuMVC.Core.Urls;
using FubuMVC.Validation.Remote;
Expand All @@ -13,32 +16,42 @@ namespace FubuMVC.Validation.Tests.UI
public class RemoteValidationElementModifierTester
{
private RemoteValidationElementModifier theModifier;
private HtmlTag theTag;
private ElementRequest theRequest;
private HtmlTag theTag, theNestedTag, theItemTag;
private ElementRequest theRequest, theNestedRequest, theItemRequest;
private RemoteRuleGraph theRemoteGraph;
private InMemoryServiceLocator theServices;
private IUrlRegistry theUrls;
private RemoteFieldRule theRemoteRule;
private RemoteFieldRule theRemoteRule, theNestedRemoteRule, theItemRemoteRule;

[SetUp]
public void SetUp()
{
theModifier = new RemoteValidationElementModifier();
theTag = new HtmlTag("input");
theNestedTag = new HtmlTag("input");
theItemTag = new HtmlTag("input");

theRequest = ElementRequest.For<RemoteTarget>(x => x.Username);
theRequest.ReplaceTag(theTag);

theNestedRequest = ElementRequest.For<NestedRemoteTarget>(x => x.NestedProperty.StringProperty);
theNestedRequest.ReplaceTag(theNestedTag);

theItemRequest = ElementRequest.For<NestedRemoteTarget>(x => x.NestedProperty.Items[3].IntProperty);
theItemRequest.ReplaceTag(theItemTag);

theRemoteGraph = new RemoteRuleGraph();
theRemoteRule = theRemoteGraph.RegisterRule(theRequest.Accessor, new UniqueUsernameRule());
theNestedRemoteRule = theRemoteGraph.RegisterRule(ReflectionHelper.GetAccessor<Nested>(p => p.StringProperty), new RemoteStringRule());
theItemRemoteRule = theRemoteGraph.RegisterRule(ReflectionHelper.GetAccessor<Item>(p => p.IntProperty), new RemoteIntRule());

theUrls = new StubUrlRegistry();

theServices = new InMemoryServiceLocator();
theServices.Add(theRemoteGraph);
theServices.Add(theUrls);

theRequest.Attach(theServices);
new[] {theRequest, theNestedRequest, theItemRequest}.Each(x => x.Attach(theServices));
}

[Test]
Expand All @@ -50,11 +63,20 @@ public void always_matches()
[Test]
public void registers_the_validation_def()
{
theModifier.Modify(theRequest);
new[]
{
Tuple.Create(theRequest, theRemoteRule),
Tuple.Create(theNestedRequest, theNestedRemoteRule),
Tuple.Create(theItemRequest, theItemRemoteRule)
}
.Each(x =>
{
theModifier.Modify(x.Item1);
var def = x.Item1.CurrentTag.Data("remote-rule").As<RemoteValidationDef>();
def.url.ShouldEqual(theUrls.RemoteRule());
def.rules.ShouldHaveTheSameElementsAs(x.Item2.ToHash());
});

var def = theRequest.CurrentTag.Data("remote-rule").As<RemoteValidationDef>();
def.url.ShouldEqual(theUrls.RemoteRule());
def.rules.ShouldHaveTheSameElementsAs(theRemoteRule.ToHash());
}

[Test]
Expand All @@ -63,14 +85,39 @@ public void no_registration_when_no_rules_are_found()
theRemoteGraph = new RemoteRuleGraph();
theServices.Add(theRemoteGraph);

theModifier.Modify(theRequest);
new[]
{
theRequest,
theNestedRequest,
theItemRequest
}
.Each(x =>
{
theModifier.Modify(x);
x.CurrentTag.Data("remote-rule").ShouldBeNull();
});

theRequest.CurrentTag.Data("remote-rule").ShouldBeNull();
}

public class RemoteTarget
{
public string Username { get; set; }
}

public class NestedRemoteTarget
{
public Nested NestedProperty { get; set; }
}

public class Nested
{
public List<Item> Items { get; set; }
public string StringProperty { get; set; }
}

public class Item
{
public int IntProperty { get; set; }
}
}
}
4 changes: 2 additions & 2 deletions src/FubuMVC.Validation/Remote/RemoteRuleGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public RemoteRuleGraph()
public RemoteFieldRule RegisterRule(Accessor accessor, IFieldValidationRule rule)
{
var remote = RemoteFieldRule.For(accessor, rule);
_rules[accessor].Fill(remote);
_rules[accessor is SingleProperty ? accessor : new SingleProperty(accessor.InnerProperty, accessor.OwnerType)].Fill(remote);

return remote;
}

public IEnumerable<RemoteFieldRule> RulesFor(Accessor accessor)
{
return _rules[accessor];
return _rules[accessor is SingleProperty ? accessor : new SingleProperty(accessor.InnerProperty, accessor.OwnerType)];
}

public RemoteFieldRule RuleFor(string hash)
Expand Down
Loading