Created
October 25, 2012 22:28
-
-
Save jeremy-holt/3955867 to your computer and use it in GitHub Desktop.
Failing test for json serialization when child properties are created in the ctor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Linq; | |
| using AutoMapper; | |
| using NUnit.Framework; | |
| using Newtonsoft.Json; | |
| using Raven.Client; | |
| using Raven.Client.Document; | |
| using Raven.Client.Embedded; | |
| using Raven.Client.Listeners; | |
| namespace ClassInitializationTests | |
| { | |
| [TestFixture] | |
| public class Serialization_tests | |
| { | |
| private static Parent GetParent() | |
| { | |
| var parent = new Parent {Child = new Child {FirstName = "FirstName", LastName = "LastName"}}; | |
| return parent; | |
| } | |
| [Test] | |
| public void Serialize_to_automapper_should_initialize_child_properties_in_ctor() | |
| { | |
| // Arrange | |
| Mapper.CreateMap<Parent, ParentDto>(); | |
| var parent = GetParent(); | |
| // Act | |
| var dto = Mapper.Map<Parent, ParentDto>(parent); | |
| // Assert | |
| Assert.That(dto.ChildFirstName, Is.EqualTo("FirstName")); | |
| Assert.That(dto.CalculatedName, Is.EqualTo("FirstName LastName")); | |
| } | |
| [Test] | |
| public void Serialize_to_raven_should_initialize_child_properties_in_ctor() | |
| { | |
| using (var store = InitRaven.EmbeddedStore()) | |
| { | |
| using (var writeSession = store.OpenSession()) | |
| { | |
| var parent = GetParent(); | |
| writeSession.Store(parent); | |
| writeSession.SaveChanges(); | |
| } | |
| using (var readSession = store.OpenSession()) | |
| { | |
| var parents = readSession.Query<Parent>().ToList(); | |
| foreach (var parent in parents) | |
| { | |
| Assert.That(parent.Calculated, Is.Not.Null, "parent.Calculated should not be null because it was initiated in the Parent.ctor"); | |
| Assert.That(parent.Calculated.Parent, Is.Not.Null, "parent.Calculated.Parent should not be null because it was initiated in the Parent.ctor"); | |
| Assert.That(parent.Calculated.Name, Is.EqualTo("FirstName LastName")); | |
| } | |
| } | |
| } | |
| } | |
| [Test] | |
| public void Simple_deserialization_test() | |
| { | |
| // Arrange | |
| var parent = GetParent(); | |
| var serializedString = JsonConvert.SerializeObject(parent); | |
| // Act | |
| var deserializedParent = JsonConvert.DeserializeObject<Parent>(serializedString); | |
| // Assert | |
| Assert.That(deserializedParent.Calculated.Name, Is.EqualTo("FirstName LastName")); | |
| } | |
| [Test] | |
| public void Simple_serialization_test() | |
| { | |
| // Arrange | |
| var parent = GetParent(); | |
| // Act | |
| var serializedString = JsonConvert.SerializeObject(parent); | |
| // Assert | |
| Assert.That(serializedString, Is.EqualTo("{\"Child\":{\"FirstName\":\"FirstName\",\"LastName\":\"LastName\"},\"Calculated\":{\"Name\":\"FirstName LastName\"}}")); | |
| } | |
| } | |
| public class InitRaven | |
| { | |
| private static DocumentStore _embeddedStore; | |
| public static DocumentStore EmbeddedStore() | |
| { | |
| if (_embeddedStore == null) | |
| { | |
| _embeddedStore = new EmbeddableDocumentStore | |
| { | |
| RunInMemory = true, | |
| UseEmbeddedHttpServer = false, | |
| Conventions = {DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites} | |
| }; | |
| _embeddedStore.Initialize(); | |
| _embeddedStore.RegisterListener(new NonStaleQueryListener()); | |
| } | |
| return _embeddedStore; | |
| } | |
| private class NonStaleQueryListener : IDocumentQueryListener | |
| { | |
| public void BeforeQueryExecuted(IDocumentQueryCustomization customization) | |
| { | |
| customization.WaitForNonStaleResults(); | |
| } | |
| } | |
| } | |
| public class Parent | |
| { | |
| public Parent() | |
| { | |
| Calculated = new Calculator(this); | |
| } | |
| public Child Child { get; set; } | |
| public Calculator Calculated { get; private set; } | |
| } | |
| public class Child | |
| { | |
| public string FirstName { get; set; } | |
| public string LastName { get; set; } | |
| } | |
| public class Calculator | |
| { | |
| public Calculator(Parent parent) | |
| { | |
| Parent = parent; | |
| } | |
| public Parent Parent { get; private set; } | |
| public string Name | |
| { | |
| get { return Parent.Child.FirstName + " " + Parent.Child.LastName; } | |
| } | |
| } | |
| public class ParentDto | |
| { | |
| public string ChildFirstName { get; set; } | |
| public string ChildLastName { get; set; } | |
| public string CalculatedName { get; set; } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment