-
-
Notifications
You must be signed in to change notification settings - Fork 731
/
Copy pathRowAdapterTests.swift
61 lines (49 loc) · 2.55 KB
/
RowAdapterTests.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
import XCTest
import GRDB
class RowAdapterTests: GRDBTestCase {
func testRowAdapter() {
let dbQueue = DatabaseQueue()
dbQueue.inDatabase { db in
let adapter = RowAdapter(
// Main mapping
mapping: ["id": "fooid", "val": "fooval"],
subrows: [
// Alternate mapping
"foo": ["id": "barid", "val": "barval"],
// Other mapping which references a column that does not exist.
// I still don't know if this should be supported. If not, we should
// fatalError very early.
"odd": ["id": "missingid"]])
let row = Row.fetchOne(db, "SELECT 1 AS fooid, 'foo' AS fooval, 2 as barid, 'bar' AS barval", adapter: adapter)!
// # Row values
XCTAssertEqual(row.count, 2)
XCTAssertEqual(row.value(named: "id") as Int, 1)
XCTAssertEqual(row.value(named: "val") as String, "foo")
XCTAssertEqual(row.databaseValue(named: "id"), 1.databaseValue)
XCTAssertEqual(row.databaseValue(named: "val"), "foo".databaseValue)
XCTAssertTrue(row.value(named: "barid") == nil)
XCTAssertTrue(row.value(named: "missing") == nil)
XCTAssertTrue(row.hasColumn("id"))
XCTAssertTrue(row.hasColumn("val"))
XCTAssertFalse(row.hasColumn("barid"))
XCTAssertFalse(row.hasColumn("missing"))
// TODO: test row.value(atIndex: 0) and row.value(atIndex: 1)
// TODO: test for (key, value) in row { ... }
// # Row equality
// One of this tests is failing - can't know which one because dictionaries are unordered.
// TODO: think about that.
let altRow1 = Row.fetchOne(db, "SELECT 1 AS id, 'foo' AS val")!
XCTAssertEqual(row, altRow1)
let altRow2 = Row.fetchOne(db, "SELECT 'foo' AS val, 1 AS id")!
XCTAssertEqual(row, altRow2)
// # Subrows
let row2 = row.subrows["foo"]!
XCTAssertEqual(row2.count, 2)
XCTAssertEqual(row2.value(named: "id") as Int, 2)
XCTAssertEqual(row2.value(named: "val") as String, "bar")
let row3 = row.subrows["odd"]!
XCTAssertEqual(row3.count, 1)
XCTAssertTrue(row3.value(named: "id") == nil) // TODO: is that what we want? Should we crash instead?
}
}
}