Looking at Database Library and it seems you run a query and then check each element of an array, cell by cell, which to me seems a bit tedious. I’m trying to come up with some syntax like this:
*** Settings ***
Library psql.py
*** Test Cases ***
Example
SQL Should Return SELECT prtran_hdr_type, count(*) FROM prtran_hdr WHERE pay_period_date='2022-01-01' GROUP BY 1;
... | prtran_hdr_type | count |
... | Template 1 | 182 |
... | Projected | 182 |
I think this reads quite nicely.
So far I have implemented this in psql.py
to turn the arguments that follow the query into a multiline string:
def sql_should_return(sql, *data):
print(f"sql: {sql}")
prev_data = ""
result = ""
for d in data:
if prev_data:
if d.startswith("|") and prev_data.endswith("|"):
result += "\n"
else:
result += " "
result += d
prev_data = d
print(f"result: {result}")
The print output is
sql: SELECT prtran_hdr_type, count(*) FROM prtran_hdr WHERE pay_period_date='112233' GROUP BY 1;
result: | prtran_hdr_type | count |
| Template 1 | 182 |
| Projected | 182 |
I obviously haven’t implemented the actual running of the SQL yet or checking the results. Just playing with the syntax for the keyword. I guess the next step is to break the result
string up into rows and columns, run the query and compare the result.
Only thinking if using this for queries that return small amounts of data. For larger results I’d compare against an expected CSV file.
The main problem is it loses the three space gap between Template
and 1
. So I would need to strip multiple spaces from the SQL result, before comparison. Which is a shame, as sometimes I have found myself doing things like this were I need to check for a certain number of spaces in something:
Editbox Should Equal Description 05:37-11:07${SPACE} 11:58-14:58${SPACE}
That is so I can check there are two spaces between and a space on the end. I think it would be much nicer to read:
Editbox Should Equal Description """05:37-11:07 11:58-14:58 """
Or alternatively:
Editbox Should Equal Description ||05:37-11:07 11:58-14:58 ||
Similarly for the pipe format table, this would be nice:
SQL Should Return SELECT prtran_hdr_type, count(*) FROM prtran_hdr WHERE pay_period_date='2022-01-01' GROUP BY 1;
... """ prtran_hdr_type | count """
... """ Template 1 | 182 """
... """ Projected | 182 """
or
SQL Should Return SELECT prtran_hdr_type, count(*) FROM prtran_hdr WHERE pay_period_date='2022-01-01' GROUP BY 1;
... || prtran_hdr_type | count ||
... || Template 1 | 182 ||
... || Projected | 182 ||
With everything between a pair of ||
being passed through as an argument with spaces preserved.
Any thoughts?