本文整理匯總了Python中rest_framework.tests.models.Person類的典型用法代碼示例。如果您正苦於以下問題:Python Person類的具體用法?Python Person怎麽用?Python Person使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Person類的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: setUp
def setUp(self):
self.comment = Comment("[email protected]", "Happy new year!", datetime.datetime(2012, 1, 1))
self.data = {
"email": "[email protected]",
"content": "Happy new year!",
"created": datetime.datetime(2012, 1, 1),
"sub_comment": "This wont change",
}
self.expected = {
"email": "[email protected]",
"content": "Happy new year!",
"created": datetime.datetime(2012, 1, 1),
"sub_comment": "And Merry Christmas!",
}
self.person_data = {"name": "dwight", "age": 35}
self.person = Person(**self.person_data)
self.person.save()
示例2: setUp
def setUp(self):
self.comment = Comment(
'[email protected]',
'Happy new year!',
datetime.datetime(2012, 1, 1)
)
self.data = {
'email': '[email protected]',
'content': 'Happy new year!',
'created': datetime.datetime(2012, 1, 1),
'sub_comment': 'This wont change'
}
self.expected = {
'email': '[email protected]',
'content': 'Happy new year!',
'created': '2012-01-01T00:00:00',
'sub_comment': 'And Merry Christmas!'
}
self.person_data = {'name': 'dwight', 'age': 35}
self.person = Person(**self.person_data)
self.person.save()
示例3: BasicTests
class BasicTests(TestCase):
def setUp(self):
self.comment = Comment(
'[email protected]',
'Happy new year!',
datetime.datetime(2012, 1, 1)
)
self.data = {
'email': '[email protected]',
'content': 'Happy new year!',
'created': datetime.datetime(2012, 1, 1),
'sub_comment': 'This wont change'
}
self.expected = {
'email': '[email protected]',
'content': 'Happy new year!',
'created': '2012-01-01T00:00:00',
'sub_comment': 'And Merry Christmas!'
}
self.person_data = {'name': 'dwight', 'age': 35}
self.person = Person(**self.person_data)
self.person.save()
def test_empty(self):
serializer = CommentSerializer()
expected = {
'email': '',
'content': '',
'created': None,
'sub_comment': ''
}
self.assertEqual(serializer.data, expected)
def test_retrieve(self):
serializer = CommentSerializer(self.comment)
self.assertEqual(serializer.data, self.expected)
def test_create(self):
serializer = CommentSerializer(data=self.data)
expected = self.comment
self.assertEqual(serializer.is_valid(), True)
self.assertEqual(serializer.object, expected)
self.assertFalse(serializer.object is expected)
self.assertEqual(serializer.data['sub_comment'], 'And Merry Christmas!')
def test_update(self):
serializer = CommentSerializer(self.comment, data=self.data)
expected = self.comment
self.assertEqual(serializer.is_valid(), True)
self.assertEqual(serializer.object, expected)
self.assertTrue(serializer.object is expected)
self.assertEqual(serializer.data['sub_comment'], 'And Merry Christmas!')
def test_partial_update(self):
msg = 'Merry New Year!'
partial_data = {'content': msg}
serializer = CommentSerializer(self.comment, data=partial_data)
self.assertEqual(serializer.is_valid(), False)
serializer = CommentSerializer(self.comment, data=partial_data, partial=True)
expected = self.comment
self.assertEqual(serializer.is_valid(), True)
self.assertEqual(serializer.object, expected)
self.assertTrue(serializer.object is expected)
self.assertEqual(serializer.data['content'], msg)
def test_model_fields_as_expected(self):
"""
Make sure that the fields returned are the same as defined
in the Meta data
"""
serializer = PersonSerializer(self.person)
self.assertEqual(set(serializer.data.keys()),
set(['name', 'age', 'info']))
def test_field_with_dictionary(self):
"""
Make sure that dictionaries from fields are left intact
"""
serializer = PersonSerializer(self.person)
expected = self.person_data
self.assertEqual(serializer.data['info'], expected)
def test_read_only_fields(self):
"""
Attempting to update fields set as read_only should have no effect.
"""
serializer = PersonSerializer(self.person, data={'name': 'dwight', 'age': 99})
self.assertEqual(serializer.is_valid(), True)
instance = serializer.save()
self.assertEqual(serializer.errors, {})
# Assert age is unchanged (35)
self.assertEqual(instance.age, self.person_data['age'])
示例4: BasicTests
class BasicTests(TestCase):
def setUp(self):
self.comment = Comment(
'[email protected]',
'Happy new year!',
datetime.datetime(2012, 1, 1)
)
self.data = {
'email': '[email protected]',
'content': 'Happy new year!',
'created': datetime.datetime(2012, 1, 1),
'sub_comment': 'This wont change'
}
self.expected = {
'email': '[email protected]',
'content': 'Happy new year!',
'created': datetime.datetime(2012, 1, 1),
'sub_comment': 'And Merry Christmas!'
}
self.person_data = {'name': 'dwight', 'age': 35}
self.person = Person(**self.person_data)
self.person.save()
def test_empty(self):
serializer = CommentSerializer()
expected = {
'email': '',
'content': '',
'created': None,
'sub_comment': ''
}
self.assertEquals(serializer.data, expected)
def test_retrieve(self):
serializer = CommentSerializer(self.comment)
self.assertEquals(serializer.data, self.expected)
def test_create(self):
serializer = CommentSerializer(data=self.data)
expected = self.comment
self.assertEquals(serializer.is_valid(), True)
self.assertEquals(serializer.object, expected)
self.assertFalse(serializer.object is expected)
self.assertEquals(serializer.data['sub_comment'], 'And Merry Christmas!')
def test_update(self):
serializer = CommentSerializer(self.comment, data=self.data)
expected = self.comment
self.assertEquals(serializer.is_valid(), True)
self.assertEquals(serializer.object, expected)
self.assertTrue(serializer.object is expected)
self.assertEquals(serializer.data['sub_comment'], 'And Merry Christmas!')
def test_model_fields_as_expected(self):
""" Make sure that the fields returned are the same as defined
in the Meta data
"""
serializer = PersonSerializer(self.person)
self.assertEquals(set(serializer.data.keys()),
set(['name', 'age', 'info']))
def test_field_with_dictionary(self):
""" Make sure that dictionaries from fields are left intact
"""
serializer = PersonSerializer(self.person)
expected = self.person_data
self.assertEquals(serializer.data['info'], expected)
示例5: BasicTests
class BasicTests(TestCase):
def setUp(self):
self.comment = Comment(
'[email protected]',
'Happy new year!',
datetime.datetime(2012, 1, 1)
)
self.data = {
'email': '[email protected]',
'content': 'Happy new year!',
'created': datetime.datetime(2012, 1, 1),
'sub_comment': 'This wont change'
}
self.expected = {
'email': '[email protected]',
'content': 'Happy new year!',
'created': datetime.datetime(2012, 1, 1),
'sub_comment': 'And Merry Christmas!'
}
self.person_data = {'name': 'dwight', 'age': 35}
self.person = Person(**self.person_data)
self.person.save()
def test_empty(self):
serializer = CommentSerializer()
expected = {
'email': '',
'content': '',
'created': None,
'sub_comment': ''
}
self.assertEqual(serializer.data, expected)
def test_retrieve(self):
serializer = CommentSerializer(self.comment)
self.assertEqual(serializer.data, self.expected)
def test_create(self):
serializer = CommentSerializer(data=self.data)
expected = self.comment
self.assertEqual(serializer.is_valid(), True)
self.assertEqual(serializer.object, expected)
self.assertFalse(serializer.object is expected)
self.assertEqual(serializer.data['sub_comment'], 'And Merry Christmas!')
def test_create_nested(self):
"""Test a serializer with nested data."""
names = {'first': 'John', 'last': 'Doe', 'initials': 'jd'}
data = {'ssn': '1234567890', 'names': names}
serializer = PersonIdentifierSerializer(data=data)
self.assertEqual(serializer.is_valid(), True)
self.assertEqual(serializer.object, data)
self.assertFalse(serializer.object is data)
self.assertEqual(serializer.data['names'], names)
def test_create_partial_nested(self):
"""Test a serializer with nested data which has missing fields."""
names = {'first': 'John'}
data = {'ssn': '1234567890', 'names': names}
serializer = PersonIdentifierSerializer(data=data)
expected_names = {'first': 'John', 'last': '', 'initials': ''}
data['names'] = expected_names
self.assertEqual(serializer.is_valid(), True)
self.assertEqual(serializer.object, data)
self.assertFalse(serializer.object is expected_names)
self.assertEqual(serializer.data['names'], expected_names)
def test_null_nested(self):
"""Test a serializer with a nonexistent nested field"""
data = {'ssn': '1234567890'}
serializer = PersonIdentifierSerializer(data=data)
self.assertEqual(serializer.is_valid(), True)
self.assertEqual(serializer.object, data)
self.assertFalse(serializer.object is data)
expected = {'ssn': '1234567890', 'names': None}
self.assertEqual(serializer.data, expected)
def test_update(self):
serializer = CommentSerializer(self.comment, data=self.data)
expected = self.comment
self.assertEqual(serializer.is_valid(), True)
self.assertEqual(serializer.object, expected)
self.assertTrue(serializer.object is expected)
self.assertEqual(serializer.data['sub_comment'], 'And Merry Christmas!')
def test_partial_update(self):
msg = 'Merry New Year!'
partial_data = {'content': msg}
serializer = CommentSerializer(self.comment, data=partial_data)
self.assertEqual(serializer.is_valid(), False)
serializer = CommentSerializer(self.comment, data=partial_data, partial=True)
expected = self.comment
self.assertEqual(serializer.is_valid(), True)
self.assertEqual(serializer.object, expected)
self.assertTrue(serializer.object is expected)
self.assertEqual(serializer.data['content'], msg)
#.........這裏部分代碼省略.........
示例6: BasicTests
class BasicTests(TestCase):
def setUp(self):
self.comment = Comment("[email protected]", "Happy new year!", datetime.datetime(2012, 1, 1))
self.data = {
"email": "[email protected]",
"content": "Happy new year!",
"created": datetime.datetime(2012, 1, 1),
"sub_comment": "This wont change",
}
self.expected = {
"email": "[email protected]",
"content": "Happy new year!",
"created": datetime.datetime(2012, 1, 1),
"sub_comment": "And Merry Christmas!",
}
self.person_data = {"name": "dwight", "age": 35}
self.person = Person(**self.person_data)
self.person.save()
def test_empty(self):
serializer = CommentSerializer()
expected = {"email": "", "content": "", "created": None, "sub_comment": ""}
self.assertEquals(serializer.data, expected)
def test_retrieve(self):
serializer = CommentSerializer(self.comment)
self.assertEquals(serializer.data, self.expected)
def test_create(self):
serializer = CommentSerializer(data=self.data)
expected = self.comment
self.assertEquals(serializer.is_valid(), True)
self.assertEquals(serializer.object, expected)
self.assertFalse(serializer.object is expected)
self.assertEquals(serializer.data["sub_comment"], "And Merry Christmas!")
def test_update(self):
serializer = CommentSerializer(self.comment, data=self.data)
expected = self.comment
self.assertEquals(serializer.is_valid(), True)
self.assertEquals(serializer.object, expected)
self.assertTrue(serializer.object is expected)
self.assertEquals(serializer.data["sub_comment"], "And Merry Christmas!")
def test_model_fields_as_expected(self):
"""
Make sure that the fields returned are the same as defined
in the Meta data
"""
serializer = PersonSerializer(self.person)
self.assertEquals(set(serializer.data.keys()), set(["name", "age", "info"]))
def test_field_with_dictionary(self):
"""
Make sure that dictionaries from fields are left intact
"""
serializer = PersonSerializer(self.person)
expected = self.person_data
self.assertEquals(serializer.data["info"], expected)
def test_read_only_fields(self):
"""
Attempting to update fields set as read_only should have no effect.
"""
serializer = PersonSerializer(self.person, data={"name": "dwight", "age": 99})
self.assertEquals(serializer.is_valid(), True)
instance = serializer.save()
self.assertEquals(serializer.errors, {})
# Assert age is unchanged (35)
self.assertEquals(instance.age, self.person_data["age"])