forked from pointfreeco/sqlite-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssertQueryTests.swift
More file actions
180 lines (171 loc) · 5.06 KB
/
AssertQueryTests.swift
File metadata and controls
180 lines (171 loc) · 5.06 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
import DependenciesTestSupport
import Foundation
import SQLiteData
import SQLiteDataTestSupport
import SnapshotTesting
import Testing
@MainActor
@Suite(
.dependency(\.defaultDatabase, try .database()),
.snapshots(record: .failed),
)
struct AssertQueryTests {
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func assertQueryBasic() throws {
assertQuery(
Record.all.select(\.id)
) {
"""
┌───┐
│ 1 │
│ 2 │
│ 3 │
└───┘
"""
}
}
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func assertQueryRecord() throws {
assertQuery(
Record.where { $0.id == 1 }
) {
"""
┌────────────────────────────────────────┐
│ Record( │
│ id: 1, │
│ date: Date(1970-01-01T00:00:42.000Z) │
│ ) │
└────────────────────────────────────────┘
"""
}
}
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func assertQueryBasicUpdate() throws {
assertQuery(
Record.all
.update { $0.date = Date(timeIntervalSince1970: 45) }
.returning { ($0.id, $0.date) }
) {
"""
┌───┬────────────────────────────────┐
│ 1 │ Date(1970-01-01T00:00:45.000Z) │
│ 2 │ Date(1970-01-01T00:00:45.000Z) │
│ 3 │ Date(1970-01-01T00:00:45.000Z) │
└───┴────────────────────────────────┘
"""
}
}
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func assertQueryRecordUpdate() throws {
assertQuery(
Record
.where { $0.id == 1 }
.update { $0.date = Date(timeIntervalSince1970: 45) }
.returning(\.self)
) {
"""
┌────────────────────────────────────────┐
│ Record( │
│ id: 1, │
│ date: Date(1970-01-01T00:00:45.000Z) │
│ ) │
└────────────────────────────────────────┘
"""
}
}
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func assertQueryEmpty() throws {
assertQuery(
Record.all.where { $0.id == -1 }.select(\.id)
) {
"""
(No results)
"""
}
}
@Test(.snapshots(record: .never))
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
func assertQueryFailsNoResultsNonEmptySnapshot() {
withKnownIssue {
assertQuery(
Record.all.where { _ in false }
) {
"""
XYZ
"""
}
}
}
#if DEBUG
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func assertQueryBasicIncludeSQL() throws {
assertQuery(
includeSQL: true,
Record.all.select(\.id)
) {
"""
SELECT "records"."id"
FROM "records"
"""
} results: {
"""
┌───┐
│ 1 │
│ 2 │
│ 3 │
└───┘
"""
}
}
#endif
#if DEBUG
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Test func assertQueryRecordIncludeSQL() throws {
assertQuery(
includeSQL: true,
Record.where { $0.id == 1 }
) {
"""
SELECT "records"."id", "records"."date"
FROM "records"
WHERE ("records"."id") = (1)
"""
} results: {
"""
┌────────────────────────────────────────┐
│ Record( │
│ id: 1, │
│ date: Date(1970-01-01T00:00:42.000Z) │
│ ) │
└────────────────────────────────────────┘
"""
}
}
#endif
}
@Table
private struct Record: Equatable {
let id: Int
@Column(as: Date.UnixTimeRepresentation.self)
var date = Date(timeIntervalSince1970: 42)
}
extension DatabaseWriter where Self == DatabaseQueue {
fileprivate static func database() throws -> DatabaseQueue {
let database = try DatabaseQueue()
try database.write { db in
try #sql(
"""
CREATE TABLE "records" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"date" INTEGER NOT NULL DEFAULT 42
)
"""
)
.execute(db)
for _ in 1...3 {
_ = try Record.insert { Record.Draft() }.execute(db)
}
}
return database
}
}