"""Tests for parsing MR_AUDIT table.""" import json import pytest from parse_mr_audit import dynamo_audit_record_to_json ADD_TERRITORY = { 'correlation_id': '{\"s\": \"123\"}', 'isrc': '{\"s\": \"TR3340601592\"}', 'opcode': '{\"s\": \"ADD\"}', 'territories': '{\"m\": {\"PR\": {\"n\": \"12878799\"}}}', 'timestamp': '{\"n\": \"1507639440000.0\"}', 'user': '{\"s\": \"818\"}' } ADD_TERRITORY_PARSED = ( '{"isrc": "TR3340601592",' '"data": {"territories": {"PR": 12878799},"correlation_id": "123",' '"opcode": "ADD","user": "818","lock_reason": null,' '"source": null, "conflict": null},' '"timestamp": 1507639440.0}' ) REMOVE_TERRITORY = { 'correlation_id': '{\"s\": \"123\"}', 'isrc': '{\"s\": \"TR3340601592\"}', 'opcode': '{\"s\": \"REMOVE\"}', 'territories': '{\"l\": [{\"s\": \"PR\"}]}', 'timestamp': '{\"n\": \"1507639440000.0\"}', 'user': '{\"s\": \"818\"}' } REMOVE_TERRITORY_PARSED = ( '{"isrc": "TR3340601592", "data": {"territories": {"PR": null},' '"correlation_id": "123", "opcode": "REMOVE", "user": "818",' '"lock_reason": null, "source": null, "conflict": null},' '"timestamp": 1507639440.0}' ) LOCK_TERRITORY = { 'correlation_id': '{\"s\": \"123\"}', 'isrc': '{\"s\": \"TR3340601592\"}', 'lock_reason': '{\"s\": \"test_reason\"}', 'opcode': '{\"s\": \"LOCK\"}', 'territories': '{\"l\": [{\"s\": \"PR\"},{\"s\": \"AD\"}]}', 'timestamp': '{\"n\": \"1507639440000.0\"}', 'user': '{\"s\": \"818\"}' } LOCK_TERRITORY_PARSED = ( '{"isrc": "TR3340601592","data": {"territories": {"PR": null,"AD": null},' '"correlation_id": "123", "opcode": "LOCK", "user": "818",' '"lock_reason": "test_reason", "source": null, "conflict": null},' '"timestamp": 1507639440.0}' ) UNLOCK_TERRITORY = { 'correlation_id': '{\"s\": \"123\"}', 'isrc': '{\"s\": \"TR3340601592\"}', 'opcode': '{\"s\": \"UNLOCK\"}', 'territories': '{\"l\": [{\"s\": \"PR\"},{\"s\": \"AD\"}]}', 'timestamp': '{\"n\": \"1507639440000.0\"}', 'user': '{\"s\": \"818\"}' } UNLOCK_TERRITORY_PARSED = ( '{"isrc": "TR3340601592","data": {"territories": {"PR": null,"AD": null},' '"correlation_id": "123", "opcode": "UNLOCK", "user": "818",' '"lock_reason": null, "source": null, "conflict": null},' '"timestamp": 1507639440.0}' ) CONFLICT_CREATED = { 'conflict': ( '{\"m\": {\"conflicting_tuid\": {\"l\": [{\"n\": \"27227164\"},' '{\"n\": \"24512302\"}]},\"status\": {\"s\": \"CONFLICT_CREATED\"}}}'), 'correlation_id': '{\"s\": \"123\"}', 'isrc': '{\"s\": \"TR3340601592\"}', 'opcode': '{\"s\": \"CONFLICT_CREATED\"}', 'source': '{\"s\": \"BULK_UPDATE_UPC\"}', 'territories': ( '{\"m\": {\"AD\": {\"n\": \"27227164\"},' '\"AE\": {\"n\": \"27227164\"}}}'), 'timestamp': '{\"n\": \"1507639440000.0\"}', 'user': '{\"s\": \"818\"}' } CONFLICT_CREATED_PARSED = ( '{"isrc": "TR3340601592", "data": {"territories": {"AD": 27227164,' '"AE": 27227164}, "correlation_id": "123", "opcode": "CONFLICT_CREATED",' '"user": "818", "lock_reason": null, "source": "BULK_UPDATE_UPC",' '"conflict": {"status": "CONFLICT_CREATED", "conflicting_tuid":' '[27227164, 24512302], "resolved": null}},"timestamp": 1507639440.0}' ) CONFLICT_RESOLVED = { 'conflict': ( '{\"m\": {\"conflicting_tuid\": {\"l\": [{\"n\": \"27227164\"},' '{\"n\": \"24512302\"}]},\"resolved\": {\"n\": \"1\"},\"status\":' ' {\"s\": \"CONFLICT_RESOLVED\"}}}'), 'correlation_id': '{\"s\": \"123\"}', 'isrc': '{\"s\": \"TR3340601592\"}', 'opcode': '{\"s\": \"CONFLICT_RESOLVED\"}', 'source': '{\"s\": \"MANUAL_EDIT_ISRC\"}', 'territories': ( '{\"m\": {\"AD\": {\"n\": \"27227164\"},\"AE\": {\"n\": \"27227164\"}' '}}'), 'timestamp': '{\"n\": \"1507639440000.0\"}', 'user': '{\"s\": \"818\"}' } CONFLICT_RESOLVED_PARSED = ( '{"isrc": "TR3340601592", "data": {"territories": {"AD": 27227164,' '"AE": 27227164}, "correlation_id": "123", "opcode": "CONFLICT_RESOLVED",' '"user": "818", "lock_reason": null, "source": "MANUAL_EDIT_ISRC",' '"conflict": {"status": "CONFLICT_RESOLVED", "conflicting_tuid":' '[27227164, 24512302], "resolved": 1}}, "timestamp": 1507639440.0}' ) @pytest.mark.parametrize( 'record, expected_result', [ (ADD_TERRITORY, ADD_TERRITORY_PARSED), (REMOVE_TERRITORY, REMOVE_TERRITORY_PARSED), (LOCK_TERRITORY, LOCK_TERRITORY_PARSED), (UNLOCK_TERRITORY, UNLOCK_TERRITORY_PARSED), (CONFLICT_CREATED, CONFLICT_CREATED_PARSED), (CONFLICT_RESOLVED, CONFLICT_RESOLVED_PARSED) ] ) def test_parse_mr_audit(record, expected_result): """Test parse MR_AUDIT table success.""" parsed = dynamo_audit_record_to_json(record) parsed_str = json.loads(parsed) expected_str = json.loads(expected_result) assert parsed_str == expected_str