Ejemplo n.º 1
0
        public void SerializeSingle_NoIdWithTargetedSetAttributes_CanBuild()
        {
            // Arrange
            var resourceNoId = new TestResource {
                Id = 0, StringField = "value", NullableIntField = 123
            };

            _serializer.AttributesToSerialize = ResourceGraph.GetAttributes <TestResource>(tr => tr.StringField);

            // Act
            string serialized = _serializer.Serialize(resourceNoId);

            // Assert
            const string expectedFormatted = @"{
               ""data"":{
                  ""type"":""testResource"",
                  ""attributes"":{
                     ""stringField"":""value""
                  }
               }
            }";

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

            Assert.Equal(expected, serialized);
        }
        public void SerializeSingle_ResourceWithoutTargetedAttributes_CanBuild()
        {
            // Arrange
            var resource = new TestResource
            {
                Id               = 1,
                StringField      = "value",
                NullableIntField = 123
            };

            _serializer.AttributesToSerialize = ResourceGraph.GetAttributes <TestResource>(_ => new
            {
            });

            // Act
            string serialized = _serializer.Serialize(resource);

            // Assert
            const string expectedFormatted = @"{
               ""data"":{
                  ""type"":""testResource"",
                  ""id"":""1""
               }
            }";

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

            Assert.Equal(expected, serialized);
        }
        public void SerializeMany_ResourcesWithTargetedAttributes_CanBuild()
        {
            // Arrange
            var resources = new List <TestResource>
            {
                new TestResource
                {
                    Id               = 1,
                    StringField      = "value1",
                    NullableIntField = 123
                },
                new TestResource
                {
                    Id               = 2,
                    StringField      = "value2",
                    NullableIntField = 123
                }
            };

            _serializer.AttributesToSerialize = ResourceGraph.GetAttributes <TestResource>(tr => tr.StringField);

            // Act
            string serialized = _serializer.Serialize(resources);

            // Assert
            const string expectedFormatted = @"{
                ""data"":[
                    {
                        ""type"":""testResource"",
                        ""id"":""1"",
                        ""attributes"":{
                        ""stringField"":""value1""
                        }
                    },
                    {
                        ""type"":""testResource"",
                        ""id"":""2"",
                        ""attributes"":{
                        ""stringField"":""value2""
                        }
                    }
                ]
            }";

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

            Assert.Equal(expected, serialized);
        }
Ejemplo n.º 4
0
        public void SerializeMany_EmptyList_CanBuild()
        {
            // Arrange
            _serializer.AttributesToSerialize = ResourceGraph.GetAttributes <TestResource>(tr => tr.StringField);

            // Act
            string serialized = _serializer.Serialize(new List <TestResource>());

            // Assert
            const string expectedFormatted = @"{
                ""data"":[]
            }";
            var          expected          = Regex.Replace(expectedFormatted, @"\s+", "");

            Assert.Equal(expected, serialized);
        }
Ejemplo n.º 5
0
        public void SerializeSingle_Null_CanBuild()
        {
            // Arrange
            _serializer.AttributesToSerialize = ResourceGraph.GetAttributes <TestResource>(tr => tr.StringField);

            // Act
            string serialized = _serializer.Serialize((IIdentifiable)null);

            // Assert
            const string expectedFormatted = @"{
                ""data"":null
            }";
            var          expected          = Regex.Replace(expectedFormatted, @"\s+", "");

            Assert.Equal(expected, serialized);
        }
Ejemplo n.º 6
0
        public void ResourceToResourceObject_ResourceWithIncludedAttrs_CanBuild(string stringFieldValue, int?intFieldValue)
        {
            // Arrange
            var resource = new TestResource {
                StringField = stringFieldValue, NullableIntField = intFieldValue
            };
            var attrs = ResourceGraph.GetAttributes <TestResource>(tr => new { tr.StringField, tr.NullableIntField });

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

            // Assert
            Assert.NotNull(resourceObject.Attributes);
            Assert.Equal(2, resourceObject.Attributes.Keys.Count);
            Assert.Equal(stringFieldValue, resourceObject.Attributes["stringField"]);
            Assert.Equal(intFieldValue, resourceObject.Attributes["nullableIntField"]);
        }