コード例 #1
0
        public void Serialization_WhenDateTimeDateOnlyAttribute_RequiresDateTimeToHaveZeroTime()
        {
            var dateTimeNow = DateTime.Now;

            var person = new Human { BirthDate = dateTimeNow };

            Assert.Throws<BsonSerializationException>(() => person.ToBsonDocument());

            person.BirthDate = dateTimeNow.Date;

            Assert.DoesNotThrow(() => person.ToBsonDocument());
        }
コード例 #2
0
        public void ToJson_AutomaticallySerializes()
        {
            var person = new Human
            {
                Age = 54,
                FirstName = "Bob"
            };

            person.Address.Add("Your mom's house");
            person.Address.Add("Cloud City");

            person.ContactInfo.Email = "*****@*****.**";
            person.ContactInfo.Phone = "123-456-7890";

            string serialized = null;

            Assert.DoesNotThrow(() => serialized = person.ToJson());

            Console.WriteLine(serialized);
        }
コード例 #3
0
        public void DateTime_Always_SerializesDateTimeAsUtc()
        {
            var localTime = DateTime.Now;
            var utcTime = DateTime.UtcNow;

            var person = new Human
            {
                LocalTime = localTime,
                UnmodifiedTime = localTime
            };

            var document = person.ToBsonDocument();
            var serializedWithLocalAttributeTime = (DateTime)document["LocalTime"];
            var serializedUnmodifiedTimeTime = (DateTime)document["UnmodifiedTime"];

            Console.WriteLine(serializedWithLocalAttributeTime);
            Console.WriteLine(serializedUnmodifiedTimeTime);

            Assert.AreEqual(utcTime.Hour, serializedWithLocalAttributeTime.Hour);
            Assert.AreEqual(utcTime.Hour, serializedUnmodifiedTimeTime.Hour);
        }
コード例 #4
0
        public void WhenNoDateTimeDateOnlyAttribute_DateTimeSerializedWithUtcTime_()
        {
            var today = DateTime.Today;
            var todayUtc = today.ToUniversalTime();

            var person = new Human { UnmodifiedTime = today };

            var document = person.ToBsonDocument();

            var serializedDate = (DateTime)document["UnmodifiedTime"];

            Console.WriteLine(serializedDate);

            Assert.AreEqual(todayUtc.Hour, serializedDate.Hour);
        }
コード例 #5
0
        public void WhenBsonRepresentationDouble_DecimalSerializedAsDouble()
        {
            var person = new Human { NetWorth = 100.5m };

            var document = person.ToBsonDocument();

            Console.WriteLine(document["NetWorth"]);

            Assert.True(document["NetWorth"].IsDouble);
        }
コード例 #6
0
        public void WhenDateTimeDateOnlyAttribute_DateTimeSerializedWithZeroTime()
        {
            var today = DateTime.Today;

            var person = new Human { BirthDate = today };

            var document = person.ToBsonDocument();

            var serializedDate = (DateTime)document["BirthDate"];

            Console.WriteLine(serializedDate);

            Assert.AreEqual(today.Hour, serializedDate.Hour);
        }
コード例 #7
0
        public void WhenBsonIgnoreAttribute_PropertyOmittedFromDocument()
        {
            var person = new Human();

            var document = person.ToBsonDocument();

            Console.WriteLine(person.ToJson());

            Assert.False(document.Contains("FirstName"));
        }
コード例 #8
0
        public void WhenBsonIgnoreAttribute_FieldIsNotSerialized()
        {
            var person = new Human();

            var document = person.ToBsonDocument();

            Console.WriteLine(person.ToJson());

            Assert.False(document.Contains("IgnoreMe"));
        }
コード例 #9
0
        public void WhenBsonIdAttribute_NonIdFieldUsedAsId()
        {
            var person = new Human { PersonId = 666 };

            var document = person.ToBsonDocument();

            Console.WriteLine(person.ToJson());

            Assert.AreEqual((int)document["_id"], 666);
        }
コード例 #10
0
        public void WhenBsonElementAttribute_NonPublicMembersIncludedInDocument()
        {
            var person = new Human();

            var serialized = person.ToJson();

            Console.WriteLine(person.ToJson());

            Assert.True(serialized.Contains("mplementationDetail"));
        }
コード例 #11
0
        public void WhenBsonElementAttribute_CustomFieldNameSerializedToDocument()
        {
            var person = new Human();

            var document = person.ToBsonDocument();

            Console.WriteLine(person.ToJson());

            Assert.True(document.Contains("UpdatedMemberName"));
            Assert.False(document.Contains("Obsolete"));
        }