"""Tests for DynamoDB commands.""" import textwrap from pathlib import Path from typing import Any import pytest from mypy_boto3_dynamodb import DynamoDBClient from pdp.cli.commands.dynamo import DEV_M2M_IDENTITY from pdp.connectors.dynamo import DynamoDbConnector from tests.integration.utils import run_command @pytest.fixture() def identity_csv_filename(tmp_path: Path) -> str: """CSV file for testing load_csv commands.""" d = tmp_path / "ddb_command_test" d.mkdir() p = d / "test.csv" input_str = textwrap.dedent( """\ identity_uuid,tenant_uuid,tenant_type,role,operation e48f7802-bfb9-4a6a-8b6c-8a2dae62cc8e,bca99eb6-ef2d-4f2a-b80e-936dfccf1eda,account,songwhip_read,attach e48f7802-bfb9-4a6a-8b6c-8a2dae62cc8e,bca99eb6-ef2d-4f2a-b80e-936dfccf1eda,account,audience_development_admin,attach 6028a43a-c53a-4933-b8f9-caf082294c1a,bca99eb6-ef2d-4f2a-b80e-936dfccf1eda,account,songwhip_read,attach 6028a43a-c53a-4933-b8f9-caf082294c1a,bca99eb6-ef2d-4f2a-b80e-936dfccf1eda,account,audience_development_admin,attach b358e79b-f033-45c0-b182-1aacdfa80cee,bca99eb6-ef2d-4f2a-b80e-936dfccf1eda,account,songwhip_read,attach b358e79b-f033-45c0-b182-1aacdfa80cee,bca99eb6-ef2d-4f2a-b80e-936dfccf1eda,account,audience_development_admin,attach b358e79b-f033-45c0-b182-1aacdfa80cee,bca99eb6-ef2d-4f2a-b80e-936dfccf1eda,account,contract_viewer,attach b358e79b-f033-45c0-b182-1aacdfa80cee,bca99eb6-ef2d-4f2a-b80e-936dfccf1eda,account,contract_viewer,detach """ ) p.write_text(input_str) return str(p) def test_load_csv_command( default_boto_client: DynamoDBClient, local_dynamo_connector_with_range: DynamoDbConnector, identity_csv_filename: str, monkeypatch: Any, ) -> None: """Test the load_csv command.""" run_command( [ "uv", "run", "pdpcli", "dynamodb", "load_csv", identity_csv_filename, ] ) result = local_dynamo_connector_with_range.query_by_hash_key( "e48f7802-bfb9-4a6a-8b6c-8a2dae62cc8e", "bca99eb6-ef2d-4f2a-b80e-936dfccf1eda", ) assert_valid_item("e48f7802-bfb9-4a6a-8b6c-8a2dae62cc8e", result.get("items")) result = local_dynamo_connector_with_range.query_by_hash_key( "6028a43a-c53a-4933-b8f9-caf082294c1a", "bca99eb6-ef2d-4f2a-b80e-936dfccf1eda", ) assert_valid_item("6028a43a-c53a-4933-b8f9-caf082294c1a", result.get("items")) result = local_dynamo_connector_with_range.query_by_hash_key( "b358e79b-f033-45c0-b182-1aacdfa80cee", "bca99eb6-ef2d-4f2a-b80e-936dfccf1eda", ) assert_valid_item("b358e79b-f033-45c0-b182-1aacdfa80cee", result.get("items")) def assert_valid_item(identity_uuid: str, items: Any) -> None: """Validate load_csv test item.""" assert len(items) == 1 assert items[0]["identity_uuid"] == {"S": identity_uuid} assert items[0]["tenant_uuid"] == {"S": "bca99eb6-ef2d-4f2a-b80e-936dfccf1eda"} assert items[0]["roles"] == { "L": [ {"M": {"role": {"S": "songwhip_read"}}}, {"M": {"role": {"S": "audience_development_admin"}}}, ] } assert items[0]["created_by"] == {"S": DEV_M2M_IDENTITY}