forked from MongoEngine/flask-mongoengine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
234 lines (169 loc) · 6.97 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
from __future__ import with_statement
import unittest
import datetime
import flask
from flask.ext.mongoengine import MongoEngine
from flask.ext.mongoengine.wtf import model_form
from mongoengine import queryset_manager
import wtforms
def make_todo_model(db):
class Todo(db.Document):
title = db.StringField(max_length=60)
text = db.StringField()
done = db.BooleanField(default=False)
pub_date = db.DateTimeField(default=datetime.datetime.now)
return Todo
class BasicAppTestCase(unittest.TestCase):
def setUp(self):
app = flask.Flask(__name__)
app.config['MONGODB_DB'] = 'testing'
app.config['TESTING'] = True
db = MongoEngine()
self.Todo = make_todo_model(db)
db.init_app(app)
self.Todo.drop_collection()
@app.route('/')
def index():
return '\n'.join(x.title for x in self.Todo.objects)
@app.route('/add', methods=['POST'])
def add():
form = flask.request.form
todo = self.Todo(title=form['title'],
text=form['text'])
todo.save()
return 'added'
@app.route('/show/<id>/')
def show(id):
todo = self.Todo.objects.get_or_404(id=id)
return '\n'.join([todo.title, todo.text])
self.app = app
self.db = db
def test_connection_kwargs(self):
app = flask.Flask(__name__)
app.config['MONGODB_SETTINGS'] = {
'DB': 'testing_tz_aware',
'alias': 'tz_aware_true',
'TZ_AWARE': True,
}
app.config['TESTING'] = True
db = MongoEngine()
db.init_app(app)
self.assertTrue(db.connection.tz_aware)
app.config['MONGODB_SETTINGS'] = {
'DB': 'testing',
'alias': 'tz_aware_false',
}
db.init_app(app)
self.assertFalse(db.connection.tz_aware)
def test_with_id(self):
c = self.app.test_client()
resp = c.get('/show/38783728378090/')
self.assertEqual(resp.status_code, 404)
c.post('/add', data={'title': 'First Item', 'text': 'The text'})
resp = c.get('/show/%s/' % self.Todo.objects.first_or_404().id)
self.assertEqual(resp.status_code, 200)
self.assertEquals(resp.data, 'First Item\nThe text')
def test_basic_insert(self):
c = self.app.test_client()
c.post('/add', data={'title': 'First Item', 'text': 'The text'})
c.post('/add', data={'title': '2nd Item', 'text': 'The text'})
rv = c.get('/')
self.assertEquals(rv.data, 'First Item\n2nd Item')
def test_request_context(self):
with self.app.test_request_context():
todo = self.Todo(title='Test', text='test')
todo.save()
self.assertEqual(self.Todo.objects.count(), 1)
class WTFormsAppTestCase(unittest.TestCase):
def setUp(self):
self.db_name = 'testing'
app = flask.Flask(__name__)
app.config['MONGODB_DB'] = self.db_name
app.config['TESTING'] = True
app.config['CSRF_ENABLED'] = False
self.app = app
self.db = MongoEngine()
self.db.init_app(app)
def tearDown(self):
self.db.connection.drop_database(self.db_name)
def test_model_form(self):
with self.app.test_request_context('/'):
db = self.db
class BlogPost(db.Document):
title = db.StringField(required=True, max_length=200)
posted = db.DateTimeField(default=datetime.datetime.now)
tags = db.ListField(db.StringField(max_length=50))
class TextPost(BlogPost):
content = db.StringField(required=True)
class LinkPost(BlogPost):
url = db.StringField(required=True)
# Create a text-based post
TextPostForm = model_form(TextPost)
form = TextPostForm(**{
'title': 'Using MongoEngine',
'tags': ['mongodb', 'mongoengine']})
self.assertFalse(form.validate())
form = TextPostForm(**{
'title': 'Using MongoEngine',
'content': 'See the tutorial',
'tags': ['mongodb', 'mongoengine']})
self.assertTrue(form.validate())
form.save()
self.assertEquals(BlogPost.objects.count(), 1)
def test_model_form_with_custom_query_set(self):
with self.app.test_request_context('/'):
db = self.db
class Dog(db.Document):
breed = db.StringField()
@queryset_manager
def large_objects(cls, queryset):
return queryset(breed__in = ['german sheppard', 'wolfhound'])
class DogOwner(db.Document):
dog = db.ReferenceField(Dog)
big_dogs = [Dog(breed="german sheppard"), Dog(breed="wolfhound")]
dogs = [Dog(breed="poodle")] + big_dogs
for dog in dogs:
dog.save()
BigDogForm = model_form(DogOwner, field_args={'dog': {'queryset' : Dog.large_objects} })
form = BigDogForm(dog=big_dogs[0])
self.assertTrue(form.validate())
self.assertEqual( big_dogs, [d[1] for d in form.dog.iter_choices()] )
def test_modelselectfield(self):
with self.app.test_request_context('/'):
db = self.db
class Dog(db.Document):
name = db.StringField()
class DogOwner(db.Document):
dog = db.ReferenceField(Dog)
DogOwnerForm = model_form(DogOwner)
dog = Dog(name="fido")
dog.save()
form = DogOwnerForm(dog=dog)
self.assertTrue(form.validate())
self.assertEqual(wtforms.widgets.Select, type(form.dog.widget))
self.assertEqual(False, form.dog.widget.multiple)
def test_modelselectfield_multiple(self):
with self.app.test_request_context('/'):
db = self.db
class Dog(db.Document):
name = db.StringField()
class DogOwner(db.Document):
dogs = db.ListField(db.ReferenceField(Dog))
DogOwnerForm = model_form(DogOwner)
dogs = [Dog(name="fido"), Dog(name="rex")]
for dog in dogs:
dog.save()
form = DogOwnerForm(dogs=dogs)
self.assertTrue(form.validate())
self.assertEqual(wtforms.widgets.Select, type(form.dogs.widget))
self.assertEqual(True, form.dogs.widget.multiple)
def test_passwordfield(self):
with self.app.test_request_context('/'):
db = self.db
class User(db.Document):
password = db.StringField()
UserForm = model_form(User, field_args = { 'password': {'password' : True} })
form = UserForm(password='12345')
self.assertEqual(wtforms.widgets.PasswordInput, type(form.password.widget))
if __name__ == '__main__':
unittest.main()