Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/queryparser/testing/test_postgresql.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
# -*- coding: utf-8 -*-

from .utils import _test_parsing

from queryparser.postgresql import PostgreSQLQueryProcessor
from queryparser.exceptions import QueryError, QuerySyntaxError

import os

import pytest
import yaml

from queryparser.exceptions import QueryError, QuerySyntaxError
from queryparser.postgresql import PostgreSQLQueryProcessor

from .utils import _test_failure_parsing, _test_parsing

with open(os.path.dirname(__file__) + '/tests.yaml') as f:
tests = yaml.load(f, Loader=yaml.FullLoader)


@pytest.mark.parametrize("t", tests['common_tests'])
@pytest.mark.parametrize('t', tests['common_tests'])
def test_postgresql_parsing_common(t):
_test_parsing(PostgreSQLQueryProcessor, t)


@pytest.mark.parametrize("t", tests['postgresql_tests'])
@pytest.mark.parametrize('t', tests['postgresql_tests'])
def test_postgresql_parsing(t):
_test_parsing(PostgreSQLQueryProcessor, t)


@pytest.mark.parametrize("t", tests['common_syntax_tests'])
@pytest.mark.parametrize('t', tests['common_syntax_tests'])
def test_postgresql_syntax(t):
with pytest.raises(QuerySyntaxError):
PostgreSQLQueryProcessor(t)


@pytest.mark.parametrize("t", tests['common_query_tests'])
@pytest.mark.parametrize('t', tests['common_query_tests'])
def test_postrgresql_query(t):
with pytest.raises(QueryError):
PostgreSQLQueryProcessor(t)


@pytest.mark.parametrize('t', tests['postgresql_failure_tests'])
def test_postgresql_failure_parsing(t):
_test_failure_parsing(PostgreSQLQueryProcessor, t)
37 changes: 31 additions & 6 deletions src/queryparser/testing/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -829,14 +829,39 @@ postgresql_tests:
-

-
- "SELECT specuid, ra, dec FROM dr1.spectrum WHERE QMOST_SPEC_IS_IN_SURVEY(specuid, '04');"
- SELECT specuid, ra, dec FROM dr1.spectrum WHERE QMOST_SPEC_IS_IN_SURVEY(specuid, '04');
- ['dr1.spectrum.specuid', 'dr1.spectrum.ra', 'dr1.spectrum.dec']
- ["where"]
- ["QMOST_SPEC_IS_IN_SURVEY"]
- ["specuid: dr1.spectrum.specuid", "ra: dr1.spectrum.ra", "dec: dr1.spectrum.dec"]
- ["dr1.spectrum"]
- [where]
- [QMOST_SPEC_IS_IN_SURVEY]
- ['specuid: dr1.spectrum.specuid', 'ra: dr1.spectrum.ra', 'dec: dr1.spectrum.dec']
- [dr1.spectrum]
-
- ["QMOST_SPEC_IS_IN_SURVEY"]
- [QMOST_SPEC_IS_IN_SURVEY]

postgresql_failure_tests:
-
- SELECT specuid, ra, dec FROM dr1.spectrum WHERE QMOST_SPEC_IS_IN_SURVEY(specuid, '04');
- syntax
-
- []

-
- SELECT specuid, ra, dec FROM dr1.spectrum WHERE BLA(specuid, '04');
- syntax
-
- [QMOST_SPEC_IS_IN_SURVEY]

-
- SELECT specuid, ra, dec FROM dr1.spectrum WHERE QMOST_SPEC_IS_IN_SURVEY[specuid, '04'];
- syntax
-
- [QMOST_SPEC_IS_IN_SURVEY]

-
- SELECT specuid, ra, dec FROM dr1.spectrum WHERE a(specuid, '04');
- value
-
- [a, b, c, d, e, f, g, h, i, j, k, l]


adql_mysql_tests:
Expand Down
30 changes: 30 additions & 0 deletions src/queryparser/testing/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
import pytest

from queryparser.adql import ADQLQueryTranslator
from queryparser.exceptions import QuerySyntaxError
from queryparser.mysql import MySQLQueryProcessor
from queryparser.postgresql import PostgreSQLQueryProcessor

Expand Down Expand Up @@ -76,3 +78,31 @@ def _test_parsing(query_processor, test, translate=False):

if tables is not None:
assert set(tables) == set(qp_tables)


def _test_failure_parsing(query_processor, test, translate=False):
query, errortype, replace_schema_name, replace_function_names = test

if errortype == 'syntax':
e = QuerySyntaxError
elif errortype == 'value':
e = ValueError
else:
raise ValueError('Invalid error type')

if translate:
adt = ADQLQueryTranslator()
adt.set_query(query)
if query_processor == MySQLQueryProcessor:
query = adt.to_mysql()
elif query_processor == PostgreSQLQueryProcessor:
query = adt.to_postgresql()

qp = query_processor()
qp.set_query(query)

with pytest.raises(e):
qp.process_query(
replace_schema_name=replace_schema_name,
replace_function_names=replace_function_names,
)