from typing import Optional import pytest from db_schema.postgres.connection import Connection from db_schema.schemas import slz from sqlalchemy.orm import scoped_session from slz_apple_music_charts_scrapper.services.snapshot import SnapshotService @pytest.mark.integration def test__get_last_snapshot_hash__return_none( snapshot_service: SnapshotService, content_status: slz.ContentStatus ): assert snapshot_service._get_last_snapshot_hash(content_status) is None @pytest.mark.integration def test__get_last_snapshot_hash__return_hash( snapshot_service: SnapshotService, snapshot: slz.Snapshot, content_status: slz.ContentStatus, ): snapshot_hash: str = snapshot.hash assert snapshot_service._get_last_snapshot_hash(content_status) == snapshot_hash @pytest.mark.integration @pytest.mark.parametrize( 'test_hash, snapshot_of_hash, expected_result', [ ('7c00ff0338fcbc78d6a4487fda47c608', '202cb962ac59075b964b07152d234b70', True), ('7c00ff0338fcbc78d6a4487fda47c608', '7c00ff0338fcbc78d6a4487fda47c608', False), ], indirect=['snapshot_of_hash'], ) def test_is_hash_updated( db: scoped_session, snapshot_service: SnapshotService, content_status: slz.ContentStatus, test_hash: Optional[str], snapshot_of_hash: slz.Snapshot, expected_result: bool, ): assert snapshot_service.is_hash_updated(test_hash, content_status) == expected_result @pytest.mark.integration def test_persist_snapshot( db: Connection, content_status: slz.ContentStatus, snapshot_service: SnapshotService, ): # Make sure that initially no snapshot exist. assert db.session.query(slz.Snapshot).count() == 0 content_status_id: int = content_status.content_status_id # Simply re-use already stored value. file_name = content_status.content_name # Just calculated hash. generated_hash = '7c00ff0338fcbc78d6a4487fda47c608' snapshot_service.persist_snapshot( file_name=file_name, file_hash=generated_hash, content_status=content_status, ) assert db.session.query(slz.Snapshot).filter( slz.Snapshot.file_name == file_name, slz.Snapshot.hash == generated_hash, slz.Snapshot.content_status_id == content_status_id ).one_or_none() is not None