예제 #1
0
        public void Should_serialize_dynamic_object_for_entity_set()
        {
            var entity = new DynamicObjectMapper().MapCollection(new List <ProductEntity>
            {
                new ProductEntity
                {
                    Id    = 1,
                    Name  = "Product E. Name",
                    Price = 1234.56m,
                },
                new ProductEntity
                {
                    Id    = 2,
                    Name  = "Noname",
                    Price = 78.9m,
                },
            });

            var copy = entity.Serialize();

            var item0 = copy.ElementAt(0);

            item0["Id"].ShouldBe(1);
            item0["Name"].ShouldBe("Product E. Name");
            item0["Price"].ShouldBe(1234.56m);

            var item1 = copy.ElementAt(1);

            item1["Id"].ShouldBe(2);
            item1["Name"].ShouldBe("Noname");
            item1["Price"].ShouldBe(78.9m);
        }
예제 #2
0
        public void Should_serialize_dynamic_object(Type type, object value)
        {
            SkipUnsupportedDataType(type, value);

            var dynamicObject = new DynamicObjectMapper().MapObject(value);

            var config = CreateModelFor(type);
            var copy   = dynamicObject.Serialize(config);

            copy?.Get().ShouldBe(dynamicObject?.Get(), $"type: {type} value: {value}");

            var c = new DynamicObjectMapper().Map(copy);

            c.ShouldBe(value);
        }
예제 #3
0
        public void Should_serialize_dynamic_object_for_entity()
        {
            var entity = new DynamicObjectMapper().MapObject(new ProductEntity
            {
                Id    = 1,
                Name  = "Product E. Name",
                Price = 1234.56m,
            });

            var copy = entity.Serialize();

            copy["Id"].ShouldBe(1);
            copy["Name"].ShouldBe("Product E. Name");
            copy["Price"].ShouldBe(1234.56m);
        }
예제 #4
0
        public void Should_serialize_dynamic_object(Type type, object value, CultureInfo culture)
        {
            SkipUnsupportedDataType(type, value);

            using var cultureContext = culture.CreateContext();

            var dynamicObject = new DynamicObjectMapper().MapObject(value);
            var copy          = dynamicObject.Serialize();

            copy?.Get().ShouldBe(dynamicObject?.Get(), $"type: {type} value: {value}");

            var c = new DynamicObjectMapper().Map(copy);

            c.ShouldBe(value);
        }
예제 #5
0
        public void Should_serialize_dynamic_object_with_cirtcular_reference()
        {
            var a = new A();
            var b = new A {
                Reference = a
            };
            var c = new A {
                Reference = b
            };

            a.Reference = c;

            var entity = new DynamicObjectMapper().MapObject(a);

            var copy = entity.Serialize();

            copy.ShouldBeOfType <DynamicObject>()[
                nameof(a.Reference)].ShouldBeOfType <DynamicObject>()[
                nameof(b.Reference)].ShouldBeOfType <DynamicObject>()[
                nameof(c.Reference)].ShouldBeSameAs(copy);
        }
예제 #6
0
        public void Should_serialize_nested_dynamic_object_for_entity_collection()
        {
            var entity = new DynamicObjectMapper().MapObject(new OrderEntity
            {
                Id    = 1,
                Items = new List <OrderItemEntity>
                {
                    new OrderItemEntity
                    {
                        ProductId = 11,
                        Quantity  = 2,
                        Product   = new ProductEntity
                        {
                            Id    = 11,
                            Name  = "Product E. Name",
                            Price = 1234.56m,
                        },
                    },
                    new OrderItemEntity
                    {
                        ProductId = 22,
                        Quantity  = 4,
                        Product   = new ProductEntity
                        {
                            Id    = 22,
                            Name  = "Noname",
                            Price = 78.9m,
                        },
                    },
                },
            });

            var copy = entity.Serialize();

            copy["Id"].ShouldBe(1);
            var items = copy["Items"].ShouldBeAssignableTo <ICollection <DynamicObject> >();

            items.Count.ShouldBe(2);
        }
예제 #7
0
        public void Should_serialize_dynamic_object_collection(Type type, IEnumerable value)
        {
            SkipUnsupportedDataType(type, value);

            var dynamicObjects = new DynamicObjectMapper().MapCollection(value);

            var config = CreateModelFor(type);
            var copy   = dynamicObjects.Serialize(config);

            var dynamicObjectsCount = dynamicObjects?.Count() ?? 0;
            var copyCount           = copy?.Count() ?? 0;

            copyCount.ShouldBe(dynamicObjectsCount);

            for (int i = 0; i < copyCount; i++)
            {
                var originalValue = dynamicObjects.ElementAt(i)?.Get();
                var copyValue     = copy.ElementAt(i)?.Get();
                copyValue.ShouldBe(originalValue);
            }

            var c = new DynamicObjectMapper().Map(copy);

            if (value is null)
            {
                c.ShouldBeNull();
            }
            else
            {
                var array     = value.Cast <object>().ToArray();
                var copyArray = c.Cast <object>().ToArray();

                for (int i = 0; i < copyArray.Length; i++)
                {
                    copyArray[i].ShouldBe(array[i]);
                }
            }
        }
예제 #8
0
        public void Should_serialize_nested_dynamic_object_for_entity_relation()
        {
            var entity = new DynamicObjectMapper().MapObject(new OrderItemEntity
            {
                ProductId = 1,
                Quantity  = 2,
                Product   = new ProductEntity
                {
                    Id    = 1,
                    Name  = "Product E. Name",
                    Price = 1234.56m,
                },
            });

            var copy = entity.Serialize();

            copy["ProductId"].ShouldBe(1);
            copy["Quantity"].ShouldBe(2u);
            var product = copy["Product"].ShouldBeAssignableTo <DynamicObject>();

            product["Id"].ShouldBe(1);
            product["Name"].ShouldBe("Product E. Name");
            product["Price"].ShouldBe(1234.56m);
        }