forked from memeticlabs/flask-mongokit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
149 lines (114 loc) · 4.88 KB
/
tests.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
# -*- coding: utf-8 -*-
import unittest
import os
from datetime import datetime
from flask import Flask
from flaskext.mongokit import MongoKit, BSONObjectIdConverter, \
Document, Collection
from werkzeug.exceptions import BadRequest, NotFound
from bson import ObjectId
class BlogPost(Document):
__collection__ = "posts"
structure = {
'title': unicode,
'body': unicode,
'author': unicode,
'date_creation': datetime,
'rank': int,
'tags': [unicode],
}
required_fields = ['title', 'author', 'date_creation']
default_values = {'rank': 0, 'date_creation': datetime.utcnow}
use_dot_notation = True
class TestCase(unittest.TestCase):
def setUp(self):
self.app = Flask(__name__)
self.app.config['TESTING'] = True
self.app.config['MONGODB_DATABASE'] = 'flask_testing'
maybe_conf_file = os.path.join(os.getcwd(), "config_test.cfg")
if os.path.exists(maybe_conf_file):
self.app.config.from_pyfile(maybe_conf_file)
self.db = MongoKit(self.app)
self.ctx = self.app.test_request_context('/')
self.ctx.push()
def tearDown(self):
self.ctx.pop()
def test_initialization(self):
self.db.register([BlogPost])
assert len(self.db.registered_documents) > 0
assert self.db.registered_documents[0] == BlogPost
assert isinstance(self.db, MongoKit)
assert self.db.name == self.app.config['MONGODB_DATABASE']
assert isinstance(self.db.test, Collection)
def test_decorator_registration(self):
@self.db.register
class DecoratorRegistered(Document):
pass
assert len(self.db.registered_documents) > 0
assert self.db.registered_documents[0] == DecoratorRegistered
def test_property_connected(self):
assert not self.db.connected
self.db.connect()
assert self.db.connected
self.db.disconnect()
assert not self.db.connected
def test_bson_object_id_converter(self):
converter = BSONObjectIdConverter("/")
self.assertRaises(BadRequest, converter.to_python, ("132"))
assert converter.to_python("4e4ac5cfffc84958fa1f45fb") == \
ObjectId("4e4ac5cfffc84958fa1f45fb")
assert converter.to_url(ObjectId("4e4ac5cfffc84958fa1f45fb")) == \
"4e4ac5cfffc84958fa1f45fb"
def test_save_and_find_document(self):
self.db.register([BlogPost])
assert len(self.db.registered_documents) > 0
assert self.db.registered_documents[0] == BlogPost
post = self.db.BlogPost()
post.title = u"Flask-MongoKit"
post.body = u"Flask-MongoKit is a layer between Flask and MongoKit"
post.author = u"Christoph Heer"
post.save()
assert self.db.BlogPost.find().count() > 0
rec_post = self.db.BlogPost.find_one({'title': u"Flask-MongoKit"})
assert rec_post.title == post.title
assert rec_post.body == rec_post.body
assert rec_post.author == rec_post.author
def test_get_or_404(self):
self.db.register([BlogPost])
assert len(self.db.registered_documents) > 0
assert self.db.registered_documents[0] == BlogPost
post = self.db.BlogPost()
post.title = u"Flask-MongoKit"
post.body = u"Flask-MongoKit is a layer between Flask and MongoKit"
post.author = u"Christoph Heer"
post.save()
assert self.db.BlogPost.find().count() > 0
assert "get_or_404" in dir(self.db.BlogPost)
try:
self.db.BlogPost.get_or_404(post['_id'])
except NotFound:
self.fail("There should be a document with this id")
self.assertRaises(NotFound, self.db.BlogPost.get_or_404, ObjectId())
def test_find_one_or_404(self):
self.db.register([BlogPost])
assert len(self.db.registered_documents) > 0
assert self.db.registered_documents[0] == BlogPost
post = self.db.BlogPost()
post.title = u"Flask-MongoKit"
post.body = u"Flask-MongoKit is a layer between Flask and MongoKit"
post.author = u"Christoph Heer"
post.save()
assert self.db.BlogPost.find().count() > 0
assert "find_one_or_404" in dir(self.db.BlogPost)
try:
self.db.BlogPost.find_one_or_404({'title': u'Flask-MongoKit'})
except NotFound:
self.fail("There should be a document with this title")
self.assertRaises(NotFound, self.db.BlogPost.find_one_or_404,
{'title': u'Flask is great'})
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestCase))
return suite
if __name__ == '__main__':
unittest.main()