Test the sql output adapter.
(mycli)
| 26 | |
| 27 | @dbtest |
| 28 | def 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 |
nothing calls this directly
no test coverage detected