<s> """<STR_LIT>""" <EOL> import os <EOL> from unittest import TestCase <EOL> from testfixtures import TempDirectory <EOL> from nose . tools import assert_not_equal <EOL> from nose . tools import eq_ <EOL> from nose . tools import raises <EOL> from . . database import Database <EOL> from . helpers import create_table <EOL> from . helpers import create_view <EOL> from . helpers import compare_scripts <EOL> class TestDatabase ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . basic_table = create_table ( <EOL> '<STR_LIT>' ) <EOL> self . basic_view = create_view ( <EOL> '<STR_LIT>' ) <EOL> self . first_table = create_table ( <EOL> """<STR_LIT>""" ) <EOL> self . first_table_dependent = create_table ( <EOL> """<STR_LIT>""" ) <EOL> self . second_table = create_table ( <EOL> """<STR_LIT>""" ) <EOL> self . second_table_dependent = create_table ( <EOL> """<STR_LIT>""" ) <EOL> table = create_table ( '<STR_LIT>' ) <EOL> view = create_view ( """<STR_LIT>""" ) <EOL> self . script_database = Database ( relations = [ table , view ] ) <EOL> def test_create ( self ) : <EOL> """<STR_LIT>""" <EOL> database = Database ( relations = [ self . basic_table ] ) <EOL> eq_ ( database . num_tables , <NUM_LIT:1> ) <EOL> eq_ ( database . num_views , <NUM_LIT:0> ) <EOL> assert_not_equal ( database . relation ( self . basic_table . full_name ) , None ) <EOL> def test_create_from_file ( self ) : <EOL> """<STR_LIT>""" <EOL> with TempDirectory ( ) as d : <EOL> d . write ( self . basic_table . full_name , <EOL> self . basic_table . sql_statement . sql ( ) ) <EOL> d . write ( self . basic_view . full_name , <EOL> self . basic_view . sql_statement . sql ( ) ) <EOL> database = Database ( <EOL> files = [ os . path . join ( d . path , self . basic_table . full_name ) , <EOL> os . path . join ( d . path , self . basic_view . full_name ) ] ) <EOL> eq_ ( database . num_tables , <NUM_LIT:1> ) <EOL> eq_ ( database . num_views , <NUM_LIT:1> ) <EOL> assert_not_equal ( <EOL> database . relation ( self . basic_table . full_name ) , None ) <EOL> assert_not_equal ( <EOL> database . relation ( self . basic_view . full_name ) , None ) <EOL> @ staticmethod <EOL> @ raises ( ValueError ) <EOL> def test_create_from_file_no_relation ( ) : <EOL> """<STR_LIT>""" <EOL> with TempDirectory ( ) as d : <EOL> d . write ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> Database ( files = [ os . path . join ( d . path , '<STR_LIT>' ) ] ) <EOL> @ staticmethod <EOL> @ raises ( ValueError ) <EOL> def test_create_two_arguments ( ) : <EOL> """<STR_LIT>""" <EOL> Database ( relations = [ '<STR_LIT>' ] , files = [ '<STR_LIT>' ] ) <EOL> @ raises ( ValueError ) <EOL> def test_create_duplicate_relations ( self ) : <EOL> """<STR_LIT>""" <EOL> Database ( relations = [ self . basic_table , self . basic_table ] ) <EOL> def test_database_copy ( self ) : <EOL> """<STR_LIT>""" <EOL> database = Database ( relations = [ self . basic_table ] ) <EOL> database_copy = database . copy ( ) <EOL> assert_not_equal ( <EOL> database_copy . relation ( self . basic_table . full_name ) , None ) <EOL> database_copy . _relations = { } <EOL> assert_not_equal ( <EOL> database . relation ( self . basic_table . full_name ) , None ) <EOL> def test_database_has_cycles ( self ) : <EOL> """<STR_LIT>""" <EOL> database = Database ( relations = [ self . first_table_dependent , <EOL> self . second_table_dependent ] ) <EOL> eq_ ( database . has_cycles ( ) , True ) <EOL> def test_database_has_no_cycles ( self ) : <EOL> """<STR_LIT>""" <EOL> database = Database ( relations = [ self . first_table_dependent , <EOL> self . second_table ] ) <EOL> eq_ ( database . has_cycles ( ) , False ) <EOL> def test_database_has_no_cycles_2 ( self ) : <EOL> """<STR_LIT>""" <EOL> database = Database ( relations = [ self . first_table , <EOL> self . second_table_dependent ] ) <EOL> eq_ ( database . has_cycles ( ) , False ) <EOL> def test_database_sorted_relations ( self ) : <EOL> """<STR_LIT>""" <EOL> database = Database ( relations = [ self . first_table_dependent , <EOL> self . second_table ] ) <EOL> relations = database . sorted_relations ( ) <EOL> eq_ ( len ( relations ) , <NUM_LIT:2> ) <EOL> eq_ ( relations [ <NUM_LIT:0> ] . table_name , self . second_table . full_name ) <EOL> eq_ ( relations [ <NUM_LIT:1> ] . table_name , self . first_table_dependent . full_name ) <EOL> @ raises ( RuntimeError ) <EOL> def test_database_sorted_relations_cyclic ( self ) : <EOL> """<STR_LIT>""" <EOL> database = Database ( relations = [ self . first_table_dependent , <EOL> self . second_table_dependent ] ) <EOL> database . sorted_relations ( ) <EOL> def test_database_create_relations_script ( self ) : <EOL> """<STR_LIT>""" <EOL> result = [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> compare_scripts ( <EOL> self . script_database . create_relations_script ( False ) , <EOL> result ) <EOL> def test_database_drop_relations_script ( self ) : <EOL> """<STR_LIT>""" <EOL> result = [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> compare_scripts ( <EOL> self . script_database . drop_relations_script ( ) , <EOL> result ) <EOL> def test_database_recreate_relations_script ( self ) : <EOL> """<STR_LIT>""" <EOL> result = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> compare_scripts ( <EOL> self . script_database . recreate_relations_script ( False ) , <EOL> result ) <EOL> def test_database_recreate_table_dependencies ( self ) : <EOL> """<STR_LIT>""" <EOL> view = create_view ( <EOL> """<STR_LIT>""" ) <EOL> database = Database ( relations = [ self . first_table_dependent , <EOL> self . second_table , view ] ) <EOL> result = [ '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> compare_scripts ( <EOL> database . recreate_table_dependencies ( '<STR_LIT>' , False ) , <EOL> result ) <EOL> eq_ ( database . recreate_table_dependencies ( '<STR_LIT>' , False ) . sql ( ) , <EOL> '<STR_LIT:;>' ) </s>
