22
33import logging
44import typing as tp
5+ from enum import Enum
56
7+ import allure
68import pytest
79
810from cardano_node_tests .cluster_management import cluster_management
911from cardano_node_tests .utils import configuration
12+ from cardano_node_tests .utils import dbsync_service_manager as db_sync
1013from cardano_node_tests .utils import dbsync_utils
14+ from cardano_node_tests .utils import helpers
1115
1216LOGGER = logging .getLogger (__name__ )
1317
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,96 @@ def check_dbsync_state(expected_state: dict) -> None:
7394@pytest .fixture
7495def 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 )
87108class TestDBSyncConfig :
88109 """Basic tests for DB-Sync Config."""
89110
111+ @allure .link (helpers .get_vcs_link ())
112+ @pytest .mark .dbsync_config
90113 def test_basic_tx_out (
91114 self ,
92- db_sync_manager : dbsync_utils .DBSyncManager ,
115+ db_sync_manager : db_sync .DBSyncManager ,
93116 ):
94117 """Test tx_out option."""
95118 db_config = db_sync_manager .get_config_builder ()
96119
97120 # Test tx_out : enable
98121 db_sync_manager .restart_with_config (
99- db_config .with_tx_out (value = "enable" , force_tx_in = False , use_address_table = False )
122+ db_config .with_tx_out (
123+ value = db_sync .TxOutMode .ENABLE , force_tx_in = False , use_address_table = False
124+ )
100125 )
101126 check_dbsync_state (
102127 {
103- "address" : "not_exists" ,
104- "tx_in" : "not_empty" ,
105- "tx_out" : "not_empty" ,
106- "ma_tx_out" : "not_empty" ,
128+ db_sync . Table . ADDRESS : TableCondition . NOT_EXISTS ,
129+ db_sync . Table . TX_IN : TableCondition . NOT_EMPTY ,
130+ db_sync . Table . TX_OUT : TableCondition . NOT_EMPTY ,
131+ db_sync . Table . MA_TX_OUT : TableCondition . NOT_EMPTY ,
107132 }
108133 )
109134
110135 # Test tx_out : disable
111136 db_sync_manager .restart_with_config (
112- db_config .with_tx_out (value = "disable" , force_tx_in = True , use_address_table = True )
137+ db_config .with_tx_out (
138+ value = db_sync .TxOutMode .DISABLE , force_tx_in = True , use_address_table = True
139+ )
113140 )
114141 check_dbsync_state (
115142 {
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" ,
143+ db_sync . Table . ADDRESS : TableCondition . NOT_EXISTS ,
144+ db_sync . Table . TX_IN : TableCondition . EMPTY ,
145+ db_sync . Table . TX_OUT : TableCondition . EMPTY ,
146+ db_sync . Table . MA_TX_OUT : TableCondition . EMPTY ,
147+ db_sync . Column . Tx . FEE : ColumnCondition . ZERO ,
148+ db_sync . Column . Redeemer . SCRIPT_HASH : ColumnCondition . IS_NULL ,
122149 }
123150 )
124151
152+ @allure .link (helpers .get_vcs_link ())
153+ @pytest .mark .dbsync_config
125154 @pytest .mark .parametrize (
126- ("tx_cbor_value" , "expected_state" ), [("enable" , "not_empty" ), ("disable" , "empty" )]
155+ ("tx_cbor_value" , "expected_state" ),
156+ [
157+ (db_sync .SettingState .ENABLE , TableCondition .NOT_EMPTY ),
158+ (db_sync .SettingState .DISABLE , TableCondition .EMPTY ),
159+ ],
127160 )
128161 def test_cbor (
129162 self ,
130- db_sync_manager : dbsync_utils .DBSyncManager ,
131- tx_cbor_value : tp . Literal [ "enable" , "disable" ] ,
132- expected_state : str ,
163+ db_sync_manager : db_sync .DBSyncManager ,
164+ tx_cbor_value : db_sync . SettingState ,
165+ expected_state : TableCondition ,
133166 ):
134167 """Test tx_cbor option with parametrization."""
135168 db_config = db_sync_manager .get_config_builder ()
136169
137170 db_sync_manager .restart_with_config (db_config .with_tx_cbor (tx_cbor_value ))
138- check_dbsync_state ({"tx_cbor" : expected_state })
171+ check_dbsync_state ({db_sync . Table . TX_CBOR : expected_state })
139172
173+ @allure .link (helpers .get_vcs_link ())
174+ @pytest .mark .dbsync_config
140175 @pytest .mark .parametrize (
141- ("multi_asset_enable" , "expected_state" ), [(True , "not_empty" ), (False , "empty" )]
176+ ("multi_asset_enable" , "expected_state" ),
177+ [(True , TableCondition .NOT_EMPTY ), (False , TableCondition .EMPTY )],
142178 )
143179 def test_multi_asset (
144180 self ,
145- db_sync_manager : dbsync_utils .DBSyncManager ,
181+ db_sync_manager : db_sync .DBSyncManager ,
146182 multi_asset_enable : bool ,
147- expected_state : str ,
183+ expected_state : TableCondition ,
148184 ):
149185 """Test multi_asset option with parametrization."""
150186 db_config = db_sync_manager .get_config_builder ()
151187
152188 db_sync_manager .restart_with_config (db_config .with_multi_asset (enable = multi_asset_enable ))
153- check_dbsync_state ({"multi_asset" : expected_state })
189+ check_dbsync_state ({db_sync . Table . MULTI_ASSET : expected_state })
0 commit comments