-
Notifications
You must be signed in to change notification settings - Fork 491
Expand file tree
/
Copy pathEssentialFeedAPIEndToEndTests.swift
More file actions
130 lines (110 loc) · 3.72 KB
/
EssentialFeedAPIEndToEndTests.swift
File metadata and controls
130 lines (110 loc) · 3.72 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
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
//
// Copyright © Essential Developer. All rights reserved.
//
import XCTest
import EssentialFeed
@MainActor
class EssentialFeedAPIEndToEndTests: XCTestCase {
func test_endToEndTestServerGETFeedResult_matchesFixedTestAccountData() async {
switch await getFeedResult() {
case let .success(imageFeed)?:
XCTAssertEqual(imageFeed.count, 8, "Expected 8 images in the test account image feed")
XCTAssertEqual(imageFeed[0], expectedImage(at: 0))
XCTAssertEqual(imageFeed[1], expectedImage(at: 1))
XCTAssertEqual(imageFeed[2], expectedImage(at: 2))
XCTAssertEqual(imageFeed[3], expectedImage(at: 3))
XCTAssertEqual(imageFeed[4], expectedImage(at: 4))
XCTAssertEqual(imageFeed[5], expectedImage(at: 5))
XCTAssertEqual(imageFeed[6], expectedImage(at: 6))
XCTAssertEqual(imageFeed[7], expectedImage(at: 7))
case let .failure(error)?:
XCTFail("Expected successful feed result, got \(error) instead")
default:
XCTFail("Expected successful feed result, got no result instead")
}
}
func test_endToEndTestServerGETFeedImageDataResult_matchesFixedTestAccountData() async {
switch await getFeedImageDataResult() {
case let .success(data)?:
XCTAssertFalse(data.isEmpty, "Expected non-empty image data")
case let .failure(error)?:
XCTFail("Expected successful image data result, got \(error) instead")
default:
XCTFail("Expected successful image data result, got no result instead")
}
}
// MARK: - Helpers
private func getFeedResult(file: StaticString = #filePath, line: UInt = #line) async -> Swift.Result<[FeedImage], Error>? {
let client = ephemeralClient()
do {
let (data, response) = try await client.get(from: feedTestServerURL)
return .success(try FeedItemsMapper.map(data, from: response))
} catch {
return .failure(error)
}
}
private func getFeedImageDataResult(file: StaticString = #filePath, line: UInt = #line) async -> Result<Data, Error>? {
let client = ephemeralClient()
let url = feedTestServerURL.appendingPathComponent("73A7F70C-75DA-4C2E-B5A3-EED40DC53AA6/image")
do {
let (data, response) = try await client.get(from: url)
return .success(try FeedImageDataMapper.map(data, from: response))
} catch {
return .failure(error)
}
}
private var feedTestServerURL: URL {
return URL(string: "https://essentialdeveloper.com/feed-case-study/test-api/feed")!
}
private func ephemeralClient(file: StaticString = #filePath, line: UInt = #line) -> HTTPClient {
let client = URLSessionHTTPClient(session: URLSession(configuration: .ephemeral))
trackForMemoryLeaks(client, file: file, line: line)
return client
}
private func expectedImage(at index: Int) -> FeedImage {
return FeedImage(
id: id(at: index),
description: description(at: index),
location: location(at: index),
url: imageURL(at: index))
}
private func id(at index: Int) -> UUID {
return UUID(uuidString: [
"73A7F70C-75DA-4C2E-B5A3-EED40DC53AA6",
"BA298A85-6275-48D3-8315-9C8F7C1CD109",
"5A0D45B3-8E26-4385-8C5D-213E160A5E3C",
"FF0ECFE2-2879-403F-8DBE-A83B4010B340",
"DC97EF5E-2CC9-4905-A8AD-3C351C311001",
"557D87F1-25D3-4D77-82E9-364B2ED9CB30",
"A83284EF-C2DF-415D-AB73-2A9B8B04950B",
"F79BD7F8-063F-46E2-8147-A67635C3BB01"
][index])!
}
private func description(at index: Int) -> String? {
return [
"Description 1",
nil,
"Description 3",
nil,
"Description 5",
"Description 6",
"Description 7",
"Description 8"
][index]
}
private func location(at index: Int) -> String? {
return [
"Location 1",
"Location 2",
nil,
nil,
"Location 5",
"Location 6",
"Location 7",
"Location 8"
][index]
}
private func imageURL(at index: Int) -> URL {
return URL(string: "https://url-\(index+1).com")!
}
}