/
test_document.py
217 lines (150 loc) · 6.34 KB
/
test_document.py
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import os
import unittest
from pdb import set_trace
from pymongo import DESCENDING, ASCENDING
from pymongo.connection import Connection
from pymongo.database import Database
from mongobongo.document import Document, get_doc_class_for_collection
from mongobongo.attributed import AttributedDict
def get_connection():
host = os.environ.get("DB_IP", "localhost")
port = int(os.environ.get("DB_PORT", 27017))
return Connection(host, port)
class TestDoc(Document):
collection = 'test_docs'
def get_url(self):
return 'http://svetlyak.ru'
class OrderedDoc(Document):
collection = 'test_docs'
class Meta:
ordering = [('user', ASCENDING)]
class Documents(unittest.TestCase):
def setUp(self):
db = Database(get_connection(), "pymongo_test")
TestDoc.objects.db = db
TestDoc.objects.remove()
def test_save_and_find(self):
TestDoc(author = 'art', tags = ['test', 'python']).save()
TestDoc(author = 'vasily', tags = ['test', 'django']).save()
TestDoc.objects.ensure_index([('author', ASCENDING)])
docs = TestDoc.objects.all()
self.assertEqual(2, len(docs))
docs = list(docs)
self.assertEqual('art', docs[0].author)
self.assertEqual('vasily', docs[1].author)
self.assertEqual('art', TestDoc.objects.find({'tags': 'python'})[0].author)
def test_save_returns_object_with_id(self):
doc = TestDoc(author = 'art')
self.assertEqual(None, doc._id)
self.assertEqual(doc, doc.save())
self.assert_(doc._id)
def test_custom_method(self):
doc = TestDoc(author = 'art')
self.assertEqual('http://svetlyak.ru', doc.get_url())
def test_find_one(self):
TestDoc(author = 'art', tags = ['test', 'python']).save()
TestDoc(author = 'vasily', tags = ['test', 'django']).save()
doc = TestDoc.objects.find_one({'author': 'art'})
self.assertEqual('art', doc.author)
def test_update(self):
doc = TestDoc(author = 'art')
doc.update({'tag': 'test'})
self.assertEqual('test', doc.tag)
def test_find_one_can_return_none(self):
doc = TestDoc.objects.find_one({'author': 'unknown'})
self.assertEqual(None, doc)
def test_doc_can_be_subscriptable(self):
doc = TestDoc(__kwargs = {'the-title': 'Just a Poem'})
self.assertEqual('Just a Poem', doc['the-title'])
def test_access_to_inner_attributes(self):
doc = TestDoc(author = dict(
name = 'Alexander',
nick = 'svetlyak40wt',
address = dict(
country = 'Russia',
city = 'Moscow')
))
self.assertEqual('Alexander', doc.author.name)
self.assertEqual('Russia', doc.author.address.country)
self.assertEqual(dict(country = 'Russia', city = 'Moscow'), doc.author.address)
def test_change_attributes(self):
doc = TestDoc()
doc.author = 'Alexander'
doc.article = {}
doc.article.title = 'Just a Poem'
doc.article.tags = ['one', 'two']
doc.save()
doc = TestDoc.objects.find_one()
self.assertEqual('Alexander', doc.author)
self.assertEqual(dict(title = 'Just a Poem', tags = ['one', 'two']), doc.article)
def test_dict_proxy(self):
d = dict(title = 'Just an Example', tags = ['test', 'python'])
dp = AttributedDict(d)
dp.author = dict(name = 'Alexander')
dp.author.lastname = 'Artemenko'
self.assertEqual('Alexander', d['author']['name'])
self.assertEqual('Artemenko', d['author']['lastname'])
del dp.author.lastname
self.assertEqual(dict(name='Alexander'), dp.author)
def test_sort_documents(self):
TestDoc(user = 'vasily').save()
TestDoc(user = 'alex').save()
TestDoc(user = 'zuger').save()
TestDoc(user = 'olga').save()
TestDoc.objects.ensure_index([('user', ASCENDING)])
docs = list(TestDoc.objects.all().sort([('user', ASCENDING)])[:2])
self.assertEqual(2, len(docs))
self.assertEqual('alex', docs[0].user)
self.assertEqual('olga', docs[1].user)
def test_default_ordering_for_iterator(self):
names = ['vasily', 'alex', 'zuger', 'olga']
for name in names:
OrderedDoc(user = name).save()
for name, doc in zip(sorted(names), OrderedDoc.objects.all()):
self.assertEqual(name, doc.user)
def test_default_ordering_for_single_items(self):
names = ['vasily', 'alex', 'zuger', 'olga']
for name in names:
OrderedDoc(user = name).save()
names = sorted(names)
objects = OrderedDoc.objects.all()
for i in range(len(names)):
self.assertEqual(names[i], objects[i].user)
def test_remove(self):
TestDoc(user = 'vasily').save()
alex = TestDoc(user = 'alex').save()
self.assertEqual(2, TestDoc.objects.count())
alex.remove()
self.assertEqual(1, TestDoc.objects.count())
def test_default_ordering_for_find_one(self):
names = ['vasily', 'alex', 'zuger', 'olga']
for name in names:
OrderedDoc(user = name).save()
self.assertEqual('alex', OrderedDoc.objects.find_one().user)
class Article(Document):
collection = 'articles'
class Author(Document):
collection = 'authors'
class References(unittest.TestCase):
def setUp(self):
db = Database(get_connection(), "pymongo_test")
Article.objects.db = db
Article.objects.remove()
Author.objects.remove()
def testDocClassAutoregistration(self):
self.assertEqual(Article, get_doc_class_for_collection('articles'))
self.assertEqual(Author, get_doc_class_for_collection('authors'))
def testReferenceObject(self):
author = Author(name = 'Alexander')
author.save()
article = Article(title = 'Life is miracle', author = author)
article.save()
article = Article.objects.find_one()
self.assertEqual('Alexander', article.author.name)
self.assertEqual(Author, type(article.author))
def testSaveReferenceObjectWithParent(self):
author = Author(name = 'Alexander')
article = Article(title = 'Life is miracle', author = author)
self.assert_(author._id is None)
article.save()
self.assert_(author._id is not None)