import copy from unittest.mock import patch from unittest.mock import Mock import pytest from freezegun import freeze_time import takedown ISRC = 'ABC123' FROZEN_TIMESTAMP = 1529676000000 TUID = 123 TUID_UPDATE = 222 NOT_EXISTING_TUID = 333 CORRELATION_ID = '111' TEST_ISRC = { 'isrc': ISRC, 'territories': { 'US': [{'tuid': TUID}], 'CA': {'tuid': TUID} }, 'locked_territories': [], 'timestamp': FROZEN_TIMESTAMP, } TEST_UPDATE_ISRC = { 'isrc': ISRC, 'territories': { 'US': [{'tuid': TUID_UPDATE}], 'CA': {'tuid': TUID} }, 'locked_territories': [], 'timestamp': FROZEN_TIMESTAMP, } AUDIT_ITEM = { 'isrc': ISRC, 'timestamp': FROZEN_TIMESTAMP, 'user': '179', 'opcode': 'TAKEDOWN', 'correlation_id': CORRELATION_ID, 'territories': {} } @pytest.fixture() def takedown_isrc_fixture(): """Fixture for ISRC record that should be removed from the registry.""" return TEST_ISRC @pytest.fixture() def update_isrc_fixture(): """Fixture for ISRC record that should be updated in the registry.""" return TEST_UPDATE_ISRC @pytest.fixture() def takedown_audit_fixture(): """Fixture for TAKEDOWN audit record.""" item = copy.deepcopy(AUDIT_ITEM) item['opcode'] = 'TAKEDOWN' item['territories'] = {'US': TUID, 'CA': TUID} return item @pytest.fixture() def update_audit_fixture(): """Fixture for REMOVE audit record.""" item = copy.deepcopy(AUDIT_ITEM) item['opcode'] = 'REMOVE' item['territories'] = {'US': TUID_UPDATE} return item @patch('takedown.active_table.delete_item', new=Mock()) @patch('takedown.audit_table.put_item', new=Mock()) @patch('takedown.correlation_id', CORRELATION_ID) @freeze_time('2018-06-22 14:00:00') def test_takedown_isrc(takedown_isrc_fixture, takedown_audit_fixture): """Test completely remove ISRC record.""" tuid = 123 takedown.takedown_isrc(takedown_isrc_fixture, tuid) takedown.active_table.delete_item.assert_called_with(Key={'isrc': ISRC}) takedown.audit_table.put_item.assert_called_with( Item=takedown_audit_fixture) @patch('takedown.active_table.delete_item', new=Mock()) @patch('takedown.active_table.put_item', new=Mock()) @patch('takedown.audit_table.put_item', new=Mock()) @patch('takedown.correlation_id', CORRELATION_ID) @freeze_time('2018-06-22 14:00:00') def test_update_isrc(update_isrc_fixture, update_audit_fixture): """Test update ISRC record.""" tuid = 222 updated_item = copy.deepcopy(update_isrc_fixture) updated_item['territories'] = {'CA': [{'tuid': 123}]} takedown.takedown_isrc(update_isrc_fixture, tuid) takedown.active_table.delete_item.assert_not_called() takedown.active_table.put_item.assert_called_with(Item=updated_item) takedown.audit_table.put_item.assert_called_with(Item=update_audit_fixture) @patch('takedown.active_table.delete_item', new=Mock()) @patch('takedown.active_table.put_item', new=Mock()) @patch('takedown.audit_table.put_item', new=Mock()) def test_skip_isrc(takedown_isrc_fixture): """Test update ISRC record with not-existing TUID.""" tuid = NOT_EXISTING_TUID takedown.takedown_isrc(takedown_isrc_fixture, tuid) takedown.active_table.delete_item.assert_not_called() takedown.active_table.put_item.assert_not_called() takedown.audit_table.put_item.assert_not_called()