Ejemplo n.º 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());
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 6
0
        public void WhenBsonIgnoreAttribute_PropertyOmittedFromDocument()
        {
            var person = new Human();

            var document = person.ToBsonDocument();

            Console.WriteLine(person.ToJson());

            Assert.False(document.Contains("FirstName"));
        }
Ejemplo n.º 7
0
        public void WhenBsonIgnoreAttribute_FieldIsNotSerialized()
        {
            var person = new Human();

            var document = person.ToBsonDocument();

            Console.WriteLine(person.ToJson());

            Assert.False(document.Contains("IgnoreMe"));
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
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"));
        }