"""Test ownership model operations.""" from unittest.mock import call from unittest.mock import patch import pytest from sound_recordings.cypher import ownership as cypher from sound_recordings.models import ownership @pytest.mark.parametrize( 'results, error', [ ( [ {'track_id': 1, 'vendor_id': 222, 'subaccount_id': 0}, {'track_id': 2, 'vendor_id': 333, 'subaccount_id': 0}, ], False ), ( [ {'track_id': 1, 'vendor_id': 222, 'subaccount_id': 0}, {'track_id': 2, 'vendor_id': 333, 'subaccount_id': 0}, {'track_id': 1, 'vendor_id': 222, 'subaccount_id': 0}, ], False ), ( [ {'track_id': 1, 'vendor_id': 222, 'subaccount_id': 0}, {'track_id': 2, 'vendor_id': 333, 'subaccount_id': 0}, {'track_id': 1, 'vendor_id': 222, 'subaccount_id': 2}, ], True ), ( [], True ), ( [ {'track_id': 1, 'vendor_id': 222, 'subaccount_id': 0}, {'track_id': 1, 'vendor_id': 333, 'subaccount_id': 0} ], True ), ( [ {'track_id': 1, 'vendor_id': None, 'subaccount_id': None} ], True ) ] ) @patch('sound_recordings.models.ownership.get_session') def test_bulk_track(mock_neo4j_get_session, results, error): """Test bulk track ownership query.""" mock_neo4j_session = mock_neo4j_get_session.return_value mock_neo4j_session.run.return_value = results track_ids = [1, 1, 1, 2] if not error: track_ownership = ownership.get_bulk_track_owners(track_ids) assert track_ownership[1]['vendor_id'] == results[0]['vendor_id'] assert track_ownership[2]['subaccount_id'] == results[0]['subaccount_id'] else: with pytest.raises(ownership.InvalidOwnershipData): ownership.get_bulk_track_owners(track_ids) assert mock_neo4j_session.run.call_args_list == [ call( cypher.GET_BULK_TRACK_OWNERS, track_ids=[1, 2] ) ] @patch('sound_recordings.models.ownership.get_session') def test_bulk_missing_track_ownership(mock_neo4j_get_session): """Test bulk missing track ownership.""" mock_neo4j_session = mock_neo4j_get_session.return_value mock_neo4j_session.run.return_value = [ {'track_id': 1, 'vendor_id': 111, 'subaccount_id': 1} ] track_ids = [1, 2] with pytest.raises(ownership.InvalidOwnershipData): ownership.get_bulk_track_owners(track_ids) @pytest.mark.parametrize( 'results, error', [ ( [ {'vendor_id': 222, 'subaccount_id': 1} ], False ), ( [], True ), ( [ {'vendor_id': 222, 'subaccount_id': 1}, {'vendor_id': 333, 'subaccount_id': 1} ], True ), ( [ {'vendor_id': None, 'subaccount_id': 1} ], True ) ] ) @patch('sound_recordings.models.ownership.get_session') def test_bulk_subaccount(mock_neo4j_get_session, results, error): """Test bulk subaccount ownership query.""" mock_neo4j_session = mock_neo4j_get_session.return_value mock_neo4j_session.run.return_value = results subaccount_ids = [1] if not error: subaccount_ownership = ownership.get_bulk_subaccount_owner(subaccount_ids) assert subaccount_ownership[1]['vendor_id'] == results[0]['vendor_id'] else: with pytest.raises(ownership.InvalidOwnershipData): ownership.get_bulk_subaccount_owner(subaccount_ids) assert mock_neo4j_session.run.call_args_list == [ call( cypher.GET_BULK_SUBACCOUNT_OWNER, subaccount_ids=subaccount_ids ) ] @patch('sound_recordings.models.ownership.get_session') def test_bulk_missing_subaccount_ownership(mock_neo4j_get_session): """Test bulk missing subaccount ownership.""" mock_neo4j_session = mock_neo4j_get_session.return_value mock_neo4j_session.run.return_value = [ {'vendor_id': 111, 'subaccount_id': 1} ] subaccount_ids = [1, 2] with pytest.raises(ownership.InvalidOwnershipData): ownership.get_bulk_subaccount_owner(subaccount_ids)