Merge lp:~3v1n0/u1db-qt/objects-subfields-list-all into lp:u1db-qt

Proposed by Marco Trevisan (Treviño)
Status: Merged
Approved by: Cris Dywan
Approved revision: 132
Merged at revision: 130
Proposed branch: lp:~3v1n0/u1db-qt/objects-subfields-list-all
Merge into: lp:u1db-qt
Prerequisite: lp:~3v1n0/u1db-qt/uri-path-parsing
Diff against target: 157 lines (+120/-2)
3 files modified
examples/u1db-qt-example-7/u1db-qt-example-7.qml (+75/-0)
src/query.cpp (+4/-2)
tests/test-database.cpp (+41/-0)
To merge this branch: bzr merge lp:~3v1n0/u1db-qt/objects-subfields-list-all
Reviewer Review Type Date Requested Status
Cris Dywan Approve
PS Jenkins bot continuous-integration Approve
Review via email: mp+252059@code.launchpad.net

Commit message

Query: allow adding more than one result for each doc, allowing indexing subfields of objects in a list

This is to be conformant to what the u1db specs (implemented in python) say.

Description of the change

Indexing subfields of ojects in a list doesn't work as expected in u1db-qt, we only list the first element and not everyone as defined in docs [1].

The reason was that we didn't add a result if we already had one for that doc... And this seems wrong to me.

In a next MP we should probably also fix the "Name a list" case (see [1] again).
As test-case you can also try [2].

[1] https://pythonhosted.org/u1db/high-level-api.html#index-expressions
[2] http://bazaar.launchpad.net/~ahayzen/+junk/test-u1db-playlists/view/head:/test-u1db-playlists.qml

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Cris Dywan (kalikiana) wrote :

This turns out to be a surprising explanation to the related bug. This makes sense.

I might feel that a QML unit test is potentially simpler, but no strong feelings.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added directory 'examples/u1db-qt-example-7'
2=== added file 'examples/u1db-qt-example-7/u1db-qt-example-7.qml'
3--- examples/u1db-qt-example-7/u1db-qt-example-7.qml 1970-01-01 00:00:00 +0000
4+++ examples/u1db-qt-example-7/u1db-qt-example-7.qml 2015-03-06 05:17:53 +0000
5@@ -0,0 +1,75 @@
6+/*
7+ * Copyright (C) 2015 Canonical, Ltd.
8+ *
9+ * Authors:
10+ * Marco Trevisan <marco.trevisan@canonical.com>
11+ *
12+ * This program is free software; you can redistribute it and/or modify
13+ * it under the terms of the GNU Lesser General Public License as published by
14+ * the Free Software Foundation; version 3.
15+ *
16+ * This program is distributed in the hope that it will be useful,
17+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+ * GNU Lesser General Public License for more details.
20+ *
21+ * You should have received a copy of the GNU Lesser General Public License
22+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
23+ */
24+
25+import QtQuick 2.0
26+import U1db 1.0 as U1db
27+import Ubuntu.Components 0.1
28+
29+MainView {
30+ width: units.gu(45)
31+ height: units.gu(80)
32+
33+ U1db.Database {
34+ id: aDatabase
35+ }
36+
37+ U1db.Document {
38+ database: aDatabase
39+ docId: "department"
40+ contents: {"department": "department of redundancy department",
41+ "managers": [
42+ {"name": "Mary", "phone_number": "12345"},
43+ {"name": "Katherine"},
44+ {"name": "Rob", "phone_number": "54321"}
45+ ]
46+ }
47+ }
48+
49+ U1db.Index{
50+ database: aDatabase
51+ id: by_phonenumber
52+ expression: ["managers.phone_number"]
53+ }
54+
55+ U1db.Query{
56+ id: aQuery
57+ index: by_phonenumber
58+ }
59+
60+ Tabs {
61+ id: tabs
62+
63+ Tab {
64+ title: i18n.tr("Hello U1Db!")
65+
66+ page: Page {
67+ id: helloPage
68+
69+ ListView {
70+ anchors.fill: parent
71+ model: aQuery
72+ delegate: Text {
73+ text: "(" + index + ") " + JSON.stringify(contents)
74+ }
75+ }
76+ }
77+ }
78+ }
79+}
80+
81
82=== modified file 'src/query.cpp'
83--- src/query.cpp 2014-03-13 19:31:58 +0000
84+++ src/query.cpp 2015-03-06 05:17:53 +0000
85@@ -172,9 +172,11 @@
86
87 if(match == true){
88 // Results must be unique and not empty aka deleted
89- if (!m_documents.contains(docId) && result_variant.isValid())
90+ if (result_variant.isValid())
91 {
92- m_documents.append(docId);
93+ if (!m_documents.contains(docId))
94+ m_documents.append(docId);
95+
96 m_results.append(result);
97 }
98 }
99
100=== modified file 'tests/test-database.cpp'
101--- tests/test-database.cpp 2015-03-06 05:17:53 +0000
102+++ tests/test-database.cpp 2015-03-06 05:17:53 +0000
103@@ -21,6 +21,7 @@
104 #include <QObject>
105
106 #include "database.h"
107+#include "document.h"
108 #include "index.h"
109 #include "query.h"
110
111@@ -110,6 +111,46 @@
112 query.setQuery(QStringList() << "2014*" << "basketball" << "linux");
113 }
114
115+ void testSingleDocumentQuery()
116+ {
117+ const char * json = "{\"department\": \"department of redundancy department\"," \
118+ " \"managers\": [" \
119+ " {\"name\": \"Mary\", \"phone_number\": \"12345\"}," \
120+ " {\"name\": \"Katherine\"}," \
121+ " {\"name\": \"Rob\", \"phone_number\": [\"54321\"]}" \
122+ " ]" \
123+ "}";
124+ Database db;
125+ Document doc;
126+ doc.setDocId("department");
127+ doc.setDatabase(&db);
128+ doc.setContents(QJsonDocument::fromJson(QByteArray(json)).toVariant());
129+
130+ Index index;
131+ index.setDatabase(&db);
132+ index.setName("by-phone-number");
133+ index.setExpression(QStringList() << "managers.phone_number");
134+
135+ Query query;
136+ query.setIndex(&index);
137+
138+ QCOMPARE(query.getDocuments().size(), 1);
139+ QCOMPARE(query.getDocuments().front(), QString("department"));
140+
141+ QList<QVariant> expected_numbers;
142+ Q_FOREACH (QVariant manager, doc.getContents().toMap()["managers"].toList())
143+ {
144+ QVariantMap man = manager.toMap();
145+ if (man.keys().contains("phone_number"))
146+ {
147+ man.remove("name");
148+ expected_numbers.append(man);
149+ }
150+ }
151+
152+ QCOMPARE(query.getResults(), expected_numbers);
153+ }
154+
155 void cleanupTestCase()
156 {
157 }

Subscribers

People subscribed via source and target branches

to all changes: