Skip to content

Commit 9e29b05

Browse files
committed
Add CustomerMapper unit tests
1 parent 28826ce commit 9e29b05

File tree

1 file changed

+65
-11
lines changed

1 file changed

+65
-11
lines changed
Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,82 @@
11
import XCTest
22
@testable import Networking
33

4+
/// CustomerMapper Unit Tests
5+
///
46
class CustomerMapperTests: XCTestCase {
57

6-
func test_data_is_mapped() {
7-
let mapper = CustomerMapper(siteID: 123)
8-
guard let data = Loader.contentsOf("customer") else {
8+
/// Dummy Site ID.
9+
///
10+
private let dummySiteID: Int64 = 123
11+
12+
/// Local file that holds Customer data representing the API endpoint
13+
///
14+
private let filename: String = "customer"
15+
16+
/// Verifies that the Customer object can be mapped fron the Encoded data
17+
///
18+
func test_Customer_is_mapped_from_encoded_data() {
19+
// Given
20+
let mapper = CustomerMapper(siteID: dummySiteID)
21+
guard let data = Loader.contentsOf(filename) else {
922
XCTFail("customer.json not found")
1023
return
1124
}
25+
26+
// When
1227
let customer = try? mapper.map(response: data)
28+
29+
// Then
1330
XCTAssertNotNil(mapper)
1431
XCTAssertNotNil(customer)
15-
XCTAssertEqual(customer?.customerID, 25)
32+
XCTAssertNotNil(customer?.customerID)
33+
XCTAssertNotNil(customer?.email)
34+
XCTAssertNotNil(customer?.firstName)
35+
XCTAssertNotNil(customer?.lastName)
36+
XCTAssertNotNil(customer?.billing)
37+
XCTAssertNotNil(customer?.shipping)
38+
}
39+
40+
/// Verifies that all of the Customer response values are parsed correctly
41+
///
42+
func test_Customer_response_values_are_correctly_parsed() throws {
43+
// Given
44+
guard let customer = try mapCustomer(from: filename) else {
45+
XCTFail()
46+
return
47+
}
48+
49+
// Then
50+
XCTAssertNotNil(customer)
51+
XCTAssertEqual(customer.customerID, 25)
52+
XCTAssertEqual(customer.email, "[email protected]")
53+
XCTAssertEqual(customer.firstName, "John")
54+
XCTAssertEqual(customer.lastName, "Doe")
55+
56+
let dummyAddresses = [customer.shipping, customer.billing].compactMap({ $0 })
57+
XCTAssertEqual(dummyAddresses.count, 2)
58+
59+
for address in dummyAddresses {
60+
XCTAssertEqual(address.firstName, "John")
61+
XCTAssertEqual(address.lastName, "Doe")
62+
XCTAssertEqual(address.company, "")
63+
XCTAssertEqual(address.address1, "969 Market")
64+
XCTAssertEqual(address.address2, "")
65+
XCTAssertEqual(address.city, "San Francisco")
66+
XCTAssertEqual(address.state, "CA")
67+
XCTAssertEqual(address.postcode, "94103")
68+
XCTAssertEqual(address.country, "US")
69+
}
1670
}
1771
}
1872

1973
private extension CustomerMapperTests {
20-
func mapCustomer(from filename: String) throws -> Customer {
21-
return Customer(
22-
customerID: 25,
23-
email: "",
24-
firstName: "",
25-
lastName: ""
26-
)
74+
/// Returns the CustomerMapper output upon receiving `filename` (Data Encoded)
75+
///
76+
func mapCustomer(from filename: String) throws -> Customer? {
77+
guard let response = Loader.contentsOf(filename) else {
78+
return nil
79+
}
80+
return try! CustomerMapper(siteID: dummySiteID).map(response: response)
2781
}
2882
}

0 commit comments

Comments
 (0)