from unittest import TestCase from unittest.mock import Mock from apollo_delphi_migration.migration import ( MigrationTaskFactory, MigrationTask, ApolloReportLicensorKeyMatcher, MigrationContext, TheOrchardKeyMatcher) from apollo_delphi_migration.renaming import ( SpotifyRenamingStrategy, ApolloAppleMusicRenamingStrategy, TheOrchardAppleMusicRenamingStrategy ) from apollo_delphi_migration.s3 import Bucket class MigrationTaskFactoryTestCase(TestCase): def setUp(self) -> None: self.__logger_mock = Mock() self.__s3_client_mock = Mock() self.__src_bucket = Bucket(self.__logger_mock, self.__s3_client_mock, 'source') self.__dst_bucket = Bucket(self.__logger_mock, self.__s3_client_mock, 'destination') self.__context = MigrationContext( src_bucket=self.__src_bucket, dst_bucket=self.__dst_bucket, renaming_strategies=[ SpotifyRenamingStrategy(self.__logger_mock), ApolloAppleMusicRenamingStrategy(self.__logger_mock), TheOrchardAppleMusicRenamingStrategy(self.__logger_mock) ], post_processors=[]) self.__factory = MigrationTaskFactory(self.__logger_mock, self.__context) def test_create_should_select_spotify_renaming_when_spotify_key_is_provided(self): key = 'spotify/2014-12-01/spotify_2014-12-01_sony_v1.0_us_streams.ndjson' task = self.__factory.create(key) dst_key = 'spotify/streams/v1/report_date=2014-12-01/report_licensor=sme/streams_20141201_US.gz' self.assertEqual(dst_key, task.dst_key) def test_create_should_select_apple_renaming_when_apple_key_is_provided(self): key = 'apple/2019-01-01/apple_2019-01-01_smej_86780702_v1_0_amStreams.tsv' task = self.__factory.create(key) dst_key = 'apple/streams/v1_0/report_date=2019-01-01/report_licensor=smejp/AppleMusic_Streams_86780702_20190101_V1_0.txt.gz' self.assertEqual(dst_key, task.dst_key) def test_create_should_raise_error_when_unsupported_key_provided(self): self.assertRaises(NotImplementedError, self.__factory.create, 'unsupported') class MigrationTaskTestCase(TestCase): def setUp(self) -> None: self.__logger_mock = Mock() self.__s3_client = Mock() self.__src_bucket = Bucket(self.__logger_mock, self.__s3_client, 'src_bucket') self.__dst_bucket = Bucket(self.__logger_mock, self.__s3_client, 'dst_bucket') self.__first_post_processor_mock = Mock() self.__second_post_processor_mock = Mock() self.__context = MigrationContext( src_bucket=self.__src_bucket, dst_bucket=self.__dst_bucket, renaming_strategies=[ SpotifyRenamingStrategy(self.__logger_mock), ApolloAppleMusicRenamingStrategy(self.__logger_mock), TheOrchardAppleMusicRenamingStrategy(self.__logger_mock) ], post_processors=[ self.__first_post_processor_mock, self.__second_post_processor_mock ]) def test_execute_should_call_s3_client(self): task = MigrationTask(self.__logger_mock, 'src_key', 'dst_key', self.__context) task.execute() self.__s3_client.copy.assert_called_with( CopySource={'Bucket': self.__src_bucket.name, 'Key': 'src_key'}, Bucket=self.__dst_bucket.name, Key='dst_key') def test_execute_should_call_all_post_processors(self): task = MigrationTask(self.__logger_mock, 'src_key', 'dst_key', self.__context) task.execute() self.__first_post_processor_mock.post_process.assert_called_with('src_key', 'dst_key', self.__context) self.__second_post_processor_mock.post_process.assert_called_with('src_key', 'dst_key', self.__context) class ApolloReportLicensorKeyMatcherTestCase(TestCase): keys = [ 'spotify/2014-12-01/spotify_2014-12-01_sony_v1.0_us_streams.ndjson', 'spotify/2019-01-01/spotify_2019-01-01_sony_v2.4_ru_sub_30_sec_streams.ndjson', 'apple/2019-01-01/apple_2019-01-01_sony_86780702_v1_0_amStreams.tsv', 'apple/2019-01-01/apple_2019-01-01_sony_86780702_v1_1_amNonRoyaltyStreams.tsv' ] def test_match_should_not_return_anything(self): matcher = ApolloReportLicensorKeyMatcher('streams', 'smej') result = list(filter(matcher.match, self.keys)) self.assertEqual(0, len(result)) def test_match_should_match_spotify_streams_key(self): matcher = ApolloReportLicensorKeyMatcher('streams', 'sony') result = list(filter(matcher.match, self.keys)) self.assertEqual(1, len(result)) self.assertEqual('spotify/2014-12-01/spotify_2014-12-01_sony_v1.0_us_streams.ndjson', result[0]) def test_match_should_match_spotify_sub_30_sec_streams_key(self): matcher = ApolloReportLicensorKeyMatcher('sub_30_sec_streams', 'sony') result = list(filter(matcher.match, self.keys)) self.assertEqual(1, len(result)) self.assertEqual('spotify/2019-01-01/spotify_2019-01-01_sony_v2.4_ru_sub_30_sec_streams.ndjson', result[0]) class TheOrchardKeyMatcherTestCase(TestCase): def test_match_should_match_streams_key(self): result = TheOrchardKeyMatcher('streams') \ .match('apple/2016-06-13/AppleMusic_Streams_80029727_20160613.txt.gz') self.assertTrue(result) def test_match_should_match_content_key(self): result = TheOrchardKeyMatcher('content') \ .match('apple/2016-06-13/AppleMusic_Content_80029727_20160613.txt.gz') self.assertTrue(result)