summaryrefslogtreecommitdiff
path: root/src/leap/soledad/tests/u1db_tests/test_document.py
blob: e706e1a973255b1905bf1169ea2d69d3ad460b9f (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Copyright 2011 Canonical Ltd.
#
# This file is part of u1db.
#
# u1db is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation.
#
# u1db is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with u1db.  If not, see <http://www.gnu.org/licenses/>.


from u1db import errors

from leap.soledad.tests import u1db_tests as tests


class TestDocument(tests.TestCase):

    scenarios = ([(
        'py', {'make_document_for_test': tests.make_document_for_test})])  # +
        #tests.C_DATABASE_SCENARIOS)

    def test_create_doc(self):
        doc = self.make_document('doc-id', 'uid:1', tests.simple_doc)
        self.assertEqual('doc-id', doc.doc_id)
        self.assertEqual('uid:1', doc.rev)
        self.assertEqual(tests.simple_doc, doc.get_json())
        self.assertFalse(doc.has_conflicts)

    def test__repr__(self):
        doc = self.make_document('doc-id', 'uid:1', tests.simple_doc)
        self.assertEqual(
            '%s(doc-id, uid:1, \'{"key": "value"}\')'
            % (doc.__class__.__name__,),
            repr(doc))

    def test__repr__conflicted(self):
        doc = self.make_document('doc-id', 'uid:1', tests.simple_doc,
                                 has_conflicts=True)
        self.assertEqual(
            '%s(doc-id, uid:1, conflicted, \'{"key": "value"}\')'
            % (doc.__class__.__name__,),
            repr(doc))

    def test__lt__(self):
        doc_a = self.make_document('a', 'b', '{}')
        doc_b = self.make_document('b', 'b', '{}')
        self.assertTrue(doc_a < doc_b)
        self.assertTrue(doc_b > doc_a)
        doc_aa = self.make_document('a', 'a', '{}')
        self.assertTrue(doc_aa < doc_a)

    def test__eq__(self):
        doc_a = self.make_document('a', 'b', '{}')
        doc_b = self.make_document('a', 'b', '{}')
        self.assertTrue(doc_a == doc_b)
        doc_b = self.make_document('a', 'b', '{}', has_conflicts=True)
        self.assertFalse(doc_a == doc_b)

    def test_non_json_dict(self):
        self.assertRaises(
            errors.InvalidJSON, self.make_document, 'id', 'uid:1',
            '"not a json dictionary"')

    def test_non_json(self):
        self.assertRaises(
            errors.InvalidJSON, self.make_document, 'id', 'uid:1',
            'not a json dictionary')

    def test_get_size(self):
        doc_a = self.make_document('a', 'b', '{"some": "content"}')
        self.assertEqual(
            len('a' + 'b' + '{"some": "content"}'), doc_a.get_size())

    def test_get_size_empty_document(self):
        doc_a = self.make_document('a', 'b', None)
        self.assertEqual(len('a' + 'b'), doc_a.get_size())


class TestPyDocument(tests.TestCase):

    scenarios = ([(
        'py', {'make_document_for_test': tests.make_document_for_test})])

    def test_get_content(self):
        doc = self.make_document('id', 'rev', '{"content":""}')
        self.assertEqual({"content": ""}, doc.content)
        doc.set_json('{"content": "new"}')
        self.assertEqual({"content": "new"}, doc.content)

    def test_set_content(self):
        doc = self.make_document('id', 'rev', '{"content":""}')
        doc.content = {"content": "new"}
        self.assertEqual('{"content": "new"}', doc.get_json())

    def test_set_bad_content(self):
        doc = self.make_document('id', 'rev', '{"content":""}')
        self.assertRaises(
            errors.InvalidContent, setattr, doc, 'content',
            '{"content": "new"}')

    def test_is_tombstone(self):
        doc_a = self.make_document('a', 'b', '{}')
        self.assertFalse(doc_a.is_tombstone())
        doc_a.set_json(None)
        self.assertTrue(doc_a.is_tombstone())

    def test_make_tombstone(self):
        doc_a = self.make_document('a', 'b', '{}')
        self.assertFalse(doc_a.is_tombstone())
        doc_a.make_tombstone()
        self.assertTrue(doc_a.is_tombstone())

    def test_same_content_as(self):
        doc_a = self.make_document('a', 'b', '{}')
        doc_b = self.make_document('d', 'e', '{}')
        self.assertTrue(doc_a.same_content_as(doc_b))
        doc_b = self.make_document('p', 'q', '{}', has_conflicts=True)
        self.assertTrue(doc_a.same_content_as(doc_b))
        doc_b.content['key'] = 'value'
        self.assertFalse(doc_a.same_content_as(doc_b))

    def test_same_content_as_json_order(self):
        doc_a = self.make_document(
            'a', 'b', '{"key1": "val1", "key2": "val2"}')
        doc_b = self.make_document(
            'c', 'd', '{"key2": "val2", "key1": "val1"}')
        self.assertTrue(doc_a.same_content_as(doc_b))

    def test_set_json(self):
        doc = self.make_document('id', 'rev', '{"content":""}')
        doc.set_json('{"content": "new"}')
        self.assertEqual('{"content": "new"}', doc.get_json())

    def test_set_json_non_dict(self):
        doc = self.make_document('id', 'rev', '{"content":""}')
        self.assertRaises(errors.InvalidJSON, doc.set_json, '"is not a dict"')

    def test_set_json_error(self):
        doc = self.make_document('id', 'rev', '{"content":""}')
        self.assertRaises(errors.InvalidJSON, doc.set_json, 'is not json')


load_tests = tests.load_with_scenarios