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()); }
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); }
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); }
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); }
public void WhenBsonRepresentationDouble_DecimalSerializedAsDouble() { var person = new Human { NetWorth = 100.5m }; var document = person.ToBsonDocument(); Console.WriteLine(document["NetWorth"]); Assert.True(document["NetWorth"].IsDouble); }
public void WhenBsonIgnoreAttribute_PropertyOmittedFromDocument() { var person = new Human(); var document = person.ToBsonDocument(); Console.WriteLine(person.ToJson()); Assert.False(document.Contains("FirstName")); }
public void WhenBsonIgnoreAttribute_FieldIsNotSerialized() { var person = new Human(); var document = person.ToBsonDocument(); Console.WriteLine(person.ToJson()); Assert.False(document.Contains("IgnoreMe")); }
public void WhenBsonIdAttribute_NonIdFieldUsedAsId() { var person = new Human { PersonId = 666 }; var document = person.ToBsonDocument(); Console.WriteLine(person.ToJson()); Assert.AreEqual((int)document["_id"], 666); }
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")); }