Ejemplo n.º 1
0
        public void BeforeUpdate_Deleting_Relationship()
        {
            // Arrange
            var todoDiscovery   = SetDiscoverableHooks <TodoItem>(_targetHooks, EnableDbValues);
            var personDiscovery = SetDiscoverableHooks <Person>(_targetHooks, EnableDbValues);

            var(_, ufMock, hookExecutor, todoResourceMock, ownerResourceMock) = CreateTestObjects(todoDiscovery, personDiscovery, repoDbContextOptions: _options);

            ufMock.Setup(c => c.Relationships).Returns(ResourceGraph.GetRelationships((TodoItem t) => t.OneToOnePerson).ToHashSet);

            // Act
            var todoList = new List <TodoItem> {
                new TodoItem {
                    Id = _todoList[0].Id
                }
            };

            hookExecutor.BeforeUpdate(todoList, ResourcePipeline.Patch);

            // Assert
            todoResourceMock.Verify(rd => rd.BeforeUpdate(It.Is <IDiffableResourceHashSet <TodoItem> >(diff => TodoCheckDiff(diff, Description)), ResourcePipeline.Patch), Times.Once());
            ownerResourceMock.Verify(rd => rd.BeforeImplicitUpdateRelationship(
                                         It.Is <IRelationshipsDictionary <Person> >(rh => PersonCheck(LastName + LastName, rh)),
                                         ResourcePipeline.Patch),
                                     Times.Once());
            VerifyNoOtherCalls(todoResourceMock, ownerResourceMock);
        }
Ejemplo n.º 2
0
        public void ResourceWithRelationshipsToResourceObject_WithIncludedRelationshipsAttributes_CanBuild()
        {
            // Arrange
            var resource = new MultipleRelationshipsPrincipalPart
            {
                PopulatedToOne = new OneToOneDependent {
                    Id = 10
                },
                PopulatedToManies = new HashSet <OneToManyDependent> {
                    new OneToManyDependent {
                        Id = 20
                    }
                }
            };
            var relationships = ResourceGraph.GetRelationships <MultipleRelationshipsPrincipalPart>(tr => new { tr.PopulatedToManies, tr.PopulatedToOne, tr.EmptyToOne, tr.EmptyToManies });

            // Act
            var resourceObject = _builder.Build(resource, relationships: relationships);

            // Assert
            Assert.Equal(4, resourceObject.Relationships.Count);
            Assert.Null(resourceObject.Relationships["emptyToOne"].Data);
            Assert.Empty((IList)resourceObject.Relationships["emptyToManies"].Data);
            var populatedToOneData = (ResourceIdentifierObject)resourceObject.Relationships["populatedToOne"].Data;

            Assert.NotNull(populatedToOneData);
            Assert.Equal("10", populatedToOneData.Id);
            Assert.Equal("oneToOneDependents", populatedToOneData.Type);
            var populatedToManiesData = (List <ResourceIdentifierObject>)resourceObject.Relationships["populatedToManies"].Data;

            Assert.Single(populatedToManiesData);
            Assert.Equal("20", populatedToManiesData.First().Id);
            Assert.Equal("oneToManyDependents", populatedToManiesData.First().Type);
        }
Ejemplo n.º 3
0
 public ResponseResourceObjectBuilderTests()
 {
     _relationshipsForBuild = ResourceGraph.GetRelationships <OneToManyPrincipal>(relationship => new
     {
         relationship.Dependents
     }).ToList();
 }
Ejemplo n.º 4
0
        public void BeforeUpdate_Deleting_Relationship()
        {
            // Arrange
            IHooksDiscovery <TodoItem> todoDiscovery   = SetDiscoverableHooks <TodoItem>(_targetHooks, EnableDbValues);
            IHooksDiscovery <Person>   personDiscovery = SetDiscoverableHooks <Person>(_targetHooks, EnableDbValues);

            (_, Mock <ITargetedFields> ufMock, IResourceHookExecutor hookExecutor, Mock <IResourceHookContainer <TodoItem> > todoResourceMock,
             Mock <IResourceHookContainer <Person> > ownerResourceMock) = CreateTestObjects(todoDiscovery, personDiscovery, _options);

            ufMock.Setup(targetedFields => targetedFields.Relationships)
            .Returns(ResourceGraph.GetRelationships((TodoItem todoItem) => todoItem.OneToOnePerson).ToHashSet);

            // Act
            var todoList = new List <TodoItem>
            {
                new TodoItem
                {
                    Id = _todoList[0].Id
                }
            };

            hookExecutor.BeforeUpdate(todoList, ResourcePipeline.Patch);

            // Assert
            todoResourceMock.Verify(
                rd => rd.BeforeUpdate(It.Is <IDiffableResourceHashSet <TodoItem> >(diff => TodoCheckDiff(diff, Description)), ResourcePipeline.Patch),
                Times.Once());

            ownerResourceMock.Verify(
                rd => rd.BeforeImplicitUpdateRelationship(It.Is <IRelationshipsDictionary <Person> >(rh => PersonCheck(Name + Name, rh)), ResourcePipeline.Patch),
                Times.Once());

            VerifyNoOtherCalls(todoResourceMock, ownerResourceMock);
        }
Ejemplo n.º 5
0
        public void SerializeSingle_ResourceWithTargetedRelationships_CanBuild()
        {
            // Arrange
            var resourceWithRelationships = new MultipleRelationshipsPrincipalPart
            {
                PopulatedToOne = new OneToOneDependent {
                    Id = 10
                },
                PopulatedToManies = new HashSet <OneToManyDependent> {
                    new OneToManyDependent {
                        Id = 20
                    }
                }
            };

            _serializer.RelationshipsToSerialize = ResourceGraph.GetRelationships <MultipleRelationshipsPrincipalPart>(tr => new { tr.EmptyToOne, tr.EmptyToManies, tr.PopulatedToOne, tr.PopulatedToManies });

            // Act
            string serialized = _serializer.Serialize(resourceWithRelationships);
            // Assert
            const string expectedFormatted = @"{
                ""data"":{
                    ""type"":""multiPrincipals"",
                    ""attributes"":{
                        ""attributeMember"":null
                    },
                    ""relationships"":{
                        ""emptyToOne"":{
                        ""data"":null
                        },
                        ""emptyToManies"":{
                        ""data"":[ ]
                        },
                        ""populatedToOne"":{
                        ""data"":{
                            ""type"":""oneToOneDependents"",
                            ""id"":""10""
                           }
                        },
                        ""populatedToManies"":{
                        ""data"":[
                            {
                                ""type"":""oneToManyDependents"",
                                ""id"":""20""
                            }
                          ]
                        }
                    }
                }
            }";
            var          expected          = Regex.Replace(expectedFormatted, @"\s+", "");

            Assert.Equal(expected, serialized);
        }
Ejemplo n.º 6
0
        public void ResourceWithRelationshipsToResourceObject_DeviatingForeignKeyAndNoNavigationWhileRelationshipIncluded_IgnoresForeignKeyDuringBuild()
        {
            // Arrange
            var resource = new OneToOneDependent {
                Principal = null, PrincipalId = 123
            };
            var relationships = ResourceGraph.GetRelationships <OneToOneDependent>(tr => tr.Principal);

            // Act
            var resourceObject = _builder.Build(resource, relationships: relationships);

            // Assert
            Assert.Null(resourceObject.Relationships["principal"].Data);
        }
Ejemplo n.º 7
0
        public void ResourceWithRequiredRelationshipsToResourceObject_DeviatingForeignKeyWhileRelationshipIncluded_IgnoresForeignKeyDuringBuild()
        {
            // Arrange
            var resource = new OneToOneRequiredDependent {
                Principal = new OneToOnePrincipal {
                    Id = 10
                }, PrincipalId = 123
            };
            var relationships = ResourceGraph.GetRelationships <OneToOneRequiredDependent>(tr => tr.Principal);

            // Act
            var resourceObject = _builder.Build(resource, relationships: relationships);

            // Assert
            Assert.Single(resourceObject.Relationships);
            Assert.NotNull(resourceObject.Relationships["principal"].Data);
            var ro = (ResourceIdentifierObject)resourceObject.Relationships["principal"].Data;

            Assert.Equal("10", ro.Id);
        }
Ejemplo n.º 8
0
        public void SerializeSingle_ResourceWithIncludedRelationships_CanSerialize()
        {
            // Arrange
            var resource = new MultipleRelationshipsPrincipalPart
            {
                Id             = 1,
                PopulatedToOne = new OneToOneDependent {
                    Id = 10
                },
                PopulatedToManies = new HashSet <OneToManyDependent> {
                    new OneToManyDependent {
                        Id = 20
                    }
                }
            };
            var chain      = ResourceGraph.GetRelationships <MultipleRelationshipsPrincipalPart>().Select(r => r.AsEnumerable()).ToList();
            var serializer = GetResponseSerializer <MultipleRelationshipsPrincipalPart>(inclusionChains: chain);

            // Act
            string serialized = serializer.SerializeSingle(resource);

            // Assert
            const string expectedFormatted = @"{
               ""data"":{
                  ""type"":""multiPrincipals"",
                  ""id"":""1"",
                  ""attributes"":{ ""attributeMember"":null },
                  ""relationships"":{
                     ""populatedToOne"":{
                        ""data"":{
                           ""type"":""oneToOneDependents"",
                           ""id"":""10""
                        }
                     },
                     ""emptyToOne"": { ""data"":null },
                     ""populatedToManies"":{
                        ""data"":[
                           {
                              ""type"":""oneToManyDependents"",
                              ""id"":""20""
                           }
                        ]
                     },
                     ""emptyToManies"": { ""data"":[ ] },
                     ""multi"":{ ""data"":null }
                  }
               },
               ""included"":[
                  {
                     ""type"":""oneToOneDependents"",
                     ""id"":""10"",
                     ""attributes"":{ ""attributeMember"":null }
                  },
                  {
                   ""type"":""oneToManyDependents"",
                     ""id"":""20"",
                     ""attributes"":{ ""attributeMember"":null }
                  }
               ]
            }";

            var expected = Regex.Replace(expectedFormatted, @"\s+", "");

            Assert.Equal(expected, serialized);
        }
Ejemplo n.º 9
0
        public void SerializeSingle_ResourceWithDeeplyIncludedRelationships_CanSerialize()
        {
            // Arrange
            var deeplyIncludedResource = new OneToManyPrincipal {
                Id = 30, AttributeMember = "deep"
            };
            var includedResource = new OneToManyDependent {
                Id = 20, Principal = deeplyIncludedResource
            };
            var resource = new MultipleRelationshipsPrincipalPart
            {
                Id = 10,
                PopulatedToManies = new HashSet <OneToManyDependent> {
                    includedResource
                }
            };

            var chains = ResourceGraph.GetRelationships <MultipleRelationshipsPrincipalPart>()
                         .Select(r =>
            {
                var chain = r.AsList();
                if (r.PublicName != "populatedToManies")
                {
                    return(chain);
                }

                chain.AddRange(ResourceGraph.GetRelationships <OneToManyDependent>());
                return(chain);
            }).ToList();

            var serializer = GetResponseSerializer <MultipleRelationshipsPrincipalPart>(inclusionChains: chains);

            // Act
            string serialized = serializer.SerializeSingle(resource);

            // Assert
            const string expectedFormatted = @"{
               ""data"":{ 
                  ""type"":""multiPrincipals"",
                  ""id"":""10"",
                  ""attributes"":{ 
                     ""attributeMember"":null
                  },
                  ""relationships"":{ 
                     ""populatedToOne"":{ 
                        ""data"":null
                     },
                     ""emptyToOne"":{ 
                        ""data"":null
                     },
                     ""populatedToManies"":{ 
                        ""data"":[ 
                           { 
                              ""type"":""oneToManyDependents"",
                              ""id"":""20""
                           }
                        ]
                     },
                     ""emptyToManies"":{ 
                        ""data"":[]
                     },
                     ""multi"":{ 
                        ""data"":null
                     }
                  }
               },
               ""included"":[
                  { 
                     ""type"":""oneToManyDependents"",
                     ""id"":""20"",
                     ""attributes"":{ 
                        ""attributeMember"":null
                     },
                     ""relationships"":{ 
                        ""principal"":{ 
                           ""data"":{ 
                              ""type"":""oneToManyPrincipals"",
                              ""id"":""30""
                           }
                        }
                     }
                  },
                  { 
                     ""type"":""oneToManyPrincipals"",
                     ""id"":""30"",
                     ""attributes"":{ 
                        ""attributeMember"":""deep""
                     }
                  }
               ]
            }";

            var expected = Regex.Replace(expectedFormatted, @"\s+", "");

            Assert.Equal(expected, serialized);
        }