示例#1
0
        public void ShouldSerializeClass()
        {
            // Given

            var date1 = Date.Now.AddDays(1);
            var date2 = Date.Now.AddDays(2);
            var date3 = Date.Now.AddDays(3);

            var serializer = JsonObjectSerializer.Default;

            var instance = new DateClassExample
            {
                DateProperty         = date1,
                NullableDateProperty = date2,
                ComplexProperty      = new DateClassExample
                {
                    DateProperty = date3
                }
            };

            var expectedJson = @"{" +
                               "\"DateProperty\":" + date1.UnixTime + "," +
                               "\"NullableDateProperty\":" + date2.UnixTime + "," +
                               "\"ComplexProperty\":{" +
                               "\"DateProperty\":" + date3.UnixTime +
                               "}" +
                               "}";

            // When

            var actualJson = serializer.ConvertToString(instance);

            // Then
            Assert.AreEqual(expectedJson, actualJson);
        }
示例#2
0
        public void ShouldSaveClassWithDate()
        {
            // Given

            var date1 = Date.Now.AddDays(1);
            var date2 = Date.Now.AddDays(2);
            var date3 = Date.Now.AddDays(3);

            var instance = new DateClassExample
            {
                _id                  = 1,
                DateProperty         = date1,
                NullableDateProperty = date2,
                ComplexProperty      = new DateClassExample
                {
                    DateProperty = date3
                }
            };

            var classStorage   = MongoTestHelpers.GetEmptyStorageProvider <DateClassExample>(nameof(ShouldSaveClassWithDate));
            var dynamicStorage = MongoTestHelpers.GetStorageProvider(nameof(ShouldSaveClassWithDate));

            // When

            classStorage.InsertOne(instance);

            var classInstance   = classStorage.Find().FirstOrDefault();
            var dynamicInstance = dynamicStorage.Find().FirstOrDefault();

            // Then

            Assert.IsNotNull(classInstance);
            Assert.AreEqual(date1, classInstance.DateProperty);
            Assert.AreEqual(date2, classInstance.NullableDateProperty);
            Assert.IsNotNull(classInstance.ComplexProperty);
            Assert.AreEqual(date3, classInstance.ComplexProperty.DateProperty);
            Assert.IsNull(classInstance.ComplexProperty.NullableDateProperty);

            Assert.IsNotNull(dynamicInstance);
            Assert.AreEqual(date1.UnixTime, dynamicInstance["DateProperty"]);
            Assert.AreEqual(date2.UnixTime, dynamicInstance["NullableDateProperty"]);
            Assert.IsNotNull(dynamicInstance["ComplexProperty"]);
            Assert.AreEqual(date3.UnixTime, ((DynamicWrapper)dynamicInstance["ComplexProperty"])["DateProperty"]);
            Assert.IsNull(((DynamicWrapper)dynamicInstance["ComplexProperty"])["NullableDateProperty"]);
        }