protected ICypherFluentQuery AddRoles(ICypherFluentQuery query, IList <IdentityRole> roles, string userId)
        {
            if (roles == null || roles.Count == 0)
            {
                return(query);
            }

            for (var i = 0; i < roles.Count; i++)
            {
                var roleVar     = $"r{i}";
                var userRoleVar = $"ur{i}";
                query = query
                        .With("u")
                        .Match(p => p.Pattern <IdentityRole>(roleVar)
                               .Constrain(r => r.NormalizedName == roles[i].NormalizedName))
                        .Merge(p => p.Pattern <TUser, IdentityUser_Role, IdentityRole>("u", userRoleVar, roleVar)
                               .Constrain(null,
                                          ur => ur.RoleId == CypherVariables.Get <IdentityRole>(roleVar).Id&& ur.UserId == userId,
                                          null))
                        .OnCreate()
                        .Set <IdentityUser_Role>(ur => ur.CreatedOn == new Occurrence(), userRoleVar);
            }

            return(query);
        }
Example #2
0
        public void Constraints_FinalProperties(string testContextName, TestContext testContext)
        {
            var client = testContext.AnnotationsContext.GraphClient;
            var query  = testContext.Query;

            var builder = new PathBuilder(query);

            var pattern = builder
                          .Pattern <ActorNode>("Ambode")
                          .Constrain(actor =>
                                     actor.Name == "Ellen Pompeo" &&
                                     actor.Born == CypherVariables.Get <ActorNode>("shondaRhimes").Born &&
                                     actor.Roles == new[] { "Meredith Grey" })
                          .Pattern as Pattern;

            var aConstraints = pattern.AConstraints;

            Assert.NotNull(aConstraints);

            var aFinalProperties = pattern.AFinalProperties;

            Assert.NotNull(aFinalProperties);

            var expected = new Dictionary <string, dynamic>
            {
                { "Name", "\"Ellen Pompeo\"" },
                { "Born", "shondaRhimes.Born" },
                { "Roles", $"[{Environment.NewLine}  \"Meredith Grey\"{Environment.NewLine}]" }
            };

            TestUtilities.TestFinalPropertiesForEquality(instance => client.Serializer.Serialize(instance), expected,
                                                         aFinalProperties);

            var str = pattern.Build(ref query);
        }
 public static IPath BuildTestPath(IPathBuilder P)
 {
     return(P
            .Pattern <MovieNode, MovieActorRelationship, ActorNode>("greysAnatomy", "ellenPompeo")
            .Label(new[] { "Series" }, new[] { "STARRED_IN" }, new[] { "Female" }, true, false, false)
            .Constrain(movie => movie.Title == "Grey's Anatomy" && movie.Year == 2017, null, actor =>
                       actor.Name == "Ellen Pompeo" &&
                       actor.Born == CypherVariables.Get <ActorNode>("shondaRhimes").Born &&
                       actor.Roles == new[] { "Meredith Grey" })
            .Hop(1)  //not necessary, but for tests
            );
 }
        public void PredicateComplexTypeSet(string testContextName, TestContext testContext)
        {
            Expression <Func <ActorNode, bool> > expression = a => a.Address == new AddressWithComplexType
            {
                //Use this style only if you're sure all the properties here are assigned,
                //because this address object would replace the instance address property entirely.
                //Also note that there's a good chance the parameters set inline here wouldn't make it to the generated pattern.
                //This was done mainly for testing.
                //Use a => a.Address.Location.Longitude == (double)CypherVariables.Get("ellenPompeo")["NewAddressName_Location_Longitude"] instead.

                AddressLine = CypherVariables.Get <ActorNode>("shondaRhimes").Address.AddressLine,
                Location    = new Location
                {
                    Latitude  = 4.0,
                    Longitude = (double)CypherVariables.Get("ellenPompeo")["NewAddressName_Location_Longitude"]
                }
            } && a.Name == "Shonda Rhimes";

            var entityVisitor =
                new EntityExpressionVisitor(testContext.QueryContext, expression.Parameters.FirstOrDefault());
            var newExpression = entityVisitor.Visit(expression.Body);

            Assert.NotNull(newExpression);
            Assert.NotEqual(newExpression, expression.Body);

            Assert.Equal(2, entityVisitor.PredicateAssignments.Count);
            Assert.Equal(6, entityVisitor.PendingAssignments.Count);

            dynamic result = newExpression.ExecuteExpression <Dictionary <string, object> >();

            Assert.NotNull(result);

            var tokensExpected = new Dictionary <string, object>
            {
                { "Name", "Shonda Rhimes" },
                { "NewAddressName_AddressLine", null },
                { "NewAddressName_City", null },
                { "NewAddressName_State", null },
                { "NewAddressName_Country", null },
                { "NewAddressName_Location_Latitude", 0.0 },
                { "NewAddressName_Location_Longitude", 0.0 }
            };

            Assert.Equal(tokensExpected.Count, result.Count);

            foreach (var pair in result)
            {
                Assert.Equal(tokensExpected[pair.Key], pair.Value);
            }
        }
Example #5
0
        public void SetPredicate(string testContextName, TestContext testContext)
        {
            var query = testContext.Query;

            var actual = query.Set((ActorNode actor) =>
                                   actor.Name == "Ellen Pompeo" &&
                                   actor.Born == CypherVariables.Get <ActorNode>("shondaRhimes").Born &&
                                   actor.Roles == new[] { "Meredith Grey" }, out var setParam).Query.QueryText;

            var expected =
                $"SET actor.Name = ${setParam}.Name, actor.Born = shondaRhimes.Born, actor.Roles = ${setParam}.Roles";

            Assert.Equal(expected, actual);
        }
        public void ComplexAnonymousType(string testContextName, TestContext testContext)
        {
            //the following is purely a test, and not necessarily a good example for neo4j cypher.
            Expression <Func <object> > expression = () => new
            {
                new AddressWithComplexType
                {
                    AddressLine = CypherVariables.Get("A")["AddressLine"] as string,
                    Location    = new Location
                    {
                        Latitude  = (double)CypherVariables.Get("A")["Location_Latitude"],
                        Longitude = 56.90
                    }
                }.Location
            };

            var entityVisitor = new EntityExpressionVisitor(testContext.QueryContext);
            var newExpression = entityVisitor.Visit(expression.Body);

            Assert.NotNull(newExpression);
            Assert.NotEqual(newExpression, expression.Body);

            Assert.Equal(2, entityVisitor.PendingAssignments.Count);

            Assert.Equal("Location_Latitude", entityVisitor.PendingAssignments.First().Key.ComplexJsonName);
            Assert.Contains("Get(\"A\").get_Item(\"Location_Latitude\")",
                            entityVisitor.PendingAssignments.First().Value.ToString());

            Assert.Equal("Location_Longitude", entityVisitor.PendingAssignments.Last().Key.ComplexJsonName);
            Assert.Contains("Get(\"A\").get_Item(\"AddressLine\")",
                            entityVisitor.PendingAssignments.Last().Value.ToString());

            var result = newExpression.ExecuteExpression <Dictionary <string, object> >();

            Assert.NotNull(result);

            var tokensExpected = new Dictionary <string, object>
            {
                { "Location_Latitude", 0.0 }, //because the assignment is pending due to the CypherVariables Get method.
                { "Location_Longitude", 0.0 } //because the assignment is pending due to the CypherVariables Get method.
            };

            Assert.Equal(tokensExpected.Count, result.Count);

            foreach (var pair in result)
            {
                Assert.Equal(tokensExpected[pair.Key], pair.Value);
            }
        }
        public void PredicateComplexTypeMemberAccessSet(string testContextName, TestContext testContext)
        {
            Expression <Func <ActorNode, bool> > expression = a =>
                                                              (a.Address as AddressWithComplexType).Location.Longitude == new AddressWithComplexType
            {
                //Using this style, parameters set inline of a member access may or may not make it to the generated pattern, or even throw an exception.
                //This is because this MemberInit may be taken as an object value, since it was accessed, and then used directly.
                //This was done mainly for testing.
                //Use a => a.Address.Location.Longitude == (double)CypherVariables.Get("ellenPompeo")["NewAddressName_Location_Longitude"] instead.

                AddressLine = CypherVariables.Get <ActorNode>("shondaRhimes").Address.AddressLine,
                Location    = new Location
                {
                    Latitude  = 4.0,
                    Longitude = (double)CypherVariables.Get("ellenPompeo")["NewAddressName_Location_Longitude"]
                }
            }.Location.Longitude&& a.Name == "Shonda Rhimes";

            var entityVisitor =
                new EntityExpressionVisitor(testContext.QueryContext, expression.Parameters.FirstOrDefault());
            var newExpression = entityVisitor.Visit(expression.Body);

            Assert.NotNull(newExpression);
            Assert.NotEqual(newExpression, expression.Body);

            Assert.Equal(2, entityVisitor.PredicateAssignments.Count);
            Assert.Single(entityVisitor.PendingAssignments);

            Assert.Equal("NewAddressName_Location_Longitude",
                         entityVisitor.PendingAssignments.First().Key.ComplexJsonName);

            dynamic result = newExpression.ExecuteExpression <Dictionary <string, object> >();

            Assert.NotNull(result);

            var tokensExpected = new Dictionary <string, object>
            {
                { "Name", "Shonda Rhimes" },
                { "NewAddressName_Location_Longitude", 0.0 }
            };

            Assert.Equal(tokensExpected.Count, result.Count);

            foreach (var pair in result)
            {
                Assert.Equal(tokensExpected[pair.Key], pair.Value);
            }
        }
        public void EscapedComplexAnonymousType(string testContextName, TestContext testContext)
        {
            //the following is purely a test, and not necessarily a good example for neo4j cypher.
            Expression <Func <object> > expression = () => new
            {
                Address     = CypherFunctions._(TestUtilities.Actor.Address as AddressWithComplexType),
                Coordinates = new[]
                {
                    (TestUtilities.Actor.Address as AddressWithComplexType).Location.Latitude,
                    (double)CypherVariables.Get("shondaRhimes")["NewAddressName_Location_Longitude"]
                }
            };

            var entityVisitor = new EntityExpressionVisitor(testContext.QueryContext);
            var newExpression = entityVisitor.Visit(expression.Body);

            Assert.NotNull(newExpression);
            Assert.NotEqual(newExpression, expression.Body);

            Assert.Single(entityVisitor.PendingAssignments);

            Assert.Equal("Coordinates", entityVisitor.PendingAssignments.First().Key.ComplexJsonName);
            Assert.Contains("Get(\"shondaRhimes\").get_Item(\"NewAddressName_Location_Longitude\")",
                            entityVisitor.PendingAssignments.First().Value.ToString());

            var result = newExpression.ExecuteExpression <Dictionary <string, object> >();

            Assert.NotNull(result);

            var tokensExpected = new Dictionary <string, object>
            {
                //same name (Address) returned because it was escaped (instead of: NewAddressName)
                { "Address_AddressLine", null },
                { "Address_City", "Los Angeles" },
                { "Address_State", "California" },
                { "Address_Country", "US" },
                { "Address_Location_Latitude", 34.0522 },
                { "Address_Location_Longitude", -118.2437 },
                { "Coordinates", null } //assignment pending due to CypherVariables Get call
            };

            Assert.Equal(tokensExpected.Count, result.Count);

            foreach (var pair in result)
            {
                Assert.Equal(tokensExpected[pair.Key], pair.Value);
            }
        }
 public static IPath BuildTestPathMixed(IPathBuilder P)
 {
     return(P
            .Pattern <MovieNode, MovieActorRelationship, ActorNode>("greysAnatomy", "ellenPompeo")
            .Label(new[] { "Series" }, new[] { "STARRED_IN" }, new[] { "Female" }, true, false, false)
            .Prop(() => new MovieNode //could have used constrain, but would be good to test both methods
     {
         Title = "Grey's Anatomy",
         Year = 2017
     })
            .Hop(1) //not necessary, but for tests
            .Constrain(null, null, actor =>
                       actor.Name == "Ellen Pompeo" &&
                       actor.Born == CypherVariables.Get <ActorNode>("shondaRhimes").Born &&
                       actor.Roles == new[] { "Meredith Grey" }));
 }
Example #10
0
        public void SetPredicateWithParamsWithComplexVarsGetAssigment(string testContextName, TestContext testContext)
        {
            var query = testContext.Query;

            var actual = query.UsingBuildStrategy(PropertiesBuildStrategy.WithParams)
                         .Set((ActorNode actor) =>
                              actor.Name == "Ellen Pompeo" &&
                              actor.Born == CypherVariables.Get <ActorNode>("actor").Born + 1 && //((int?)null ?? 1969) + 1
                              actor.Roles == new[] { "Meredith Grey" }, out var setParam).Query.QueryText;

            //When using Set predicate, WithParams strategy is the same as WithParamsForValues
            var expected =
                $"SET actor.Name = ${setParam}.Name, actor.Born = actor.Born + $p1, actor.Roles = ${setParam}.Roles";

            Assert.Equal(expected, actual);
        }
        public void AnonymousType(string testContextName, TestContext testContext)
        {
            Expression <Func <object> > expression = () =>
                                                     new
            {
                Name = "Ellen Pompeo",
                CypherVariables.Get <ActorNode>("shondaRhimes").Born,
                Roles = new[] { "Meredith Grey" },
                Age   = 47.ToString()
            };

            var entityVisitor = new EntityExpressionVisitor(testContext.QueryContext);
            var newExpression = entityVisitor.Visit(expression.Body);

            Assert.NotNull(newExpression);
            Assert.NotEqual(newExpression, expression.Body);

            Assert.Single(entityVisitor
                          .PendingAssignments); //because we are accounting for an implicit convert to object type.

            Assert.Equal("Born", entityVisitor.PendingAssignments.First().Key.ComplexJsonName);
            Assert.Equal("Get(\"shondaRhimes\").Born", entityVisitor.PendingAssignments.First().Value.ToString());

            var result = newExpression.ExecuteExpression <Dictionary <string, object> >();

            Assert.NotNull(result);

            var tokensExpected = new Dictionary <string, object>
            {
                { "Name", "Ellen Pompeo" },
                { "Born", 0 }, //to be later replaced at serialization with params
                { "Roles", new[] { "Meredith Grey" } },
                { "Age", "47" }
            };

            Assert.Equal(tokensExpected.Count, result.Count);

            foreach (var pair in result)
            {
                Assert.Equal(tokensExpected[pair.Key], pair.Value);
            }
        }
        public void Predicate(string testContextName, TestContext testContext)
        {
            Expression <Func <ActorNode, bool> > expression = a =>
                                                              a.Born == CypherVariables.Get <ActorNode>("ellenPompeo").Born &&
                                                              a.Name == "Shonda Rhimes" &&
                                                              a.Address.AddressLine == CypherVariables.Get <ActorNode>("shondaRhimes").Address.AddressLine;

            var entityVisitor =
                new EntityExpressionVisitor(testContext.QueryContext, expression.Parameters.FirstOrDefault());
            var newExpression = entityVisitor.Visit(expression.Body);

            Assert.NotNull(newExpression);
            Assert.NotEqual(newExpression, expression.Body);

            Assert.Equal(3, entityVisitor.PredicateAssignments.Count);

            Assert.Equal(2, entityVisitor.PendingAssignments.Count);

            Assert.Equal("Born", entityVisitor.PendingAssignments.First().Key.ComplexJsonName);
            Assert.Equal("Get(\"ellenPompeo\").Born._()",
                         entityVisitor.PendingAssignments.First().Value
                         .ToString()); //the npf escape will be added for concrete classes

            var result = newExpression.ExecuteExpression <Dictionary <string, object> >();

            Assert.NotNull(result);

            var tokensExpected = new Dictionary <string, object>
            {
                { "Name", "Shonda Rhimes" },
                { "Born", 0 }, //assignment pending due to CypherVariables Get call
                { "NewAddressName_AddressLine", null }
            };

            Assert.Equal(tokensExpected.Count, result.Count);

            foreach (var pair in result)
            {
                Assert.Equal(tokensExpected[pair.Key], pair.Value);
            }
        }
Example #13
0
        public void Properties_FinalProperties(string testContextName, TestContext testContext)
        {
            var client = testContext.AnnotationsContext.GraphClient;
            var query  = testContext.Query;

            var builder = new PathBuilder(query);

            var pattern = builder
                          .Pattern <ActorNode>("Ambode")
                          .Prop(() => new
            {
                Name = "Ellen Pompeo",
                CypherVariables.Get <ActorNode>("shondaRhimes").Born,
                Roles = new[] { "Meredith Grey" },
                Age   = 47.ToString()
            })
                          .Pattern as Pattern;

            var aProperties = pattern.AProperties;

            Assert.NotNull(aProperties);

            var aFinalProperties = pattern.AFinalProperties;

            Assert.NotNull(aFinalProperties);

            var expected = new Dictionary <string, dynamic>
            {
                { "Name", "\"Ellen Pompeo\"" },
                { "Born", "shondaRhimes.Born" },
                { "Roles", "[" + Environment.NewLine + "  \"Meredith Grey\"" + Environment.NewLine + "]" },
                { "Age", "\"47\"" } //because we assigned a 47 string and not a 47 int.
            };

            TestUtilities.TestFinalPropertiesForEquality(instance => client.Serializer.Serialize(instance), expected,
                                                         aFinalProperties);
        }
        public void PredicateDictionarySet(string testContextName, TestContext testContext)
        {
            Expression <Func <Dictionary <string, object>, bool> > expression = a =>
                                                                                a["Address"] == CypherVariables.Get <ActorNode>("ellenPompeo").Address &&
                                                                                (int)a["Born"] == 1671 &&
                                                                                a["Name"] == "Shonda Rhimes";

            var entityVisitor =
                new EntityExpressionVisitor(testContext.QueryContext, expression.Parameters.FirstOrDefault());
            var newExpression = entityVisitor.Visit(expression.Body);

            Assert.NotNull(newExpression);
            Assert.NotEqual(newExpression, expression.Body);

            Assert.Equal(3, entityVisitor.PredicateAssignments.Count);
            Assert.Equal(4,
                         entityVisitor.PendingAssignments.Count); //the address complex type should have exploded to make 4

            Assert.Equal("NewAddressName_AddressLine", entityVisitor.PendingAssignments.First().Key.ComplexJsonName);
            Assert.Contains("Get(\"ellenPompeo\").Address.AddressLine",
                            entityVisitor.PendingAssignments.First().Value.ToString());

            dynamic result = newExpression.ExecuteExpression <Dictionary <string, object> >();

            Assert.NotNull(result);

            var tokensExpected = new Dictionary <string, object>
            {
                { "Name", "Shonda Rhimes" },
                { "Born", 1671 },
                { "NewAddressName_AddressLine", null },
                { "NewAddressName_City", null },
                { "NewAddressName_State", null },
                { "NewAddressName_Country", null }
            };

            Assert.Equal(tokensExpected.Count, result.Count);

            foreach (var pair in result)
            {
                Assert.Equal(tokensExpected[pair.Key], pair.Value);
            }
        }