<s> from agate import Table <EOL> from agate . data_types import * <EOL> from agate . testcase import AgateTestCase <EOL> class TestJoin ( AgateTestCase ) : <EOL> def setUp ( self ) : <EOL> self . left_rows = ( <EOL> ( <NUM_LIT:1> , <NUM_LIT:4> , '<STR_LIT:a>' ) , <EOL> ( <NUM_LIT:2> , <NUM_LIT:3> , '<STR_LIT:b>' ) , <EOL> ( None , <NUM_LIT:2> , '<STR_LIT:c>' ) <EOL> ) <EOL> self . right_rows = ( <EOL> ( <NUM_LIT:1> , <NUM_LIT:4> , '<STR_LIT:a>' ) , <EOL> ( <NUM_LIT:2> , <NUM_LIT:3> , '<STR_LIT:b>' ) , <EOL> ( None , <NUM_LIT:2> , '<STR_LIT:c>' ) <EOL> ) <EOL> self . number_type = Number ( ) <EOL> self . text_type = Text ( ) <EOL> self . left_column_names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . right_column_names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . column_types = [ self . number_type , self . number_type , self . text_type ] <EOL> self . left = Table ( self . left_rows , self . left_column_names , self . column_types ) <EOL> self . right = Table ( self . right_rows , self . right_column_names , self . column_types ) <EOL> def test_join ( self ) : <EOL> new_table = self . left . join ( self . right , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertIsNot ( new_table , self . left ) <EOL> self . assertIsNot ( new_table , self . right ) <EOL> self . assertColumnNames ( new_table , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertColumnTypes ( new_table , [ Number , Number , Text , Number , Text ] ) <EOL> self . assertRows ( new_table , [ <EOL> ( <NUM_LIT:1> , <NUM_LIT:4> , '<STR_LIT:a>' , <NUM_LIT:4> , '<STR_LIT:a>' ) , <EOL> ( <NUM_LIT:2> , <NUM_LIT:3> , '<STR_LIT:b>' , <NUM_LIT:3> , '<STR_LIT:b>' ) , <EOL> ( None , <NUM_LIT:2> , '<STR_LIT:c>' , <NUM_LIT:2> , '<STR_LIT:c>' ) <EOL> ] ) <EOL> def test_join_match_multiple ( self ) : <EOL> left_rows = ( <EOL> ( <NUM_LIT:1> , <NUM_LIT:4> , '<STR_LIT:a>' ) , <EOL> ( <NUM_LIT:2> , <NUM_LIT:3> , '<STR_LIT:b>' ) <EOL> ) <EOL> right_rows = ( <EOL> ( <NUM_LIT:1> , <NUM_LIT:1> , '<STR_LIT:a>' ) , <EOL> ( <NUM_LIT:1> , <NUM_LIT:2> , '<STR_LIT:a>' ) , <EOL> ( <NUM_LIT:2> , <NUM_LIT:2> , '<STR_LIT:b>' ) <EOL> ) <EOL> left = Table ( left_rows , self . left_column_names , self . column_types ) <EOL> right = Table ( right_rows , self . right_column_names , self . column_types ) <EOL> new_table = left . join ( right , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertIsNot ( new_table , left ) <EOL> self . assertIsNot ( new_table , right ) <EOL> self . assertColumnNames ( new_table , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertColumnTypes ( new_table , [ Number , Number , Text , Number , Text ] ) <EOL> self . assertRows ( new_table , [ <EOL> ( <NUM_LIT:1> , <NUM_LIT:4> , '<STR_LIT:a>' , <NUM_LIT:1> , '<STR_LIT:a>' ) , <EOL> ( <NUM_LIT:2> , <NUM_LIT:3> , '<STR_LIT:b>' , <NUM_LIT:1> , '<STR_LIT:a>' ) , <EOL> ( <NUM_LIT:2> , <NUM_LIT:3> , '<STR_LIT:b>' , <NUM_LIT:2> , '<STR_LIT:b>' ) <EOL> ] ) <EOL> def test_join2 ( self ) : <EOL> new_table = self . left . join ( self . right , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertIsNot ( new_table , self . left ) <EOL> self . assertIsNot ( new_table , self . right ) <EOL> self . assertColumnNames ( new_table , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertColumnTypes ( new_table , [ Number , Number , Text , Number , Text ] ) <EOL> self . assertRows ( new_table , [ <EOL> ( <NUM_LIT:1> , <NUM_LIT:4> , '<STR_LIT:a>' , None , None ) , <EOL> ( <NUM_LIT:2> , <NUM_LIT:3> , '<STR_LIT:b>' , None , '<STR_LIT:c>' ) , <EOL> ( None , <NUM_LIT:2> , '<STR_LIT:c>' , None , None ) <EOL> ] ) <EOL> def test_join_same_column_name ( self ) : <EOL> right_column_names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> right = Table ( self . right_rows , right_column_names , self . column_types ) <EOL> new_table = self . left . join ( right , '<STR_LIT>' ) <EOL> self . assertIsNot ( new_table , self . left ) <EOL> self . assertIsNot ( new_table , right ) <EOL> self . assertColumnNames ( new_table , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertColumnTypes ( new_table , [ Number , Number , Text , Number , Text ] ) <EOL> self . assertRows ( new_table , [ <EOL> ( <NUM_LIT:1> , <NUM_LIT:4> , '<STR_LIT:a>' , None , None ) , <EOL> ( <NUM_LIT:2> , <NUM_LIT:3> , '<STR_LIT:b>' , None , '<STR_LIT:c>' ) , <EOL> ( None , <NUM_LIT:2> , '<STR_LIT:c>' , None , None ) <EOL> ] ) <EOL> def test_join_multiple_columns ( self ) : <EOL> new_table = self . left . join ( <EOL> self . right , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> ) <EOL> self . assertIsNot ( new_table , self . left ) <EOL> self . assertIsNot ( new_table , self . right ) <EOL> self . assertColumnNames ( new_table , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertColumnTypes ( new_table , [ Number , Number , Text , Number ] ) <EOL> self . assertRows ( new_table , [ <EOL> ( <NUM_LIT:1> , <NUM_LIT:4> , '<STR_LIT:a>' , <NUM_LIT:1> ) , <EOL> ( <NUM_LIT:2> , <NUM_LIT:3> , '<STR_LIT:b>' , <NUM_LIT:2> ) , <EOL> ( None , <NUM_LIT:2> , '<STR_LIT:c>' , None ) <EOL> ] ) <EOL> def test_join_func ( self ) : <EOL> new_table = self . left . join ( <EOL> self . right , <EOL> lambda left : '<STR_LIT>' % ( left [ '<STR_LIT>' ] , left [ '<STR_LIT>' ] ) , <EOL> lambda right : '<STR_LIT>' % ( right [ '<STR_LIT>' ] , right [ '<STR_LIT>' ] ) <EOL> ) <EOL> self . assertIsNot ( new_table , self . left ) <EOL> self . assertIsNot ( new_table , self . right ) <EOL> self . assertColumnNames ( new_table , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertColumnTypes ( new_table , [ Number , Number , Text , Number , Number , Text ] ) <EOL> self . assertRows ( new_table , [ <EOL> ( <NUM_LIT:1> , <NUM_LIT:4> , '<STR_LIT:a>' , <NUM_LIT:1> , <NUM_LIT:4> , '<STR_LIT:a>' ) , <EOL> ( <NUM_LIT:2> , <NUM_LIT:3> , '<STR_LIT:b>' , <NUM_LIT:2> , <NUM_LIT:3> , '<STR_LIT:b>' ) , <EOL> ( None , <NUM_LIT:2> , '<STR_LIT:c>' , None , <NUM_LIT:2> , '<STR_LIT:c>' ) <EOL> ] ) <EOL> def test_join_column_does_not_exist ( self ) : <EOL> with self . assertRaises ( KeyError ) : <EOL> self . left . join ( self . right , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_inner_join ( self ) : <EOL> new_table = self . left . join ( self . right , '<STR_LIT>' , '<STR_LIT>' , inner = True ) <EOL> self . assertIsNot ( new_table , self . left ) <EOL> self . assertIsNot ( new_table , self . right ) <EOL> self . assertColumnNames ( new_table , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertColumnTypes ( new_table , [ Number , Number , Text , Number , Text ] ) <EOL> self . assertRows ( new_table , [ <EOL> ( <NUM_LIT:1> , <NUM_LIT:4> , '<STR_LIT:a>' , <NUM_LIT:4> , '<STR_LIT:a>' ) , <EOL> ( <NUM_LIT:2> , <NUM_LIT:3> , '<STR_LIT:b>' , <NUM_LIT:3> , '<STR_LIT:b>' ) , <EOL> ( None , <NUM_LIT:2> , '<STR_LIT:c>' , <NUM_LIT:2> , '<STR_LIT:c>' ) <EOL> ] ) <EOL> def test_inner_join2 ( self ) : <EOL> new_table = self . left . join ( self . right , '<STR_LIT>' , '<STR_LIT>' , inner = True ) <EOL> self . assertIsNot ( new_table , self . left ) <EOL> self . assertIsNot ( new_table , self . right ) <EOL> self . assertColumnNames ( new_table , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertColumnTypes ( new_table , [ Number , Number , Text , Number , Text ] ) <EOL> self . assertRows ( new_table , [ <EOL> ( <NUM_LIT:2> , <NUM_LIT:3> , '<STR_LIT:b>' , None , '<STR_LIT:c>' ) <EOL> ] ) <EOL> def test_inner_join_same_column_name ( self ) : <EOL> right_column_names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> right = Table ( self . right_rows , right_column_names , self . column_types ) <EOL> new_table = self . left . join ( right , '<STR_LIT>' , inner = True ) <EOL> self . assertIsNot ( new_table , self . left ) <EOL> self . assertIsNot ( new_table , right ) <EOL> self . assertColumnNames ( new_table , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertColumnTypes ( new_table , [ Number , Number , Text , Number , Text ] ) <EOL> self . assertRows ( new_table , [ <EOL> ( <NUM_LIT:2> , <NUM_LIT:3> , '<STR_LIT:b>' , None , '<STR_LIT:c>' ) <EOL> ] ) <EOL> def test_inner_join_func ( self ) : <EOL> new_table = self . left . join ( <EOL> self . right , <EOL> lambda left : '<STR_LIT>' % ( left [ '<STR_LIT>' ] , left [ '<STR_LIT>' ] ) , <EOL> lambda right : '<STR_LIT>' % ( right [ '<STR_LIT>' ] , right [ '<STR_LIT>' ] ) , <EOL> inner = True <EOL> ) <EOL> self . assertIsNot ( new_table , self . left ) <EOL> self . assertIsNot ( new_table , self . right ) <EOL> self . assertColumnNames ( new_table , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertColumnTypes ( new_table , [ Number , Number , Text , Number , Number , Text ] ) <EOL> self . assertRows ( new_table , [ <EOL> ( <NUM_LIT:1> , <NUM_LIT:4> , '<STR_LIT:a>' , <NUM_LIT:1> , <NUM_LIT:4> , '<STR_LIT:a>' ) <EOL> ] ) <EOL> def test_join_with_row_names ( self ) : <EOL> left = Table ( self . left_rows , self . left_column_names , self . column_types , row_names = '<STR_LIT>' ) <EOL> new_table = left . join ( self . right , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertRowNames ( new_table , ( '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ) ) <EOL> def test_join_require_match ( self ) : <EOL> with self . assertRaises ( ValueError ) : <EOL> new_table = self . left . join ( self . right , '<STR_LIT>' , '<STR_LIT>' , require_match = True ) <EOL> with self . assertRaises ( ValueError ) : <EOL> new_table = self . left . join ( self . right , '<STR_LIT>' , '<STR_LIT>' , require_match = True ) <EOL> new_table = self . left . join ( self . right , '<STR_LIT>' , '<STR_LIT>' , require_match = True ) <EOL> def test_join_columns_kwarg ( self ) : <EOL> new_table = self . left . join ( self . right , '<STR_LIT>' , '<STR_LIT>' , columns = [ '<STR_LIT>' ] ) <EOL> self . assertIsNot ( new_table , self . left ) <EOL> self . assertIsNot ( new_table , self . right ) <EOL> self . assertColumnNames ( new_table , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertColumnTypes ( new_table , [ Number , Number , Text , Text ] ) <EOL> self . assertRows ( new_table , [ <EOL> ( <NUM_LIT:1> , <NUM_LIT:4> , '<STR_LIT:a>' , '<STR_LIT:a>' ) , <EOL> ( <NUM_LIT:2> , <NUM_LIT:3> , '<STR_LIT:b>' , '<STR_LIT:b>' ) , <EOL> ( None , <NUM_LIT:2> , '<STR_LIT:c>' , '<STR_LIT:c>' ) <EOL> ] ) <EOL> def test_join_columns_kwarg_right_key ( self ) : <EOL> new_table = self . left . join ( self . right , '<STR_LIT>' , '<STR_LIT>' , columns = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertIsNot ( new_table , self . left ) <EOL> self . assertIsNot ( new_table , self . right ) <EOL> self . assertColumnNames ( new_table , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertColumnTypes ( new_table , [ Number , Number , Text , Number , Text ] ) <EOL> self . assertRows ( new_table , [ <EOL> ( <NUM_LIT:1> , <NUM_LIT:4> , '<STR_LIT:a>' , <NUM_LIT:1> , '<STR_LIT:a>' ) , <EOL> ( <NUM_LIT:2> , <NUM_LIT:3> , '<STR_LIT:b>' , <NUM_LIT:2> , '<STR_LIT:b>' ) , <EOL> ( None , <NUM_LIT:2> , '<STR_LIT:c>' , None , '<STR_LIT:c>' ) <EOL> ] ) </s>
