Skip to content
Newer
Older
100644 210 lines (144 sloc) 5.84 KB
838e3fe @rozza Initial commit
rozza authored May 9, 2011
1 from __future__ import with_statement
2
3 import unittest
4 import datetime
5 import flask
d28c6e2 @rozza Updated app
rozza authored May 17, 2012
6
dc3e73d @rozza Version 3.0
rozza authored May 18, 2012
7 from flask.ext.mongoengine import MongoEngine
8 from flask.ext.mongoengine.wtf import model_form
838e3fe @rozza Initial commit
rozza authored May 9, 2011
9
e2585de @rodcloutier Added test to validate custom query set for ModelSelectField
rodcloutier authored Jan 25, 2012
10 from mongoengine import queryset_manager
469b315 @rodcloutier Added tests for model select fields
rodcloutier authored Feb 7, 2012
11 import wtforms
838e3fe @rozza Initial commit
rozza authored May 9, 2011
12
13
14 def make_todo_model(db):
15 class Todo(db.Document):
16 title = db.StringField(max_length=60)
17 text = db.StringField()
18 done = db.BooleanField(default=False)
19 pub_date = db.DateTimeField(default=datetime.datetime.now)
20 return Todo
21
22
23 class BasicAppTestCase(unittest.TestCase):
24
25 def setUp(self):
26 app = flask.Flask(__name__)
27 app.config['MONGODB_DB'] = 'testing'
28 app.config['TESTING'] = True
4a27986 @rozza Updated to Flask 0.8 imports
rozza authored May 17, 2012
29 db = MongoEngine()
838e3fe @rozza Initial commit
rozza authored May 9, 2011
30 self.Todo = make_todo_model(db)
31
32 db.init_app(app)
33
dc3e73d @rozza Version 3.0
rozza authored May 18, 2012
34 self.Todo.drop_collection()
35
838e3fe @rozza Initial commit
rozza authored May 9, 2011
36 @app.route('/')
37 def index():
38 return '\n'.join(x.title for x in self.Todo.objects)
39
40 @app.route('/add', methods=['POST'])
41 def add():
42 form = flask.request.form
43 todo = self.Todo(title=form['title'],
44 text=form['text'])
45 todo.save()
46 return 'added'
47
48 @app.route('/show/<id>/')
49 def show(id):
3903508 @rozza Updated mongoengine docs and tests
rozza authored May 10, 2011
50 todo = self.Todo.objects.get_or_404(id=id)
51 return '\n'.join([todo.title, todo.text])
838e3fe @rozza Initial commit
rozza authored May 9, 2011
52
53 self.app = app
54 self.db = db
55
56 def test_with_id(self):
57 c = self.app.test_client()
58 resp = c.get('/show/38783728378090/')
59 self.assertEqual(resp.status_code, 404)
ed27f46 @atroche Removed erroneous 'meta' attr from test model
atroche authored Sep 28, 2011
60
3903508 @rozza Updated mongoengine docs and tests
rozza authored May 10, 2011
61 c.post('/add', data={'title': 'First Item', 'text': 'The text'})
ed27f46 @atroche Removed erroneous 'meta' attr from test model
atroche authored Sep 28, 2011
62
e1270bd @rozza Updated typo in tests and setup
rozza authored May 10, 2011
63 resp = c.get('/show/%s/' % self.Todo.objects.first_or_404().id)
3903508 @rozza Updated mongoengine docs and tests
rozza authored May 10, 2011
64 self.assertEqual(resp.status_code, 200)
d28c6e2 @rozza Updated app
rozza authored May 17, 2012
65 self.assertEquals(resp.data, 'First Item\nThe text')
838e3fe @rozza Initial commit
rozza authored May 9, 2011
66
67 def test_basic_insert(self):
68 c = self.app.test_client()
69 c.post('/add', data={'title': 'First Item', 'text': 'The text'})
70 c.post('/add', data={'title': '2nd Item', 'text': 'The text'})
71 rv = c.get('/')
d28c6e2 @rozza Updated app
rozza authored May 17, 2012
72 self.assertEquals(rv.data, 'First Item\n2nd Item')
838e3fe @rozza Initial commit
rozza authored May 9, 2011
73
74 def test_request_context(self):
75 with self.app.test_request_context():
76 todo = self.Todo(title='Test', text='test')
77 todo.save()
78 self.assertEqual(self.Todo.objects.count(), 1)
79
80
81 class WTFormsAppTestCase(unittest.TestCase):
82
83 def setUp(self):
d28c6e2 @rozza Updated app
rozza authored May 17, 2012
84 self.db_name = 'testing'
85
838e3fe @rozza Initial commit
rozza authored May 9, 2011
86 app = flask.Flask(__name__)
d28c6e2 @rozza Updated app
rozza authored May 17, 2012
87 app.config['MONGODB_DB'] = self.db_name
838e3fe @rozza Initial commit
rozza authored May 9, 2011
88 app.config['TESTING'] = True
89 app.config['CSRF_ENABLED'] = False
4a27986 @rozza Updated to Flask 0.8 imports
rozza authored May 17, 2012
90 self.db = MongoEngine()
838e3fe @rozza Initial commit
rozza authored May 9, 2011
91 self.db.init_app(app)
92
93 def tearDown(self):
d28c6e2 @rozza Updated app
rozza authored May 17, 2012
94 self.db.connection.drop_database(self.db_name)
838e3fe @rozza Initial commit
rozza authored May 9, 2011
95
96 def test_model_form(self):
97 db = self.db
98
99 class BlogPost(db.Document):
100 title = db.StringField(required=True, max_length=200)
101 posted = db.DateTimeField(default=datetime.datetime.now)
102 tags = db.ListField(db.StringField(max_length=50))
103
104 class TextPost(BlogPost):
105 content = db.StringField(required=True)
106
107 class LinkPost(BlogPost):
108 url = db.StringField(required=True)
109
83c2f30 @rodcloutier Added field arg option to generate wtforms PasswordField for mongoeng…
rodcloutier authored Feb 12, 2012
110
838e3fe @rozza Initial commit
rozza authored May 9, 2011
111 # Create a text-based post
112 TextPostForm = model_form(TextPost)
113
114 form = TextPostForm(**{
115 'title': 'Using MongoEngine',
116 'tags': ['mongodb', 'mongoengine']})
117
118 self.assertFalse(form.validate())
119
120 form = TextPostForm(**{
121 'title': 'Using MongoEngine',
122 'content': 'See the tutorial',
123 'tags': ['mongodb', 'mongoengine']})
124
125 self.assertTrue(form.validate())
126 form.save()
127
128 self.assertEquals(BlogPost.objects.count(), 1)
e2585de @rodcloutier Added test to validate custom query set for ModelSelectField
rodcloutier authored Jan 25, 2012
129
130
131 def test_model_form_with_custom_query_set(self):
132
133 db = self.db
134
135 class Dog(db.Document):
136 breed = db.StringField()
137
138 @queryset_manager
139 def large_objects(cls, queryset):
140 return queryset(breed__in = ['german sheppard', 'wolfhound'])
141
142 class DogOwner(db.Document):
143 dog = db.ReferenceField(Dog)
144
145 big_dogs = [Dog(breed="german sheppard"), Dog(breed="wolfhound")]
146 dogs = [Dog(breed="poodle")] + big_dogs
147 for dog in dogs:
148 dog.save()
149
150 BigDogForm = model_form(DogOwner, field_args={'dog': {'queryset' : Dog.large_objects} })
151
469b315 @rodcloutier Added tests for model select fields
rodcloutier authored Feb 7, 2012
152 form = BigDogForm(dog=big_dogs[0])
153 self.assertTrue(form.validate())
e2585de @rodcloutier Added test to validate custom query set for ModelSelectField
rodcloutier authored Jan 25, 2012
154 self.assertEqual( big_dogs, [d[1] for d in form.dog.iter_choices()] )
155
f4b998d @rodcloutier Added support for model based multiselect fields
rodcloutier authored Feb 6, 2012
156
469b315 @rodcloutier Added tests for model select fields
rodcloutier authored Feb 7, 2012
157 def test_modelselectfield(self):
158
159 db = self.db
160
161 class Dog(db.Document):
162 name = db.StringField()
163
164 class DogOwner(db.Document):
165 dog = db.ReferenceField(Dog)
166
167 DogOwnerForm = model_form(DogOwner)
168
169 dog = Dog(name="fido")
170 dog.save()
171
172 form = DogOwnerForm(dog=dog)
173 self.assertTrue(form.validate())
174
175 self.assertEqual(wtforms.widgets.Select, type(form.dog.widget))
176 self.assertEqual(False, form.dog.widget.multiple)
177
178
179 def test_modelselectfield_multiple(self):
180
181 db = self.db
182
183 class Dog(db.Document):
184 name = db.StringField()
185
186 class DogOwner(db.Document):
187 dogs = db.ListField(db.ReferenceField(Dog))
188
189 DogOwnerForm = model_form(DogOwner)
190
191 dogs = [Dog(name="fido"), Dog(name="rex")]
192 for dog in dogs:
193 dog.save()
194
195 form = DogOwnerForm(dogs=dogs)
196 self.assertTrue(form.validate())
197
198 self.assertEqual(wtforms.widgets.Select, type(form.dogs.widget))
199 self.assertEqual(True, form.dogs.widget.multiple)
200
83c2f30 @rodcloutier Added field arg option to generate wtforms PasswordField for mongoeng…
rodcloutier authored Feb 13, 2012
201 def test_passwordfield(self):
202 db = self.db
3967c1a @rozza Merge branch 'master' into rod
rozza authored May 17, 2012
203
83c2f30 @rodcloutier Added field arg option to generate wtforms PasswordField for mongoeng…
rodcloutier authored Feb 13, 2012
204 class User(db.Document):
205 password = db.StringField()
206
207 UserForm = model_form(User, field_args = { 'password': {'password' : True} })
208 form = UserForm(password='12345')
209 self.assertEqual(wtforms.widgets.PasswordInput, type(form.password.widget))
Something went wrong with that request. Please try again.