Skip to content

Commit b930b69

Browse files
author
ci bot
committed
Merge branch 'feat/TG-982-unit-test-improvements' into 'enterprise'
test(TG-982): improve unit test suite See merge request dkinternal/testgen/dataops-testgen!408
2 parents 5270171 + 9356e32 commit b930b69

17 files changed

Lines changed: 683 additions & 46 deletions

testgen/common/notifications/monitor_run.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from testgen.common.models.notification_settings import (
55
MonitorNotificationSettings,
66
MonitorNotificationTrigger,
7-
NotificationEvent,
87
)
98
from testgen.common.models.project import Project
109
from testgen.common.models.settings import PersistedSetting
@@ -174,7 +173,6 @@ def send_monitor_notifications(test_run: TestRun, result_list_ct=20):
174173
notifications = list(MonitorNotificationSettings.select(
175174
enabled=True,
176175
test_suite_id=test_run.test_suite_id,
177-
event=NotificationEvent.monitor_run,
178176
))
179177
if not notifications:
180178
return

testgen/common/notifications/test_run.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from testgen.common.models import get_current_session, with_database_session
66
from testgen.common.models.notification_settings import (
7-
NotificationEvent,
87
TestRunNotificationSettings,
98
TestRunNotificationTrigger,
109
)
@@ -249,7 +248,6 @@ def send_test_run_notifications(test_run: TestRun, result_list_ct=20, result_sta
249248
notifications = list(TestRunNotificationSettings.select(
250249
enabled=True,
251250
test_suite_id=test_run.test_suite_id,
252-
event=NotificationEvent.test_run,
253251
))
254252
if not notifications:
255253
return

tests/unit/test_refresh_data_chars_query.py renamed to tests/unit/commands/queries/test_refresh_data_chars_query.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
from testgen.common.models.connection import Connection
55
from testgen.common.models.table_group import TableGroup
66

7+
pytestmark = pytest.mark.unit
8+
79

8-
@pytest.mark.unit
910
def test_include_exclude_mask_basic():
1011
connection = Connection(sql_flavor="postgresql")
1112
table_group = TableGroup(
@@ -26,7 +27,6 @@ def test_include_exclude_mask_basic():
2627
)""" in query
2728

2829

29-
@pytest.mark.unit
3030
@pytest.mark.parametrize("mask", ("", None))
3131
def test_include_empty_exclude_mask(mask):
3232
connection = Connection(sql_flavor="snowflake")
@@ -44,7 +44,6 @@ def test_include_empty_exclude_mask(mask):
4444
)""" in query
4545

4646

47-
@pytest.mark.unit
4847
@pytest.mark.parametrize("mask", ("", None))
4948
def test_include_empty_include_mask(mask):
5049
connection = Connection(sql_flavor="mssql")
@@ -60,3 +59,51 @@ def test_include_empty_include_mask(mask):
6059
assert r"""AND (
6160
(c.table_name LIKE 'important%' ) OR (c.table_name LIKE '%useful[_]%' )
6261
)""" in query
62+
63+
64+
def test_table_set_only():
65+
connection = Connection(sql_flavor="postgresql")
66+
table_group = TableGroup(
67+
table_group_schema="test_schema",
68+
profiling_table_set="users, orders, products",
69+
profiling_include_mask="",
70+
profiling_exclude_mask="",
71+
)
72+
sql_generator = RefreshDataCharsSQL(connection, table_group)
73+
criteria = sql_generator._get_table_criteria()
74+
75+
assert "IN ('users','orders','products')" in criteria
76+
assert "LIKE" not in criteria
77+
78+
79+
@pytest.mark.parametrize("include", ("", None))
80+
@pytest.mark.parametrize("exclude", ("", None))
81+
def test_no_filters(include, exclude):
82+
connection = Connection(sql_flavor="postgresql")
83+
table_group = TableGroup(
84+
table_group_schema="test_schema",
85+
profiling_table_set="",
86+
profiling_include_mask=include,
87+
profiling_exclude_mask=exclude,
88+
)
89+
sql_generator = RefreshDataCharsSQL(connection, table_group)
90+
criteria = sql_generator._get_table_criteria()
91+
92+
assert criteria == ""
93+
94+
95+
def test_table_set_with_include_exclude():
96+
connection = Connection(sql_flavor="postgresql")
97+
table_group = TableGroup(
98+
table_group_schema="test_schema",
99+
profiling_table_set="users, orders",
100+
profiling_include_mask="important%",
101+
profiling_exclude_mask="temp%",
102+
)
103+
sql_generator = RefreshDataCharsSQL(connection, table_group)
104+
criteria = sql_generator._get_table_criteria()
105+
106+
assert "IN ('users','orders')" in criteria
107+
assert "LIKE 'important%'" in criteria
108+
assert "AND NOT" in criteria
109+
assert "LIKE 'temp%'" in criteria

tests/unit/test_run_observability_exporter.py renamed to tests/unit/commands/test_run_observability_exporter.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
calculate_chunk_size,
77
)
88

9+
pytestmark = pytest.mark.unit
10+
911

1012
@pytest.fixture()
1113
def test_outcome():
@@ -22,7 +24,6 @@ def test_outcome():
2224
}
2325

2426

25-
@pytest.mark.unit
2627
@pytest.mark.parametrize(
2728
"test_outcomes_length",
2829
[1, 100, 10000],
@@ -40,7 +41,6 @@ def test_calculate_chunk_size(test_outcome, test_outcomes_length):
4041
assert 100 < chunk_size < 500
4142

4243

43-
@pytest.mark.unit
4444
@pytest.mark.parametrize(
4545
"profiling_table_set, expected_outcome",
4646
(
@@ -54,7 +54,6 @@ def test_get_processed_profiling_table_set(profiling_table_set, expected_outcome
5454
assert expected_outcome == actual_outcome
5555

5656

57-
@pytest.mark.unit
5857
@pytest.mark.parametrize(
5958
"input_parameters, expected_outcome",
6059
(
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
from datetime import UTC, datetime
2+
from unittest.mock import patch
3+
4+
import pytest
5+
6+
from testgen.common.models.custom_types import (
7+
EncryptedBytea,
8+
EncryptedJson,
9+
NullIfEmptyString,
10+
UpdateTimestamp,
11+
YNString,
12+
ZeroIfEmptyInteger,
13+
)
14+
15+
pytestmark = pytest.mark.unit
16+
17+
18+
# --- NullIfEmptyString ---
19+
20+
@pytest.mark.parametrize(
21+
"value, expected",
22+
[
23+
("", None),
24+
("hello", "hello"),
25+
(None, None),
26+
],
27+
)
28+
def test_null_if_empty_string(value, expected):
29+
t = NullIfEmptyString()
30+
assert t.process_bind_param(value, None) == expected
31+
32+
33+
# --- YNString ---
34+
35+
@pytest.mark.parametrize(
36+
"value, expected",
37+
[
38+
(True, "Y"),
39+
(False, "N"),
40+
("Y", "Y"),
41+
("N", "N"),
42+
(None, None),
43+
],
44+
)
45+
def test_yn_string_bind(value, expected):
46+
t = YNString()
47+
assert t.process_bind_param(value, None) == expected
48+
49+
50+
@pytest.mark.parametrize(
51+
"value, expected",
52+
[
53+
("Y", True),
54+
("N", False),
55+
(None, None),
56+
],
57+
)
58+
def test_yn_string_result(value, expected):
59+
t = YNString()
60+
assert t.process_result_value(value, None) == expected
61+
62+
63+
# --- ZeroIfEmptyInteger ---
64+
65+
@pytest.mark.parametrize(
66+
"value, expected",
67+
[
68+
(5, 5),
69+
(0, 0),
70+
("", 0),
71+
(None, 0),
72+
],
73+
)
74+
def test_zero_if_empty_integer(value, expected):
75+
t = ZeroIfEmptyInteger()
76+
assert t.process_bind_param(value, None) == expected
77+
78+
79+
# --- UpdateTimestamp ---
80+
81+
def test_update_timestamp():
82+
t = UpdateTimestamp()
83+
before = datetime.now(UTC)
84+
result = t.process_bind_param("ignored", None)
85+
after = datetime.now(UTC)
86+
assert before <= result <= after
87+
88+
89+
# --- EncryptedBytea roundtrip ---
90+
91+
@patch("testgen.common.encrypt.settings")
92+
def test_encrypted_bytea_roundtrip(mock_settings):
93+
mock_settings.APP_ENCRYPTION_SALT = "testsalt12345678"
94+
mock_settings.APP_ENCRYPTION_SECRET = "testsecret123456" # noqa: S105
95+
96+
t = EncryptedBytea()
97+
original = "sensitive data"
98+
99+
encrypted = t.process_bind_param(original, None)
100+
assert encrypted != original.encode()
101+
102+
decrypted = t.process_result_value(encrypted, None)
103+
assert decrypted == original
104+
105+
106+
@patch("testgen.common.encrypt.settings")
107+
def test_encrypted_bytea_none(mock_settings):
108+
t = EncryptedBytea()
109+
assert t.process_bind_param(None, None) is None
110+
assert t.process_result_value(None, None) is None
111+
112+
113+
# --- EncryptedJson roundtrip ---
114+
115+
@patch("testgen.common.encrypt.settings")
116+
def test_encrypted_json_roundtrip(mock_settings):
117+
mock_settings.APP_ENCRYPTION_SALT = "testsalt12345678"
118+
mock_settings.APP_ENCRYPTION_SECRET = "testsecret123456" # noqa: S105
119+
120+
t = EncryptedJson()
121+
original = {"key": "value", "num": 42, "list": [1, 2, 3]}
122+
123+
encrypted = t.process_bind_param(original, None)
124+
decrypted = t.process_result_value(encrypted, None)
125+
assert decrypted == original
126+
127+
128+
@patch("testgen.common.encrypt.settings")
129+
def test_encrypted_json_none(mock_settings):
130+
t = EncryptedJson()
131+
assert t.process_bind_param(None, None) is None
132+
assert t.process_result_value(None, None) is None

tests/unit/test_common_email.py renamed to tests/unit/common/notifications/test_common_email.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from testgen.common.notifications.base import BaseEmailTemplate, EmailTemplateException
66

7+
pytestmark = pytest.mark.unit
8+
79

810
class TestEmailTemplate(BaseEmailTemplate):
911

0 commit comments

Comments
 (0)