2
2
3
3
import logging
4
4
import typing as tp
5
+ from enum import Enum
5
6
7
+ import allure
6
8
import pytest
7
9
8
10
from cardano_node_tests .cluster_management import cluster_management
9
11
from cardano_node_tests .utils import configuration
12
+ from cardano_node_tests .utils import dbsync_service_manager as db_sync
10
13
from cardano_node_tests .utils import dbsync_utils
14
+ from cardano_node_tests .utils import helpers
11
15
12
16
LOGGER = logging .getLogger (__name__ )
13
17
20
24
]
21
25
22
26
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 :
24
46
"""Check the state of db-sync tables and columns against expected conditions.
25
47
26
48
Args:
@@ -29,39 +51,38 @@ def check_dbsync_state(expected_state: dict) -> None:
29
51
* "table" for table-level checks
30
52
* "table.column" for column-level checks
31
53
- 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
38
56
39
57
Returns:
40
58
bool: True if all conditions match, False otherwise
41
59
"""
42
60
for key , condition in expected_state .items ():
43
61
if "." in key : # Column-level check
44
62
table , column = key .split ("." , 1 )
45
- assert condition . startswith ( "column_condition:" ), (
63
+ assert isinstance ( condition , ColumnCondition ), (
46
64
f"Invalid column condition format: { condition } "
47
65
)
48
- column_condition = condition .split (":" , 1 )[1 ]
66
+ column_condition = condition .value . split (":" , 1 )[1 ]
49
67
dbsync_utils .check_column_condition (table , column , column_condition )
50
68
else : # Table-level check
69
+ assert isinstance (condition , TableCondition ), (
70
+ f"Invalid table condition format: { condition } "
71
+ )
51
72
match condition :
52
- case "empty" :
73
+ case TableCondition . EMPTY :
53
74
assert dbsync_utils .table_empty (key ), (
54
75
f"Expected { key } to be empty, but it is not."
55
76
)
56
- case "not_empty" :
77
+ case TableCondition . NOT_EMPTY :
57
78
assert not dbsync_utils .table_empty (key ), (
58
79
f"Expected { key } to have data, but it is empty."
59
80
)
60
- case "exists" :
81
+ case TableCondition . EXISTS :
61
82
assert dbsync_utils .table_exists (key ), (
62
83
f"Expected { key } to exist, but it does not."
63
84
)
64
- case "not_exists" :
85
+ case TableCondition . NOT_EXISTS :
65
86
assert not dbsync_utils .table_exists (key ), (
66
87
f"Expected { key } to NOT exist, but it does."
67
88
)
@@ -73,81 +94,93 @@ def check_dbsync_state(expected_state: dict) -> None:
73
94
@pytest .fixture
74
95
def db_sync_manager (
75
96
request : pytest .FixtureRequest , cluster_manager : cluster_management .ClusterManager
76
- ) -> dbsync_utils .DBSyncManager :
97
+ ) -> db_sync .DBSyncManager :
77
98
"""Provide db-sync manager on a singleton cluster.
78
99
79
100
Creates and returns a DBSyncManager instance with locked cluster resources
80
101
to ensure exclusive access during testing.
81
102
"""
82
103
cluster_manager .get (lock_resources = [cluster_management .Resources .CLUSTER ])
83
- return dbsync_utils .DBSyncManager (request )
104
+ return db_sync .DBSyncManager (request )
84
105
85
106
86
107
@pytest .mark .order (- 1 )
87
108
class TestDBSyncConfig :
88
109
"""Basic tests for DB-Sync Config."""
89
110
111
+ @allure .link (helpers .get_vcs_link ())
90
112
def test_basic_tx_out (
91
113
self ,
92
- db_sync_manager : dbsync_utils .DBSyncManager ,
114
+ db_sync_manager : db_sync .DBSyncManager ,
93
115
):
94
116
"""Test tx_out option."""
95
117
db_config = db_sync_manager .get_config_builder ()
96
118
97
119
# Test tx_out : enable
98
120
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
+ )
100
124
)
101
125
check_dbsync_state (
102
126
{
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 ,
107
131
}
108
132
)
109
133
110
134
# Test tx_out : disable
111
135
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
+ )
113
139
)
114
140
check_dbsync_state (
115
141
{
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 ,
122
148
}
123
149
)
124
150
151
+ @allure .link (helpers .get_vcs_link ())
125
152
@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
+ ],
127
158
)
128
159
def test_cbor (
129
160
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 ,
133
164
):
134
165
"""Test tx_cbor option with parametrization."""
135
166
db_config = db_sync_manager .get_config_builder ()
136
167
137
168
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 })
139
170
171
+ @allure .link (helpers .get_vcs_link ())
140
172
@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 )],
142
175
)
143
176
def test_multi_asset (
144
177
self ,
145
- db_sync_manager : dbsync_utils .DBSyncManager ,
178
+ db_sync_manager : db_sync .DBSyncManager ,
146
179
multi_asset_enable : bool ,
147
- expected_state : str ,
180
+ expected_state : TableCondition ,
148
181
):
149
182
"""Test multi_asset option with parametrization."""
150
183
db_config = db_sync_manager .get_config_builder ()
151
184
152
185
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