本文整理汇总了Python中flask_peewee.tests.test_app.User类的典型用法代码示例。如果您正苦于以下问题:Python User类的具体用法?Python User怎么用?Python User使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了User类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
def setUp(self):
Note.drop_table(True)
Message.drop_table(True)
User.drop_table(True)
User.create_table()
Message.create_table()
Note.create_table()
self.flask_app = test_app.app
self.flask_app._template_context = {}
self.app = test_app.app.test_client()
示例2: test_get_object_or_404
def test_get_object_or_404(self):
user = self.create_user('test', 'test')
# test with model as first arg
self.assertRaises(NotFound, get_object_or_404, User, username='not-here')
self.assertEqual(user, get_object_or_404(User, username='test'))
# test with query as first arg
active = User.select().where(active=True)
inactive = User.select().where(active=False)
self.assertRaises(NotFound, get_object_or_404, active, username='not-here')
self.assertRaises(NotFound, get_object_or_404, inactive, username='test')
self.assertEqual(user, get_object_or_404(active, username='test'))
示例3: test_model_admin_delete
def test_model_admin_delete(self):
self.create_users()
with self.flask_app.test_client() as c:
self.login(c)
# do a basic get, nothing much going on
resp = c.get('/admin/user/delete/')
self.assertEqual(resp.status_code, 200)
self.assertContext('user', self.admin)
self.assertContext('model_admin', admin._registry[User])
query = self.get_context('query')
self.assertEqual(list(query), [])
# send it a single id
resp = c.get('/admin/user/delete/?id=%d' % (self.normal.id))
self.assertEqual(resp.status_code, 200)
query = self.get_context('query')
self.assertEqual(list(query), [self.normal])
# ensure nothing was deleted
self.assertEqual(User.select().count(), 3)
# post to it, get a redirect on success
resp = c.post('/admin/user/delete/', data={'id': self.normal.id})
self.assertEqual(resp.status_code, 302)
# ensure the user was deleted
self.assertEqual(User.select().count(), 2)
self.assertRaises(User.DoesNotExist, User.get, id=self.normal.id)
self.assertTrue(resp.headers['location'].endswith('/admin/user/'))
# do a multi-delete
resp = c.get('/admin/user/delete/?id=%d&id=%d' % (self.admin.id, self.inactive.id))
self.assertEqual(resp.status_code, 200)
query = self.get_context('query')
self.assertEqual(list(query), [self.admin, self.inactive])
# post to it and check both deleted
resp = c.post('/admin/user/delete/', data={'id': [self.admin.id, self.inactive.id]})
self.assertEqual(resp.status_code, 302)
self.assertEqual(User.select().count(), 0)
示例4: test_model_deserializer
def test_model_deserializer(self):
users = self.create_users()
deserialized = self.md.deserialize_object(
{
"__model__": "User",
"__module__": "flask_peewee.tests.test_app",
"id": self.admin.id,
"username": "admin",
"password": self.admin.password,
"join_date": self.admin.join_date.strftime("%Y-%m-%d %H:%M:%S"),
"active": True,
"admin": True,
}
)
for attr in ["id", "username", "password", "active", "admin"]:
self.assertEqual(getattr(deserialized, attr), getattr(self.admin, attr))
self.assertEqual(
deserialized.join_date.strftime("%Y-%m-%d %H:%M:%S"), self.admin.join_date.strftime("%Y-%m-%d %H:%M:%S")
)
admin_pk = self.admin.id
deserialized = self.md.deserialize_object(
{
"__model__": "User",
"__module__": "flask_peewee.tests.test_app",
"username": "edited",
"active": False,
"admin": False,
},
self.admin,
)
self.assertEqual(deserialized.username, "edited")
self.assertEqual(deserialized.admin, False)
self.assertEqual(deserialized.active, False)
self.assertEqual(deserialized.id, admin_pk)
deserialized.save()
self.assertEqual(User.select().count(), 3)
edited = User.get(username="edited")
self.assertEqual(edited.id, admin_pk)
示例5: test_deserializer
def test_deserializer(self):
users = self.create_users()
deserialized, models = self.d.deserialize_object(User(), {
'id': self.admin.id,
'username': 'admin',
'password': self.admin.password,
'join_date': self.admin.join_date.strftime('%Y-%m-%d %H:%M:%S'),
'active': True,
'admin': True,
})
for attr in ['id', 'username', 'password', 'active', 'admin']:
self.assertEqual(
getattr(deserialized, attr),
getattr(self.admin, attr),
)
self.assertEqual(
deserialized.join_date.strftime('%Y-%m-%d %H:%M:%S'),
self.admin.join_date.strftime('%Y-%m-%d %H:%M:%S'),
)
admin_pk = self.admin.id
deserialized, models = self.d.deserialize_object(self.admin, {
'username': 'edited',
'active': False,
'admin': False,
})
self.assertEqual(deserialized.username, 'edited')
self.assertEqual(deserialized.admin, False)
self.assertEqual(deserialized.active, False)
self.assertEqual(deserialized.id, admin_pk)
deserialized.save()
self.assertEqual(User.select().count(), 3)
edited = User.get(username='edited')
self.assertEqual(edited.id, admin_pk)
示例6: test_delete
def test_delete(self):
self.create_users()
url = '/api/user/%s/' % self.normal.id
# authorized as an admin
resp = self.app.delete(url, headers=self.auth_headers('admin', 'admin'))
self.assertEqual(resp.status_code, 200)
self.assertEqual(User.select().count(), 2)
resp_json = self.response_json(resp)
self.assertEqual(resp_json, {'deleted': 1})
示例7: test_deserializer
def test_deserializer(self):
users = self.create_users()
deserialized, models = self.d.deserialize_object(
User(),
{
"id": self.admin.id,
"username": "admin",
"password": self.admin.password,
"join_date": self.admin.join_date.strftime("%Y-%m-%d %H:%M:%S"),
"active": True,
"admin": True,
},
)
for attr in ["id", "username", "password", "active", "admin"]:
self.assertEqual(getattr(deserialized, attr), getattr(self.admin, attr))
self.assertEqual(
deserialized.join_date.strftime("%Y-%m-%d %H:%M:%S"), self.admin.join_date.strftime("%Y-%m-%d %H:%M:%S")
)
admin_pk = self.admin.id
deserialized, models = self.d.deserialize_object(
self.admin, {"username": "edited", "active": False, "admin": False}
)
self.assertEqual(deserialized.username, "edited")
self.assertEqual(deserialized.admin, False)
self.assertEqual(deserialized.active, False)
self.assertEqual(deserialized.id, admin_pk)
deserialized.save()
self.assertEqual(User.select().count(), 3)
edited = User.get(username="edited")
self.assertEqual(edited.id, admin_pk)
示例8: test_edit
def test_edit(self):
self.create_users()
user_data = {'username': 'edited'}
serialized = json.dumps(user_data)
url = '/api/user/%s/' % self.normal.id
# authorized as an admin
resp = self.app.put(url, data=serialized, headers=self.auth_headers('admin', 'admin'))
self.assertEqual(resp.status_code, 200)
user = User.get(id=self.normal.id)
self.assertEqual(user.username, 'edited')
resp_json = self.response_json(resp)
self.assertAPIUser(resp_json, user)
示例9: test_create
def test_create(self):
self.create_users()
new_pass = make_password('test')
user_data = {'username': 'test', 'password': new_pass, 'email': ''}
serialized = json.dumps(user_data)
# authorized as an admin
resp = self.app.post('/api/user/', data=serialized, headers=self.auth_headers('admin', 'admin'))
self.assertEqual(resp.status_code, 200)
new_user = User.get(username='test')
self.assertTrue(check_password('test', new_user.password))
resp_json = self.response_json(resp)
self.assertAPIUser(resp_json, new_user)
示例10: test_model_admin_recursive_delete
def test_model_admin_recursive_delete(self):
self.create_users()
m1 = Message.create(user=self.normal, content='test1')
m2 = Message.create(user=self.normal, content='test2')
m3 = Message.create(user=self.admin, content='test3')
n1 = Note.create(user=self.normal, message='test1')
n2 = Note.create(user=self.normal, message='test2')
n3 = Note.create(user=self.admin, message='test3')
a1 = AModel.create(a_field='a1')
a2 = AModel.create(a_field='a2')
b1 = BModel.create(b_field='b1', a=a1)
b2 = BModel.create(b_field='b2', a=a2)
bd1= BDetails.create(b=b1)
bd2= BDetails.create(b=b2)
c1 = CModel.create(c_field='c1', b=b1)
c2 = CModel.create(c_field='c2', b=b2)
d1 = DModel.create(d_field='d1', c=c1)
d2 = DModel.create(d_field='d2', c=c2)
with self.flask_app.test_client() as c:
self.login(c)
resp = c.get('/admin/amodel/delete/?id=%d' % (a1.id))
self.assertEqual(resp.status_code, 200)
collected = self.get_context('collected')
self.assertEqual(collected, {
a1.id: [
(1, BModel, 'a', [b1]),
(2, BDetails, 'a', [bd1]),
(2, CModel, 'a', [c1]),
(3, DModel, 'a', [d1]),
]
})
resp = c.post('/admin/amodel/delete/', data={'id': a1.id})
self.assertEqual(resp.status_code, 302)
self.assertEqual(AModel.select().count(), 1)
self.assertEqual(BModel.select().count(), 1)
self.assertEqual(BDetails.select().count(), 1)
self.assertEqual(CModel.select().count(), 1)
self.assertEqual(DModel.select().count(), 1)
# send it a single id
resp = c.get('/admin/user/delete/?id=%d' % (self.normal.id))
self.assertEqual(resp.status_code, 200)
query = self.get_context('query')
self.assertEqual(list(query), [self.normal])
collected = self.get_context('collected')
self.assertEqual(len(collected), 1)
u_k = collected[self.normal.id]
self.assertEqual(len(u_k), 2)
self.assertEqual(u_k, [
(1, Message, 'user', [m1, m2]),
(1, Note, 'user', [n1, n2]),
])
# post to it, get a redirect on success
resp = c.post('/admin/user/delete/', data={'id': self.normal.id})
self.assertEqual(resp.status_code, 302)
self.assertEqual(User.select().count(), 2)
self.assertEqual(Message.select().count(), 1)
self.assertEqual(Note.select().count(), 1)
resp = c.get('/admin/user/delete/?id=%d&id=%d' % (self.admin.id, self.inactive.id))
self.assertEqual(resp.status_code, 200)
collected = self.get_context('collected')
self.assertEqual(len(collected), 2)
u_k = collected[self.admin.id]
self.assertEqual(len(u_k), 2)
self.assertEqual(u_k, [
(1, Message, 'user', [m3]),
(1, Note, 'user', [n3]),
])
u_k = collected[self.inactive.id]
self.assertEqual(len(u_k), 0)
# post to it, get a redirect on success
resp = c.post('/admin/user/delete/', data={'id': [self.admin.id, self.inactive.id]})
self.assertEqual(resp.status_code, 302)
self.assertEqual(User.select().count(), 0)
self.assertEqual(Message.select().count(), 0)
self.assertEqual(Note.select().count(), 0)
示例11: test_model_admin_edit
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',
})
#.........这里部分代码省略.........
示例12: test_model_admin_add
def test_model_admin_add(self):
self.create_users()
self.assertEqual(User.select().count(), 3)
with self.flask_app.test_client() as c:
self.login(c)
# the add url returns a 200
resp = c.get('/admin/user/add/')
self.assertEqual(resp.status_code, 200)
# ensure 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',
])
# make an incomplete post and get a 200 with errors
resp = c.post('/admin/user/add/', data={
'username': '',
'password': 'xxx',
'active': '1',
'email': '',
'join_date': '2011-01-01 00:00:00',
})
self.assertEqual(resp.status_code, 200)
# no new user created
self.assertEqual(User.select().count(), 3)
# check the form for errors
frm = self.get_context('form')
self.assertEqual(frm.errors, {
'username': ['This field is required.'],
'email': ['This field is required.'],
})
# make a complete post and get a 302 to the edit page
resp = c.post('/admin/user/add/', data={
'username': 'new',
'password': 'new',
'active': '1',
'email': '[email protected]',
'join_date': '2011-01-01 00:00:00',
})
self.assertEqual(resp.status_code, 302)
# new user was created
self.assertEqual(User.select().count(), 4)
# check they have the correct data on the new instance
user = User.get(username='new')
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(check_password('new', user.password))
# check the redirect was correct
self.assertTrue(resp.headers['location'].endswith('/admin/user/%d/' % user.id))
示例13: test_filtering
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))
示例14: create_user
def create_user(self, username, password, **kwargs):
user = User(username=username, email=kwargs.pop('email', ''), **kwargs)
user.set_password(password)
user.save()
return user