Repository: nackjicholson/aiosql

Issue:
Trailing lines in SQL causes a parse exception.
```python
Python 3.8.5 (default, Jul 27 2020, 08:42:51)
[GCC 10.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import aiosql
>>> import psycopg2
>>> from psycopg2.extras import execute_values
>>> sql_str = """
... -- name: create_schema#
... create table test (id int primary key, v1 int, v2 int);
...
... -- name: insert!
... INSERT INTO test (id, v1, v2) VALUES %s;
...
... -- name: update!
... UPDATE test SET v1 = data.v1 FROM (VALUES %s) AS data (id, v1)
... WHERE test.id = data.id;
...
... -- name: getem
... select * from test order by id;
... """
>>>
>>> queries = aiosql.from_str(sql_str, "psycopg2")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/nackjicholson/repos/aiosql/aiosql/aiosql.py", line 83, in from_str
    query_data = query_loader.load_query_data_from_sql(sql)
  File "/home/nackjicholson/repos/aiosql/aiosql/query_loader.py", line 74, in load_query_data_from_sql
    query_data.append(self._make_query_datum(query_sql_str))
  File "/home/nackjicholson/repos/aiosql/aiosql/query_loader.py", line 43, in _make_query_datum
    raise SQLParseException(
aiosql.exceptions.SQLParseException: name must convert to valid python variable, got "create_schema#                                                   ".
```

Kind of hard to tell from that. But the way I copy pasted the lines in created trailing space on the end of the lines that messes up our code. There probably needs to be a regex change, or maybe a `.strip()` call somewhere in `query_loader.py`

Discussion:

Patch:
diff --git a/aiosql/query_loader.py b/aiosql/query_loader.py
index 7a9efb6..d8ec807 100644
--- a/aiosql/query_loader.py
+++ b/aiosql/query_loader.py
@@ -18,7 +18,7 @@ class QueryLoader:
         self.record_classes = record_classes if record_classes is not None else {}
 
     def _make_query_datum(self, query_str: str):
-        lines = query_str.strip().splitlines()
+        lines = [l.strip() for l in query_str.strip().splitlines()]
         query_name = lines[0].replace("-", "_")
 
         if query_name.endswith("<!"):


Test patch:
diff --git a/tests/test_loading.py b/tests/test_loading.py
index f861246..e0bfc22 100644
--- a/tests/test_loading.py
+++ b/tests/test_loading.py
@@ -4,6 +4,7 @@ from unittest import mock
 import pytest
 import aiosql
 
+from aiosql.exceptions import SQLParseException
 from aiosql.queries import Queries
 from aiosql.query_loader import QueryLoader
 
@@ -49,3 +50,14 @@ def test_fromstr_queryloader_cls(sql):
     aiosql.from_str(sql, "aiosqlite", loader_cls=mock_loader)
 
     assert mock_loader.called
+
+
+def test_trailing_space_on_lines_does_not_error():
+    # There is whitespace in this string after the line ends
+    sql_str = "-- name: trailing-space^    \n"
+    sql_str += "select * from test;     \n"
+
+    try:
+        aiosql.from_str(sql_str, "aiosqlite")
+    except SQLParseException:
+        pytest.fail("Raised SQLParseException due to trailing space in query.")