<s> from __future__ import unicode_literals <EOL> import json <EOL> import re <EOL> from django . core import serializers <EOL> from django . core . serializers . base import DeserializationError <EOL> from django . core . serializers . json import DjangoJSONEncoder <EOL> from django . test import SimpleTestCase , TestCase , TransactionTestCase <EOL> from django . utils . translation import override , ugettext_lazy <EOL> from . models import Score <EOL> from . tests import SerializersTestBase , SerializersTransactionTestBase <EOL> class JsonSerializerTestCase ( SerializersTestBase , TestCase ) : <EOL> serializer_name = "<STR_LIT>" <EOL> pkless_str = """<STR_LIT>""" <EOL> mapping_ordering_str = """<STR_LIT>""" <EOL> @ staticmethod <EOL> def _validate_output ( serial_str ) : <EOL> try : <EOL> json . loads ( serial_str ) <EOL> except Exception : <EOL> return False <EOL> else : <EOL> return True <EOL> @ staticmethod <EOL> def _get_pk_values ( serial_str ) : <EOL> ret_list = [ ] <EOL> serial_list = json . loads ( serial_str ) <EOL> for obj_dict in serial_list : <EOL> ret_list . append ( obj_dict [ "<STR_LIT>" ] ) <EOL> return ret_list <EOL> @ staticmethod <EOL> def _get_field_values ( serial_str , field_name ) : <EOL> ret_list = [ ] <EOL> serial_list = json . loads ( serial_str ) <EOL> for obj_dict in serial_list : <EOL> if field_name in obj_dict [ "<STR_LIT>" ] : <EOL> ret_list . append ( obj_dict [ "<STR_LIT>" ] [ field_name ] ) <EOL> return ret_list <EOL> def test_indentation_whitespace ( self ) : <EOL> Score . objects . create ( score = <NUM_LIT> ) <EOL> Score . objects . create ( score = <NUM_LIT> ) <EOL> qset = Score . objects . all ( ) <EOL> s = serializers . json . Serializer ( ) <EOL> json_data = s . serialize ( qset , indent = <NUM_LIT:2> ) <EOL> for line in json_data . splitlines ( ) : <EOL> if re . search ( r'<STR_LIT>' , line ) : <EOL> self . assertEqual ( line , line . rstrip ( ) ) <EOL> def test_json_deserializer_exception ( self ) : <EOL> with self . assertRaises ( DeserializationError ) : <EOL> for obj in serializers . deserialize ( "<STR_LIT>" , """<STR_LIT>""" ) : <EOL> pass <EOL> def test_helpful_error_message_invalid_pk ( self ) : <EOL> """<STR_LIT>""" <EOL> test_string = """<STR_LIT>""" <EOL> with self . assertRaisesMessage ( DeserializationError , "<STR_LIT>" ) : <EOL> list ( serializers . deserialize ( '<STR_LIT>' , test_string ) ) <EOL> def test_helpful_error_message_invalid_field ( self ) : <EOL> """<STR_LIT>""" <EOL> test_string = """<STR_LIT>""" <EOL> expected = "<STR_LIT>" <EOL> with self . assertRaisesMessage ( DeserializationError , expected ) : <EOL> list ( serializers . deserialize ( '<STR_LIT>' , test_string ) ) <EOL> def test_helpful_error_message_for_foreign_keys ( self ) : <EOL> """<STR_LIT>""" <EOL> test_string = """<STR_LIT>""" <EOL> key = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> expected = "<STR_LIT>" % key <EOL> with self . assertRaisesMessage ( DeserializationError , expected ) : <EOL> list ( serializers . deserialize ( '<STR_LIT>' , test_string ) ) <EOL> def test_helpful_error_message_for_many2many_non_natural ( self ) : <EOL> """<STR_LIT>""" <EOL> test_string = """<STR_LIT>""" <EOL> expected = "<STR_LIT>" <EOL> with self . assertRaisesMessage ( DeserializationError , expected ) : <EOL> list ( serializers . deserialize ( '<STR_LIT>' , test_string ) ) <EOL> def test_helpful_error_message_for_many2many_natural1 ( self ) : <EOL> """<STR_LIT>""" <EOL> test_string = """<STR_LIT>""" <EOL> key = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> expected = "<STR_LIT>" % key <EOL> with self . assertRaisesMessage ( DeserializationError , expected ) : <EOL> for obj in serializers . deserialize ( '<STR_LIT>' , test_string ) : <EOL> obj . save ( ) <EOL> def test_helpful_error_message_for_many2many_natural2 ( self ) : <EOL> """<STR_LIT>""" <EOL> test_string = """<STR_LIT>""" <EOL> expected = "<STR_LIT>" <EOL> with self . assertRaisesMessage ( DeserializationError , expected ) : <EOL> for obj in serializers . deserialize ( '<STR_LIT>' , test_string , ignore = False ) : <EOL> obj . save ( ) <EOL> class JsonSerializerTransactionTestCase ( SerializersTransactionTestBase , TransactionTestCase ) : <EOL> serializer_name = "<STR_LIT>" <EOL> fwd_ref_str = """<STR_LIT>""" <EOL> class DjangoJSONEncoderTests ( SimpleTestCase ) : <EOL> def test_lazy_string_encoding ( self ) : <EOL> self . assertEqual ( <EOL> json . dumps ( { '<STR_LIT>' : ugettext_lazy ( "<STR_LIT>" ) } , cls = DjangoJSONEncoder ) , <EOL> '<STR_LIT>' <EOL> ) <EOL> with override ( '<STR_LIT>' ) : <EOL> self . assertEqual ( <EOL> json . dumps ( { '<STR_LIT>' : ugettext_lazy ( "<STR_LIT>" ) } , cls = DjangoJSONEncoder ) , <EOL> '<STR_LIT>' <EOL> ) </s>
