-
Notifications
You must be signed in to change notification settings - Fork 5
/
InvoiceTests.swift
249 lines (207 loc) · 7.71 KB
/
InvoiceTests.swift
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//
// InvoiceTests.swift
// CodableTests
//
// Created by Nate Walczak on 9/29/18.
// Copyright © 2018 Detroit Labs. All rights reserved.
//
import XCTest
@testable import Codable
class InvoiceTests: XCTestCase {
func testDecodable() throws {
// Given
let json = Data("""
{
"shipping-address": {
"name": "Detroit Labs",
"city": "Detroit",
"state": "MI"
},
"billing-address": {
"name": "Paula Goodski",
},
"items": [
{ "sku": "I001", "name": "Labs Beats V1", "quantity": 10000 }
]
}
""".utf8)
// When
let invoice: Invoice
do {
invoice = try JSONDecoder().decode(Invoice.self, from: json)
} catch {
XCTFail(error.localizedDescription)
return
}
// Then
let shippingAddress = try XCTUnwrap(invoice.shippingAddress)
XCTAssertEqual(shippingAddress.name, "Detroit Labs")
XCTAssertEqual(shippingAddress.city, "Detroit")
XCTAssertEqual(shippingAddress.state, "MI")
let billingAddress = try XCTUnwrap(invoice.billingAddress)
XCTAssertEqual(billingAddress.name, "Paula Goodski")
let items = invoice.items
XCTAssertEqual(items.count, 1)
let firstItem = try XCTUnwrap(items.first)
XCTAssertEqual(firstItem.sku, "I001")
XCTAssertEqual(firstItem.name, "Labs Beats V1")
XCTAssertEqual(firstItem.quantity, 10_000)
}
func testOptionalDecodable() {
// Given
let json = Data("""
{
}
""".utf8)
// When
let invoice: Invoice
do {
invoice = try JSONDecoder().decode(Invoice.self, from: json)
} catch {
XCTFail(error.localizedDescription)
return
}
let shippingAddress = invoice.shippingAddress
let billingAddress = invoice.billingAddress
let items = invoice.items
// Then
XCTAssertNil(shippingAddress)
XCTAssertNil(billingAddress)
XCTAssertEqual(items.count, 0)
}
func testEncodable() {
// Given
let invoice = Invoice()
invoice.shippingAddress = Address(city: "Detroit", state: "MI")
invoice.billingAddress = Address(city: "Atlanta", state: "GA")
invoice.items = [
Item(sku: "I001", name: "Labs Beats V1", quantity: 4),
Item(sku: "I002", name: "Labs Beats V2", quantity: 8)
]
// When
let data: Data
let json: [String: Any]?
do {
data = try JSONEncoder().encode(invoice)
json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
XCTFail(error.localizedDescription)
return
}
let shippingAddress = json?["shipping-address"] as? [String: Any]
let billingAddress = json?["billing-address"] as? [String: Any]
let items = json?["items"] as? [Any]
let firstItem = items?.first as? [String: Any]
let secondItem = items?.second as? [String: Any]
// Then
XCTAssertNotNil(json)
XCTAssertEqual(json?.count, 3)
XCTAssertNotNil(shippingAddress)
XCTAssertEqual(shippingAddress?.count, 2)
XCTAssertEqual(shippingAddress?["city"] as? String, "Detroit")
XCTAssertEqual(shippingAddress?["state"] as? String, "MI")
XCTAssertNotNil(billingAddress)
XCTAssertEqual(billingAddress?.count, 2)
XCTAssertEqual(billingAddress?["city"] as? String, "Atlanta")
XCTAssertEqual(billingAddress?["state"] as? String, "GA")
XCTAssertNotNil(items)
XCTAssertEqual(items?.count, 2)
XCTAssertEqual(firstItem?["sku"] as? String, "I001")
XCTAssertEqual(firstItem?["name"] as? String, "Labs Beats V1")
XCTAssertEqual(firstItem?["quantity"] as? Int, 4)
XCTAssertEqual(secondItem?["sku"] as? String, "I002")
XCTAssertEqual(secondItem?["name"] as? String, "Labs Beats V2")
XCTAssertEqual(secondItem?["quantity"] as? Int, 8)
}
func testOptionalEncodable() {
// Given
let invoice = Invoice()
// When
let data: Data
let json: [String: Any]?
do {
data = try JSONEncoder().encode(invoice)
json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
XCTFail(error.localizedDescription)
return
}
// Then
XCTAssertNotNil(json)
XCTAssertEqual(json?.count, 0)
}
func testDecodableWithoutItems() throws {
// Given
let json = Data("""
{
"shipping-address": {
"name": "Detroit Labs",
"city": "Detroit",
"state": "MI"
},
"billing-address": {
"name": "Paula Goodski",
},
"items": [
{ "sku": "I001", "name": "Labs Beats V1", "quantity": 10000 }
]
}
""".utf8)
// When
let invoice: Invoice
do {
let decoder = JSONDecoder()
decoder.userInfo = [.includeInvoiceItems: false]
invoice = try decoder.decode(Invoice.self, from: json)
} catch {
XCTFail(error.localizedDescription)
return
}
// Then
let shippingAddress = try XCTUnwrap(invoice.shippingAddress)
XCTAssertEqual(shippingAddress.name, "Detroit Labs")
XCTAssertEqual(shippingAddress.city, "Detroit")
XCTAssertEqual(shippingAddress.state, "MI")
let billingAddress = try XCTUnwrap(invoice.billingAddress)
XCTAssertEqual(billingAddress.name, "Paula Goodski")
let items = invoice.items
XCTAssertEqual(items.count, 0)
}
func testEncodableWithoutItems() {
// Given
let invoice = Invoice()
invoice.shippingAddress = Address(city: "Detroit", state: "MI")
invoice.billingAddress = Address(city: "Atlanta", state: "GA")
invoice.items = [
Item(sku: "I001", name: "Labs Beats V1", quantity: 4),
Item(sku: "I002", name: "Labs Beats V2", quantity: 8)
]
// When
let data: Data
let json: [String: Any]?
do {
let encoder = JSONEncoder()
encoder.userInfo = [.includeInvoiceItems: false]
data = try encoder.encode(invoice)
json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
XCTFail(error.localizedDescription)
return
}
let shippingAddress = json?["shipping-address"] as? [String: Any]
let billingAddress = json?["billing-address"] as? [String: Any]
let items = json?["items"] as? [Any]
// Then
XCTAssertNotNil(json)
XCTAssertEqual(json?.count, 2)
XCTAssertNotNil(shippingAddress)
XCTAssertEqual(shippingAddress?.count, 2)
XCTAssertEqual(shippingAddress?["city"] as? String, "Detroit")
XCTAssertEqual(shippingAddress?["state"] as? String, "MI")
XCTAssertNotNil(billingAddress)
XCTAssertEqual(billingAddress?.count, 2)
XCTAssertEqual(billingAddress?["city"] as? String, "Atlanta")
XCTAssertEqual(billingAddress?["state"] as? String, "GA")
XCTAssertNil(items)
}
}