def test_command_timeout(self): with CrateShell(node.http_url) as crash: crash.process(""" CREATE FUNCTION fib(long) RETURNS LONG LANGUAGE javascript AS ' function fib(n) { if (n < 2) return 1; return fib(n - 1) + fib(n - 2); }' """) timeout = 0.1 slow_query = "SELECT fib(35)" # without verbose with CrateShell(node.http_url, error_trace=False, timeout=timeout) as crash: crash.logger = Mock() crash.process(slow_query) crash.logger.warn.assert_any_call( "Use \\connect <server> to connect to one or more servers first." ) # with verbose with CrateShell(node.http_url, error_trace=True, timeout=timeout) as crash: crash.logger = Mock() crash.process(slow_query) crash.logger.warn.assert_any_call( "No more Servers available, exception from last server: HTTPConnectionPool(host='127.0.0.1', port=44209): Read timed out. (read timeout=0.1)" ) crash.logger.warn.assert_any_call( "Use \\connect <server> to connect to one or more servers first." )
def test_help_command(self): """Test output of help command""" command = CrateShell(is_tty=False) expected = "\n".join([ '\\? print this help', '\\autocapitalize toggle automatic capitalization of SQL keywords', '\\autocomplete toggle autocomplete', '\\c connect to the given server, e.g.: \\connect localhost:4200', '\\check print failed cluster and/or node checks, e.g. \\check nodes', '\\connect connect to the given server, e.g.: \\connect localhost:4200', '\\dt print the existing tables within the \'doc\' schema', '\\format switch output format', '\\q quit crash', '\\r read and execute statements from a file', '\\sysinfo print system and cluster info', '\\verbose toggle verbose mode', ]) help_ = command.commands['?'] self.assertTrue(isinstance(help_, Command)) self.assertEqual(expected, help_(command)) with CrateShell(is_tty=False) as cmd: output = StringIO() cmd.logger = ColorPrinter(False, stream=output) text = help_(cmd, 'arg1', 'arg2') self.assertEqual(None, text) self.assertEqual('Command does not take any arguments.\n', output.getvalue())
def test_post_2_0(self): cmd = CrateShell() cmd._exec_and_print = MagicMock() cmd.connection.lowest_server_version = StrictVersion("2.0.0") cmd._show_tables() cmd._exec_and_print.assert_called_with( "SELECT format('%s.%s', table_schema, table_name) AS name FROM information_schema.tables WHERE table_schema NOT IN ('sys','information_schema', 'pg_catalog') AND table_type = 'BASE TABLE'" )
def test_pre_0_57(self): cmd = CrateShell() cmd._exec_and_print = MagicMock() cmd.connection.lowest_server_version = StrictVersion("0.56.4") cmd._show_tables() cmd._exec_and_print.assert_called_with( "SELECT format('%s.%s', schema_name, table_name) AS name FROM information_schema.tables WHERE schema_name NOT IN ('sys','information_schema', 'pg_catalog')" )
def test_close_shell(self): crash = CrateShell(node.http_url) self.assertFalse(crash.is_closed()) self.assertTrue(crash.is_conn_available()) crash.close() self.assertTrue(crash.is_closed()) self.assertFalse(crash.is_conn_available()) with self.assertRaises(ProgrammingError) as ctx: crash.close() self.assertEqual('CrateShell is already closed', ctx.exception.message)
def test_first_column_first_row_empty(self): self.maxDiff = None rows = (['', 1.0], ['Aldebaran', 1.0], ['Algol', 1.0], ['Allosimanius Syneca', 1.0], ['Alpha Centauri', 1.0], ['Argabuthon', 1.0], ['Arkintoofle Minor', 1.0], ['Galactic Sector QQ7 Active J Gamma', 1.0], ['North West Ripple', 1.0], ['Outer Eastern Rim', 1.0], ['NULL', 1.0]) expected = "\n".join([ '+------------------------------------+--------+', '| name | _score |', '+------------------------------------+--------+', '| | 1.0 |', '| Aldebaran | 1.0 |', '| Algol | 1.0 |', '| Allosimanius Syneca | 1.0 |', '| Alpha Centauri | 1.0 |', '| Argabuthon | 1.0 |', '| Arkintoofle Minor | 1.0 |', '| Galactic Sector QQ7 Active J Gamma | 1.0 |', '| North West Ripple | 1.0 |', '| Outer Eastern Rim | 1.0 |', '| NULL | 1.0 |', '+------------------------------------+--------+\n' ]) with CrateShell() as cmd: with patch('sys.stdout', new_callable=StringIO) as output: cmd.pprint(rows, cols=['name', '_score']) self.assertEqual(expected, output.getvalue())
def test_any_empty(self): self.maxDiff = None rows = (['Features and conformance views', 'FALSE', '', ''], [ 'Features and conformance views', 'TRUE', 1, 'SQL_FEATURES view' ], ['Features and conformance views', 'FALSE', 2, 'SQL_SIZING view'], [ 'Features and conformance views', 'FALSE', 3, 'SQL_LANGUAGES view' ]) expected = "\n".join([ '+--------------------------------+--------------+----------------+--------------------+', '| feature_name | is_supported | sub_feature_id | sub_feature_name |', '+--------------------------------+--------------+----------------+--------------------+', '| Features and conformance views | FALSE | | |', '| Features and conformance views | TRUE | 1 | SQL_FEATURES view |', '| Features and conformance views | FALSE | 2 | SQL_SIZING view |', '| Features and conformance views | FALSE | 3 | SQL_LANGUAGES view |', '+--------------------------------+--------------+----------------+--------------------+\n' ]) with CrateShell() as cmd: with patch('sys.stdout', new_callable=StringIO) as output: cmd.pprint(rows, cols=[ 'feature_name', 'is_supported', 'sub_feature_id', 'sub_feature_name' ]) self.assertEqual(expected, output.getvalue())
def test_ssl_params(self): tmpdirname = tempfile.mkdtemp() cert_filename = os.path.join(tmpdirname, "cert_file") key_filename = os.path.join(tmpdirname, "key_file") ca_cert_filename = os.path.join(tmpdirname, "ca_cert_file") open(cert_filename, 'a').close() open(key_filename, 'a').close() open(ca_cert_filename, 'a').close() with CrateShell(node.http_url, verify_ssl=False, cert_file=cert_filename, key_file=key_filename, ca_cert_file=ca_cert_filename) as crash: self.assertEqual(crash.verify_ssl, False) self.assertEqual(crash.connection.client._pool_kw['cert_reqs'], ssl.CERT_NONE) self.assertEqual(crash.cert_file, cert_filename) self.assertEqual(crash.connection.client._pool_kw['cert_file'], cert_filename) self.assertEqual(crash.key_file, key_filename) self.assertEqual(crash.connection.client._pool_kw['key_file'], key_filename) self.assertEqual(crash.ca_cert_file, ca_cert_filename) self.assertEqual(crash.connection.client._pool_kw['ca_certs'], ca_cert_filename)
def test_verbose_no_error_trace(self): with CrateShell(error_trace=True) as cmd: cmd.logger = Mock() cmd.cursor.execute = Mock(side_effect=ProgrammingError( msg="the error message", error_trace=None)) cmd._exec_and_print("select invalid statement") # only the message is logged cmd.logger.critical.assert_called_once_with("the error message")
def test_rendering_float(self): """Test rendering an array""" expected = "\n".join([ '+---------------+', '| number |', '+---------------+', '| 3.1415926535 |', '| 42.0 |', '+---------------+\n' ]) with CrateShell() as cmd: with patch('sys.stdout', new_callable=StringIO) as output: cmd.pprint([[3.1415926535], [42.0]], ['number']) self.assertEqual(expected, output.getvalue())
def test_pprint_dont_guess_type(self): "Output: table with duplicate keys" expected = "\n".join([ "+---------+", "| version |", "+---------+", "| 0.50 |", "+---------+\n" ]) with CrateShell() as cmd: with patch('sys.stdout', new_callable=StringIO) as output: cmd.pprint([["0.50"]], ['version']) self.assertEqual(expected, output.getvalue())
def test_pprint_duplicate_keys(self): "Output: table with duplicate keys" expected = "\n".join([ "+------+------+", "| name | name |", "+------+------+", "+------+------+\n" ]) with CrateShell() as cmd: with patch('sys.stdout', new_callable=StringIO) as output: cmd.pprint([], ['name', 'name']) self.assertEqual(expected, output.getvalue())
def test_connect_info(self): with CrateShell(node.http_url, username='******', schema='test') as crash: self.assertEqual(crash.connect_info.user, "crate") self.assertEqual(crash.connect_info.schema, "test") self.execute = crash.cursor.execute crash.cursor.execute = self.mock_execute crash._fetch_session_info() self.assertEqual(crash.connect_info.user, None) self.assertEqual(crash.connect_info.schema, "test")
def test_rendering_array(self): """Test rendering an array""" names = ['Arthur', 'Ford'] expected = "\n".join([ '+--------------------+', '| names |', '+--------------------+', '| ["Arthur", "Ford"] |', '+--------------------+\n' ]) with CrateShell() as cmd: with patch('sys.stdout', new_callable=StringIO) as output: cmd.pprint([[names]], ['names']) self.assertEqual(expected, output.getvalue())
def test_tabulate_null_int_column(self): """ Create a column with a non-string value and NULL. """ rows = [[1], [None]] expected = "\n".join([ '+------+', '| x |', '+------+', '| 1 |', '| NULL |', '+------+\n' ]) with CrateShell() as cmd: with patch('sys.stdout', new_callable=StringIO) as output: cmd.pprint(rows, cols=['x']) self.assertEqual(expected, output.getvalue())
def test_multiline_header(self): """ Create another column with a non-string value and FALSE. """ rows = [['FALSE'], [1]] expected = "\n".join([ '+-------+', '| x |', '| y |', '+-------+', '| FALSE |', '| 1 |', '+-------+\n' ]) with CrateShell() as cmd: with patch('sys.stdout', new_callable=StringIO) as output: cmd.pprint(rows, cols=['x\ny']) self.assertEqual(expected, output.getvalue())
def test_rendering_object(self): """Test rendering an object""" user = {'name': 'Arthur', 'age': 42} expected = "\n".join([ '+-------------------------------+', '| user |', '+-------------------------------+', '| {"age": 42, "name": "Arthur"} |', '+-------------------------------+\n' ]) with CrateShell() as cmd: with patch('sys.stdout', new_callable=StringIO) as output: cmd.pprint([[user]], ['user']) self.assertEqual(expected, output.getvalue())
def test_empty_line_first_row_first_column(self): self.maxDiff = None rows = (['', 'Planet'], ['Aldebaran', 'Star System'], ['Berlin', 'City'], ['Galactic Sector QQ7 Active J Gamma', 'Galaxy']) expected = "\n".join([ '+------------------------------------+-------------+', '| min(name) | kind |', '+------------------------------------+-------------+', '| | Planet |', '| Aldebaran | Star System |', '| Berlin | City |', '| Galactic Sector QQ7 Active J Gamma | Galaxy |', '+------------------------------------+-------------+\n' ]) with CrateShell() as cmd: with patch('sys.stdout', new_callable=StringIO) as output: cmd.pprint(rows, cols=['min(name)', 'kind']) self.assertEqual(expected, output.getvalue())
def test_empty_first_row(self): self.maxDiff = None rows = (['', ''], ['Aldebaran', 'Aldebaran'], ['Algol', 'Algol'], ['Allosimanius Syneca', 'Allosimanius - Syneca' ], ['Alpha Centauri', 'Alpha - Centauri']) expected = "\n".join([ '+---------------------+-----------------------+', '| name | replaced |', '+---------------------+-----------------------+', '| | |', '| Aldebaran | Aldebaran |', '| Algol | Algol |', '| Allosimanius Syneca | Allosimanius - Syneca |', '| Alpha Centauri | Alpha - Centauri |', '+---------------------+-----------------------+\n' ]) with CrateShell() as cmd: with patch('sys.stdout', new_callable=StringIO) as output: cmd.pprint(rows, cols=['name', 'replaced']) self.assertEqual(expected, output.getvalue())
def test_multiline_row(self): """ Create ta column that holds rows with multiline text. """ rows = [[ 'create table foo (\n id integer,\n name string\n)', 'foo\nbar', 1 ]] expected = "\n".join([ '+-----------------------+-----+---+', '| show create table foo | a | b |', '+-----------------------+-----+---+', '| create table foo ( | foo | 1 |', '| id integer, | bar | |', '| name string | | |', '| ) | | |', '+-----------------------+-----+---+\n' ]) with CrateShell() as cmd: with patch('sys.stdout', new_callable=StringIO) as output: cmd.pprint(rows, cols=['show create table foo', 'a', 'b']) self.assertEqual(expected, output.getvalue())
def setUp(self): cmd = CrateShell() self.capitalizer = Capitalizer(cmd)
def test_create_buffer(self): cmd = CrateShell() buffer = create_buffer(cmd, '/tmp/history') self.assertEqual(buffer.on_text_insert.fire(), None)
def setUp(self): cmd = CrateShell() self.completer = SQLCompleter(cmd)
def test_connect_info_not_available(self, is_conn_available): is_conn_available.return_value = False with CrateShell(node.http_url, username='******', schema='test') as crash: self.assertEqual(crash.connect_info.user, None) self.assertEqual(crash.connect_info.schema, None)
def test_username_param(self): with CrateShell(node.http_url, username='******') as crash: self.assertEqual(crash.username, "crate") self.assertEqual(crash.connection.client.username, "crate")