def test_format_alias_output(self): settings = OutputSettings(table_format="psql") formatter = Formatter(settings) data = { "schema": [{ "name": "name", "alias": "n", "type": "text" }], "total": 1, "datarows": [["Tim"]], "size": 1, "status": 200, } results = formatter.format_output(data) expected = [ "fetched rows / total rows = 1/1", "+-----+", "| n |", "|-----|", "| Tim |", "+-----+", ] assert list(results) == expected
def test_format_output_vertical(self): settings = OutputSettings(table_format="psql", max_width=1) formatter = Formatter(settings) data = { "schema": [{"name": "name", "type": "text"}, {"name": "age", "type": "long"}], "total": 1, "datarows": [["Tim", 24]], "size": 1, "status": 200, } expanded = [ "fetched rows / total rows = 1/1", "-[ RECORD 1 ]-------------------------", "name | Tim", "age | 24", ] with mock.patch("src.opensearch_sql_cli.main.click.secho") as mock_secho, mock.patch("src.opensearch_sql_cli.main.click.confirm") as mock_confirm: expanded_results = formatter.format_output(data) mock_secho.assert_called_with(message="Output longer than terminal width", fg="red") mock_confirm.assert_called_with("Do you want to display data vertically for better visual effect?") assert "\n".join(expanded_results) == "\n".join(expanded)
def test_fake_large_output(self): settings = OutputSettings(table_format="psql") formatter = Formatter(settings) fake_large_data = { "schema": [{ "name": "name", "type": "text" }, { "name": "age", "type": "long" }], "total": 1000, "datarows": [["Tim", [24, 25]]], "size": 200, "status": 200, } results = formatter.format_output(fake_large_data) expected = [ "fetched rows / total rows = 200/1000\n" "Attention: Use LIMIT keyword when retrieving more than 200 rows of data", "+--------+---------+", "| name | age |", "|--------+---------|", "| Tim | [24,25] |", "+--------+---------+", ] assert list(results) == expected
def run(test_executor, query, use_console=True): data = test_executor.execute_query(query=query, use_console=use_console) settings = OutputSettings(table_format="psql") formatter = Formatter(settings) if data: res = formatter.format_output(data) res = "\n".join(res) return res