|
| 1 | +"""Tests for basic DB-Sync configuration options.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +import typing as tp |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from cardano_node_tests.cluster_management import cluster_management |
| 9 | +from cardano_node_tests.utils import configuration |
| 10 | +from cardano_node_tests.utils import dbsync_utils |
| 11 | + |
| 12 | +LOGGER = logging.getLogger(__name__) |
| 13 | + |
| 14 | +pytestmark = [ |
| 15 | + pytest.mark.skipif( |
| 16 | + configuration.CLUSTERS_COUNT != 1 and not configuration.HAS_DBSYNC, |
| 17 | + reason="db-sync config tests can run on a single cluster only", |
| 18 | + ), |
| 19 | + pytest.mark.dbsync_config, |
| 20 | +] |
| 21 | + |
| 22 | + |
| 23 | +def check_dbsync_state(expected_state: dict) -> None: |
| 24 | + """Check the state of db-sync tables and columns against expected conditions. |
| 25 | +
|
| 26 | + Args: |
| 27 | + expected_state: Dictionary specifying conditions to verify where: |
| 28 | + - Key format: |
| 29 | + * "table" for table-level checks |
| 30 | + * "table.column" for column-level checks |
| 31 | + - 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 |
| 38 | +
|
| 39 | + Returns: |
| 40 | + bool: True if all conditions match, False otherwise |
| 41 | + """ |
| 42 | + for key, condition in expected_state.items(): |
| 43 | + if "." in key: # Column-level check |
| 44 | + table, column = key.split(".", 1) |
| 45 | + assert condition.startswith("column_condition:"), ( |
| 46 | + f"Invalid column condition format: {condition}" |
| 47 | + ) |
| 48 | + column_condition = condition.split(":", 1)[1] |
| 49 | + dbsync_utils.check_column_condition(table, column, column_condition) |
| 50 | + else: # Table-level check |
| 51 | + match condition: |
| 52 | + case "empty": |
| 53 | + assert dbsync_utils.table_empty(key), ( |
| 54 | + f"Expected {key} to be empty, but it is not." |
| 55 | + ) |
| 56 | + case "not_empty": |
| 57 | + assert not dbsync_utils.table_empty(key), ( |
| 58 | + f"Expected {key} to have data, but it is empty." |
| 59 | + ) |
| 60 | + case "exists": |
| 61 | + assert dbsync_utils.table_exists(key), ( |
| 62 | + f"Expected {key} to exist, but it does not." |
| 63 | + ) |
| 64 | + case "not_exists": |
| 65 | + assert not dbsync_utils.table_exists(key), ( |
| 66 | + f"Expected {key} to NOT exist, but it does." |
| 67 | + ) |
| 68 | + case _: |
| 69 | + error_msg = f"Unknown table condition '{condition}' for table '{key}'" |
| 70 | + raise ValueError(error_msg) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.fixture |
| 74 | +def db_sync_manager( |
| 75 | + request: pytest.FixtureRequest, cluster_manager: cluster_management.ClusterManager |
| 76 | +) -> dbsync_utils.DBSyncManager: |
| 77 | + """Provide db-sync manager on a singleton cluster. |
| 78 | +
|
| 79 | + Creates and returns a DBSyncManager instance with locked cluster resources |
| 80 | + to ensure exclusive access during testing. |
| 81 | + """ |
| 82 | + cluster_manager.get(lock_resources=[cluster_management.Resources.CLUSTER]) |
| 83 | + return dbsync_utils.DBSyncManager(request) |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.order(-1) |
| 87 | +class TestDBSyncConfig: |
| 88 | + """Basic tests for DB-Sync Config.""" |
| 89 | + |
| 90 | + def test_basic_tx_out( |
| 91 | + self, |
| 92 | + db_sync_manager: dbsync_utils.DBSyncManager, |
| 93 | + ): |
| 94 | + """Test tx_out option.""" |
| 95 | + db_config = db_sync_manager.get_config_builder() |
| 96 | + |
| 97 | + # Test tx_out : enable |
| 98 | + db_sync_manager.restart_with_config( |
| 99 | + db_config.with_tx_out(value="enable", force_tx_in=False, use_address_table=False) |
| 100 | + ) |
| 101 | + check_dbsync_state( |
| 102 | + { |
| 103 | + "address": "not_exists", |
| 104 | + "tx_in": "not_empty", |
| 105 | + "tx_out": "not_empty", |
| 106 | + "ma_tx_out": "not_empty", |
| 107 | + } |
| 108 | + ) |
| 109 | + |
| 110 | + # Test tx_out : disable |
| 111 | + db_sync_manager.restart_with_config( |
| 112 | + db_config.with_tx_out(value="disable", force_tx_in=True, use_address_table=True) |
| 113 | + ) |
| 114 | + check_dbsync_state( |
| 115 | + { |
| 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", |
| 122 | + } |
| 123 | + ) |
| 124 | + |
| 125 | + @pytest.mark.parametrize( |
| 126 | + ("tx_cbor_value", "expected_state"), [("enable", "not_empty"), ("disable", "empty")] |
| 127 | + ) |
| 128 | + def test_cbor( |
| 129 | + self, |
| 130 | + db_sync_manager: dbsync_utils.DBSyncManager, |
| 131 | + tx_cbor_value: tp.Literal["enable", "disable"], |
| 132 | + expected_state: str, |
| 133 | + ): |
| 134 | + """Test tx_cbor option with parametrization.""" |
| 135 | + db_config = db_sync_manager.get_config_builder() |
| 136 | + |
| 137 | + db_sync_manager.restart_with_config(db_config.with_tx_cbor(tx_cbor_value)) |
| 138 | + check_dbsync_state({"tx_cbor": expected_state}) |
| 139 | + |
| 140 | + @pytest.mark.parametrize( |
| 141 | + ("multi_asset_enable", "expected_state"), [(True, "not_empty"), (False, "empty")] |
| 142 | + ) |
| 143 | + def test_multi_asset( |
| 144 | + self, |
| 145 | + db_sync_manager: dbsync_utils.DBSyncManager, |
| 146 | + multi_asset_enable: bool, |
| 147 | + expected_state: str, |
| 148 | + ): |
| 149 | + """Test multi_asset option with parametrization.""" |
| 150 | + db_config = db_sync_manager.get_config_builder() |
| 151 | + |
| 152 | + db_sync_manager.restart_with_config(db_config.with_multi_asset(enable=multi_asset_enable)) |
| 153 | + check_dbsync_state({"multi_asset": expected_state}) |
0 commit comments