Skip to content

Commit 0dcdbca

Browse files
refactor: improve type handling and code structure
1 parent 03963b3 commit 0dcdbca

File tree

4 files changed

+533
-385
lines changed

4 files changed

+533
-385
lines changed

cardano_node_tests/cluster_scripts/conway_fast/postgres-setup.sh

100644100755
File mode changed.

cardano_node_tests/tests/test_dbsync_config.py

Lines changed: 70 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
import logging
44
import typing as tp
5+
from enum import Enum
56

7+
import allure
68
import pytest
79

810
from cardano_node_tests.cluster_management import cluster_management
911
from cardano_node_tests.utils import configuration
12+
from cardano_node_tests.utils import dbsync_service_manager as db_sync
1013
from cardano_node_tests.utils import dbsync_utils
14+
from cardano_node_tests.utils import helpers
1115

1216
LOGGER = logging.getLogger(__name__)
1317

@@ -20,7 +24,25 @@
2024
]
2125

2226

23-
def check_dbsync_state(expected_state: dict) -> None:
27+
class TableCondition(str, Enum):
28+
"""Enum for table-level db-sync state conditions."""
29+
30+
EMPTY = "empty"
31+
NOT_EMPTY = "not_empty"
32+
EXISTS = "exists"
33+
NOT_EXISTS = "not_exists"
34+
35+
36+
class ColumnCondition(str, Enum):
37+
"""Enum for column-level db-sync condition checks."""
38+
39+
ZERO = "column_condition:=0"
40+
IS_NULL = "column_condition:IS NULL"
41+
42+
43+
def check_dbsync_state(
44+
expected_state: dict[tp.Union[str, db_sync.Table], TableCondition | ColumnCondition],
45+
) -> None:
2446
"""Check the state of db-sync tables and columns against expected conditions.
2547
2648
Args:
@@ -29,39 +51,38 @@ def check_dbsync_state(expected_state: dict) -> None:
2951
* "table" for table-level checks
3052
* "table.column" for column-level checks
3153
- Value format:
32-
* "empty" - verify table is empty
33-
* "not_empty" - verify table has rows
34-
* "exists" - verify table/column exists
35-
* "not_exists" - verify table/column doesn't exist
36-
* "column_condition:=0" - custom SQL condition
37-
* "column_condition:IS NULL" - NULL check condition
54+
* TableCondition enum values for table-level checks
55+
* ColumnCondition enum values for column-level checks
3856
3957
Returns:
4058
bool: True if all conditions match, False otherwise
4159
"""
4260
for key, condition in expected_state.items():
4361
if "." in key: # Column-level check
4462
table, column = key.split(".", 1)
45-
assert condition.startswith("column_condition:"), (
63+
assert isinstance(condition, ColumnCondition), (
4664
f"Invalid column condition format: {condition}"
4765
)
48-
column_condition = condition.split(":", 1)[1]
66+
column_condition = condition.value.split(":", 1)[1]
4967
dbsync_utils.check_column_condition(table, column, column_condition)
5068
else: # Table-level check
69+
assert isinstance(condition, TableCondition), (
70+
f"Invalid table condition format: {condition}"
71+
)
5172
match condition:
52-
case "empty":
73+
case TableCondition.EMPTY:
5374
assert dbsync_utils.table_empty(key), (
5475
f"Expected {key} to be empty, but it is not."
5576
)
56-
case "not_empty":
77+
case TableCondition.NOT_EMPTY:
5778
assert not dbsync_utils.table_empty(key), (
5879
f"Expected {key} to have data, but it is empty."
5980
)
60-
case "exists":
81+
case TableCondition.EXISTS:
6182
assert dbsync_utils.table_exists(key), (
6283
f"Expected {key} to exist, but it does not."
6384
)
64-
case "not_exists":
85+
case TableCondition.NOT_EXISTS:
6586
assert not dbsync_utils.table_exists(key), (
6687
f"Expected {key} to NOT exist, but it does."
6788
)
@@ -73,81 +94,93 @@ def check_dbsync_state(expected_state: dict) -> None:
7394
@pytest.fixture
7495
def db_sync_manager(
7596
request: pytest.FixtureRequest, cluster_manager: cluster_management.ClusterManager
76-
) -> dbsync_utils.DBSyncManager:
97+
) -> db_sync.DBSyncManager:
7798
"""Provide db-sync manager on a singleton cluster.
7899
79100
Creates and returns a DBSyncManager instance with locked cluster resources
80101
to ensure exclusive access during testing.
81102
"""
82103
cluster_manager.get(lock_resources=[cluster_management.Resources.CLUSTER])
83-
return dbsync_utils.DBSyncManager(request)
104+
return db_sync.DBSyncManager(request)
84105

85106

86107
@pytest.mark.order(-1)
87108
class TestDBSyncConfig:
88109
"""Basic tests for DB-Sync Config."""
89110

111+
@allure.link(helpers.get_vcs_link())
90112
def test_basic_tx_out(
91113
self,
92-
db_sync_manager: dbsync_utils.DBSyncManager,
114+
db_sync_manager: db_sync.DBSyncManager,
93115
):
94116
"""Test tx_out option."""
95117
db_config = db_sync_manager.get_config_builder()
96118

97119
# Test tx_out : enable
98120
db_sync_manager.restart_with_config(
99-
db_config.with_tx_out(value="enable", force_tx_in=False, use_address_table=False)
121+
db_config.with_tx_out(
122+
value=db_sync.TxOutMode.ENABLE, force_tx_in=False, use_address_table=False
123+
)
100124
)
101125
check_dbsync_state(
102126
{
103-
"address": "not_exists",
104-
"tx_in": "not_empty",
105-
"tx_out": "not_empty",
106-
"ma_tx_out": "not_empty",
127+
db_sync.Table.ADDRESS: TableCondition.NOT_EXISTS,
128+
db_sync.Table.TX_IN: TableCondition.NOT_EMPTY,
129+
db_sync.Table.TX_OUT: TableCondition.NOT_EMPTY,
130+
db_sync.Table.MA_TX_OUT: TableCondition.NOT_EMPTY,
107131
}
108132
)
109133

110134
# Test tx_out : disable
111135
db_sync_manager.restart_with_config(
112-
db_config.with_tx_out(value="disable", force_tx_in=True, use_address_table=True)
136+
db_config.with_tx_out(
137+
value=db_sync.TxOutMode.DISABLE, force_tx_in=True, use_address_table=True
138+
)
113139
)
114140
check_dbsync_state(
115141
{
116-
"address": "not_exists",
117-
"tx_in": "empty",
118-
"tx_out": "empty",
119-
"ma_tx_out": "empty",
120-
"tx.fee": "column_condition:=0",
121-
"redeemer.script_hash": "column_condition:IS NULL",
142+
db_sync.Table.ADDRESS: TableCondition.NOT_EXISTS,
143+
db_sync.Table.TX_IN: TableCondition.EMPTY,
144+
db_sync.Table.TX_OUT: TableCondition.EMPTY,
145+
db_sync.Table.MA_TX_OUT: TableCondition.EMPTY,
146+
db_sync.Column.Tx.FEE: ColumnCondition.ZERO,
147+
db_sync.Column.Redeemer.SCRIPT_HASH: ColumnCondition.IS_NULL,
122148
}
123149
)
124150

151+
@allure.link(helpers.get_vcs_link())
125152
@pytest.mark.parametrize(
126-
("tx_cbor_value", "expected_state"), [("enable", "not_empty"), ("disable", "empty")]
153+
("tx_cbor_value", "expected_state"),
154+
[
155+
(db_sync.SettingState.ENABLE, TableCondition.NOT_EMPTY),
156+
(db_sync.SettingState.DISABLE, TableCondition.EMPTY),
157+
],
127158
)
128159
def test_cbor(
129160
self,
130-
db_sync_manager: dbsync_utils.DBSyncManager,
131-
tx_cbor_value: tp.Literal["enable", "disable"],
132-
expected_state: str,
161+
db_sync_manager: db_sync.DBSyncManager,
162+
tx_cbor_value: db_sync.SettingState,
163+
expected_state: TableCondition,
133164
):
134165
"""Test tx_cbor option with parametrization."""
135166
db_config = db_sync_manager.get_config_builder()
136167

137168
db_sync_manager.restart_with_config(db_config.with_tx_cbor(tx_cbor_value))
138-
check_dbsync_state({"tx_cbor": expected_state})
169+
check_dbsync_state({db_sync.Table.TX_CBOR: expected_state})
139170

171+
@allure.link(helpers.get_vcs_link())
140172
@pytest.mark.parametrize(
141-
("multi_asset_enable", "expected_state"), [(True, "not_empty"), (False, "empty")]
173+
("multi_asset_enable", "expected_state"),
174+
[(True, TableCondition.NOT_EMPTY), (False, TableCondition.EMPTY)],
142175
)
143176
def test_multi_asset(
144177
self,
145-
db_sync_manager: dbsync_utils.DBSyncManager,
178+
db_sync_manager: db_sync.DBSyncManager,
146179
multi_asset_enable: bool,
147-
expected_state: str,
180+
expected_state: TableCondition,
148181
):
149182
"""Test multi_asset option with parametrization."""
150183
db_config = db_sync_manager.get_config_builder()
151184

152185
db_sync_manager.restart_with_config(db_config.with_multi_asset(enable=multi_asset_enable))
153-
check_dbsync_state({"multi_asset": expected_state})
186+
check_dbsync_state({db_sync.Table.MULTI_ASSET: expected_state})

0 commit comments

Comments
 (0)