本文整理汇总了Python中flask_peewee.tests.test_app.User.filter方法的典型用法代码示例。如果您正苦于以下问题:Python User.filter方法的具体用法?Python User.filter怎么用?Python User.filter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类flask_peewee.tests.test_app.User
的用法示例。
在下文中一共展示了User.filter方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_model_admin_edit
# 需要导入模块: from flask_peewee.tests.test_app import User [as 别名]
# 或者: from flask_peewee.tests.test_app.User import filter [as 别名]
def test_model_admin_edit(self):
users = self.create_users()
self.assertEqual(User.select().count(), 3)
# grab an id so we can test a 404 on non-existent user
unused_id = [x for x in range(1, 5) if not User.filter(id=x).exists()][0]
with self.flask_app.test_client() as c:
self.login(c)
# nonexistant user 404s
resp = c.get('/admin/user/%d/' % unused_id)
self.assertEqual(resp.status_code, 404)
# edit page returns a 200
resp = c.get('/admin/user/%d/' % self.normal.id)
self.assertEqual(resp.status_code, 200)
# check the user, model_admin and form are correct in the context
self.assertContext('user', self.admin)
self.assertContext('model_admin', admin._registry[User])
self.assertTrue('form' in self.flask_app._template_context)
frm = self.flask_app._template_context['form']
self.assertEqual(sorted(frm._fields.keys()), [
'active',
'admin',
'email',
'join_date',
'password',
'username',
])
# check the form pulled the right data off the model
self.assertEqual(frm.data, {
'username': 'normal',
'password': frm.password.data, # skip this
'email': '',
'admin': False,
'active': True,
'join_date': frm.join_date.data, # microseconds...bleh
})
# make an incomplete post to update the user and get a 200 w/errors
resp = c.post('/admin/user/%d/' % self.normal.id, data={
'username': '',
'password': '',
'active': '1',
'email': '[email protected]',
'join_date': '2011-01-01 00:00:00',
})
self.assertEqual(resp.status_code, 200)
# no new user created
self.assertEqual(User.select().count(), 3)
# refresh database content
normal = User.get(id=self.normal.id)
self.assertEqual(normal.username, 'normal') # was not saved
# check the form for errors
frm = self.get_context('form')
self.assertEqual(frm.errors, {
'username': ['This field is required.'],
'password': ['This field is required.'],
})
# make a complete post
resp = c.post('/admin/user/%d/' % self.normal.id, data={
'username': 'edited',
'password': 'edited',
'active': '1',
'email': '[email protected]',
'join_date': '2011-01-01 00:00:00',
})
self.assertEqual(resp.status_code, 302)
# no new user was created
self.assertEqual(User.select().count(), 3)
# grab from the database
user = User.get(username='edited')
self.assertEqual(user.id, self.normal.id) # it is the same user
self.assertTrue(check_password('edited', user.password))
self.assertEqual(user.active, True)
self.assertEqual(user.admin, False)
self.assertEqual(user.email, '[email protected]')
self.assertEqual(user.join_date, datetime.datetime(2011, 1, 1))
self.assertTrue(resp.headers['location'].endswith('/admin/user/%d/' % user.id))
# make another post without modifying the password, should stay same
resp = c.post('/admin/user/%d/' % user.id, data={
'username': 'edited2',
'password': user.password,
'active': '1',
'email': '[email protected]',
'join_date': '2011-01-01 00:00:00',
})
#.........这里部分代码省略.........
示例2: test_filtering
# 需要导入模块: from flask_peewee.tests.test_app import User [as 别名]
# 或者: from flask_peewee.tests.test_app.User import filter [as 别名]
def test_filtering(self):
users, notes = self.get_users_and_notes()
# do a simple filter on a related model
resp = self.app.get('/api/note/?user=%s&ordering=id' % self.normal.id)
resp_json = self.response_json(resp)
self.assertAPIMeta(resp_json, {
'model': 'note',
'previous': '',
'next': '',
'page': 1,
})
self.assertAPINotes(resp_json, self.normal.note_set.order_by(Note.id))
# do a filter following a join
resp = self.app.get('/api/note/?user__username=admin&ordering=id')
resp_json = self.response_json(resp)
self.assertAPIMeta(resp_json, {
'model': 'note',
'previous': '',
'next': '',
'page': 1,
})
self.assertAPINotes(resp_json, self.admin.note_set.order_by(Note.id))
# filter multiple fields
notes = list(self.admin.note_set.order_by(Note.id))
third_id = notes[3].id
resp = self.app.get('/api/note/?user__username=admin&id__lt=%s&ordering=id' % third_id)
resp_json = self.response_json(resp)
self.assertAPINotes(resp_json, notes[:3])
# do a filter using multiple values
resp = self.app.get('/api/note/?user__username=admin&user__username=inactive&ordering=id')
resp_json = self.response_json(resp)
self.assertAPIMeta(resp_json, {
'model': 'note',
'previous': '',
'next': '',
'page': 1,
})
self.assertAPINotes(resp_json, Note.filter(user__in=[self.admin, self.inactive]).order_by(Note.id))
# do a filter with a negation
resp = self.app.get('/api/note/?-user__username=admin&ordering=id')
resp_json = self.response_json(resp)
self.assertAPINotes(resp_json, Note.filter(user__in=[
self.normal, self.inactive]).order_by(Note.id))
# do a filter with an IN operator and multiple IDs
# https://github.com/coleifer/flask-peewee/issues/112
resp = self.app.get('/api/note/?id__in=1,2,5')
resp_json = self.response_json(resp)
self.assertAPINotes(resp_json, Note.filter(id__in=[1,2,5]).order_by(Note.id))
# also test that the IN operator works with list of strings
resp = self.app.get('/api/user/?username__in=admin,normal')
resp_json = self.response_json(resp)
self.assertAPIUsers(resp_json, User.filter(username__in=['admin', 'normal']).order_by(User.id))