<s> from __future__ import unicode_literals <EOL> from unittest import skipIf <EOL> from django . db import ConnectionHandler , connection , connections <EOL> from django . db . migrations . exceptions import ( <EOL> AmbiguityError , InconsistentMigrationHistory , NodeNotFoundError , <EOL> ) <EOL> from django . db . migrations . loader import MigrationLoader <EOL> from django . db . migrations . recorder import MigrationRecorder <EOL> from django . test import TestCase , modify_settings , override_settings <EOL> from django . utils import six <EOL> class RecorderTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_apply ( self ) : <EOL> """<STR_LIT>""" <EOL> recorder = MigrationRecorder ( connection ) <EOL> self . assertEqual ( <EOL> set ( ( x , y ) for ( x , y ) in recorder . applied_migrations ( ) if x == "<STR_LIT>" ) , <EOL> set ( ) , <EOL> ) <EOL> recorder . record_applied ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . assertEqual ( <EOL> set ( ( x , y ) for ( x , y ) in recorder . applied_migrations ( ) if x == "<STR_LIT>" ) , <EOL> { ( "<STR_LIT>" , "<STR_LIT>" ) } , <EOL> ) <EOL> recorder_other = MigrationRecorder ( connections [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( <EOL> set ( ( x , y ) for ( x , y ) in recorder_other . applied_migrations ( ) if x == "<STR_LIT>" ) , <EOL> set ( ) , <EOL> ) <EOL> recorder . record_unapplied ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . assertEqual ( <EOL> set ( ( x , y ) for ( x , y ) in recorder . applied_migrations ( ) if x == "<STR_LIT>" ) , <EOL> set ( ) , <EOL> ) <EOL> class LoaderTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> @ override_settings ( MIGRATION_MODULES = { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> @ modify_settings ( INSTALLED_APPS = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> def test_load ( self ) : <EOL> """<STR_LIT>""" <EOL> migration_loader = MigrationLoader ( connection ) <EOL> self . assertEqual ( <EOL> migration_loader . graph . forwards_plan ( ( "<STR_LIT>" , "<STR_LIT>" ) ) , <EOL> [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ] , <EOL> ) <EOL> project_state = migration_loader . project_state ( ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> self . assertEqual ( len ( project_state . models ) , <NUM_LIT:2> ) <EOL> author_state = project_state . models [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> self . assertEqual ( <EOL> [ x for x , y in author_state . fields ] , <EOL> [ "<STR_LIT:id>" , "<STR_LIT:name>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> ) <EOL> book_state = project_state . models [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> self . assertEqual ( <EOL> [ x for x , y in book_state . fields ] , <EOL> [ "<STR_LIT:id>" , "<STR_LIT>" ] <EOL> ) <EOL> self . assertIn ( "<STR_LIT>" , project_state . real_apps ) <EOL> @ override_settings ( MIGRATION_MODULES = { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def test_load_unmigrated_dependency ( self ) : <EOL> """<STR_LIT>""" <EOL> migration_loader = MigrationLoader ( connection ) <EOL> self . assertEqual ( <EOL> migration_loader . graph . forwards_plan ( ( "<STR_LIT>" , "<STR_LIT>" ) ) , <EOL> [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ] , <EOL> ) <EOL> project_state = migration_loader . project_state ( ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> self . assertEqual ( len ( [ m for a , m in project_state . models if a == "<STR_LIT>" ] ) , <NUM_LIT:1> ) <EOL> book_state = project_state . models [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> self . assertEqual ( <EOL> [ x for x , y in book_state . fields ] , <EOL> [ "<STR_LIT:id>" , "<STR_LIT:user>" ] <EOL> ) <EOL> @ override_settings ( MIGRATION_MODULES = { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def test_run_before ( self ) : <EOL> """<STR_LIT>""" <EOL> migration_loader = MigrationLoader ( connection ) <EOL> self . assertEqual ( <EOL> migration_loader . graph . forwards_plan ( ( "<STR_LIT>" , "<STR_LIT>" ) ) , <EOL> [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ] , <EOL> ) <EOL> @ override_settings ( MIGRATION_MODULES = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } ) <EOL> @ modify_settings ( INSTALLED_APPS = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> def test_first ( self ) : <EOL> """<STR_LIT>""" <EOL> migration_loader = MigrationLoader ( connection ) <EOL> self . assertEqual ( <EOL> migration_loader . graph . forwards_plan ( ( "<STR_LIT>" , "<STR_LIT>" ) ) , <EOL> [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ] , <EOL> ) <EOL> @ override_settings ( MIGRATION_MODULES = { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def test_name_match ( self ) : <EOL> "<STR_LIT>" <EOL> migration_loader = MigrationLoader ( connection ) <EOL> self . assertEqual ( <EOL> migration_loader . get_migration_by_prefix ( "<STR_LIT>" , "<STR_LIT>" ) . name , <EOL> "<STR_LIT>" , <EOL> ) <EOL> with self . assertRaises ( AmbiguityError ) : <EOL> migration_loader . get_migration_by_prefix ( "<STR_LIT>" , "<STR_LIT:0>" ) <EOL> with self . assertRaises ( KeyError ) : <EOL> migration_loader . get_migration_by_prefix ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def test_load_import_error ( self ) : <EOL> with override_settings ( MIGRATION_MODULES = { "<STR_LIT>" : "<STR_LIT>" } ) : <EOL> with self . assertRaises ( ImportError ) : <EOL> MigrationLoader ( connection ) <EOL> def test_load_module_file ( self ) : <EOL> with override_settings ( MIGRATION_MODULES = { "<STR_LIT>" : "<STR_LIT>" } ) : <EOL> loader = MigrationLoader ( connection ) <EOL> self . assertIn ( <EOL> "<STR_LIT>" , loader . unmigrated_apps , <EOL> "<STR_LIT>" <EOL> ) <EOL> @ skipIf ( six . PY2 , "<STR_LIT>" ) <EOL> def test_load_empty_dir ( self ) : <EOL> with override_settings ( MIGRATION_MODULES = { "<STR_LIT>" : "<STR_LIT>" } ) : <EOL> loader = MigrationLoader ( connection ) <EOL> self . assertIn ( <EOL> "<STR_LIT>" , loader . unmigrated_apps , <EOL> "<STR_LIT>" <EOL> ) <EOL> @ override_settings ( <EOL> INSTALLED_APPS = [ '<STR_LIT>' ] , <EOL> ) <EOL> def test_marked_as_migrated ( self ) : <EOL> """<STR_LIT>""" <EOL> migration_loader = MigrationLoader ( connection ) <EOL> self . assertEqual ( migration_loader . migrated_apps , { '<STR_LIT>' } ) <EOL> self . assertEqual ( migration_loader . unmigrated_apps , set ( ) ) <EOL> @ override_settings ( <EOL> INSTALLED_APPS = [ '<STR_LIT>' ] , <EOL> MIGRATION_MODULES = { "<STR_LIT>" : None } , <EOL> ) <EOL> def test_marked_as_unmigrated ( self ) : <EOL> """<STR_LIT>""" <EOL> migration_loader = MigrationLoader ( connection ) <EOL> self . assertEqual ( migration_loader . migrated_apps , set ( ) ) <EOL> self . assertEqual ( migration_loader . unmigrated_apps , { '<STR_LIT>' } ) <EOL> @ override_settings ( <EOL> INSTALLED_APPS = [ '<STR_LIT>' ] , <EOL> ) <EOL> def test_disable_migrations ( self ) : <EOL> connections = ConnectionHandler ( { <EOL> '<STR_LIT:default>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : False , <EOL> } , <EOL> } , <EOL> } ) <EOL> migration_loader = MigrationLoader ( connections [ '<STR_LIT:default>' ] ) <EOL> self . assertEqual ( migration_loader . migrated_apps , set ( ) ) <EOL> self . assertEqual ( migration_loader . unmigrated_apps , { '<STR_LIT>' } ) <EOL> @ override_settings ( MIGRATION_MODULES = { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def test_loading_squashed ( self ) : <EOL> "<STR_LIT>" <EOL> migration_loader = MigrationLoader ( connection ) <EOL> recorder = MigrationRecorder ( connection ) <EOL> self . addCleanup ( recorder . flush ) <EOL> self . assertEqual ( <EOL> len ( [ x for x in migration_loader . graph . nodes if x [ <NUM_LIT:0> ] == "<STR_LIT>" ] ) , <EOL> <NUM_LIT:1> , <EOL> ) <EOL> recorder . record_applied ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> migration_loader . build_graph ( ) <EOL> self . assertEqual ( <EOL> len ( [ x for x in migration_loader . graph . nodes if x [ <NUM_LIT:0> ] == "<STR_LIT>" ] ) , <EOL> <NUM_LIT:2> , <EOL> ) <EOL> @ override_settings ( MIGRATION_MODULES = { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def test_loading_squashed_complex ( self ) : <EOL> "<STR_LIT>" <EOL> loader = MigrationLoader ( connection ) <EOL> recorder = MigrationRecorder ( connection ) <EOL> self . addCleanup ( recorder . flush ) <EOL> def num_nodes ( ) : <EOL> plan = set ( loader . graph . forwards_plan ( ( '<STR_LIT>' , '<STR_LIT>' ) ) ) <EOL> return len ( plan - loader . applied_migrations ) <EOL> loader . build_graph ( ) <EOL> self . assertEqual ( num_nodes ( ) , <NUM_LIT:5> ) <EOL> recorder . record_applied ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> loader . build_graph ( ) <EOL> self . assertEqual ( num_nodes ( ) , <NUM_LIT:4> ) <EOL> recorder . record_applied ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> loader . build_graph ( ) <EOL> self . assertEqual ( num_nodes ( ) , <NUM_LIT:3> ) <EOL> recorder . record_applied ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> loader . build_graph ( ) <EOL> self . assertEqual ( num_nodes ( ) , <NUM_LIT:4> ) <EOL> recorder . record_applied ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> loader . build_graph ( ) <EOL> self . assertEqual ( num_nodes ( ) , <NUM_LIT:3> ) <EOL> recorder . record_applied ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> loader . build_graph ( ) <EOL> self . assertEqual ( num_nodes ( ) , <NUM_LIT:2> ) <EOL> recorder . record_applied ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> loader . build_graph ( ) <EOL> self . assertEqual ( num_nodes ( ) , <NUM_LIT:1> ) <EOL> recorder . record_applied ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> loader . build_graph ( ) <EOL> self . assertEqual ( num_nodes ( ) , <NUM_LIT:0> ) <EOL> @ override_settings ( MIGRATION_MODULES = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } ) <EOL> @ modify_settings ( INSTALLED_APPS = { '<STR_LIT>' : [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] } ) <EOL> def test_loading_squashed_complex_multi_apps ( self ) : <EOL> loader = MigrationLoader ( connection ) <EOL> loader . build_graph ( ) <EOL> plan = set ( loader . graph . forwards_plan ( ( '<STR_LIT>' , '<STR_LIT>' ) ) ) <EOL> expected_plan = { <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> } <EOL> self . assertEqual ( plan , expected_plan ) <EOL> @ override_settings ( MIGRATION_MODULES = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } ) <EOL> @ modify_settings ( INSTALLED_APPS = { '<STR_LIT>' : [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] } ) <EOL> def test_loading_squashed_complex_multi_apps_partially_applied ( self ) : <EOL> loader = MigrationLoader ( connection ) <EOL> recorder = MigrationRecorder ( connection ) <EOL> recorder . record_applied ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> recorder . record_applied ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> loader . build_graph ( ) <EOL> plan = set ( loader . graph . forwards_plan ( ( '<STR_LIT>' , '<STR_LIT>' ) ) ) <EOL> plan = plan - loader . applied_migrations <EOL> expected_plan = { <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> } <EOL> self . assertEqual ( plan , expected_plan ) <EOL> @ override_settings ( MIGRATION_MODULES = { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> def test_loading_squashed_erroneous ( self ) : <EOL> "<STR_LIT>" <EOL> loader = MigrationLoader ( connection ) <EOL> recorder = MigrationRecorder ( connection ) <EOL> self . addCleanup ( recorder . flush ) <EOL> def num_nodes ( ) : <EOL> plan = set ( loader . graph . forwards_plan ( ( '<STR_LIT>' , '<STR_LIT>' ) ) ) <EOL> return len ( plan - loader . applied_migrations ) <EOL> loader . build_graph ( ) <EOL> self . assertEqual ( num_nodes ( ) , <NUM_LIT:5> ) <EOL> recorder . record_applied ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> loader . build_graph ( ) <EOL> self . assertEqual ( num_nodes ( ) , <NUM_LIT:4> ) <EOL> recorder . record_applied ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> loader . build_graph ( ) <EOL> self . assertEqual ( num_nodes ( ) , <NUM_LIT:3> ) <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> recorder . record_applied ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> with self . assertRaisesMessage ( NodeNotFoundError , msg ) : <EOL> loader . build_graph ( ) <EOL> recorder . record_applied ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> with self . assertRaisesMessage ( NodeNotFoundError , msg ) : <EOL> loader . build_graph ( ) <EOL> recorder . record_applied ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> loader . build_graph ( ) <EOL> self . assertEqual ( num_nodes ( ) , <NUM_LIT:2> ) <EOL> recorder . record_applied ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> loader . build_graph ( ) <EOL> self . assertEqual ( num_nodes ( ) , <NUM_LIT:1> ) <EOL> recorder . record_applied ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> loader . build_graph ( ) <EOL> self . assertEqual ( num_nodes ( ) , <NUM_LIT:0> ) <EOL> @ override_settings ( <EOL> MIGRATION_MODULES = { '<STR_LIT>' : '<STR_LIT>' } , <EOL> INSTALLED_APPS = [ '<STR_LIT>' ] , <EOL> ) <EOL> def test_check_consistent_history ( self ) : <EOL> loader = MigrationLoader ( connection = None ) <EOL> loader . check_consistent_history ( connection ) <EOL> recorder = MigrationRecorder ( connection ) <EOL> recorder . record_applied ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> msg = "<STR_LIT>" <EOL> with self . assertRaisesMessage ( InconsistentMigrationHistory , msg ) : <EOL> loader . check_consistent_history ( connection ) </s>
