<s> from __future__ import unicode_literals <EOL> import json <EOL> from tempfile import NamedTemporaryFile <EOL> from django . core . management import call_command <EOL> from django . core . serializers . base import DeserializationError <EOL> from django . core . serializers . json import Serializer as JSONSerializer <EOL> from django . utils . encoding import force_bytes <EOL> from django . utils . six import StringIO <EOL> from mutant . models import ModelDefinition <EOL> from mutant . test . testcases import ModelDefinitionDDLTestCase <EOL> from mutant . utils import remove_from_app_cache <EOL> class DataCommandTestCase ( ModelDefinitionDDLTestCase ) : <EOL> def setUp ( self ) : <EOL> self . model_def = ModelDefinition . objects . create ( <EOL> app_label = '<STR_LIT>' , object_name = '<STR_LIT>' <EOL> ) <EOL> self . model_cls = self . model_def . model_class ( ) <EOL> def tearDown ( self ) : <EOL> if self . model_def . pk : <EOL> self . model_def . delete ( ) <EOL> class DumpDataTestCase ( DataCommandTestCase ) : <EOL> def dump_model_data ( self ) : <EOL> output = StringIO ( ) <EOL> remove_from_app_cache ( self . model_cls ) <EOL> call_command ( <EOL> '<STR_LIT>' , str ( self . model_def ) , stdout = output , commit = False <EOL> ) <EOL> output . seek ( <NUM_LIT:0> ) <EOL> return json . load ( output ) <EOL> def test_dump_mutable_models ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( self . dump_model_data ( ) , [ ] ) <EOL> instance = self . model_cls . objects . create ( ) <EOL> self . assertEqual ( <EOL> self . dump_model_data ( ) , [ { <EOL> '<STR_LIT>' : instance . pk , <EOL> '<STR_LIT>' : str ( self . model_def ) . lower ( ) , <EOL> '<STR_LIT>' : { } <EOL> } ] <EOL> ) <EOL> class BytesWritter ( object ) : <EOL> def __init__ ( self , stream ) : <EOL> self . _stream = stream <EOL> def __getattr__ ( self , name ) : <EOL> return getattr ( self . _stream , name ) <EOL> def write ( self , data ) : <EOL> self . _stream . write ( force_bytes ( data ) ) <EOL> class LoadDataTestCase ( DataCommandTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( LoadDataTestCase , self ) . setUp ( ) <EOL> self . serializer = JSONSerializer ( ) <EOL> def test_load_mutable_models ( self ) : <EOL> """<STR_LIT>""" <EOL> instance = self . model_cls ( pk = <NUM_LIT:1> ) <EOL> remove_from_app_cache ( self . model_cls ) <EOL> with NamedTemporaryFile ( suffix = '<STR_LIT>' ) as stream : <EOL> self . serializer . serialize ( [ instance ] , stream = BytesWritter ( stream ) ) <EOL> stream . seek ( <NUM_LIT:0> ) <EOL> call_command ( <EOL> '<STR_LIT>' , stream . name , stdout = StringIO ( ) , commit = False <EOL> ) <EOL> self . assertTrue ( self . model_cls . objects . filter ( pk = instance . pk ) . exists ( ) ) <EOL> def test_invalid_model_idenfitier_raises ( self ) : <EOL> """<STR_LIT>""" <EOL> instance = self . model_cls ( pk = <NUM_LIT:1> ) <EOL> with NamedTemporaryFile ( suffix = '<STR_LIT>' ) as stream : <EOL> self . serializer . serialize ( [ instance ] , stream = BytesWritter ( stream ) ) <EOL> stream . seek ( <NUM_LIT:0> ) <EOL> self . model_def . delete ( ) <EOL> with self . assertRaisesMessage ( <EOL> DeserializationError , "<STR_LIT>" ) : <EOL> call_command ( <EOL> '<STR_LIT>' , stream . name , stdout = StringIO ( ) <EOL> ) </s>
