Skip to content

Commit 3336ab4

Browse files
Merge branch 'dev' of https://github.com/ThalesGroup/sql-data-guard into dev
closes #7; Added test_duckdb.py and modified test.yaml to install test dependencies
2 parents 1df4120 + 72bcecd commit 3336ab4

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

src/sql_data_guard/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .sql_data_guard import verify_sql
1+
from .sql_data_guard import verify_sql
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class UnsupportedRestrictionError(Exception):
2+
pass
3+
4+
5+
def validate_restrictions(config: dict):
6+
"""
7+
Validates the restrictions in the configuration to ensure only supported operations are used.
8+
9+
Args:
10+
config (dict): The configuration containing the restrictions to validate.
11+
12+
Raises:
13+
UnsupportedRestrictionError: If an unsupported restriction operation is found.
14+
"""
15+
supported_operations = ["=", ">", "<", ">=", "<=", "!="] # Allowed operations
16+
17+
for table in config["tables"]:
18+
for restriction in table.get("restrictions", []):
19+
operation = restriction.get("operation")
20+
if operation and operation.lower() not in supported_operations:
21+
raise UnsupportedRestrictionError(
22+
f"Invalid restriction: 'operation={operation}' is not supported."
23+
)

src/sql_data_guard/sql_data_guard.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import sqlglot.expressions as expr
66
from sqlglot.optimizer.simplify import simplify
77

8-
from sql_data_guard.verification_context import VerificationContext
8+
from .verification_context import VerificationContext
9+
from .restriction_validation import validate_restrictions, UnsupportedRestrictionError
910

1011

1112
def verify_sql(sql: str, config: dict, dialect: str = None) -> dict:
@@ -24,6 +25,13 @@ def verify_sql(sql: str, config: dict, dialect: str = None) -> dict:
2425
- "fixed" (Optional[str]): The fixed query if modifications were made.
2526
- "risk" (float): Verification risk score (0 - no risk, 1 - high risk)
2627
"""
28+
29+
# First, validate restrictions
30+
try:
31+
validate_restrictions(config)
32+
except UnsupportedRestrictionError as e:
33+
return {"allowed": False, "errors": [str(e)], "fixed": None, "risk": 1.0}
34+
# ___
2735
result = VerificationContext(config, dialect)
2836
try:
2937
parsed = sqlglot.parse_one(sql, dialect=dialect)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import pytest
2+
from sql_data_guard.restriction_validation import (
3+
validate_restrictions,
4+
UnsupportedRestrictionError,
5+
)
6+
7+
8+
def test_valid_restrictions():
9+
config = {
10+
"tables": [
11+
{
12+
"table_name": "products",
13+
"restrictions": [
14+
{"column": "price", "value": 100, "operation": ">="},
15+
{"column": "category", "value": "A", "operation": "="},
16+
],
17+
}
18+
]
19+
}
20+
21+
try:
22+
validate_restrictions(config)
23+
except UnsupportedRestrictionError as e:
24+
pytest.fail(f"Unexpected error: {e}")
25+
26+
27+
def test_invalid_restriction():
28+
config = {
29+
"tables": [
30+
{
31+
"table_name": "products",
32+
"restrictions": [
33+
{"column": "price", "value": [80, 150], "operation": "BETWEEN"},
34+
],
35+
}
36+
]
37+
}
38+
39+
with pytest.raises(
40+
UnsupportedRestrictionError,
41+
match="Invalid restriction: 'operation=BETWEEN' is not supported.",
42+
):
43+
validate_restrictions(config)

0 commit comments

Comments
 (0)