-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathReadWriteTests.swift
More file actions
260 lines (207 loc) · 7.93 KB
/
ReadWriteTests.swift
File metadata and controls
260 lines (207 loc) · 7.93 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
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
250
251
252
253
254
255
256
257
258
259
260
import Foundation
import Sworm
import SwormTools
import XCTest
@available(OSX 10.15, *)
final class ReadWriteTests: XCTestCase {
func testCRUD() {
TestDB.perform(with: DataModels.bookLibrary) { pc in
let Author = BookLibrary.Author(
name: "cool author",
age: 50
)
let Book = BookLibrary.Book(
name: "cool book 1",
year: 2010
)
let Review1 = BookLibrary.Book.Review(
text: "wow",
mark: 9
)
let Review2 = BookLibrary.Book.Review(
text: "so much wow",
mark: 10
)
let Cover = BookLibrary.Book.Cover(
url: URL(string: "https://fantlab.ru/work2699")
)
// insert objects
try pc.perform { ctx in
let authorObject = try ctx.insert(Author)
let bookObject = try ctx.insert(Book)
let review1Object = try ctx.insert(Review1)
let review2Object = try ctx.insert(Review2)
let coverObject = try ctx.insert(Cover)
bookObject.reviews.add(review1Object)
bookObject.reviews.add(review2Object)
bookObject.cover = coverObject
authorObject.books.add(bookObject)
}
// read and check
try pc.perform { ctx in
guard let author = try ctx.fetchOne(BookLibrary.Author.all) else {
XCTFail("wtf, no author")
return
}
XCTAssert((try author.decode()) == Author)
let books = Array(author.books)
XCTAssert(books.count == 1)
let book = books[0]
XCTAssert((try book.decode()) == Book)
XCTAssert((try book.author?.decode()) == Author)
XCTAssert((try book.cover?.decode()) == Cover)
XCTAssert((try book.cover?.book?.decode()) == Book)
let reviews = Array(book.reviews)
XCTAssert(reviews.count == 2)
let review1 = reviews[0]
let review2 = reviews[1]
XCTAssert((try review1.decode()) == Review1)
XCTAssert((try review1.book?.decode()) == Book)
XCTAssert((try review2.decode()) == Review2)
XCTAssert((try review2.book?.decode()) == Book)
}
// update
let Review3 = BookLibrary.Book.Review(
text: "trash!",
mark: 1
)
try pc.perform { ctx in
guard let author = try ctx.fetchOne(BookLibrary.Author.all),
let book = Array(author.books).first
else {
return
}
book.author = nil
book.cover = nil
book.reviews.forEach {
book.reviews.remove($0)
ctx.delete($0)
}
try book.reviews.add(ctx.insert(Review3))
}
// read and check
try pc.perform { ctx in
do {
guard let author = try ctx.fetchOne(BookLibrary.Author.all) else {
XCTFail("wtf, no author")
return
}
XCTAssert((try author.decode()) == Author)
let books = Array(author.books)
XCTAssert(books.isEmpty)
}
do {
guard let book = try ctx.fetchOne(BookLibrary.Book.all) else {
XCTFail("wtf, no book")
return
}
XCTAssert((try book.decode()) == Book)
XCTAssert(book.author == nil)
XCTAssert(book.cover == nil)
let reviews = Array(book.reviews)
XCTAssert(reviews.count == 1)
XCTAssert((try reviews[0].decode()) == Review3)
XCTAssert((try reviews[0].book?.decode()) == Book)
}
}
}
}
func testRequestSortLimit() {
TestDB.perform(with: DataModels.bookLibrary) { pc in
try pc.perform(action: { context in
try (1 ... 10).reversed().forEach {
try context.insert(
BookLibrary.Book(
name: "\($0)",
year: $0 % 2 == 0 ? 2010 : 2020
)
)
}
})
let bookNames: [String] = try pc.perform { ctx in
let query = BookLibrary.Book
.all
.sort(\.year)
.sort(\.name)
.limit(8)
.offset(1)
return try ctx.fetch(query, \.name)
}
XCTAssert(bookNames == ["2", "4", "6", "8", "1", "3", "5", "7"])
}
}
func testNotUniqueInsertFail() {
TestDB.perform(with: DataModels.bookLibrary) { pc in
let id = UUID()
var err: Error?
do {
try pc.perform { ctx in
try ctx.insert(BookLibrary.Book(id: id))
try ctx.insert(BookLibrary.Book(id: id))
}
} catch {
err = error
}
XCTAssert(err != nil)
}
}
func testMultiThreadReadWrite() {
TestDB.perform(with: DataModels.bookLibrary) { pc in
DispatchQueue.concurrentPerform(iterations: 100) { number in
do {
try pc.perform { ctx in
try ctx.insert(BookLibrary.Book(year: number))
}
} catch {
XCTFail(error.localizedDescription)
}
}
let bookNames = try pc.perform { ctx in
try ctx.fetch(BookLibrary.Book.all.sort(\.year), \.year)
}
XCTAssert(bookNames == Array(0 ..< 100))
}
}
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
func testMultiTaskReadWrite() async {
await TestDB.schedule(with: DataModels.bookLibrary) { pc in
await withThrowingTaskGroup(of: Void.self) { group in
for number in 0 ..< 100 {
group.addTask {
return try await pc.schedule { ctx in
try ctx.insert(BookLibrary.Book(year: number))
}
}
}
}
let bookNames = try await pc.schedule { ctx in
try ctx.fetch(BookLibrary.Book.all.sort(\.year), \.year)
}
XCTAssert(bookNames == Array(0 ..< 100))
}
}
func testMultiThreadReadWriteRelationsStability() {
TestDB.perform(with: DataModels.bookLibrary, overwriteMergePolicy: true) { pc in
try pc.perform { ctx in
try ctx.insert(BookLibrary.Author(id: .init(), name: "Leo", age: 100))
}
DispatchQueue.concurrentPerform(iterations: 100) { _ in
do {
try pc.perform { ctx in
if let author = try ctx.fetchOne(BookLibrary.Author.all) {
if Bool.random() {
try author.books.add(ctx.insert(BookLibrary.Book()))
} else {
author.books.forEach {
author.books.delete($0, context: ctx)
}
}
}
}
} catch {
XCTFail(error.localizedDescription)
}
}
}
}
}