Debian Codegraph @

maz-db/tests/qml/tst_queries.qml

raw
1 import QtQuick 2.4
2 import QtQuick.Window 2.1
3 import QtTest 1.0
4 import MazDB 1.0
5
6 TestCase {
7 id: test
8 name: "MazDB::Queries"
9 property string source: Qt.resolvedUrl("~/.maztest.db")
10
11 MazDB {
12 id: db
13 }
14
15 MazDBQueryEngine {
16 id: query
17 query: MazDBQuery {
18 definition: [
19 WHERE {
20 id: where
21 },
22 OR {
23 id: or
24 },
25 AND {
26 id: and
27 }
28 ]
29 }
30 }
31
32 function initTestCase() {
33 db.source = test.source
34 query.source = test.source
35 }
36
37 function cleanupTestCase() {
38 query.close()
39 db.destroyDB(test.source)
40 }
41
42
43 function cleanup() {
44 where.property = ""
45 where.value = undefined
46 where.comparitor = undefined
47 or.property = ""
48 or.value = undefined
49 or.comparitor = undefined
50 and.property = ""
51 and.value = undefined
52 and.comparitor = undefined
53 db.del("data")
54 }
55
56 readonly property var dataMap: {
57 "a" : { "b" : {"c" : "hello"} },
58 "d" : { "e" : {"f" : "world"} }
59 }
60
61 function test_object_paths() {
62 compare(db.putSync("data", dataMap), true)
63 where.property = "a.b.c"
64 where.value = "hello"
65 and.property = "d.e.f"
66 and.value = "world"
67
68 var count = 0
69 query.queryStream(function(key, value) {
70 compare(key, "data")
71 compare(value.a.b.c, "hello")
72 ++count
73 return true;
74 })
75 verify(count === 1)
76 }
77
78 function test_where_or() {
79 compare(db.putSync("data", "foo"), true)
80 compare(db.putSync("data1", "bar"), true)
81
82 where.value = "foo"
83 or.value = "bar"
84
85 var count = 0
86 query.queryStream(function(key, value) {
87 ++count
88 return true;
89 })
90 verify(count, 2)
91 db.del("data1")
92 }
93
94 function test_custom_comparitor() {
95 compare(db.putSync("data", "working"), true)
96 where.comparitor = Qt.binding(customComparitor)
97 var count = 0
98 query.queryStream(function(key, value) {
99 ++count
100 return true;
101 })
102 verify(count, 1)
103 }
104
105 function customComparitor(value) {
106 return value === "working"
107 }
108
109 }