<s> from contextlib import contextmanager <EOL> from six . moves import cStringIO as StringIO <EOL> from datetime import datetime <EOL> try : <EOL> from unittest import skipUnless <EOL> except ImportError : <EOL> from unittest2 import skipUnless <EOL> import django <EOL> from django . test import TestCase <EOL> from django . core import management <EOL> from simple_history import models as sh_models <EOL> from simple_history . management . commands import populate_history <EOL> from . . import models <EOL> @ contextmanager <EOL> def replace_registry ( new_value = None ) : <EOL> hidden_registry = sh_models . registered_models <EOL> sh_models . registered_models = new_value or { } <EOL> try : <EOL> yield <EOL> except Exception : <EOL> raise <EOL> finally : <EOL> sh_models . registered_models = hidden_registry <EOL> class TestPopulateHistory ( TestCase ) : <EOL> command_name = '<STR_LIT>' <EOL> command_error = ( management . CommandError , SystemExit ) <EOL> def test_no_args ( self ) : <EOL> out = StringIO ( ) <EOL> management . call_command ( self . command_name , <EOL> stdout = out , stderr = StringIO ( ) ) <EOL> self . assertIn ( populate_history . Command . COMMAND_HINT , out . getvalue ( ) ) <EOL> def test_bad_args ( self ) : <EOL> test_data = ( <EOL> ( populate_history . Command . MODEL_NOT_HISTORICAL , ( "<STR_LIT>" , ) ) , <EOL> ( populate_history . Command . MODEL_NOT_FOUND , ( "<STR_LIT>" , ) ) , <EOL> ( populate_history . Command . MODEL_NOT_FOUND , ( "<STR_LIT>" , ) ) , <EOL> ) <EOL> for msg , args in test_data : <EOL> out = StringIO ( ) <EOL> self . assertRaises ( self . command_error , management . call_command , <EOL> self . command_name , * args , <EOL> stdout = StringIO ( ) , stderr = out ) <EOL> self . assertIn ( msg , out . getvalue ( ) ) <EOL> def test_auto_populate ( self ) : <EOL> models . Poll . objects . create ( question = "<STR_LIT>" , <EOL> pub_date = datetime . now ( ) ) <EOL> models . Poll . history . all ( ) . delete ( ) <EOL> management . call_command ( self . command_name , auto = True , <EOL> stdout = StringIO ( ) , stderr = StringIO ( ) ) <EOL> self . assertEqual ( models . Poll . history . all ( ) . count ( ) , <NUM_LIT:1> ) <EOL> def test_specific_populate ( self ) : <EOL> models . Poll . objects . create ( question = "<STR_LIT>" , <EOL> pub_date = datetime . now ( ) ) <EOL> models . Poll . history . all ( ) . delete ( ) <EOL> models . Book . objects . create ( isbn = "<STR_LIT>" ) <EOL> models . Book . history . all ( ) . delete ( ) <EOL> management . call_command ( self . command_name , "<STR_LIT>" , <EOL> stdout = StringIO ( ) , stderr = StringIO ( ) ) <EOL> self . assertEqual ( models . Book . history . all ( ) . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( models . Poll . history . all ( ) . count ( ) , <NUM_LIT:0> ) <EOL> def test_failing_wont_save ( self ) : <EOL> models . Poll . objects . create ( question = "<STR_LIT>" , <EOL> pub_date = datetime . now ( ) ) <EOL> models . Poll . history . all ( ) . delete ( ) <EOL> self . assertRaises ( self . command_error , <EOL> management . call_command , self . command_name , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> stdout = StringIO ( ) , stderr = StringIO ( ) ) <EOL> self . assertEqual ( models . Poll . history . all ( ) . count ( ) , <NUM_LIT:0> ) <EOL> def test_multi_table ( self ) : <EOL> data = { '<STR_LIT>' : <NUM_LIT:5> , '<STR_LIT:name>' : "<STR_LIT>" } <EOL> models . Restaurant . objects . create ( ** data ) <EOL> models . Restaurant . updates . all ( ) . delete ( ) <EOL> management . call_command ( self . command_name , '<STR_LIT>' , <EOL> stdout = StringIO ( ) , stderr = StringIO ( ) ) <EOL> update_record = models . Restaurant . updates . all ( ) [ <NUM_LIT:0> ] <EOL> for attr , value in data . items ( ) : <EOL> self . assertEqual ( getattr ( update_record , attr ) , value ) <EOL> def test_existing_objects ( self ) : <EOL> data = { '<STR_LIT>' : <NUM_LIT:5> , '<STR_LIT:name>' : "<STR_LIT>" } <EOL> out = StringIO ( ) <EOL> models . Restaurant . objects . create ( ** data ) <EOL> pre_call_count = models . Restaurant . updates . count ( ) <EOL> management . call_command ( self . command_name , '<STR_LIT>' , <EOL> stdout = StringIO ( ) , stderr = out ) <EOL> self . assertEqual ( models . Restaurant . updates . count ( ) , pre_call_count ) <EOL> self . assertIn ( populate_history . Command . EXISTING_HISTORY_FOUND , <EOL> out . getvalue ( ) ) <EOL> def test_no_historical ( self ) : <EOL> out = StringIO ( ) <EOL> with replace_registry ( ) : <EOL> management . call_command ( self . command_name , auto = True , <EOL> stdout = out ) <EOL> self . assertIn ( populate_history . Command . NO_REGISTERED_MODELS , <EOL> out . getvalue ( ) ) <EOL> @ skipUnless ( django . get_version ( ) >= "<STR_LIT>" , "<STR_LIT>" ) <EOL> class TestMigrate ( TestCase ) : <EOL> def test_makemigration_command ( self ) : <EOL> management . call_command ( <EOL> '<STR_LIT>' , '<STR_LIT>' , stdout = StringIO ( ) ) <EOL> def test_migrate_command ( self ) : <EOL> management . call_command ( <EOL> '<STR_LIT>' , '<STR_LIT>' , fake = True , stdout = StringIO ( ) ) </s>
