Skip to content

Commit 78bde35

Browse files
Antony BaileyAntony Bailey
authored andcommitted
a few tests
1 parent 5753628 commit 78bde35

File tree

6 files changed

+276
-35
lines changed

6 files changed

+276
-35
lines changed

.github/workflows/python-package-conda.yml

Lines changed: 0 additions & 34 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pip install pysqly
2424
### Using the Python API
2525

2626
```python
27-
from sqly.executor import SQLYExecutor
27+
from sqly.SQLYExecutor import SQLYExecutor
2828

2929
datasource = "your_database_connection"
3030
db_type = "postgres" # Choose from sqlite, mariadb, postgres, oracle, mssql

test_BaseDBConnector.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import unittest
2+
from unittest.mock import Mock
3+
from BaseDBConnector import BaseDBConnector
4+
5+
class TestBaseDBConnector(unittest.TestCase):
6+
"""
7+
Unit tests for the BaseDBConnector class.
8+
9+
TestBaseDBConnector is a test case for testing the initialization of the BaseDBConnector class.
10+
11+
Methods:
12+
test_init: Tests the initialization of the BaseDBConnector class with a mock connection object.
13+
"""
14+
def test_init(self):
15+
"""
16+
Test the initialization of the BaseDBConnector class.
17+
18+
This test creates a mock connection object and initializes the BaseDBConnector
19+
with it. It then asserts that the connection attribute of the connector is set
20+
correctly to the mock connection.
21+
22+
Assertions:
23+
- The connection attribute of the BaseDBConnector instance should be equal
24+
to the mock connection object.
25+
"""
26+
# Create a mock connection object
27+
mock_connection = Mock()
28+
29+
# Initialize the BaseDBConnector with the mock connection
30+
connector = BaseDBConnector(mock_connection)
31+
32+
# Assert that the connection attribute is set correctly
33+
self.assertEqual(connector.connection, mock_connection)
34+
35+
if __name__ == '__main__':
36+
unittest.main()

test_SQLYExecutor.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import unittest
2+
from SQLYExecutor import SQLYExecutor
3+
4+
class TestSQLYExecutor(unittest.TestCase):
5+
"""
6+
Unit tests for the SQLYExecutor class.
7+
8+
This test suite contains tests to verify the correct initialization of the
9+
SQLYExecutor class with different parameters.
10+
11+
Classes:
12+
TestSQLYExecutor: Contains unit tests for the SQLYExecutor class.
13+
14+
Methods:
15+
test_init_with_datasource_only: Tests the initialization of SQLYExecutor with only the datasource parameter.
16+
test_init_with_datasource_and_db_type: Tests the initialization of SQLYExecutor with a datasource and database type.
17+
"""
18+
def test_init_with_datasource_only(self):
19+
"""
20+
Test the initialization of SQLYExecutor with only the datasource parameter.
21+
22+
This test verifies that the SQLYExecutor is correctly initialized when only
23+
the datasource parameter is provided. It checks that the datasource attribute
24+
is set correctly and the db_type attribute is None.
25+
26+
Assertions:
27+
- The datasource attribute of the executor should match the provided datasource.
28+
- The db_type attribute of the executor should be None.
29+
"""
30+
datasource = "path/to/datasource"
31+
executor = SQLYExecutor(datasource)
32+
self.assertEqual(executor.datasource, datasource)
33+
self.assertIsNone(executor.db_type)
34+
35+
def test_init_with_datasource_and_db_type(self):
36+
"""
37+
Test the initialization of SQLYExecutor with a datasource and database type.
38+
39+
This test verifies that the SQLYExecutor object is correctly initialized
40+
with the provided datasource and database type. It checks that the
41+
datasource and db_type attributes of the executor match the expected values.
42+
43+
Assertions:
44+
- The datasource attribute of the executor should match the provided datasource.
45+
- The db_type attribute of the executor should match the provided db_type.
46+
"""
47+
datasource = "path/to/datasource"
48+
db_type = "sqlite"
49+
executor = SQLYExecutor(datasource, db_type)
50+
self.assertEqual(executor.datasource, datasource)
51+
self.assertEqual(executor.db_type, db_type)
52+
53+
54+
55+
if __name__ == '__main__':
56+
unittest.main()

test_SQLYParser.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import unittest
2+
from SQLYParser import SQLYParser
3+
from SQLYParseError import SQLYParseError
4+
5+
class TestSQLYParser(unittest.TestCase):
6+
"""
7+
Test suite for the SQLYParser class.
8+
9+
This test suite contains the following test cases:
10+
11+
1. test_parse_valid_query:
12+
- Test case for parsing a valid SQLY query.
13+
- Verifies that the SQLYParser correctly parses a valid query string into the expected dictionary format.
14+
- The query string contains key-value pairs separated by colons and new lines.
15+
- Assertions: The parsed result from SQLYParser.parse(query) should match the expected_result dictionary.
16+
17+
2. test_parse_invalid_query:
18+
- Test case for parsing an invalid SQLY query.
19+
- Verifies that the SQLYParser raises an SQLYParseError when attempting to parse a query with invalid syntax.
20+
- The query provided has a missing colon between 'key2' and 'value2', which should trigger the error.
21+
22+
3. test_parse_empty_query:
23+
- Test case for parsing an empty SQL query.
24+
- Verifies that the SQLYParser correctly handles an empty query string by returning None.
25+
- Steps:
26+
1. Define an empty query string.
27+
2. Define the expected result as None.
28+
3. Parse the empty query string using SQLYParser.
29+
4. Assert that the result matches the expected result (None).
30+
31+
Classes:
32+
TestSQLYParser: Contains unit tests for the SQLYParser class.
33+
34+
Methods:
35+
test_parse_valid_query: Test case for parsing a valid SQLY query.
36+
test_parse_invalid_query: Test case for parsing an invalid SQLY query.
37+
test_parse_empty_query: Test case for parsing an empty SQL query.
38+
"""
39+
def test_parse_valid_query(self):
40+
"""
41+
Test case for parsing a valid SQLY query.
42+
43+
This test verifies that the SQLYParser correctly parses a valid query string
44+
into the expected dictionary format.
45+
46+
The query string contains key-value pairs separated by colons and new lines.
47+
The expected result is a dictionary with the corresponding key-value pairs.
48+
49+
Assertions:
50+
The parsed result from SQLYParser.parse(query) should match the expected_result dictionary.
51+
"""
52+
query = """
53+
key1: value1
54+
key2: value2
55+
"""
56+
expected_result = {
57+
'key1': 'value1',
58+
'key2': 'value2'
59+
}
60+
result = SQLYParser.parse(query)
61+
self.assertEqual(result, expected_result)
62+
63+
def test_parse_invalid_query(self):
64+
"""
65+
Test case for parsing an invalid SQLY query.
66+
67+
This test verifies that the SQLYParser raises an SQLYParseError
68+
when attempting to parse a query with invalid syntax. The query
69+
provided has a missing colon between 'key2' and 'value2', which
70+
should trigger the error.
71+
"""
72+
query = """
73+
key1: value1
74+
key2 value2
75+
"""
76+
with self.assertRaises(SQLYParseError):
77+
SQLYParser.parse(query)
78+
79+
def test_parse_empty_query(self):
80+
"""
81+
Test case for parsing an empty SQL query.
82+
83+
This test verifies that the SQLYParser correctly handles an empty query string
84+
by returning None.
85+
86+
Steps:
87+
1. Define an empty query string.
88+
2. Define the expected result as None.
89+
3. Parse the empty query string using SQLYParser.
90+
4. Assert that the result matches the expected result (None).
91+
"""
92+
query = ""
93+
expected_result = None
94+
result = SQLYParser.parse(query)
95+
self.assertEqual(result, expected_result)
96+
97+
if __name__ == '__main__':
98+
unittest.main()

test_SQLYUtils.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import unittest
2+
from SQLYUtils import SQLYUtils
3+
4+
class TestSQLYUtils(unittest.TestCase):
5+
"""
6+
Unit tests for the SQLYUtils class.
7+
8+
Test Cases:
9+
- test_validate_query_valid: Tests that a valid query dictionary with 'select' and 'from' keys returns True.
10+
- test_validate_query_missing_select: Tests that a query dictionary missing the 'select' key returns False.
11+
- test_validate_query_missing_from: Tests that a query dictionary missing the 'from' key returns False.
12+
- test_validate_query_empty_dict: Tests that an empty query dictionary returns False.
13+
- test_validate_query_not_dict: Tests that a query which is not a dictionary returns False.
14+
"""
15+
16+
def test_validate_query_valid(self):
17+
"""
18+
Test case for validating a correct SQL query structure.
19+
20+
This test checks if the `validate_query` method of `SQLYUtils` correctly
21+
identifies a valid query structure. The query structure being tested
22+
includes a "select" key with a list of columns and a "from" key with the
23+
table name.
24+
25+
The test asserts that the `validate_query` method returns True for the
26+
given valid query.
27+
"""
28+
query = {
29+
"select": ["id", "name"],
30+
"from": "users"
31+
}
32+
self.assertTrue(SQLYUtils.validate_query(query))
33+
34+
def test_validate_query_missing_select(self):
35+
"""
36+
Test case for validating a query that is missing the 'select' clause.
37+
38+
This test ensures that the `validate_query` method of the `SQLYUtils` class
39+
returns `False` when the query dictionary does not contain the required 'select' key.
40+
41+
The query dictionary in this test case only contains the 'from' key with the value 'users'.
42+
"""
43+
query = {
44+
"from": "users"
45+
}
46+
self.assertFalse(SQLYUtils.validate_query(query))
47+
48+
def test_validate_query_missing_from(self):
49+
"""
50+
Test case for the validate_query method in SQLYUtils class.
51+
52+
This test checks the scenario where the 'from' clause is missing in the query.
53+
The query dictionary contains only the 'select' clause with fields 'id' and 'name'.
54+
The test asserts that the validate_query method should return False, indicating
55+
that the query is invalid due to the missing 'from' clause.
56+
"""
57+
query = {
58+
"select": ["id", "name"]
59+
}
60+
self.assertFalse(SQLYUtils.validate_query(query))
61+
62+
def test_validate_query_empty_dict(self):
63+
"""
64+
Test case for SQLYUtils.validate_query method with an empty dictionary.
65+
66+
This test verifies that the validate_query method returns False when
67+
an empty dictionary is passed as the query parameter.
68+
"""
69+
query = {}
70+
self.assertFalse(SQLYUtils.validate_query(query))
71+
72+
def test_validate_query_not_dict(self):
73+
"""
74+
Test case for SQLYUtils.validate_query method.
75+
76+
This test checks that the validate_query method returns False when the input query is not a dictionary.
77+
78+
Assertions:
79+
- The method should return False when a string query is provided.
80+
"""
81+
query = "SELECT * FROM users"
82+
self.assertFalse(SQLYUtils.validate_query(query))
83+
84+
if __name__ == '__main__':
85+
unittest.main()

0 commit comments

Comments
 (0)