-
Notifications
You must be signed in to change notification settings - Fork 566
/
StripeEntityTest.cs
209 lines (167 loc) · 5.82 KB
/
StripeEntityTest.cs
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
namespace StripeTests
{
using Newtonsoft.Json;
using Stripe;
using Stripe.Infrastructure;
using Xunit;
public class StripeEntityTest : BaseStripeTest
{
public StripeEntityTest(
StripeMockFixture stripeMockFixture)
: base(stripeMockFixture)
{
}
[Fact]
public void FromJsonAuto()
{
var json = "{\"id\": \"ch_123\", \"object\": \"charge\"}";
var o = StripeEntity.FromJson(json);
Assert.NotNull(o);
Assert.IsType<Charge>(o);
Assert.Equal("ch_123", ((Charge)o).Id);
}
[Fact]
public void FromJsonAutoUnknownObject()
{
var json = "{\"id\": \"ch_123\", \"object\": \"foo\"}";
var o = StripeEntity.FromJson(json);
Assert.Null(o);
}
[Fact]
public void FromJsonAutoNoObject()
{
var json = "{\"id\": \"ch_123\"}";
var o = StripeEntity.FromJson(json);
Assert.Null(o);
}
[Fact]
public void FromJsonOnType()
{
var json = "{\"integer\": 234, \"string\": \"String!\"}";
var o = TestEntity.FromJson(json);
Assert.NotNull(o);
Assert.Equal(234, o.Integer);
Assert.Equal("String!", o.String);
}
[Fact]
public void FromJsonGeneric()
{
var json = "{\"integer\": 234, \"string\": \"String!\"}";
var o = StripeEntity.FromJson<TestEntity>(json);
Assert.NotNull(o);
Assert.Equal(234, o.Integer);
Assert.Equal("String!", o.String);
}
[Fact]
public void ToJson()
{
var o = new TestEntity
{
Integer = 234,
String = "String!",
};
var json = o.ToJson().Replace("\r\n", "\n");
var expectedJson = "{\n \"integer\": 234,\n \"string\": \"String!\",\n \"nested\": null\n}";
Assert.Equal(expectedJson, json);
}
[Fact]
public void ExpandableAccessors_Id()
{
var o = new TestEntity
{
NestedId = "id_unexpanded",
};
Assert.Equal("id_unexpanded", o.NestedId);
Assert.Null(o.Nested);
}
[Fact]
public void ExpandableAccessors_ExpandedObject()
{
var o = new TestEntity
{
Nested = new TestNestedEntity { Id = "id_expanded", Bar = 42 },
};
Assert.Equal("id_expanded", o.NestedId);
Assert.NotNull(o.Nested);
Assert.Equal("id_expanded", o.Nested.Id);
Assert.Equal(42, o.Nested.Bar);
}
[Fact]
public void ExpandableAccessors_Null()
{
var o = new TestEntity();
Assert.Null(o.NestedId);
Assert.Null(o.Nested);
}
[Fact]
public void ExpandableAccessors_Id_ResetsExistingExpandedObjectIfIdIsDifferent()
{
var o = new TestEntity
{
Nested = new TestNestedEntity { Id = "id_expanded", Bar = 42 },
};
o.NestedId = "id_new";
Assert.Equal("id_new", o.NestedId);
Assert.Null(o.Nested);
}
[Fact]
public void ExpandableAccessors_Id_DoesNotResetExistingExpandedObjectIfIdIsSame()
{
var o = new TestEntity
{
Nested = new TestNestedEntity { Id = "id_expanded", Bar = 42 },
};
o.NestedId = "id_expanded";
Assert.Equal("id_expanded", o.NestedId);
Assert.NotNull(o.Nested);
Assert.Equal("id_expanded", o.Nested.Id);
Assert.Equal(42, o.Nested.Bar);
}
[Fact]
public void RawJObject()
{
var service = new SubscriptionService(this.StripeClient);
var subscription = service.Get("sub_123");
Assert.NotNull(subscription);
// Access `id`, a string element
Assert.Equal(subscription.Id, subscription.RawJObject["id"]);
// Access `created`, a number element
Assert.Equal(subscription.Created, subscription.RawJObject["created"]);
// Access `plan[id]`, a nested string element
Assert.Equal(subscription.Plan.Id, subscription.RawJObject["plan"]["id"]);
// Access `items[data][0][id]`, a deeply nested string element
Assert.Equal(
subscription.Items.Data[0].Id,
subscription.RawJObject["items"]["data"][0]["id"]);
}
private class TestEntity : StripeEntity<TestEntity>
{
[JsonProperty("integer")]
public int Integer { get; set; }
[JsonProperty("string")]
public string String { get; set; }
[JsonIgnore]
public string NestedId
{
get => this.InternalNested?.Id;
set => this.InternalNested = SetExpandableFieldId(value, this.InternalNested);
}
[JsonIgnore]
public TestNestedEntity Nested
{
get => this.InternalNested?.ExpandedObject;
set => this.InternalNested = SetExpandableFieldObject(value, this.InternalNested);
}
[JsonProperty("nested")]
[JsonConverter(typeof(ExpandableFieldConverter<TestNestedEntity>))]
internal ExpandableField<TestNestedEntity> InternalNested { get; set; }
}
private class TestNestedEntity : StripeEntity<TestNestedEntity>, IHasId
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("bar")]
public int Bar { get; set; }
}
}
}