<s> import numpy as np <EOL> from nose . tools import raises <EOL> from dbtools import Table <EOL> from . import RewriteDocstringMeta <EOL> from . table_base import TestTable <EOL> try : <EOL> xrange <EOL> except NameError : <EOL> xrange = range <EOL> class TestTablePrimaryKey ( TestTable ) : <EOL> __metaclass__ = RewriteDocstringMeta <EOL> def setup ( self ) : <EOL> self . tbl = Table . create ( <EOL> '<STR_LIT>' , "<STR_LIT>" , self . dtypes , <EOL> primary_key = '<STR_LIT:id>' , autoincrement = True , <EOL> verbose = True ) <EOL> def check_data ( self , indata , outdata ) : <EOL> out = True <EOL> if not ( indata [ : , <NUM_LIT:1> : ] == outdata . as_matrix ( ) ) . all ( ) : <EOL> out = False <EOL> return out <EOL> def check_index ( self , indata , outdata ) : <EOL> out = True <EOL> if not ( indata [ : , <NUM_LIT:0> ] == np . array ( outdata . index ) ) . all ( ) : <EOL> out = False <EOL> return out <EOL> def test_create_autoincrement ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . tbl . autoincrement <EOL> def test_create_primary_key ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . tbl . primary_key == '<STR_LIT:id>' <EOL> def test_create_from_dataframe ( self ) : <EOL> """<STR_LIT>""" <EOL> self . insert ( ) <EOL> data = self . tbl . select ( ) <EOL> data . index . name = None <EOL> tbl = Table . create ( '<STR_LIT>' , "<STR_LIT>" , data , verbose = True , <EOL> primary_key = '<STR_LIT:id>' , autoincrement = True ) <EOL> self . check ( self . idata , tbl . select ( ) ) <EOL> @ raises ( ValueError ) <EOL> def test_create_from_dataframe_invalid_pk ( self ) : <EOL> """<STR_LIT>""" <EOL> self . insert ( ) <EOL> data = self . tbl . select ( ) <EOL> Table . create ( <EOL> '<STR_LIT>' , "<STR_LIT>" , data , <EOL> primary_key = '<STR_LIT:foo>' , verbose = True ) <EOL> def test_create_from_dicts ( self ) : <EOL> """<STR_LIT>""" <EOL> cols = list ( zip ( * self . dtypes ) ) [ <NUM_LIT:0> ] <EOL> dicts = [ dict ( [ ( cols [ i ] , d [ i ] ) for i in xrange ( len ( d ) ) ] ) <EOL> for d in self . idata ] <EOL> tbl = Table . create ( <EOL> '<STR_LIT>' , "<STR_LIT>" , dicts , verbose = True , <EOL> primary_key = '<STR_LIT:id>' , autoincrement = True ) <EOL> self . check_index ( self . idata , tbl . select ( ) ) <EOL> for idx , col in enumerate ( cols ) : <EOL> if col == '<STR_LIT:id>' : <EOL> continue <EOL> self . check_data ( self . idata [ : , [ <NUM_LIT:0> , idx ] ] , tbl [ col ] ) <EOL> def test_select_columns ( self ) : <EOL> """<STR_LIT>""" <EOL> self . insert ( ) <EOL> data = self . tbl . select ( ) <EOL> assert ( u'<STR_LIT:id>' , ) + tuple ( data . columns ) == self . tbl . columns <EOL> def test_slice_name ( self ) : <EOL> """<STR_LIT>""" <EOL> self . insert ( ) <EOL> data = self . tbl [ '<STR_LIT:name>' ] <EOL> assert self . check ( self . idata [ : , [ <NUM_LIT:0> , <NUM_LIT:1> ] ] , data ) <EOL> def test_slice_name_age ( self ) : <EOL> """<STR_LIT>""" <EOL> self . insert ( ) <EOL> data = self . tbl [ '<STR_LIT:name>' , '<STR_LIT>' ] <EOL> assert self . check ( self . idata [ : , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] ] , data ) <EOL> def test_slice_name_height ( self ) : <EOL> """<STR_LIT>""" <EOL> self . insert ( ) <EOL> data = self . tbl [ '<STR_LIT:name>' , '<STR_LIT>' ] <EOL> assert self . check ( self . idata [ : , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:3> ] ] , data ) <EOL> def test_index_0 ( self ) : <EOL> """<STR_LIT>""" <EOL> self . insert ( ) <EOL> data = self . tbl [ <NUM_LIT:0> ] <EOL> assert self . check ( self . idata [ : <NUM_LIT:0> ] , data ) <EOL> def test_index_1 ( self ) : <EOL> """<STR_LIT>""" <EOL> self . insert ( ) <EOL> data = self . tbl [ <NUM_LIT:2> ] <EOL> assert self . check ( self . idata [ : <NUM_LIT:1> ] , data ) <EOL> def test_index_12 ( self ) : <EOL> """<STR_LIT>""" <EOL> self . insert ( ) <EOL> data = self . tbl [ <NUM_LIT:2> : <NUM_LIT:6> ] <EOL> assert self . check ( self . idata [ : <NUM_LIT:2> ] , data ) <EOL> def test_index_lt_3 ( self ) : <EOL> """<STR_LIT>""" <EOL> self . insert ( ) <EOL> data = self . tbl [ : <NUM_LIT:6> ] <EOL> assert self . check ( self . idata [ : <NUM_LIT:2> ] , data ) <EOL> def test_index_geq_3 ( self ) : <EOL> """<STR_LIT>""" <EOL> self . insert ( ) <EOL> data = self . tbl [ <NUM_LIT:6> : ] <EOL> assert self . check ( self . idata [ <NUM_LIT:2> : ] , data ) <EOL> @ raises ( ValueError ) <EOL> def test_index_alternate ( self ) : <EOL> """<STR_LIT>""" <EOL> self . insert ( ) <EOL> self . tbl [ : : <NUM_LIT:2> ] </s>
