Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json.Tests\LinqToSql\LinqToSqlClassesSerializationTests.cs

Symbol Coverage: 100.00% (29 of 29)

Branch Coverage: 100.00% (3 of 3)

Cyclomatic Complexity Avg: 1.00 Max:1

Code Lines: 76


L V Source
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel.DataAnnotations;
4
using System.Linq;
5
using System.Text;
6
using Newtonsoft.Json.Tests.LinqToSql;
7
using NUnit.Framework;
8
using System.Reflection;
9
using System.ComponentModel;
10
using Newtonsoft.Json.Serialization;
11
using System.Data.Linq.Mapping;
12

  
13
namespace Newtonsoft.Json.Tests.LinqToSql
14
{
15
  public class LinqToSqlClassesSerializationTests : TestFixtureBase
16
  {
17
    [Test]
18
    public void Serialize()
19
    {
20
 1
      Role role = new Role();
21
 1
      role.Name = "Role1";
22
 1
      role.RoleId = new Guid("67EA92B7-4BD3-4718-BD75-3C7EDF800B34");
23

  
24
 1
      Person person = new Person();
25
 1
      person.FirstName = "FirstName!";
26
 1
      person.LastName = "LastName!";
27
 1
      person.PersonId = new Guid("7AA027AA-C995-4986-908D-999D8063599F");
28
 1
      person.PersonRoles.Add(new PersonRole
29
 1
                               {
30
 1
                                 PersonRoleId = new Guid("B012DD41-71DF-4839-B8D5-D1333FB886BC"),
31
 1
                                 Role = role
32
 1
                               });
33

  
34
 1
      person.Department = new Department
35
 1
                               {
36
 1
                                 DepartmentId = new Guid("08F68BF9-929B-4434-BC47-C9489D22112B"),
37
 1
                                 Name = "Name!"
38
 1
                               };
39

  
40
 1
      string json = JsonConvert.SerializeObject(person, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
41

  
42
 1
      Assert.AreEqual(@"{
43
 1
  ""first_name"": ""FirstName!"",
44
 1
  ""LastName"": ""LastName!"",
45
 1
  ""PersonId"": ""7aa027aa-c995-4986-908d-999d8063599f"",
46
 1
  ""DepartmentId"": ""08f68bf9-929b-4434-bc47-c9489d22112b"",
47
 1
  ""PersonRoles"": [
48
 1
    {
49
 1
      ""PersonId"": ""7aa027aa-c995-4986-908d-999d8063599f"",
50
 1
      ""RoleId"": ""67ea92b7-4bd3-4718-bd75-3c7edf800b34"",
51
 1
      ""PersonRoleId"": ""b012dd41-71df-4839-b8d5-d1333fb886bc"",
52
 1
      ""Role"": {
53
 1
        ""Name"": ""Role1"",
54
 1
        ""RoleId"": ""t5LqZ9NLGEe9dTx+34ALNA==""
55
 1
      }
56
 1
    }
57
 1
  ],
58
 1
  ""Department"": {
59
 1
    ""DepartmentId"": ""08f68bf9-929b-4434-bc47-c9489d22112b"",
60
 1
    ""Name"": ""!emaN""
61
 1
  }
62
 1
}", json);
63
 1
    }
64

  
65
    [Test]
66
    public void Deserialize()
67
    {
68
 1
      string json = @"{
69
 1
  ""first_name"": ""FirstName!"",
70
 1
  ""LastName"": ""LastName!"",
71
 1
  ""PersonId"": ""7aa027aa-c995-4986-908d-999d8063599f"",
72
 1
  ""PersonRoles"": [
73
 1
    {
74
 1
      ""PersonId"": ""7aa027aa-c995-4986-908d-999d8063599f"",
75
 1
      ""RoleId"": ""67ea92b7-4bd3-4718-bd75-3c7edf800b34"",
76
 1
      ""PersonRoleId"": ""b012dd41-71df-4839-b8d5-d1333fb886bc"",
77
 1
      ""Role"": {
78
 1
        ""Name"": ""Role1"",
79
 1
        ""RoleId"": ""t5LqZ9NLGEe9dTx+34ALNA==""
80
 1
      }
81
 1
    }
82
 1
  ],
83
 1
  ""Department"": {
84
 1
    ""DepartmentId"": ""08f68bf9-929b-4434-bc47-c9489d22112b"",
85
 1
    ""Name"": ""!emaN""
86
 1
  }
87
 1
}";
88

  
89
 1
      Person person = JsonConvert.DeserializeObject<Person>(json);
90
 1
      Assert.IsNotNull(person);
91

  
92
 1
      Assert.AreEqual(new Guid("7AA027AA-C995-4986-908D-999D8063599F"), person.PersonId);
93
 1
      Assert.AreEqual("FirstName!", person.FirstName);
94
 1
      Assert.AreEqual("LastName!", person.LastName);
95
 1
      Assert.AreEqual(1, person.PersonRoles.Count);
96
 1
      Assert.AreEqual(person.PersonId, person.PersonRoles[0].PersonId);
97
 1
      Assert.AreEqual(new Guid("67EA92B7-4BD3-4718-BD75-3C7EDF800B34"), person.PersonRoles[0].RoleId);
98
 1
      Assert.IsNotNull(person.PersonRoles[0].Role);
99
 1
      Assert.AreEqual(1, person.PersonRoles[0].Role.PersonRoles.Count);
100

  
101
 1
      Assert.AreEqual("Name!", person.Department.Name);
102

  
103
 1
      TableAttribute tableAttribute = JsonTypeReflector.GetAttribute<TableAttribute>(typeof(Person));
104
 1
      Assert.AreEqual("", tableAttribute.Name);
105

  
106
 1
      ColumnAttribute columnAttribute = JsonTypeReflector.GetAttribute<ColumnAttribute>(typeof(Person).GetProperty("FirstName"));
107
 1
      Assert.AreEqual("_FirstName", columnAttribute.Storage);
108
 1
    }
109
  }
110
}