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
=== added directory 'examples/u1db-qt-example-7'
=== added file 'examples/u1db-qt-example-7/u1db-qt-example-7.qml'
--- examples/u1db-qt-example-7/u1db-qt-example-7.qml 1970-01-01 00:00:00 +0000
+++ examples/u1db-qt-example-7/u1db-qt-example-7.qml 2015-03-06 05:17:53 +0000
@@ -0,0 +1,75 @@
1/*
2 * Copyright (C) 2015 Canonical, Ltd.
3 *
4 * Authors:
5 * Marco Trevisan <marco.trevisan@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
20import QtQuick 2.0
21import U1db 1.0 as U1db
22import Ubuntu.Components 0.1
23
24MainView {
25 width: units.gu(45)
26 height: units.gu(80)
27
28 U1db.Database {
29 id: aDatabase
30 }
31
32 U1db.Document {
33 database: aDatabase
34 docId: "department"
35 contents: {"department": "department of redundancy department",
36 "managers": [
37 {"name": "Mary", "phone_number": "12345"},
38 {"name": "Katherine"},
39 {"name": "Rob", "phone_number": "54321"}
40 ]
41 }
42 }
43
44 U1db.Index{
45 database: aDatabase
46 id: by_phonenumber
47 expression: ["managers.phone_number"]
48 }
49
50 U1db.Query{
51 id: aQuery
52 index: by_phonenumber
53 }
54
55 Tabs {
56 id: tabs
57
58 Tab {
59 title: i18n.tr("Hello U1Db!")
60
61 page: Page {
62 id: helloPage
63
64 ListView {
65 anchors.fill: parent
66 model: aQuery
67 delegate: Text {
68 text: "(" + index + ") " + JSON.stringify(contents)
69 }
70 }
71 }
72 }
73 }
74}
75
076
=== modified file 'src/query.cpp'
--- src/query.cpp 2014-03-13 19:31:58 +0000
+++ src/query.cpp 2015-03-06 05:17:53 +0000
@@ -172,9 +172,11 @@
172172
173 if(match == true){173 if(match == true){
174 // Results must be unique and not empty aka deleted174 // Results must be unique and not empty aka deleted
175 if (!m_documents.contains(docId) && result_variant.isValid())175 if (result_variant.isValid())
176 {176 {
177 m_documents.append(docId);177 if (!m_documents.contains(docId))
178 m_documents.append(docId);
179
178 m_results.append(result);180 m_results.append(result);
179 }181 }
180 }182 }
181183
=== modified file 'tests/test-database.cpp'
--- tests/test-database.cpp 2015-03-06 05:17:53 +0000
+++ tests/test-database.cpp 2015-03-06 05:17:53 +0000
@@ -21,6 +21,7 @@
21#include <QObject>21#include <QObject>
2222
23#include "database.h"23#include "database.h"
24#include "document.h"
24#include "index.h"25#include "index.h"
25#include "query.h"26#include "query.h"
2627
@@ -110,6 +111,46 @@
110 query.setQuery(QStringList() << "2014*" << "basketball" << "linux");111 query.setQuery(QStringList() << "2014*" << "basketball" << "linux");
111 }112 }
112113
114 void testSingleDocumentQuery()
115 {
116 const char * json = "{\"department\": \"department of redundancy department\"," \
117 " \"managers\": [" \
118 " {\"name\": \"Mary\", \"phone_number\": \"12345\"}," \
119 " {\"name\": \"Katherine\"}," \
120 " {\"name\": \"Rob\", \"phone_number\": [\"54321\"]}" \
121 " ]" \
122 "}";
123 Database db;
124 Document doc;
125 doc.setDocId("department");
126 doc.setDatabase(&db);
127 doc.setContents(QJsonDocument::fromJson(QByteArray(json)).toVariant());
128
129 Index index;
130 index.setDatabase(&db);
131 index.setName("by-phone-number");
132 index.setExpression(QStringList() << "managers.phone_number");
133
134 Query query;
135 query.setIndex(&index);
136
137 QCOMPARE(query.getDocuments().size(), 1);
138 QCOMPARE(query.getDocuments().front(), QString("department"));
139
140 QList<QVariant> expected_numbers;
141 Q_FOREACH (QVariant manager, doc.getContents().toMap()["managers"].toList())
142 {
143 QVariantMap man = manager.toMap();
144 if (man.keys().contains("phone_number"))
145 {
146 man.remove("name");
147 expected_numbers.append(man);
148 }
149 }
150
151 QCOMPARE(query.getResults(), expected_numbers);
152 }
153
113 void cleanupTestCase()154 void cleanupTestCase()
114 {155 {
115 }156 }

Subscribers

People subscribed via source and target branches

to all changes: