<s> from __future__ import division <EOL> import json <EOL> import fnmatch <EOL> import nose <EOL> import os <EOL> import shutil <EOL> import subprocess <EOL> import tempfile <EOL> import time <EOL> import unittest <EOL> import signal <EOL> import kudu <EOL> class KuduBasicsBase ( object ) : <EOL> """<STR_LIT>""" <EOL> BASE_PORT = <NUM_LIT> <EOL> NUM_TABLET_SERVERS = <NUM_LIT:3> <EOL> @ classmethod <EOL> def start_cluster ( cls ) : <EOL> local_path = tempfile . mkdtemp ( dir = os . getenv ( "<STR_LIT>" , None ) ) <EOL> bin_path = "<STR_LIT>" . format ( os . getenv ( "<STR_LIT>" ) ) <EOL> os . makedirs ( "<STR_LIT>" . format ( local_path ) ) <EOL> os . makedirs ( "<STR_LIT>" . format ( local_path ) ) <EOL> os . makedirs ( "<STR_LIT>" . format ( local_path ) ) <EOL> path = [ "<STR_LIT>" . format ( bin_path ) , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" . format ( local_path ) , <EOL> "<STR_LIT>" . format ( local_path ) , <EOL> "<STR_LIT>" . format ( local_path ) , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" . format ( local_path ) <EOL> ] <EOL> p = subprocess . Popen ( path , shell = False ) <EOL> fid = open ( "<STR_LIT>" . format ( local_path ) , "<STR_LIT>" ) <EOL> fid . write ( "<STR_LIT>" . format ( p . pid ) ) <EOL> fid . close ( ) <EOL> config_file = "<STR_LIT>" . format ( local_path ) <EOL> for _ in range ( <NUM_LIT:30> ) : <EOL> if os . path . exists ( config_file ) : <EOL> break <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> master_config = json . load ( open ( "<STR_LIT>" . format ( local_path ) , "<STR_LIT:r>" ) ) <EOL> master_port = master_config [ "<STR_LIT>" ] [ <NUM_LIT:0> ] [ "<STR_LIT:port>" ] <EOL> for m in range ( cls . NUM_TABLET_SERVERS ) : <EOL> os . makedirs ( "<STR_LIT>" . format ( local_path , m ) ) <EOL> os . makedirs ( "<STR_LIT>" . format ( local_path , m ) ) <EOL> path = [ "<STR_LIT>" . format ( bin_path ) , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" . format ( master_port ) , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" . format ( local_path ) , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" . format ( local_path , m ) , <EOL> "<STR_LIT>" . format ( local_path , m ) , <EOL> ] <EOL> p = subprocess . Popen ( path , shell = False ) <EOL> fid = open ( "<STR_LIT>" . format ( local_path , m ) , "<STR_LIT>" ) <EOL> fid . write ( "<STR_LIT>" . format ( p . pid ) ) <EOL> fid . close ( ) <EOL> return local_path , master_port <EOL> @ classmethod <EOL> def stop_cluster ( cls , path ) : <EOL> for root , dirnames , filenames in os . walk ( '<STR_LIT>' . format ( path ) ) : <EOL> for filename in fnmatch . filter ( filenames , '<STR_LIT>' ) : <EOL> with open ( os . path . join ( root , filename ) ) as fid : <EOL> a = fid . read ( ) <EOL> r = subprocess . Popen ( [ "<STR_LIT>" , "<STR_LIT>" . format ( a ) ] ) <EOL> r . wait ( ) <EOL> os . remove ( os . path . join ( root , filename ) ) <EOL> shutil . rmtree ( path , True ) <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> cls . cluster_path , master_port = cls . start_cluster ( ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> cls . client = kudu . Client ( '<STR_LIT>' . format ( master_port ) ) <EOL> cls . schema = cls . example_schema ( ) <EOL> cls . ex_table = '<STR_LIT>' <EOL> if cls . client . table_exists ( cls . ex_table ) : <EOL> cls . client . delete_table ( cls . ex_table ) <EOL> cls . client . create_table ( cls . ex_table , cls . schema ) <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> cls . stop_cluster ( cls . cluster_path ) <EOL> @ classmethod <EOL> def example_schema ( cls ) : <EOL> col1 = kudu . ColumnSchema . create ( '<STR_LIT:key>' , kudu . INT32 ) <EOL> col2 = kudu . ColumnSchema . create ( '<STR_LIT>' , kudu . INT32 ) <EOL> col3 = kudu . ColumnSchema . create ( '<STR_LIT>' , kudu . STRING ) <EOL> return kudu . schema_from_list ( [ col1 , col2 , col3 ] , <NUM_LIT:1> ) <EOL> class TestSchema ( unittest . TestCase ) : <EOL> def test_column_schema ( self ) : <EOL> pass <EOL> def test_create_schema ( self ) : <EOL> col1 = kudu . ColumnSchema . create ( '<STR_LIT:key>' , kudu . INT32 ) <EOL> col2 = kudu . ColumnSchema . create ( '<STR_LIT>' , kudu . INT32 ) <EOL> col3 = kudu . ColumnSchema . create ( '<STR_LIT>' , kudu . STRING ) <EOL> cols = [ col1 , col2 , col3 ] <EOL> schema = kudu . schema_from_list ( cols , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( schema ) , <NUM_LIT:3> ) <EOL> self . assertTrue ( schema . at ( <NUM_LIT:0> ) . equals ( col1 ) ) <EOL> self . assertTrue ( schema . at ( <NUM_LIT:1> ) . equals ( col2 ) ) <EOL> self . assertTrue ( schema . at ( <NUM_LIT:2> ) . equals ( col3 ) ) <EOL> def test_column_schema_repr ( self ) : <EOL> col1 = kudu . ColumnSchema . create ( '<STR_LIT:key>' , kudu . INT32 ) <EOL> result = repr ( col1 ) <EOL> expected = '<STR_LIT>' <EOL> self . assertEqual ( result , expected ) <EOL> def test_column_schema_default_value ( self ) : <EOL> pass <EOL> class TestTable ( KuduBasicsBase , unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> pass <EOL> def test_table_basics ( self ) : <EOL> table = self . client . open_table ( self . ex_table ) <EOL> self . assertEqual ( table . name , self . ex_table ) <EOL> self . assertEqual ( table . num_columns , len ( self . schema ) ) <EOL> def test_table_exists ( self ) : <EOL> self . assertFalse ( self . client . table_exists ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( self . client . table_exists ( self . ex_table ) ) <EOL> def test_delete_table ( self ) : <EOL> name = "<STR_LIT>" <EOL> self . client . create_table ( name , self . schema ) <EOL> self . assertTrue ( self . client . delete_table ( name ) ) <EOL> self . assertFalse ( self . client . table_exists ( name ) ) <EOL> val = self . client . delete_table ( name ) <EOL> self . assertFalse ( val ) <EOL> def test_open_table_nonexistent ( self ) : <EOL> self . assertRaises ( kudu . KuduException , self . client . open_table , <EOL> '<STR_LIT>' ) <EOL> def test_insert_nonexistent_field ( self ) : <EOL> table = self . client . open_table ( self . ex_table ) <EOL> op = table . insert ( ) <EOL> self . assertRaises ( KeyError , op . __setitem__ , '<STR_LIT>' , <NUM_LIT:12> ) <EOL> def test_insert_rows_and_delete ( self ) : <EOL> nrows = <NUM_LIT:100> <EOL> table = self . client . open_table ( self . ex_table ) <EOL> session = self . client . new_session ( ) <EOL> for i in range ( nrows ) : <EOL> op = table . insert ( ) <EOL> op [ '<STR_LIT:key>' ] = i <EOL> op [ '<STR_LIT>' ] = i * <NUM_LIT:2> <EOL> op [ '<STR_LIT>' ] = '<STR_LIT>' % i <EOL> session . apply ( op ) <EOL> self . assertRaises ( Exception , session . apply , op ) <EOL> self . assertTrue ( session . flush ( ) ) <EOL> for i in range ( nrows ) : <EOL> op = table . delete ( ) <EOL> op [ '<STR_LIT:key>' ] = i <EOL> session . apply ( op ) <EOL> session . flush ( ) <EOL> def test_capture_kudu_error ( self ) : <EOL> pass <EOL> class TestScanner ( KuduBasicsBase , unittest . TestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> super ( TestScanner , cls ) . setUpClass ( ) <EOL> cls . nrows = <NUM_LIT:100> <EOL> table = cls . client . open_table ( cls . ex_table ) <EOL> session = cls . client . new_session ( ) <EOL> tuples = [ ] <EOL> for i in range ( cls . nrows ) : <EOL> op = table . insert ( ) <EOL> tup = i , i * <NUM_LIT:2> , '<STR_LIT>' % i <EOL> op [ '<STR_LIT:key>' ] = tup [ <NUM_LIT:0> ] <EOL> op [ '<STR_LIT>' ] = tup [ <NUM_LIT:1> ] <EOL> op [ '<STR_LIT>' ] = tup [ <NUM_LIT:2> ] <EOL> session . apply ( op ) <EOL> tuples . append ( tup ) <EOL> session . flush ( ) <EOL> cls . table = table <EOL> cls . tuples = tuples <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> pass <EOL> def setUp ( self ) : <EOL> pass <EOL> def test_scan_rows_basic ( self ) : <EOL> scanner = self . table . scanner ( ) . open ( ) <EOL> batch = scanner . read_all ( ) <EOL> self . assertEqual ( len ( batch ) , self . nrows ) <EOL> result_tuples = batch . as_tuples ( ) <EOL> self . assertEqual ( result_tuples , self . tuples ) <EOL> def test_scan_rows_simple_predicate ( self ) : <EOL> scanner = self . table . scanner ( ) <EOL> scanner . add_comparison_predicate ( "<STR_LIT:key>" , kudu . GREATER_EQUAL , <NUM_LIT:20> ) <EOL> scanner . add_comparison_predicate ( "<STR_LIT:key>" , kudu . LESS_EQUAL , <NUM_LIT> ) <EOL> scanner . open ( ) <EOL> batch = scanner . read_all ( ) <EOL> tuples = batch . as_tuples ( ) <EOL> self . assertEqual ( tuples , self . tuples [ <NUM_LIT:20> : <NUM_LIT:50> ] ) <EOL> def test_scan_rows_string_predicate ( self ) : <EOL> scanner = self . table . scanner ( ) <EOL> scanner . add_comparison_predicate ( "<STR_LIT>" , kudu . GREATER_EQUAL , "<STR_LIT>" ) <EOL> scanner . add_comparison_predicate ( "<STR_LIT>" , kudu . LESS_EQUAL , "<STR_LIT>" ) <EOL> scanner . open ( ) <EOL> batch = scanner . read_all ( ) <EOL> tuples = batch . as_tuples ( ) <EOL> self . assertEqual ( tuples , self . tuples [ <NUM_LIT:20> : <NUM_LIT> ] ) <EOL> def test_scan_invalid_predicates ( self ) : <EOL> scanner = self . table . scanner ( ) <EOL> try : <EOL> scanner . add_comparison_predicate ( "<STR_LIT:foo>" , kudu . GREATER_EQUAL , "<STR_LIT:x>" ) <EOL> except Exception , e : <EOL> self . assertEqual ( "<STR_LIT>" , str ( e ) ) <EOL> try : <EOL> scanner . add_comparison_predicate ( "<STR_LIT>" , kudu . GREATER_EQUAL , <NUM_LIT:1> ) <EOL> except Exception , e : <EOL> self . assertEqual ( "<STR_LIT>" + <EOL> "<STR_LIT>" , str ( e ) ) <EOL> try : <EOL> scanner . add_comparison_predicate ( "<STR_LIT>" , kudu . GREATER_EQUAL , None ) <EOL> except Exception , e : <EOL> self . assertEqual ( "<STR_LIT>" , str ( e ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> nose . runmodule ( argv = [ __file__ , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] , exit = False ) </s>
