summaryrefslogtreecommitdiff
path: root/src/mango/test/13-users-db-find-test.py
blob: 73d15ea1a35609391db1016992631c71918762c2 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
# -*- coding: latin-1 -*-
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.


import mango, requests


class UsersDbFindTests(mango.UsersDbTests):
    def test_simple_find(self):
        docs = self.db.find({"name": {"$eq": "demo02"}})
        assert len(docs) == 1
        assert docs[0]["_id"] == "org.couchdb.user:demo02"

    def test_multi_cond_and(self):
        self.db.create_index(["type", "roles"])
        docs = self.db.find({"type": "user", "roles": {"$eq": ["reader"]}})
        assert len(docs) == 1
        assert docs[0]["_id"] == "org.couchdb.user:demo02"

    def test_multi_cond_or(self):
        docs = self.db.find(
            {"$and": [{"type": "user"}, {"$or": [{"order": 1}, {"order": 3}]}]}
        )
        assert len(docs) == 2
        assert docs[0]["_id"] == "org.couchdb.user:demo01"
        assert docs[1]["_id"] == "org.couchdb.user:demo03"

    def test_sort(self):
        self.db.create_index(["order", "name"])
        selector = {"name": {"$gt": "demo01"}}
        docs1 = self.db.find(selector, sort=[{"order": "asc"}])
        docs2 = list(sorted(docs1, key=lambda d: d["order"]))
        assert docs1 is not docs2 and docs1 == docs2

        docs1 = self.db.find(selector, sort=[{"order": "desc"}])
        docs2 = list(reversed(sorted(docs1, key=lambda d: d["order"])))
        assert docs1 is not docs2 and docs1 == docs2

    def test_fields(self):
        selector = {"name": {"$eq": "demo02"}}
        docs = self.db.find(selector, fields=["name", "order"])
        assert len(docs) == 1
        assert sorted(docs[0].keys()) == ["name", "order"]

    def test_empty(self):
        docs = self.db.find({})
        assert len(docs) == 3


class UsersDbIndexFindTests(UsersDbFindTests):
    def setUp(self):
        self.db.create_index(["name"])

    def test_multi_cond_and(self):
        self.db.create_index(["type", "roles"])
        super(UsersDbIndexFindTests, self).test_multi_cond_and()

    def test_multi_cond_or(self):
        self.db.create_index(["type", "order"])
        super(UsersDbIndexFindTests, self).test_multi_cond_or()

    def test_sort(self):
        self.db.create_index(["order", "name"])
        super(UsersDbIndexFindTests, self).test_sort()