<s> import pytest <EOL> from datashape import dshape <EOL> from datashape . util . testing import assert_dshape_equal <EOL> from blaze . expr import symbol <EOL> from blaze . expr . core import common_subexpression <EOL> from blaze . expr . collections import merge , join , transform , concat <EOL> from blaze . utils import raises <EOL> from blaze . compatibility import builtins <EOL> from toolz import isdistinct <EOL> t = symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> def test_merge ( ) : <EOL> e = symbol ( '<STR_LIT:e>' , '<STR_LIT>' ) <EOL> expr = merge ( name = e . name , y = e . x ) <EOL> assert set ( expr . fields ) == set ( [ '<STR_LIT:name>' , '<STR_LIT:y>' ] ) <EOL> assert expr . y . isidentical ( e . x . label ( '<STR_LIT:y>' ) ) <EOL> def test_merge_options ( ) : <EOL> s = symbol ( '<STR_LIT:s>' , '<STR_LIT>' ) <EOL> merged = merge ( a = s . a , b = s . b ) <EOL> assert_dshape_equal ( merged . dshape , dshape ( '<STR_LIT>' ) ) <EOL> assert_dshape_equal ( merged . a . dshape , dshape ( '<STR_LIT>' ) ) <EOL> assert_dshape_equal ( merged . b . dshape , dshape ( '<STR_LIT>' ) ) <EOL> def test_merge_on_single_argument_is_noop ( ) : <EOL> assert merge ( t . name ) . isidentical ( t . name ) <EOL> def test_merge_common_subexpression ( ) : <EOL> t = symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> result = common_subexpression ( t . a - t . a % <NUM_LIT:3> , t . a % <NUM_LIT:3> ) <EOL> assert result . isidentical ( t . a ) <EOL> def test_merge_exceptions ( ) : <EOL> t = symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> with pytest . raises ( ValueError ) as excinfo : <EOL> merge ( t , x = <NUM_LIT:2> * t . x ) <EOL> assert "<STR_LIT>" in str ( excinfo . value ) <EOL> def test_transform ( ) : <EOL> t = symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> expr = transform ( t , z = t . x + t . y ) <EOL> assert expr . fields == [ '<STR_LIT:x>' , '<STR_LIT:y>' , '<STR_LIT:z>' ] <EOL> assert builtins . any ( ( t . x + t . y ) . isidentical ( node ) <EOL> for node in expr . _subterms ( ) ) <EOL> def test_distinct ( ) : <EOL> assert '<STR_LIT:5>' not in str ( t . distinct ( ) . dshape ) <EOL> def test_distinct_exceptions ( ) : <EOL> t = symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> with pytest . raises ( ValueError ) as excinfo : <EOL> t . distinct ( '<STR_LIT:a>' ) <EOL> assert "<STR_LIT>" == str ( excinfo . value ) <EOL> with pytest . raises ( TypeError ) as excinfo : <EOL> t . distinct ( <NUM_LIT:10> ) <EOL> assert "<STR_LIT>" == str ( excinfo . value ) <EOL> s = symbol ( '<STR_LIT:s>' , '<STR_LIT>' ) <EOL> with pytest . raises ( ValueError ) as excinfo : <EOL> t . distinct ( s . y ) <EOL> assert "<STR_LIT>" == str ( excinfo . value ) <EOL> def test_sample_exceptions ( ) : <EOL> t = symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> with pytest . raises ( ValueError ) : <EOL> t . sample ( n = <NUM_LIT:1> , frac = <NUM_LIT:0.1> ) <EOL> with pytest . raises ( TypeError ) : <EOL> t . sample ( n = '<STR_LIT:a>' ) <EOL> with pytest . raises ( ValueError ) : <EOL> t . sample ( frac = '<STR_LIT:a>' ) <EOL> with pytest . raises ( TypeError ) : <EOL> t . sample ( foo = '<STR_LIT:a>' ) <EOL> with pytest . raises ( TypeError ) : <EOL> t . sample ( ) <EOL> def test_join_on_same_columns ( ) : <EOL> a = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> b = symbol ( '<STR_LIT:b>' , '<STR_LIT>' ) <EOL> c = join ( a , b , '<STR_LIT:x>' ) <EOL> assert isdistinct ( c . fields ) <EOL> assert len ( c . fields ) == <NUM_LIT:5> <EOL> assert '<STR_LIT>' in c . fields <EOL> assert '<STR_LIT>' in c . fields <EOL> def test_join_on_same_table ( ) : <EOL> a = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> c = join ( a , a , '<STR_LIT:x>' ) <EOL> assert isdistinct ( c . fields ) <EOL> assert len ( c . fields ) == <NUM_LIT:3> <EOL> def test_join_suffixes ( ) : <EOL> a = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> b = join ( a , a , '<STR_LIT:x>' , suffixes = ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> assert isdistinct ( b . fields ) <EOL> assert len ( b . fields ) == <NUM_LIT:3> <EOL> assert set ( b . fields ) == set ( [ '<STR_LIT:x>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_join_on_single_column ( ) : <EOL> a = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> b = symbol ( '<STR_LIT:b>' , '<STR_LIT>' ) <EOL> expr = join ( a , b . x ) <EOL> assert expr . on_right == '<STR_LIT:x>' <EOL> def test_raise_error_if_join_on_no_columns ( ) : <EOL> a = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> b = symbol ( '<STR_LIT:b>' , '<STR_LIT>' ) <EOL> assert raises ( ValueError , lambda : join ( a , b ) ) <EOL> def test_join_option_types ( ) : <EOL> a = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> b = symbol ( '<STR_LIT:b>' , '<STR_LIT>' ) <EOL> assert join ( a , b , '<STR_LIT:x>' ) . dshape == dshape ( '<STR_LIT>' ) <EOL> assert join ( b , a , '<STR_LIT:x>' ) . dshape == dshape ( '<STR_LIT>' ) <EOL> @ pytest . mark . xfail <EOL> def test_join_option_types_outer ( ) : <EOL> a = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> b = symbol ( '<STR_LIT:b>' , '<STR_LIT>' ) <EOL> assert ( join ( a , b , '<STR_LIT:x>' , how = '<STR_LIT>' ) . dshape == <EOL> join ( b , a , '<STR_LIT:x>' , how = '<STR_LIT>' ) . dshape == <EOL> dshape ( '<STR_LIT>' ) ) <EOL> def test_join_option_string_types ( ) : <EOL> a = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> b = symbol ( '<STR_LIT:b>' , '<STR_LIT>' ) <EOL> c = symbol ( '<STR_LIT:c>' , '<STR_LIT>' ) <EOL> assert ( join ( a , b , '<STR_LIT:x>' ) . dshape == <EOL> join ( b , a , '<STR_LIT:x>' ) . dshape == <EOL> dshape ( '<STR_LIT>' ) ) <EOL> assert ( join ( a , c , '<STR_LIT:x>' ) . dshape == <EOL> join ( c , a , '<STR_LIT:x>' ) . dshape == <EOL> dshape ( '<STR_LIT>' ) ) <EOL> def test_join_exceptions ( ) : <EOL> """<STR_LIT>""" <EOL> a = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> b = symbol ( '<STR_LIT:b>' , '<STR_LIT>' ) <EOL> with pytest . raises ( TypeError ) as excinfo : <EOL> join ( a , b , '<STR_LIT:x>' ) <EOL> assert "<STR_LIT>" in str ( excinfo . value ) <EOL> assert "<STR_LIT>" in str ( excinfo . value ) <EOL> b = symbol ( '<STR_LIT:b>' , '<STR_LIT>' ) <EOL> with pytest . raises ( ValueError ) as excinfo : <EOL> join ( a , b ) <EOL> assert "<STR_LIT>" in str ( excinfo . value ) <EOL> b = symbol ( '<STR_LIT:b>' , '<STR_LIT>' ) <EOL> with pytest . raises ( ValueError ) as excinfo : <EOL> join ( a , b , how = '<STR_LIT>' ) <EOL> assert "<STR_LIT>" in str ( excinfo . value ) <EOL> def test_join_type_promotion ( ) : <EOL> a = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> b = symbol ( '<STR_LIT:b>' , '<STR_LIT>' ) <EOL> assert join ( a , b , '<STR_LIT:x>' ) . dshape == dshape ( '<STR_LIT>' ) <EOL> def test_join_type_promotion_option ( ) : <EOL> a = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> b = symbol ( '<STR_LIT:b>' , '<STR_LIT>' ) <EOL> assert join ( a , b , '<STR_LIT:x>' ) . dshape == dshape ( '<STR_LIT>' ) <EOL> def test_isin ( ) : <EOL> a = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> assert hasattr ( a . x , '<STR_LIT>' ) <EOL> assert hasattr ( a . y , '<STR_LIT>' ) <EOL> assert not hasattr ( a , '<STR_LIT>' ) <EOL> def test_isin_no_expressions ( ) : <EOL> a = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> b = symbol ( '<STR_LIT:b>' , '<STR_LIT>' ) <EOL> with pytest . raises ( TypeError ) : <EOL> a . isin ( b ) <EOL> def test_concat_table ( ) : <EOL> a = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> b = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> v = symbol ( '<STR_LIT:v>' , '<STR_LIT>' ) <EOL> assert concat ( a , b ) . dshape == dshape ( '<STR_LIT>' ) <EOL> assert concat ( a , v ) . dshape == dshape ( '<STR_LIT>' ) <EOL> def test_concat_mat ( ) : <EOL> a = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> b = symbol ( '<STR_LIT:b>' , '<STR_LIT>' ) <EOL> v = symbol ( '<STR_LIT:v>' , '<STR_LIT>' ) <EOL> u = symbol ( '<STR_LIT:u>' , '<STR_LIT>' ) <EOL> assert concat ( a , b , axis = <NUM_LIT:0> ) . dshape == dshape ( '<STR_LIT>' ) <EOL> assert concat ( a , b , axis = <NUM_LIT:1> ) . dshape == dshape ( '<STR_LIT>' ) <EOL> assert concat ( a , v , axis = <NUM_LIT:0> ) . dshape == dshape ( '<STR_LIT>' ) <EOL> assert concat ( a , u , axis = <NUM_LIT:1> ) . dshape == dshape ( '<STR_LIT>' ) <EOL> def test_concat_arr ( ) : <EOL> a = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> b = symbol ( '<STR_LIT:b>' , '<STR_LIT>' ) <EOL> v = symbol ( '<STR_LIT:v>' , '<STR_LIT>' ) <EOL> assert concat ( a , b ) . dshape == dshape ( '<STR_LIT>' ) <EOL> assert concat ( a , v ) . dshape == dshape ( '<STR_LIT>' ) <EOL> def test_concat_different_measure ( ) : <EOL> a = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> b = symbol ( '<STR_LIT:b>' , '<STR_LIT>' ) <EOL> with pytest . raises ( TypeError ) as excinfo : <EOL> concat ( a , b ) <EOL> msg = '<STR_LIT>' . format ( l = a . dshape . measure , <EOL> r = b . dshape . measure ) <EOL> assert msg == str ( excinfo . value ) <EOL> def test_concat_different_along_concat_axis ( ) : <EOL> a = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> b = symbol ( '<STR_LIT:b>' , '<STR_LIT>' ) <EOL> with pytest . raises ( TypeError ) as excinfo : <EOL> concat ( a , b , axis = <NUM_LIT:0> ) <EOL> assert "<STR_LIT>" in str ( excinfo . value ) <EOL> b = symbol ( '<STR_LIT:b>' , '<STR_LIT>' ) <EOL> with pytest . raises ( TypeError ) as excinfo : <EOL> concat ( a , b , axis = <NUM_LIT:1> ) <EOL> assert "<STR_LIT>" in str ( excinfo . value ) <EOL> def test_concat_negative_axis ( ) : <EOL> a = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> b = symbol ( '<STR_LIT:b>' , '<STR_LIT>' ) <EOL> with pytest . raises ( ValueError ) as excinfo : <EOL> concat ( a , b , axis = - <NUM_LIT:1> ) <EOL> assert "<STR_LIT>" in str ( excinfo . value ) <EOL> def test_concat_axis_too_great ( ) : <EOL> a = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> b = symbol ( '<STR_LIT:b>' , '<STR_LIT>' ) <EOL> with pytest . raises ( ValueError ) as excinfo : <EOL> concat ( a , b , axis = <NUM_LIT:2> ) <EOL> assert "<STR_LIT>" in str ( excinfo . value ) <EOL> def test_shift ( ) : <EOL> t = symbol ( '<STR_LIT:t>' , '<STR_LIT>' ) <EOL> assert t . shift ( <NUM_LIT:1> ) . dshape == dshape ( '<STR_LIT>' ) <EOL> assert t . shift ( <NUM_LIT:0> ) . dshape == t . dshape <EOL> assert t . shift ( - <NUM_LIT:1> ) . dshape == dshape ( '<STR_LIT>' ) <EOL> assert str ( t . shift ( <NUM_LIT:1> ) ) == '<STR_LIT>' <EOL> assert str ( t . shift ( <NUM_LIT:0> ) ) == '<STR_LIT>' <EOL> assert str ( t . shift ( - <NUM_LIT:1> ) ) == '<STR_LIT>' <EOL> def test_shift_not_int ( ) : <EOL> a = symbol ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> with pytest . raises ( TypeError ) : <EOL> a . x . shift ( <NUM_LIT> ) </s>
