from pathlib import Path from unittest import mock import pandas as pd import pytest from botocore.response import StreamingBody from pytest_mock import MockerFixture from service.utils.aws_connectors import boto3, s3_to_pandas sample_data_path = Path(__file__).parents[2].absolute() / "sample_data" @pytest.fixture def mock_s3_response_with_file(mocker: MockerFixture): def wrapped(file_path: Path): body_mock = mock.MagicMock(spec=StreamingBody) with open(file_path, "rb") as f: body_mock.read.return_value = f.read() client_mock = mock.MagicMock() client_mock.get_object.return_value = { "Body": body_mock, } mocker.patch.object(boto3, "client", return_value=client_mock) return wrapped @pytest.mark.parametrize( "sample_file_path", [ sample_data_path / "demo_merch_data_m_458.csv", sample_data_path / "demo_merch_data_m_458-latin1.csv", ], ) def test_s3_to_pandas(mock_s3_response_with_file, sample_file_path: Path): mock_s3_response_with_file(sample_file_path) df = s3_to_pandas("test", "test", file_format="csv") assert isinstance(df, pd.DataFrame) assert len(df.columns) > 1 def test_s3_to_pandas_unsupported_file_format(mock_s3_response_with_file): sample_file_path = sample_data_path / "demo_merch_data_m_458.xlsx" mock_s3_response_with_file(sample_file_path) with pytest.raises(ValueError, match="Unsupported file format: xlsx"): s3_to_pandas("test", "test", file_format="xlsx")