MCPcopy Index your code
hub / github.com/dbcli/mycli / test_sql_output

Function test_sql_output

test/pytests/test_tabular_output.py:28–123  ·  view source on GitHub ↗

Test the sql output adapter.

(mycli)

Source from the content-addressed store, hash-verified

26
27@dbtest
28def test_sql_output(mycli):
29 """Test the sql output adapter."""
30 header = ["letters", "number", "optional", "float", "binary"]
31
32 class FakeCursor:
33 def __init__(self):
34 self.data = [("abc", 1, None, 10.0, b"\xaa"), ("d", 456, "1", 0.5, b"\xaa\xbb")]
35 self.description = [
36 (None, FIELD_TYPE.VARCHAR),
37 (None, FIELD_TYPE.LONG),
38 (None, FIELD_TYPE.LONG),
39 (None, FIELD_TYPE.FLOAT),
40 (None, FIELD_TYPE.BLOB),
41 ]
42
43 def __iter__(self):
44 return self
45
46 def __next__(self):
47 if self.data:
48 return self.data.pop(0)
49 else:
50 raise StopIteration()
51
52 def description(self):
53 return self.description
54
55 # Test sql-update output format
56 assert list(mycli.change_table_format("sql-update")) == [SQLResult(status="Changed table format to sql-update")]
57 mycli.main_formatter.query = ""
58 mycli.redirect_formatter.query = ""
59 output = mycli.format_sqlresult(SQLResult(header=header, rows=FakeCursor()))
60 actual = "\n".join(output)
61 assert actual == dedent("""\
62 UPDATE `DUAL` SET
63 `number` = 1
64 , `optional` = NULL
65 , `float` = 10.0e0
66 , `binary` = 0xaa
67 WHERE `letters` = 'abc';
68 UPDATE `DUAL` SET
69 `number` = 456
70 , `optional` = '1'
71 , `float` = 0.5e0
72 , `binary` = 0xaabb
73 WHERE `letters` = 'd';""")
74 # Test sql-update-2 output format
75 assert list(mycli.change_table_format("sql-update-2")) == [SQLResult(status="Changed table format to sql-update-2")]
76 mycli.main_formatter.query = ""
77 mycli.redirect_formatter.query = ""
78 output = mycli.format_sqlresult(SQLResult(header=header, rows=FakeCursor()))
79 assert "\n".join(output) == dedent("""\
80 UPDATE `DUAL` SET
81 `optional` = NULL
82 , `float` = 10.0e0
83 , `binary` = 0xaa
84 WHERE `letters` = 'abc' AND `number` = 1;
85 UPDATE `DUAL` SET

Callers

nothing calls this directly

Calls 5

SQLResultClass · 0.90
change_table_formatMethod · 0.80
joinMethod · 0.80
FakeCursorClass · 0.70
format_sqlresultMethod · 0.45

Tested by

no test coverage detected