/
ReadingListStorageTestCase.swift
195 lines (169 loc) · 8.58 KB
/
ReadingListStorageTestCase.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
@testable import ReadingList
import Foundation
import Shared
import XCTest
class ReadingListStorageTestCase: XCTestCase {
var storage: ReadingListStorage!
override func setUp() {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
if FileManager.default.fileExists(atPath: "\(path)/ReadingList.db") {
do {
try FileManager.default.removeItem(atPath: "\(path)/ReadingList.db")
} catch _ {
XCTFail("Cannot remove old \(path)/ReadingList.db")
}
}
storage = ReadingListSQLStorage(path: "\(path)/ReadingList.db")
}
func testCreateRecord() {
let result = storage.createRecordWithURL("http://www.anandtech.com/show/9117/analyzing-intel-core-m-performance", title: "Analyzing Intel Core M Performance: How 5Y10 can beat 5Y71 & the OEMs' Dilemma", addedBy: "Stefan's iPhone")
switch result {
case .failure(let error):
XCTFail(error.description)
case .success(let result):
XCTAssertEqual(result.value.url, "http://www.anandtech.com/show/9117/analyzing-intel-core-m-performance")
XCTAssertEqual(result.value.title, "Analyzing Intel Core M Performance: How 5Y10 can beat 5Y71 & the OEMs' Dilemma")
XCTAssertEqual(result.value.addedBy, "Stefan's iPhone")
XCTAssertEqual(result.value.unread, true)
XCTAssertEqual(result.value.archived, false)
XCTAssertEqual(result.value.favorite, false)
}
}
func testGetRecordWithURL() {
let result1 = storage.createRecordWithURL("http://www.anandtech.com/show/9117/analyzing-intel-core-m-performance", title: "Analyzing Intel Core M Performance: How 5Y10 can beat 5Y71 & the OEMs' Dilemma", addedBy: "Stefan's iPhone")
switch result1 {
case .failure(let error):
XCTFail(error.description)
case .success( _):
break
}
let result2 = storage.getRecordWithURL("http://www.anandtech.com/show/9117/analyzing-intel-core-m-performance")
switch result2 {
case .failure(let error):
XCTFail(error.description)
case .success( _):
XCTAssert(result1.successValue == result2.successValue!)
}
}
func testGetUnreadRecords() {
// Create 3 records, mark the 2nd as read.
let _ = createRecordWithURL("http://localhost/article1", title: "Test 1", addedBy: "Stefan's iPhone")
let createResult2 = createRecordWithURL("http://localhost/article2", title: "Test 2", addedBy: "Stefan's iPhone")
let _ = createRecordWithURL("http://localhost/article3", title: "Test 3", addedBy: "Stefan's iPhone")
if let record = createResult2.successValue {
let _ = updateRecord(record, unread: false)
}
// Get all unread records, make sure we only get the first and last
let getUnreadResult = storage.getUnreadRecords()
if let records = getUnreadResult.successValue {
XCTAssertEqual(2, records.count)
for record in records {
XCTAssert(record.title == "Test 1" || record.title == "Test 3")
XCTAssertEqual(record.unread, true)
}
}
}
func testGetAllRecords() {
let _ = createRecordWithURL("http://localhost/article1", title: "Test 1", addedBy: "Stefan's iPhone")
let createResult2 = createRecordWithURL("http://localhost/article2", title: "Test 2", addedBy: "Stefan's iPhone")
let _ = createRecordWithURL("http://localhost/article3", title: "Test 3", addedBy: "Stefan's iPhone")
if let record = createResult2.successValue {
let _ = updateRecord(record, unread: false)
}
let getAllResult = storage.getAllRecords()
if let records = getAllResult.successValue {
XCTAssertEqual(3, records.count)
}
}
func testGetNewRecords() {
let _ = createRecordWithURL("http://localhost/article1", title: "Test 1", addedBy: "Stefan's iPhone")
let _ = createRecordWithURL("http://localhost/article2", title: "Test 2", addedBy: "Stefan's iPhone")
let _ = createRecordWithURL("http://localhost/article3", title: "Test 3", addedBy: "Stefan's iPhone")
let getAllResult = getAllRecords()
if let records = getAllResult.successValue {
XCTAssertEqual(3, records.count)
}
// TODO When we are able to create records coming from the server, we can extend this test to see if we query correctly
}
func testDeleteRecord() {
let result1 = storage.createRecordWithURL("http://www.anandtech.com/show/9117/analyzing-intel-core-m-performance", title: "Analyzing Intel Core M Performance: How 5Y10 can beat 5Y71 & the OEMs' Dilemma", addedBy: "Stefan's iPhone")
switch result1 {
case .failure(let error):
XCTFail(error.description)
case .success(_):
break
}
let result2 = storage.deleteRecord(result1.successValue!)
switch result2 {
case .failure(let error):
XCTFail(error.description)
case .success:
break
}
let result3 = storage.getRecordWithURL("http://www.anandtech.com/show/9117/analyzing-intel-core-m-performance")
switch result3 {
case .failure(let error):
XCTFail(error.description)
case .success(let result):
XCTAssert(result.value == nil)
}
}
func testDeleteAllRecords() {
let _ = createRecordWithURL("http://localhost/article1", title: "Test 1", addedBy: "Stefan's iPhone")
let _ = createRecordWithURL("http://localhost/article2", title: "Test 2", addedBy: "Stefan's iPhone")
let _ = createRecordWithURL("http://localhost/article3", title: "Test 3", addedBy: "Stefan's iPhone")
let getAllResult1 = storage.getAllRecords()
if let records = getAllResult1.successValue {
XCTAssertEqual(3, records.count)
}
let _ = deleteAllRecords()
let getAllResult2 = getAllRecords()
if let records = getAllResult2.successValue {
XCTAssertEqual(0, records.count)
}
}
func testUpdateRecord() {
let result = createRecordWithURL("http://www.anandtech.com/show/9117/analyzing-intel-core-m-performance", title: "Analyzing Intel Core M Performance: How 5Y10 can beat 5Y71 & the OEMs' Dilemma", addedBy: "Stefan's iPhone")
if let record = result.successValue {
XCTAssertEqual(record.url, "http://www.anandtech.com/show/9117/analyzing-intel-core-m-performance")
XCTAssertEqual(record.title, "Analyzing Intel Core M Performance: How 5Y10 can beat 5Y71 & the OEMs' Dilemma")
XCTAssertEqual(record.addedBy, "Stefan's iPhone")
XCTAssertEqual(record.unread, true)
XCTAssertEqual(record.archived, false)
XCTAssertEqual(record.favorite, false)
let result = updateRecord(record, unread: false)
if let record = result.successValue! {
XCTAssertEqual(record.url, "http://www.anandtech.com/show/9117/analyzing-intel-core-m-performance")
XCTAssertEqual(record.title, "Analyzing Intel Core M Performance: How 5Y10 can beat 5Y71 & the OEMs' Dilemma")
XCTAssertEqual(record.addedBy, "Stefan's iPhone")
XCTAssertEqual(record.unread, false)
XCTAssertEqual(record.archived, false)
XCTAssertEqual(record.favorite, false)
}
}
}
// Helpers that croak if the storage call was not succesful
func createRecordWithURL(_ url: String, title: String, addedBy: String) -> Maybe<ReadingListClientRecord> {
let result = storage.createRecordWithURL(url, title: title, addedBy: addedBy)
XCTAssertTrue(result.isSuccess)
return result
}
func deleteAllRecords() -> Maybe<Void> {
let result = storage.deleteAllRecords()
XCTAssertTrue(result.isSuccess)
return result
}
func getAllRecords() -> Maybe<[ReadingListClientRecord]> {
let result = storage.getAllRecords()
XCTAssertTrue(result.isSuccess)
return result
}
func updateRecord(_ record: ReadingListClientRecord, unread: Bool) -> Maybe<ReadingListClientRecord?> {
let result = storage.updateRecord(record, unread: unread)
XCTAssertTrue(result.isSuccess)
return result
}
}