"""Tests for Physical Pricing Bulk upload.""" import json from unittest.mock import MagicMock import boto3 import ingest_physical_pricing_jenkins as ingest_script import mock from moto import mock_s3 def s3_initialize(key_name='ready_test.json'): """Function to initialize s3 test cases with dependencies.""" bucket_name = 'dev-cucumbers' key_contents = { 'oa_user': {'id': '562', 'email': 'email@domain.com'}, 'submission_date': '2018-03-21 08:33:52', 'override_data': [{'release_id': '2253210', 'orchard_price_tier': { 'id': 79, 'name': 'Vinyl Tier #16'}, 'store_pricing': { '696': '', '738': '24', '739': '0.34', '737': '34.78', '740': '3'}}]} s3 = boto3.resource('s3', region_name='eu-west-1') bucket = s3.create_bucket(Bucket=bucket_name) batchfile = s3.Object(bucket_name, key_name) batchfile.put(Body=json.dumps(key_contents)) return bucket, batchfile @mock.patch('ingest_physical_pricing_jenkins.Session') def test_getS3connection(mock_session): """Test to read pricing data from batch file.""" mock_conn = MagicMock() mock_conn.resource.return_value = 's3' mock_session.return_value = mock_conn s3conn = ingest_script.getS3connection() assert s3conn == 's3' @mock_s3 def test_get_pricing_data(): """Test to read pricing data from batch file.""" key_contents = { 'oa_user': {'id': '562', 'email': 'email@domain.com'}, 'submission_date': '2018-03-21 08:33:52', 'override_data': [{'release_id': '2253210', 'orchard_price_tier': { 'id': 79, 'name': 'Vinyl Tier #16'}, 'store_pricing': { '696': '', '738': '24', '739': '0.34', '737': '34.78', '740': '3'}}]} key_name = 'ready_test.json' bucket, batchfile = s3_initialize(key_name) pricing_data_response = ingest_script.get_pricing_data(batchfile) assert pricing_data_response == key_contents @mock_s3 def test_rename_ingest_batch(): """Test to rename ingest batch.""" key_name = 'ready_test.json' bucket, batchfile = s3_initialize(key_name) session_res = ingest_script.getS3connection() new_name = 'running_test.json' ingest_script.rename_ingest_batch(bucket, session_res, key_name, new_name) renamed_filename = [f.key for f in bucket.objects.all()][0] assert new_name == renamed_filename @mock_s3 def test_archive_batch(): """Test to archive ingest batch.""" key_name = 'bulk-physical-pricing-ingest/complete_test.json' completed_filename = \ 'bulk-physical-pricing-ingest-archive/complete_test.json' bucket, batchfile = s3_initialize(key_name) session_res = ingest_script.getS3connection() ingest_script.archive_batch(bucket, session_res, key_name) renamed_filename = [f.key for f in bucket.objects.all()][0] assert completed_filename == renamed_filename @mock_s3 def test_get_oldest_batch_to_ingest(): """Test to get oldest ingest batch.""" key_name = 'bulk-physical-pricing-ingest/ready_test.json' bucket, batchfile = s3_initialize(key_name) oldest_ingestable_batch = ingest_script.get_oldest_batch_to_ingest(bucket) assert key_name == oldest_ingestable_batch.key @mock.patch('ingest_physical_pricing_jenkins.requests') def test_ingest_pricing_row(mock_request): """Test to ingest a row from a batch.""" pricing_row = {'release_id': '2253210', 'orchard_price_tier': { 'id': 79, 'name': 'Vinyl Tier #16'}, 'store_pricing': { '696': '', '738': '24', '739': '0.34', '737': '34.78', '740': '3', '741': '34.78', '742': '3'}} product_id = pricing_row['release_id'] called_with_json = {'items': []} sort_order = 1 for store_id in pricing_row['store_pricing']: override_template = { 'sort_order': 0, 'price_code': '', 'product_id': pricing_row['release_id'], 'applies_worldwide': True, 'activated': True, 'stores': [], 'pricing_family_id': 4} override_template['sort_order'] = sort_order override_template['price_code'] = \ pricing_row['store_pricing'][store_id] override_template['stores'].append(store_id) called_with_json['items'].append(override_template) sort_order += 1 ingest_script.ingest_pricing_row(pricing_row) mock_request.delete.assert_called_with( 'https://qa-ows-pricing.theorchard.io/legacy/product/{}/unmigrate' .format(product_id)) mock_request.put.assert_called_with( 'https://qa-ows-pricing.theorchard.io/product/{}/pricing-family/4/\ orchard_pricing_tier'.format(product_id), json={ 'orchard_pricing_tier_id': pricing_row['orchard_price_tier']['id']}) mock_request.post.assert_called_with( 'http://qa-ows-pricing.theorchard.io/product/{}/override/bulk'.format( product_id), json=called_with_json)