from tests.conftest import BUCKET_NAME from unittest import mock from sample_validator import find_schema, SchemaNotFoundError, S3Error import pytest @pytest.fixture(scope='function') def s3_upload_schemas(s3_client): schemas = [ 'schemas/apple/content/v1_0/2015-04-27.json', 'schemas/apple/content/v1_0/2016-04-27.json', 'schemas/apple/content/v1_0/2017-04-17.json', 'schemas/apple/content/v1_0/2017-04-21.json', 'schemas/apple/content/v1_0/2018-04-27.json', 'schemas/apple/content/v1_1/2017-11-16.json', ] s3_client.create_bucket(Bucket=BUCKET_NAME) for schema in schemas: s3_client.put_object(Bucket=BUCKET_NAME, Body=b'', Key=schema) yield @pytest.mark.parametrize('current_date,expected_report_date', ( ('2017-04-20', '2017-04-17'), ('2015-04-27', '2015-04-27'), ('2017-04-17', '2017-04-17'), ('2019-04-20', '2018-04-27'), ('2017-04-22', '2017-04-21'), )) def test_find_schema_correct(s3_upload_schemas, s3_client, current_date, expected_report_date): logger = mock.Mock() schema_path = find_schema( logger, s3_client, bucket='test_bucket', dsp='apple', report_type='content', version='v1_0', current_date=current_date) assert schema_path == f's3://test_bucket/schemas/apple/content/v1_0/{expected_report_date}.json' def test_find_schema_correct_raise_exc(s3_upload_schemas, s3_client): logger = mock.Mock() with pytest.raises(SchemaNotFoundError): find_schema( logger, s3_client, bucket=BUCKET_NAME, dsp='apple', report_type='content', version='v1_1', current_date='2015-04-25' ) def test_incorrect_aws_credentials(s3_upload_schemas, s3_client): logger = mock.Mock() with pytest.raises(S3Error): find_schema( logger, s3_client, bucket='doesntexists', dsp='apple', report_type='content', version='v1_1', current_date='2011-04-20' )