~ubuntu-branches/ubuntu/wily/u1db-qt/wily

« back to all changes in this revision

Viewing changes to tests/tst_database.qml

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2013-08-07 10:28:13 UTC
  • Revision ID: package-import@ubuntu.com-20130807102813-uk4un76xcsoy3n12
Tags: upstream-0.1.5+13.10.20130807
Import upstream version 0.1.5+13.10.20130807

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 Canonical, Ltd.
 
3
 *
 
4
 * Authors:
 
5
 *  Christian Dywan <christian.dywan@canonical.com>
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU Lesser General Public License as published by
 
9
 * the Free Software Foundation; version 3.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU Lesser General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Lesser General Public License
 
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
import QtQuick 2.0
 
21
import QtTest 1.0
 
22
import U1db 1.0 as U1db
 
23
 
 
24
Item {
 
25
    width: 200; height: 200
 
26
 
 
27
    U1db.Database {
 
28
        id: myDatabase
 
29
        path: "aDatabaseB"
 
30
        property bool first_row_loaded: false
 
31
        property bool last_row_loaded: false
 
32
        onDocLoaded: {
 
33
            if (path == 'aDatabaseC' && docId == 'dl0')
 
34
                first_row_loaded = true
 
35
            if (path == 'aDatabaseC' && docId == 'dl99')
 
36
                last_row_loaded = true
 
37
        }
 
38
    }
 
39
 
 
40
    U1db.Document {
 
41
        id: myDocument
 
42
        database: myDatabase
 
43
        docId: 'qwertzui'
 
44
        defaults: { "eggs": "spam" }
 
45
    }
 
46
 
 
47
    U1db.Document {
 
48
        id: otherDocument
 
49
        database: myDatabase
 
50
        docId: 'shallow'
 
51
        create: true
 
52
        defaults: { "eggs": "spam" }
 
53
    }
 
54
 
 
55
    ListView {
 
56
        id: myList
 
57
        model: myDatabase
 
58
        width: 200; height: 200
 
59
        delegate: Text {
 
60
            x: 66; y: 77
 
61
            text: "otherDelegate index:%1".arg(index)
 
62
        }
 
63
    }
 
64
 
 
65
TestCase {
 
66
    name: "U1dbDatabase"
 
67
    when: windowShown
 
68
 
 
69
    function test_0_documentCreate () {
 
70
        compare(myDatabase.getDoc(otherDocument.docId), otherDocument.defaults)
 
71
    }
 
72
 
 
73
    function test_1_databasePopulated () {
 
74
        spyListCompleted.wait()
 
75
        compare(myDatabase.putDoc({"animals": ["cat", "dog", "hamster"]}) > -1, true)
 
76
 
 
77
        var myPath = "aDatabaseA"
 
78
        myDatabase.path = myPath
 
79
        spyPathChanged.wait()
 
80
        compare(myDatabase.path, myPath)
 
81
        compare(myDatabase.putDoc({"spam": "eggs"}) > -1, true)
 
82
        var json = {"foo": "bar"}
 
83
        compare(myDatabase.putDoc(json, "hijklmn") > -1, true)
 
84
        compare(myDatabase.getDoc("hijklmn"), json)
 
85
        compare(myDatabase.getDoc("hijklmn"), json)
 
86
    }
 
87
 
 
88
    function test_2_databaseError () {
 
89
        /* FIXME: unicode in Qt console output doesn't work inside dpkg
 
90
        ignoreWarning('u1db: Invalid docID 日本語')
 
91
        myDatabase.putDoc({"": ""}, "日本語")
 
92
        spyErrorChanged.wait()
 
93
        compare(myDatabase.error.indexOf("Invalid docID") > -1, true)
 
94
         */
 
95
    }
 
96
 
 
97
    function test_3_documentContents () {
 
98
        var json = {"content": {"notetext": "Lorem ipsum"}}
 
99
        myDatabase.putDoc(json, "qwertzui")
 
100
        myDocument.docId = ''
 
101
        compare(myDocument.contents, undefined)
 
102
        myDocument.docId = 'qwertzui'
 
103
        compare(myDocument.contents, json)
 
104
        compare(myDocument.contents.content.notetext, 'Lorem ipsum')
 
105
 
 
106
        var path = myDatabase.path
 
107
        myDatabase.path = ':memory:'
 
108
        myDatabase.path = path
 
109
        spyContentsChanged.wait()
 
110
    }
 
111
 
 
112
    function test_4_putIndex () {
 
113
        myDatabase.putIndex("by-phone-number", ["managers.phone_number"])
 
114
        compare(myDatabase.getIndexExpressions('by-phone-number'), ["managers.phone_number"])
 
115
        myDatabase.putDoc({ 'managers': [
 
116
            { 'name': 'Mary', 'phone_number': '12345' },
 
117
            { 'name': 'Rob', 'phone_number': '54321' },
 
118
            ] })
 
119
        // FIXME compare(myDatabase.getIndexKeys('by-phone-number'), ['12345', '54321'])
 
120
    }
 
121
 
 
122
    function test_6_fillDocument () {
 
123
        var path = "aDatabaseC"
 
124
        myDatabase.path = path
 
125
        spyPathChanged.wait()
 
126
        for (var i = 0; i < 100; i++)
 
127
            myDatabase.putDoc({'foo': 'bar'} ,'dl' + Number(i).toLocaleString())
 
128
        myDatabase.path = ":memory:"
 
129
        spyPathChanged.wait()
 
130
        compare(myDatabase.listDocs(), [])
 
131
        compare(myList.count, 0)
 
132
        myDatabase.first_row_loaded = false
 
133
        myDatabase.last_row_loaded = false
 
134
        myDatabase.path = path
 
135
        spyPathChanged.wait()
 
136
        compare(myList.count, 100)
 
137
        spyDocLoaded.wait()
 
138
        // FIXME compare(myDatabase.first_row_loaded, true)
 
139
        // FIXME compare(myDatabase.last_row_loaded, false)
 
140
    }
 
141
 
 
142
    SignalSpy {
 
143
        id: spyPathChanged
 
144
        target: myDatabase
 
145
        signalName: "pathChanged"
 
146
    }
 
147
 
 
148
    SignalSpy {
 
149
        id: spyContentsChanged
 
150
        target: myDocument
 
151
        signalName: "contentsChanged"
 
152
    }
 
153
 
 
154
    SignalSpy {
 
155
        id: spyErrorChanged
 
156
        target: myDatabase
 
157
        signalName: "errorChanged"
 
158
    }
 
159
 
 
160
    SignalSpy {
 
161
        id: spyListCompleted
 
162
        target: myDatabase.Component
 
163
        signalName: "completed"
 
164
    }
 
165
 
 
166
    SignalSpy {
 
167
        id: spyDocLoaded
 
168
        target: myDatabase
 
169
        signalName: "docLoaded"
 
170
    }
 
171
} }
 
172