def test_missing_files(project_dir, temp_dir, resolve_symlinks, caplog): args = ["backup", "-t", str(temp_dir)] if resolve_symlinks: args.append("--resolve-symlinks") result = run(args) assert result.exit_code == 1 assert log_message(logging.ERROR, r"\.FileNotFoundError:", caplog) extends_content = {"version": "2", "services": {"none": {"image": "busybox"}}} with project_dir.add_file("void.yml").open("tw") as f: yaml.dump(extends_content, f) caplog.clear() result = run(args) assert result.exit_code == 1 assert log_message( logging.ERROR, r"build path .*/lost either does not exist", caplog ) project_dir.add_folder("lost") caplog.clear() result = run(args) assert result.exit_code == 1 assert log_message( logging.ERROR, r"Couldn't find env file: .*/dangling_link$", caplog ) project_dir.add_file("gone") caplog.clear() assert result_okay(args)
def test_missing_files(project_dir, temp_dir, resolve_symlinks, caplog): args = ['backup', '-t', temp_dir] if resolve_symlinks: args.append('--resolve-symlinks') result = run(args) assert result.exit_code == 1 assert log_message(logging.ERROR, r'\.FileNotFoundError:', caplog) extends_content = {'version': '2', 'services': {'none': {'image': 'busybox'}}} with project_dir.add_file('void.yml').open('tw') as f: yaml.dump(extends_content, f) caplog.clear() result = run(args) assert result.exit_code == 1 assert log_message(logging.ERROR, r'build path .*/lost either does not exist', caplog) project_dir.add_folder('lost') caplog.clear() result = run(args) assert result.exit_code == 1 assert log_message(logging.ERROR, r"Couldn't find env file: .*/dangling_link$", caplog) project_dir.add_file('gone') caplog.clear() assert result_okay(run(args))
def test_bytea_field_support_in_output(executor): run(executor, "create table binarydata(c bytea)") run(executor, "insert into binarydata (c) values (decode('DEADBEEF', 'hex'))") assert u'\\xdeadbeef' in run(executor, "select * from binarydata", join=True)
def test_unicode_support_in_output(executor, expanded): run(executor, "create table unicodechars(t text)") run(executor, u"insert into unicodechars (t) values ('é')") # See issue #24, this raises an exception without proper handling assert u'é' in run(executor, "select * from unicodechars", join=True, expanded=expanded)
def test_json_renders_without_u_prefix(executor, expanded): run(executor, u"create table jsontest(d json)") run(executor, u"""insert into jsontest (d) values ('{"name": "Éowyn"}')""") result = run(executor, u"SELECT d FROM jsontest LIMIT 1", join=True, expanded=expanded) assert u'{"name": "Éowyn"}' in result
def test_help_argument(self): p = run("python " + self.script_path + " -h") std_out = p.stdout.read() self.assertNotIn("Please insert valid arguments", std_out) self.assertIn("Options:", std_out) p = run("python " + self.script_path + " --help") std_out = p.stdout.read() self.assertNotIn("Please insert valid arguments", std_out) self.assertIn("Options:", std_out)
def test_missing_argument(self): p = run("python " + self.script_path + " --images=" + os.path.join(self.files_path, 'empty.txt')) std_out = p.stdout.read() self.assertIn("Please insert valid arguments", std_out) self.assertIn("Options:", std_out) p = run("python " + self.script_path + " --save_dir=/tmp/save_dir") std_out = p.stdout.read() self.assertIn("Please insert valid arguments", std_out) self.assertIn("Options:", std_out)
def test_i_works(tmpdir, executor): sqlfile = tmpdir.join("test.sql") sqlfile.write("SELECT NOW()") rcfile = str(tmpdir.join("rcfile")) cli = PGCli( pgexecute=executor, pgclirc_file=rcfile, ) statement = r"\i {0}".format(sqlfile) run(executor, statement, pgspecial=cli.pgspecial)
def test_conn(executor): run(executor, '''create table test(a text)''') run(executor, '''insert into test values('abc')''') assert run(executor, '''select * from test''', join=True) == dedent("""\ +-----+ | a | |-----| | abc | +-----+ SELECT 1""")
def test_bools_are_treated_as_strings(executor): run(executor, '''create table test(a boolean)''') run(executor, '''insert into test values(True)''') assert run(executor, '''select * from test''', join=True) == dedent("""\ +------+ | a | |------| | True | +------+ SELECT 1""")
def test_function_definition(executor): run( executor, ''' CREATE OR REPLACE FUNCTION public.the_number_three() RETURNS int LANGUAGE sql AS $function$ select 3; $function$ ''') result = executor.function_definition('the_number_three')
def test_empty_file_argument(self): p = run("python " + self.script_path + " --create --save_dir=/tmp/save_dir --images=" + os.path.join( self.files_path, 'empty.txt')) std_out = p.stdout.read() std_err = p.stderr.read() self.assertIn("Exception: Images File is Empty", std_err) self.assertEqual("", std_out)
def test_save_dir_not_found(self): p = run("python " + self.script_path + " --save_dir=/not_found_dir --images=" + os.path.join(self.files_path, 'all_valid.txt')) std_out = p.stdout.read() std_err = p.stderr.read() self.assertIn("OSError: Save Directory not found", std_err) self.assertEqual("", std_out)
def test_save_dir_permission_denied(self): p = run("python " + self.script_path + " --create --save_dir=/not_found_dir --images=" + os.path.join( self.files_path, 'all_valid.txt')) std_out = p.stdout.read() std_err = p.stderr.read() self.assertIn("Permission denied", std_err) self.assertEqual("", std_out)
def test_yet_another_blog(project_dir, temp_dir, target, compression, resolve_symlinks): args = ['backup'] if target: args.extend(['-t', temp_dir]) if compression: args.extend(['--compression', compression]) if resolve_symlinks: args.append('--resolve-symlinks') assert result_okay(run(args)) if target: target_item = get_target_folder(temp_dir) else: return # FIXME https://github.com/pytest-dev/pytest/issues/1407 out, err = capsys.readouterr() target_item = temp_dir / 'archive' with target_item.open('wb') as f: f.write(out) if compression: assert target_item.is_file() with TemporaryDirectory() as tmp: archive = tarfile.open(str(target_item)) archive.extractall(tmp) assert identical_folder_contents(project_dir, Path(tmp) / 'config', resolve_symlinks) else: assert target_item.is_dir() target_folder = target_item assert identical_folder_contents(project_dir, target_folder / 'config', resolve_symlinks)
def test_nested_extends(project_dir, temp_dir, resolve_symlinks): args = ['backup', '-t', temp_dir] if resolve_symlinks: args.append('--resolve-symlinks') assert result_okay(run(args)) target_folder = get_target_folder(temp_dir) assert identical_folder_contents(project_dir, target_folder / 'config')
def test_all_valid(self): p = run( "python " + self.script_path + " --create --save_dir=/tmp/out_dir --images=" + os.path.join(self.files_path, 'all_valid.txt')) self.assertEquals(4, len(os.listdir("/tmp/out_dir/images/"))) std_out = p.stdout.read() std_err = p.stderr.read() self.assertEqual("", std_out) self.assertEqual("", std_err)
def test_foreign_key_query(executor): run(executor, "create schema schema1") run(executor, "create schema schema2") run(executor, "create table schema1.parent(parentid int PRIMARY KEY)") run( executor, "create table schema2.child(childid int PRIMARY KEY, motherid int REFERENCES schema1.parent)" ) assert set(executor.foreignkeys()) >= set( [('schema1', 'parent', 'parentid', 'schema2', 'child', 'motherid')])
def test_date_time_types(executor): run(executor, "SET TIME ZONE UTC") assert run(executor, "SELECT (CAST('00:00:00' AS time))", join=True).split("\n")[3] \ == "| 00:00:00 |" assert run(executor, "SELECT (CAST('00:00:00+14:59' AS timetz))", join=True).split("\n")[3] \ == "| 00:00:00+14:59 |" assert run(executor, "SELECT (CAST('4713-01-01 BC' AS date))", join=True).split("\n")[3] \ == "| 4713-01-01 BC |" assert run(executor, "SELECT (CAST('4713-01-01 00:00:00 BC' AS timestamp))", join=True).split("\n")[3] \ == "| 4713-01-01 00:00:00 BC |" assert run(executor, "SELECT (CAST('4713-01-01 00:00:00+00 BC' AS timestamptz))", join=True).split("\n")[3] \ == "| 4713-01-01 00:00:00+00 BC |" assert run(executor, "SELECT (CAST('-123456789 days 12:23:56' AS interval))", join=True).split("\n")[3] \ == "| -123456789 days, 12:23:56 |"
def test_quickstart_and_render(self): """Run quickstart and render""" os.chdir(self.tmpdir) run(('confgen-quickstart',)) self.assertIsDir('templates') self.assertIsFile('templates/example.html.jinja') self.assertIsDir('data') self.assertIsFile('data/example.json') self.assertIsDir('build') self.assertIsFile('Makefile') run(('make',)) self.assertIsFile('build/example.html') with open(self._rp('build/example.html'), 'r') as f: rendered_content = f.read() self.assertEqual(rendered_content.strip(), '<h1>Hello, world!</h1>')
def test_functions_query(executor): run( executor, '''create function func1() returns int language sql as $$select 1$$''') run(executor, 'create schema schema1') run( executor, '''create function schema1.func2() returns int language sql as $$select 2$$''') run( executor, '''create function func3() returns table(x int, y int) language sql as $$select 1, 2 from generate_series(1,5)$$;''') run( executor, '''create function func4(x int) returns setof int language sql as $$select generate_series(1,5)$$;''') funcs = set(executor.functions()) assert funcs >= set([ function_meta_data(func_name='func1', return_type='integer'), function_meta_data(func_name='func3', arg_names=['x', 'y'], arg_types=['integer', 'integer'], arg_modes=['t', 't'], return_type='record', is_set_returning=True), function_meta_data(schema_name='public', func_name='func4', arg_names=('x', ), arg_types=('integer', ), return_type='integer', is_set_returning=True), function_meta_data( schema_name='schema1', func_name='func2', return_type='integer'), ])
def test_view_definition(executor): run(executor, 'create table tbl1 (a text, b numeric)') run(executor, 'create view vw1 AS SELECT * FROM tbl1') run(executor, 'create materialized view mvw1 AS SELECT * FROM tbl1') result = executor.view_definition('vw1') assert 'FROM tbl1' in result # import pytest; pytest.set_trace() result = executor.view_definition('mvw1') assert 'MATERIALIZED VIEW' in result
def test_missing_files(project_dir, temp_dir, resolve_symlinks, caplog): args = ['backup', '-t', str(temp_dir)] if resolve_symlinks: args.append('--resolve-symlinks') result = run(args) assert result.exit_code == 1 assert log_message(logging.ERROR, r'\.FileNotFoundError:', caplog) extends_content = { 'version': '2', 'services': { 'none': { 'image': 'busybox' } } } with project_dir.add_file('void.yml').open('tw') as f: yaml.dump(extends_content, f) caplog.clear() result = run(args) assert result.exit_code == 1 assert log_message(logging.ERROR, r'build path .*/lost either does not exist', caplog) project_dir.add_folder('lost') caplog.clear() result = run(args) assert result.exit_code == 1 assert log_message(logging.ERROR, r"Couldn't find env file: .*/dangling_link$", caplog) project_dir.add_file('gone') caplog.clear() assert result_okay(args)
def test_format_array_output_expanded(executor): statement = u""" SELECT array[1, 2, 3]::bigint[] as bigint_array, '{{1,2},{3,4}}'::numeric[] as nested_numeric_array, '{å,魚,текст}'::text[] as 配列 UNION ALL SELECT '{}', NULL, array[NULL] """ results = run(executor, statement, expanded=True) expected = [ '-[ RECORD 1 ]-------------------------', 'bigint_array | {1,2,3}', 'nested_numeric_array | {{1,2},{3,4}}', '配列 | {å,魚,текст}', '-[ RECORD 2 ]-------------------------', 'bigint_array | {}', 'nested_numeric_array | <null>', '配列 | {<null>}', 'SELECT 2' ] assert '\n'.join(results) == '\n'.join(expected)
def test_format_array_output(executor): statement = u""" SELECT array[1, 2, 3]::bigint[] as bigint_array, '{{1,2},{3,4}}'::numeric[] as nested_numeric_array, '{å,魚,текст}'::text[] as 配列 UNION ALL SELECT '{}', NULL, array[NULL] """ results = run(executor, statement) expected = [ '+----------------+------------------------+--------------+', '| bigint_array | nested_numeric_array | 配列 |', '|----------------+------------------------+--------------|', '| {1,2,3} | {{1,2},{3,4}} | {å,魚,текст} |', '| {} | <null> | {<null>} |', '+----------------+------------------------+--------------+', 'SELECT 2' ] assert list(results) == expected
def test_volumes(compression, temp_dir): assert call(["docker", "volume", "create", "--name=dontcare"]) == 0 assert call(["docker-compose", "up", "-d"]) == 0 args = ["backup", "-t", temp_dir] if compression: args.extend(["--compression", compression]) assert result_okay(run(args)) target_item = get_target_folder(temp_dir) if compression: assert target_item.is_file() with TemporaryDirectory() as target_folder: archive = tarfile.open(str(target_item)) archive.extractall(target_folder) check_volumes_result(Path(target_folder)) else: assert target_item.is_dir() target_folder = target_item check_volumes_result(target_folder)
def test_unicode_support_in_unknown_type(executor): assert u'日本語' in run(executor, u"SELECT '日本語' AS japanese;", join=True)
def test_schemata_table_views_and_columns_query(executor): run(executor, "create table a(x text, y text)") run(executor, "create table b(z text)") run(executor, "create view d as select 1 as e") run(executor, "create schema schema1") run(executor, "create table schema1.c (w text DEFAULT 'meow')") run(executor, "create schema schema2") # schemata # don't enforce all members of the schemas since they may include postgres # temporary schemas assert set(executor.schemata()) >= set( ['public', 'pg_catalog', 'information_schema', 'schema1', 'schema2']) assert executor.search_path() == ['pg_catalog', 'public'] # tables assert set(executor.tables()) >= set([('public', 'a'), ('public', 'b'), ('schema1', 'c')]) assert set(executor.table_columns()) >= set([ ('public', 'a', 'x', 'text', False, None), ('public', 'a', 'y', 'text', False, None), ('public', 'b', 'z', 'text', False, None), ('schema1', 'c', 'w', 'text', True, "'meow'::text"), ]) # views assert set(executor.views()) >= set([('public', 'd')]) assert set(executor.view_columns()) >= set( [('public', 'd', 'e', 'integer', False, None)])
def test_python_datetime(): with run(plugin.PythonDatetime) as env: assert inspect.ismodule(env['cal'][1])
def test_base(): run(TemplatePlugin)
def test_dexy_version(): with run(plugin.DexyVersion) as env: assert env['DEXY_VERSION'][1]
def test_large_numbers_render_directly(executor, value): run(executor, "create table numbertest(a numeric)") run(executor, "insert into numbertest (a) values ({0})".format(value)) assert value in run(executor, "select * from numbertest", join=True)
def test_python_builtins(): with run(plugin.PythonBuiltins) as env: assert 'hasattr' in env
def test_simple_json(): with run(plugin.SimpleJson) as env: assert inspect.ismodule(env['json'][1])
def test_ppjson(): with run(plugin.PrettyPrintJson) as env: assert 'ppjson' in env assert hasattr(env['ppjson'][1], '__call__')
def test_special_command_help(executor, pgspecial): result = run(executor, '\\?', pgspecial=pgspecial)[1].split('|') assert u'Command' in result[1] assert u'Description' in result[2]
def test_pygments(): with run(plugin.PygmentsStylesheet) as env: assert 'pastie.tex' in env['pygments'][1].keys() assert 'pastie.css' in env['pygments'][1].keys() assert 'pastie.html' in env['pygments'][1].keys()
def test_unicode_support_in_enum_type(executor): run(executor, u"CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy', '日本語')") run(executor, u"CREATE TABLE person (name TEXT, current_mood mood)") run(executor, u"INSERT INTO person VALUES ('Moe', '日本語')") assert u'日本語' in run(executor, u"SELECT * FROM person", join=True)
def test_no_arguments(self): p = run("python " + self.script_path) std_out = p.stdout.read() self.assertIn("Please insert valid arguments", std_out) self.assertIn("Options:", std_out)
def test_pygments(): with run(plugin.PygmentsStylesheet) as env: assert 'pastie.tex' in env['pygments'][1].keys() assert 'pastie.css' in env['pygments'][1].keys() assert 'pastie.html' in env['pygments'][1].keys() assert hasattr(env['highlight'][1], '__call__')
def test_multiple_queries_same_line(executor): result = run(executor, "select 'foo'; select 'bar'") assert len(result) == 12 # 2 * (output+status) * 3 lines assert "foo" in result[3] assert "bar" in result[9]
def test_config_with_build_context_v2(project_dir, temp_dir): assert result_okay(run(['backup', '-t', temp_dir])) target_folder = get_target_folder(temp_dir) assert identical_folder_contents(project_dir, target_folder / 'config')
def test_multiple_queries_with_special_command_same_line(executor, pgspecial): result = run(executor, "select 'foo'; \d", pgspecial=pgspecial) assert len(result) == 11 # 2 * (output+status) * 3 lines assert "foo" in result[3] # This is a lame check. :( assert "Schema" in result[7]
def test_multiple_queries_same_line_syntaxerror(executor, exception_formatter): result = run(executor, u"select 'fooé'; invalid syntax é", exception_formatter=exception_formatter) assert u'fooé' in result[3] assert 'syntax error at or near "invalid"' in result[-1]
def test_nested_extends(caplog): result = run(['backup', 'spam']) assert result.exit_code == 1 assert log_message(logging.ERROR, 'Unknown services: spam', caplog) caplog.clear()