From a3d10f6a6fa4c5c0b626f23867d279d082fb4da8 Mon Sep 17 00:00:00 2001 From: simeonreusch Date: Tue, 20 May 2025 15:37:24 +0200 Subject: [PATCH 1/3] add psql failure tests --- src/queryparser/testing/test_postgresql.py | 42 ++++++++++++---------- src/queryparser/testing/tests.yaml | 16 +++++++++ src/queryparser/testing/utils.py | 23 ++++++++++++ 3 files changed, 62 insertions(+), 19 deletions(-) diff --git a/src/queryparser/testing/test_postgresql.py b/src/queryparser/testing/test_postgresql.py index 3f45895..b126027 100644 --- a/src/queryparser/testing/test_postgresql.py +++ b/src/queryparser/testing/test_postgresql.py @@ -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']) -def test_postgresql_parsing_common(t): - _test_parsing(PostgreSQLQueryProcessor, t) +# @pytest.mark.parametrize("t", tests['common_tests']) +# def test_postgresql_parsing_common(t): +# _test_parsing(PostgreSQLQueryProcessor, t) + +# @pytest.mark.parametrize('t', tests['postgresql_tests']) +# def test_postgresql_parsing(t): +# _test_parsing(PostgreSQLQueryProcessor, t) -@pytest.mark.parametrize("t", tests['postgresql_tests']) -def test_postgresql_parsing(t): - _test_parsing(PostgreSQLQueryProcessor, t) +# @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_syntax_tests']) -def test_postgresql_syntax(t): - with pytest.raises(QuerySyntaxError): - PostgreSQLQueryProcessor(t) +# @pytest.mark.parametrize("t", tests['common_query_tests']) +# def test_postrgresql_query(t): +# with pytest.raises(QueryError): +# PostgreSQLQueryProcessor(t) -@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) diff --git a/src/queryparser/testing/tests.yaml b/src/queryparser/testing/tests.yaml index fb093f6..a9f2c5d 100644 --- a/src/queryparser/testing/tests.yaml +++ b/src/queryparser/testing/tests.yaml @@ -838,6 +838,22 @@ postgresql_tests: - - ["QMOST_SPEC_IS_IN_SURVEY"] +postgresql_failure_tests: + - + - SELECT specuid, ra, dec FROM dr1.spectrum WHERE QMOST_SPEC_IS_IN_SURVEY(specuid, '04'); + - + - [] + + - + - SELECT specuid, ra, dec FROM dr1.spectrum WHERE BLA(specuid, '04'); + - + - [QMOST_SPEC_IS_IN_SURVEY] + + - + - SELECT specuid, ra, dec FROM dr1.spectrum WHERE QMOST_SPEC_IS_IN_SURVEY[specuid, '04']; + - + - [QMOST_SPEC_IS_IN_SURVEY] + adql_mysql_tests: - diff --git a/src/queryparser/testing/utils.py b/src/queryparser/testing/utils.py index 012e40a..b97557a 100644 --- a/src/queryparser/testing/utils.py +++ b/src/queryparser/testing/utils.py @@ -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 @@ -76,3 +78,24 @@ 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, replace_schema_name, replace_function_names = test + + 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(QuerySyntaxError): + qp.process_query( + replace_schema_name=replace_schema_name, + replace_function_names=replace_function_names, + ) From 2eb2457ce3bf458de7c01b39108f034d2b17fbf8 Mon Sep 17 00:00:00 2001 From: simeonreusch Date: Tue, 20 May 2025 15:42:54 +0200 Subject: [PATCH 2/3] uncomment other tests, pass error type as parameter --- src/queryparser/testing/test_postgresql.py | 28 +++++++++++----------- src/queryparser/testing/tests.yaml | 21 +++++++++++----- src/queryparser/testing/utils.py | 9 +++++-- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/queryparser/testing/test_postgresql.py b/src/queryparser/testing/test_postgresql.py index b126027..2a5e64d 100644 --- a/src/queryparser/testing/test_postgresql.py +++ b/src/queryparser/testing/test_postgresql.py @@ -14,26 +14,26 @@ tests = yaml.load(f, Loader=yaml.FullLoader) -# @pytest.mark.parametrize("t", tests['common_tests']) -# def test_postgresql_parsing_common(t): -# _test_parsing(PostgreSQLQueryProcessor, t) +@pytest.mark.parametrize('t', tests['common_tests']) +def test_postgresql_parsing_common(t): + _test_parsing(PostgreSQLQueryProcessor, t) -# @pytest.mark.parametrize('t', tests['postgresql_tests']) -# def test_postgresql_parsing(t): -# _test_parsing(PostgreSQLQueryProcessor, t) +@pytest.mark.parametrize('t', tests['postgresql_tests']) +def test_postgresql_parsing(t): + _test_parsing(PostgreSQLQueryProcessor, t) -# @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_syntax_tests']) +def test_postgresql_syntax(t): + with pytest.raises(QuerySyntaxError): + PostgreSQLQueryProcessor(t) -# @pytest.mark.parametrize("t", tests['common_query_tests']) -# def test_postrgresql_query(t): -# with pytest.raises(QueryError): -# PostgreSQLQueryProcessor(t) +@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']) diff --git a/src/queryparser/testing/tests.yaml b/src/queryparser/testing/tests.yaml index a9f2c5d..09fff89 100644 --- a/src/queryparser/testing/tests.yaml +++ b/src/queryparser/testing/tests.yaml @@ -829,31 +829,40 @@ 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: - diff --git a/src/queryparser/testing/utils.py b/src/queryparser/testing/utils.py index b97557a..0e43fcf 100644 --- a/src/queryparser/testing/utils.py +++ b/src/queryparser/testing/utils.py @@ -81,7 +81,12 @@ def _test_parsing(query_processor, test, translate=False): def _test_failure_parsing(query_processor, test, translate=False): - query, replace_schema_name, replace_function_names = test + query, errortype, replace_schema_name, replace_function_names = test + + if errortype == 'syntax': + e = QuerySyntaxError + elif errortype == 'value': + e = ValueError if translate: adt = ADQLQueryTranslator() @@ -94,7 +99,7 @@ def _test_failure_parsing(query_processor, test, translate=False): qp = query_processor() qp.set_query(query) - with pytest.raises(QuerySyntaxError): + with pytest.raises(e): qp.process_query( replace_schema_name=replace_schema_name, replace_function_names=replace_function_names, From 6a0eab89d4cd41d95de19bc7392d79bb52bce181 Mon Sep 17 00:00:00 2001 From: simeonreusch Date: Tue, 20 May 2025 15:53:22 +0200 Subject: [PATCH 3/3] raise valueerror if errortype is unknown --- src/queryparser/testing/utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/queryparser/testing/utils.py b/src/queryparser/testing/utils.py index 0e43fcf..60ee1a1 100644 --- a/src/queryparser/testing/utils.py +++ b/src/queryparser/testing/utils.py @@ -87,6 +87,8 @@ def _test_failure_parsing(query_processor, test, translate=False): e = QuerySyntaxError elif errortype == 'value': e = ValueError + else: + raise ValueError('Invalid error type') if translate: adt = ADQLQueryTranslator()