forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyValueDataContractDeserializerTests.cs
More file actions
67 lines (57 loc) · 2.34 KB
/
KeyValueDataContractDeserializerTests.cs
File metadata and controls
67 lines (57 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using NUnit.Framework;
using ServiceStack.ServiceModel.Serialization;
using ServiceStack.ServiceModel.Tests.DataContracts.Operations;
namespace ServiceStack.ServiceModel.Tests
{
[TestFixture]
public class KeyValueDataContractDeserializerTests
{
[Test]
public void create_dto_request_from_ids()
{
var dtoType = typeof(GetCustomers);
var textValue = "1,2,3";
var convertedValue = textValue.Split(',').ToList().ConvertAll(x => Convert.ToInt32(x));
var valueMap = new Dictionary<string, string> { { "CustomerIds", textValue } };
var result = (GetCustomers)KeyValueDataContractDeserializer.Instance.Parse(valueMap, dtoType);
Assert.That(result.CustomerIds, Is.EquivalentTo(convertedValue));
}
[DataContract]
public class Customer
{
[DataMember(Name = "first_name")]
public string FirstName { get; set; }
[DataMember(Name = "last_name")]
public string LastName { get; set; }
}
[Test]
public void KVP_Serializer_does_use_DataMember_Name_attribute()
{
var valueMap = new Dictionary<string, string> { { "first_name", "james" }, { "last_name", "bond" } };
var result = (Customer)KeyValueDataContractDeserializer.Instance.Parse(valueMap, typeof(Customer));
Assert.That(result.FirstName, Is.EqualTo("james"));
Assert.That(result.LastName, Is.EqualTo("bond"));
}
public class Customer2
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName
{
get { return FirstName + " " + LastName; }
}
}
[Test]
public void KVP_Serializer_ignores_readonly_properties()
{
var valueMap = new Dictionary<string, string> { { "FirstName", "james" }, { "LastName", "bond" }, { "FullName", "james bond" } };
var result = (Customer2)KeyValueDataContractDeserializer.Instance.Parse(valueMap, typeof(Customer2));
Assert.That(result.FirstName, Is.EqualTo("james"));
Assert.That(result.LastName, Is.EqualTo("bond"));
}
}
}