Exemplo n.º 1
0
        public void SerializationSpeedTest()
        {
            for (var i = 0; i < 5; i++)
            {
                var stopWatch = new Stopwatch();
                stopWatch.Start();

                for (var j = 0; j < 10000; j++)
                {
                    var obj1 = new GeneralDTO
                    {
                        Title    = null,
                        ABoolean = true,
                        AGuid    = Guid.NewGuid(),
                        AnInt    = 1,
                        Pi       = 3.14,
                        Nester   = new GeneralDTO {
                            Title = "Bob", AnInt = 42
                        }
                    };
                    var obj1Bytes = BsonSerializer.Serialize(obj1);
                    BsonDeserializer.Deserialize <GeneralDTO>(obj1Bytes);
                }
                stopWatch.Stop();
                Console.WriteLine(stopWatch.ElapsedMilliseconds);
            }
        }
Exemplo n.º 2
0
        public void SerializationOfBytesIsNotLossy()
        {
            var bin = new List <byte>();

            for (int i = 1; i < 1000; i++)
            {
                bin.AddRange(BitConverter.GetBytes(i));
            }

            var obj1 = new GeneralDTO {
                Bytes = bin.ToArray()
            };
            var obj2 = new GeneralDTO {
                Bytes = null
            };

            var obj1Bytes = BsonSerializer.Serialize(obj1);
            var obj2Bytes = BsonSerializer.Serialize(obj2);

            var hydratedObj1 = BsonDeserializer.Deserialize <GeneralDTO>(obj1Bytes);
            var hydratedObj2 = BsonDeserializer.Deserialize <GeneralDTO>(obj2Bytes);

            Assert.True(obj1.Bytes.SequenceEqual(hydratedObj1.Bytes));
            Assert.Equal(null, hydratedObj2.Bytes);
        }
Exemplo n.º 3
0
        public void SerializesAndDeserializesAFloat()
        {
            var o = new GeneralDTO {
                AFloat = 1.4f
            };

            Assert.Equal(1.4f, BsonDeserializer.Deserialize <GeneralDTO>(BsonSerializer.Serialize(o)).AFloat);
        }
Exemplo n.º 4
0
        public void SerializingPocoGeneratesBytes()
        {
            var dummy = new GeneralDTO {
                Title = "Testing"
            };

            Assert.NotEmpty(BsonSerializer.Serialize(dummy));
        }
Exemplo n.º 5
0
        public void DoesntSerializeIgnoredProperties()
        {
            var o = new GeneralDTO {
                IgnoredProperty = 4
            };

            Assert.Equal(0, BsonDeserializer.Deserialize <GeneralDTO>(BsonSerializer.Serialize(o)).IgnoredProperty);
        }
Exemplo n.º 6
0
        public void SerializesAndDeserializesAComplexObject()
        {
            var obj1 = new GeneralDTO
            {
                Flags64   = Flags64.FlagOff,
                Flags32   = Flags32.FlagOn,
                Pi        = 2d,
                AnInt     = 3,
                Title     = "telti",
                ABoolean  = false,
                Strings   = new[] { "a", "bb", "abc" },
                Bytes     = new byte[] { 1, 2, 3 },
                AGuid     = Guid.NewGuid(),
                ADateTime = new DateTime(2001, 4, 8, 10, 43, 23, 104),
                ARex      = new Regex("it's over (9000)", RegexOptions.IgnoreCase)
            };
            var nested = new GeneralDTO {
                Pi = 43d, Title = "little", ARex = new Regex("^over (9000)$")
            };

            obj1.Nester = nested;
            var obj2 = new GeneralDTO();

            var hydratedObj1 = BsonDeserializer.Deserialize <GeneralDTO>(BsonSerializer.Serialize(obj1));
            var hydratedObj2 = BsonDeserializer.Deserialize <GeneralDTO>(BsonSerializer.Serialize(obj2));

            Assert.Equal(obj1.Pi, hydratedObj1.Pi);
            Assert.Equal(obj1.AnInt, hydratedObj1.AnInt);
            Assert.Equal(obj1.Title, hydratedObj1.Title);
            Assert.Equal(obj1.ABoolean, hydratedObj1.ABoolean);
            Assert.Equal(obj1.Bytes, hydratedObj1.Bytes);
            Assert.Equal(obj1.AGuid, hydratedObj1.AGuid);
            Assert.Equal(obj1.ADateTime.Value.ToUniversalTime().Ticks,
                         hydratedObj1.ADateTime.Value.ToUniversalTime().Ticks);
            Assert.Equal(obj1.Strings, hydratedObj1.Strings);
            Assert.Equal(obj1.Flags32, hydratedObj1.Flags32);
            Assert.Equal(obj1.Flags64, hydratedObj1.Flags64);
            Assert.Equal(obj1.Nester.Title, hydratedObj1.Nester.Title);
            Assert.Equal(obj1.Nester.Pi, hydratedObj1.Nester.Pi);
            Assert.Equal(obj1.ARex.ToString(), hydratedObj1.ARex.ToString());
            Assert.Equal(obj1.ARex.Options, hydratedObj1.ARex.Options);

            Assert.Equal(obj2.Pi, hydratedObj2.Pi);
            Assert.Equal(obj2.AnInt, hydratedObj2.AnInt);
            Assert.Equal(obj2.Title, hydratedObj2.Title);
            Assert.Equal(obj2.ABoolean, hydratedObj2.ABoolean);
            Assert.Equal(obj2.Bytes, hydratedObj2.Bytes);
            Assert.Equal(obj2.AGuid, hydratedObj2.AGuid);
            Assert.Equal(obj2.ADateTime, hydratedObj2.ADateTime);
            Assert.Equal(obj2.Strings, hydratedObj2.Strings);
            Assert.Equal(obj2.Flags32, hydratedObj2.Flags32);
            Assert.Equal(obj2.Flags64, hydratedObj2.Flags64);
            Assert.Equal(obj2.Nester, hydratedObj2.Nester);
            Assert.Equal(obj2.ARex, hydratedObj2.ARex);
        }
Exemplo n.º 7
0
        public void RecursiveNestedTypesDontCauseInfiniteLoop()
        {
            var obj1 = new GeneralDTO {
                Title = "Hello World", Nester = new GeneralDTO {
                    Title = "Bob", AnInt = 42
                }
            };
            var obj1Bytes = BsonSerializer.Serialize(obj1);

            BsonDeserializer.Deserialize <GeneralDTO>(obj1Bytes);
        }
Exemplo n.º 8
0
        public void SerializationOfIEnumerableTIsNotLossy()
        {
            var gto = new GeneralDTO {
                AnIEnumerable = new List <Person>()
                {
                    new Person(), new Person()
                }
            };
            var bytes = BsonSerializer.Serialize(gto);

            var gto2 = BsonDeserializer.Deserialize <GeneralDTO>(bytes);

            Assert.Equal(2, gto2.AnIEnumerable.Count());
        }
Exemplo n.º 9
0
        public void SerializationOfEnumIsNotLossy()
        {
            var obj1 = new GeneralDTO {
                Flags32 = Flags32.FlagOn, Flags64 = Flags64.FlagOff
            };
            var obj2 = new GeneralDTO();

            var hydratedObj1 = BsonDeserializer.Deserialize <GeneralDTO>(BsonSerializer.Serialize(obj1));
            var hydratedObj2 = BsonDeserializer.Deserialize <GeneralDTO>(BsonSerializer.Serialize(obj2));

            Assert.Equal(obj1.Flags32, hydratedObj1.Flags32);
            Assert.Equal(null, hydratedObj2.Flags32);

            Assert.Equal(obj1.Flags64, hydratedObj1.Flags64);
            Assert.Equal(null, hydratedObj2.Flags64);
        }
Exemplo n.º 10
0
        public void SerializationOfNestedObjectsIsNotLossy()
        {
            var obj1 = new GeneralDTO {
                Title = "Hello World", Nester = new GeneralDTO {
                    Title = "Bob", AnInt = 42
                }
            };

            var obj1Bytes = BsonSerializer.Serialize(obj1);

            var hydratedObj1 = BsonDeserializer.Deserialize <GeneralDTO>(obj1Bytes);

            Assert.Equal(obj1.Title, hydratedObj1.Title);
            Assert.Equal(obj1.Nester.Title, hydratedObj1.Nester.Title);
            Assert.Equal(obj1.Nester.AnInt, hydratedObj1.Nester.AnInt);
        }
Exemplo n.º 11
0
        public void SerializationOfScopedCodeIsNotLossy()
        {
            var obj1 = new GeneralDTO {
                Code = new ScopedCode {
                    CodeString = "function(){return 'hello world!'}"
                }
            };
            var scope = new Expando();

            scope["$ns"]    = "root";
            obj1.Code.Scope = scope;

            var obj2 = BsonDeserializer.Deserialize <GeneralDTO>(BsonSerializer.Serialize(obj1));

            Assert.Equal(obj1.Code.CodeString, obj2.Code.CodeString);
            Assert.Equal(((Expando)obj1.Code.Scope)["$ns"], ((Expando)obj2.Code.Scope)["$ns"]);
        }
Exemplo n.º 12
0
        public void SerializationOfBooleansAreNotLossy()
        {
            var obj1 = new GeneralDTO {
                ABoolean = true
            };
            var obj2 = new GeneralDTO {
                ABoolean = null
            };

            var obj1Bytes = BsonSerializer.Serialize(obj1);
            var obj2Bytes = BsonSerializer.Serialize(obj2);

            var hydratedObj1 = BsonDeserializer.Deserialize <GeneralDTO>(obj1Bytes);
            var hydratedObj2 = BsonDeserializer.Deserialize <GeneralDTO>(obj2Bytes);

            Assert.Equal(obj1.ABoolean, hydratedObj1.ABoolean);
            Assert.Equal(null, hydratedObj2.ABoolean);
        }
Exemplo n.º 13
0
        public void SerializationOfDoublesAreNotLossy()
        {
            var obj1 = new GeneralDTO {
                Pi = 3.1415927d
            };
            var obj2 = new GeneralDTO {
                Pi = null
            };

            var obj1Bytes = BsonSerializer.Serialize(obj1);
            var obj2Bytes = BsonSerializer.Serialize(obj2);

            var hydratedObj1 = BsonDeserializer.Deserialize <GeneralDTO>(obj1Bytes);
            var hydratedObj2 = BsonDeserializer.Deserialize <GeneralDTO>(obj2Bytes);

            Assert.Equal(obj1.Pi, hydratedObj1.Pi);
            Assert.Equal(null, hydratedObj2.Pi);
        }
Exemplo n.º 14
0
        public void SerializationOfGuidIsNotLossy()
        {
            var obj1 = new GeneralDTO {
                AGuid = Guid.NewGuid()
            };
            var obj2 = new GeneralDTO {
                AGuid = null
            };

            var obj1Bytes = BsonSerializer.Serialize(obj1);
            var obj2Bytes = BsonSerializer.Serialize(obj2);

            var hydratedObj1 = BsonDeserializer.Deserialize <GeneralDTO>(obj1Bytes);
            var hydratedObj2 = BsonDeserializer.Deserialize <GeneralDTO>(obj2Bytes);

            Assert.Equal(obj1.AGuid, hydratedObj1.AGuid);
            Assert.Equal(null, hydratedObj2.AGuid);
        }
Exemplo n.º 15
0
        public void SerializationOfStringsAreNotLossy()
        {
            var obj1 = new GeneralDTO {
                Title = null
            };
            var obj2 = new GeneralDTO {
                Title = "Hello World"
            };

            var obj1Bytes = BsonSerializer.Serialize(obj1);
            var obj2Bytes = BsonSerializer.Serialize(obj2);

            var hydratedObj1 = BsonDeserializer.Deserialize <GeneralDTO>(obj1Bytes);
            var hydratedObj2 = BsonDeserializer.Deserialize <GeneralDTO>(obj2Bytes);

            Assert.Equal(null, hydratedObj1.Title);
            Assert.Equal(obj2.Title, hydratedObj2.Title);
        }
Exemplo n.º 16
0
        public void SerializationOfIntsAreNotLossy()
        {
            var obj1 = new GeneralDTO {
                AnInt = 100
            };
            var obj2 = new GeneralDTO {
                AnInt = null
            };


            var obj1Bytes = BsonSerializer.Serialize(obj1);
            var obj2Bytes = BsonSerializer.Serialize(obj2);

            var hydratedObj1 = BsonDeserializer.Deserialize <GeneralDTO>(obj1Bytes);
            var hydratedObj2 = BsonDeserializer.Deserialize <GeneralDTO>(obj2Bytes);

            Assert.Equal(obj1.AnInt, hydratedObj1.AnInt);
            Assert.Equal(null, hydratedObj2.AnInt);
        }
Exemplo n.º 17
0
        public void SerializationOfRegexIsNotLossy()
        {
            var obj1 = new GeneralDTO {
                ARex = new Regex("[0-9]{5}", RegexOptions.Multiline)
            };
            var obj2 = new GeneralDTO {
                ARex = null
            };

            var obj1Bytes = BsonSerializer.Serialize(obj1);
            var obj2Bytes = BsonSerializer.Serialize(obj2);

            var hydratedObj1 = BsonDeserializer.Deserialize <GeneralDTO>(obj1Bytes);
            var hydratedObj2 = BsonDeserializer.Deserialize <GeneralDTO>(obj2Bytes);

            Assert.Equal(obj1.ARex.ToString(), hydratedObj1.ARex.ToString());
            Assert.Equal(obj1.ARex.Options, hydratedObj1.ARex.Options);
            Assert.Equal(null, hydratedObj2.ARex);
            //more tests would be useful for all the options.
        }
Exemplo n.º 18
0
        public void SerializationOfDatesHasMillisecondPrecision()
        {
            var obj1 = new GeneralDTO {
                ADateTime = null
            };
            var obj2 = new GeneralDTO {
                ADateTime = DateTime.Now
            };

            var obj1Bytes = BsonSerializer.Serialize(obj1);
            var obj2Bytes = BsonSerializer.Serialize(obj2);

            var hydratedObj1 = BsonDeserializer.Deserialize <GeneralDTO>(obj1Bytes);
            var hydratedObj2 = BsonDeserializer.Deserialize <GeneralDTO>(obj2Bytes);

            Assert.Equal(null, hydratedObj1.ADateTime);

            //Mongo stores dates as long, therefore, we have to use double->long rounding.
            Assert.Equal((long)((obj2.ADateTime.Value.ToUniversalTime() - DateTime.MinValue)).TotalMilliseconds,
                         (long)(hydratedObj2.ADateTime.Value - DateTime.MinValue).TotalMilliseconds);
        }