<s> from peewee import * <EOL> from peewee import sort_models_topologically <EOL> from playhouse . tests . base import PeeweeTestCase <EOL> from playhouse . tests . base import test_db <EOL> class TestHelperMethods ( PeeweeTestCase ) : <EOL> def test_assert_query_count ( self ) : <EOL> def execute_queries ( n ) : <EOL> for i in range ( n ) : <EOL> test_db . execute_sql ( '<STR_LIT>' ) <EOL> with self . assertQueryCount ( <NUM_LIT:0> ) : <EOL> pass <EOL> with self . assertQueryCount ( <NUM_LIT:1> ) : <EOL> execute_queries ( <NUM_LIT:1> ) <EOL> with self . assertQueryCount ( <NUM_LIT:2> ) : <EOL> execute_queries ( <NUM_LIT:2> ) <EOL> def fails_low ( ) : <EOL> with self . assertQueryCount ( <NUM_LIT:2> ) : <EOL> execute_queries ( <NUM_LIT:1> ) <EOL> def fails_high ( ) : <EOL> with self . assertQueryCount ( <NUM_LIT:1> ) : <EOL> execute_queries ( <NUM_LIT:2> ) <EOL> self . assertRaises ( AssertionError , fails_low ) <EOL> self . assertRaises ( AssertionError , fails_high ) <EOL> class TestTopologicalSorting ( PeeweeTestCase ) : <EOL> def test_topological_sort_fundamentals ( self ) : <EOL> FKF = ForeignKeyField <EOL> class A ( Model ) : pass <EOL> class B ( Model ) : a = FKF ( A ) <EOL> class C ( Model ) : a , b = FKF ( A ) , FKF ( B ) <EOL> class D ( Model ) : c = FKF ( C ) <EOL> class E ( Model ) : e = FKF ( '<STR_LIT>' ) <EOL> class Excluded ( Model ) : e = FKF ( E ) <EOL> repeatable_ordering = None <EOL> for input_ordering in permutations ( [ A , B , C , D , E ] ) : <EOL> output_ordering = sort_models_topologically ( input_ordering ) <EOL> repeatable_ordering = repeatable_ordering or output_ordering <EOL> self . assertEqual ( repeatable_ordering , output_ordering ) <EOL> self . assertEqual ( len ( output_ordering ) , <NUM_LIT:5> ) <EOL> self . assertFalse ( Excluded in output_ordering ) <EOL> def assert_precedes ( X , Y ) : <EOL> lhs , rhs = map ( output_ordering . index , [ X , Y ] ) <EOL> self . assertTrue ( lhs < rhs ) <EOL> assert_precedes ( A , B ) <EOL> assert_precedes ( B , C ) <EOL> assert_precedes ( C , D ) <EOL> assert_precedes ( A , E ) <EOL> def permutations ( xs ) : <EOL> if not xs : <EOL> yield [ ] <EOL> else : <EOL> for y , ys in selections ( xs ) : <EOL> for pys in permutations ( ys ) : <EOL> yield [ y ] + pys <EOL> def selections ( xs ) : <EOL> for i in range ( len ( xs ) ) : <EOL> yield ( xs [ i ] , xs [ : i ] + xs [ i + <NUM_LIT:1> : ] ) </s>
