# pylint: disable=unused-argument,protected-access,too-many-arguments,too-many-locals import os from http import HTTPStatus from unittest.mock import MagicMock, patch import pytest from db_schema.schemas import slz from requests.models import Response from slz_spotify_charts_scraper.entities import Config from slz_spotify_charts_scraper.service import SpotifyChartsService from tests import FIXTURES_PATH from tests.entities import SpotifyServiceExpected @pytest.mark.integration @pytest.mark.parametrize( 'status_code, snapshot_of_hash, schema_version, expected_result', [ ( # Charts fetched, hash value is new, validation successful. HTTPStatus.OK, '202cb962ac59075b964b07152d234b70', '2017-01-01.json', SpotifyServiceExpected(slz.ContentStatusEnum.COMPLETE, 1, 0) ), ( # Charts fetched, hash hasn't changed, validation successful. HTTPStatus.OK, '33c4e168f0a72080a31e1211c5f0098b', '2017-01-01.json', SpotifyServiceExpected(slz.ContentStatusEnum.COMPLETE, 0, 0) ), ( # Charts fetched, hash hasn't changed, validation should fail, but not performed. HTTPStatus.OK, '33c4e168f0a72080a31e1211c5f0098b', '2017-01-01-invalid.json', SpotifyServiceExpected(slz.ContentStatusEnum.COMPLETE, 0, 0) ), ( # Charts not found, processing stopped early. HTTPStatus.NOT_FOUND, '202cb962ac59075b964b07152d234b70', '2017-01-01.json', SpotifyServiceExpected(slz.ContentStatusEnum.MISSING, 0, 0) ), ( # Charts fetched, hash value is new, validation failed. HTTPStatus.OK, '202cb962ac59075b964b07152d234b70', '2017-01-01-invalid.json', SpotifyServiceExpected(slz.ContentStatusEnum.FAILED, 0, 1) ), ( # Charts fetch failed, processing stopped early. HTTPStatus.FORBIDDEN, '202cb962ac59075b964b07152d234b70', '2017-01-01.json', SpotifyServiceExpected(slz.ContentStatusEnum.FAILED, 0, 1) ), ], indirect=['snapshot_of_hash'], ) @patch('slz_spotify_charts_scraper.service.requests.get') def test__process_market_charts_data( requests_mock: MagicMock, db, s3_client, spotify_service: SpotifyChartsService, config_test: Config, unit_of_work: slz.UnitOfWork, snapshot_of_hash: slz.Snapshot, charts_page_content: bytes, clean_db, status_code: HTTPStatus, schema_version: str, expected_result: SpotifyServiceExpected, ): # Prepare validation scheme. schema_path = os.path.join(FIXTURES_PATH, 'validation', schema_version) with open(schema_path, 'r', encoding='utf-8') as file_: s3_client.put_object( Body=file_.read(), Bucket=config_test.config_bucket, Key='schemas/spotify/charts/v1/2017-01-01.json', ) custom_response = Response() custom_response.status_code = status_code custom_response._content = charts_page_content requests_mock.return_value = custom_response spotify_service._process_market_charts_data(market='us') content_status = db.query(slz.ContentStatus) \ .filter(slz.ContentStatus.unit_of_work_id == unit_of_work.unit_of_work_id) \ .one() new_snapshots_count = db.query(slz.Snapshot) \ .filter(slz.Snapshot.snapshot_id != snapshot_of_hash.snapshot_id) \ .count() assert content_status.content_status == expected_result.content_status assert new_snapshots_count == expected_result.new_snapshots_count assert db.query(slz.ContentFailureLog).count() == expected_result.failure_log_count