Skip to content

Commit

Permalink
DISCO-3182 Remove duplicated fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
gruberb committed Jan 29, 2025
1 parent 1972155 commit c470a03
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 131 deletions.
16 changes: 3 additions & 13 deletions tests/unit/providers/manifest/backends/test_filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@

def test_get_file(
manifest_remote_filemanager: ManifestRemoteFilemanager,
gcs_client_mock,
gcs_bucket_mock,
gcs_blob_mock,
blob_json,
) -> None:
"""Test that the get_file method returns manifest data."""
manifest_remote_filemanager.gcs_client = MagicMock()
Expand All @@ -32,8 +29,6 @@ def test_get_file(

def test_get_file_skip(
manifest_remote_filemanager: ManifestRemoteFilemanager,
gcs_client_mock,
gcs_bucket_mock,
) -> None:
"""Test that the get_file method returns the SKIP code when there's no new generation."""
manifest_remote_filemanager.gcs_client = MagicMock()
Expand All @@ -48,7 +43,6 @@ def test_get_file_skip(
def test_get_file_fail(
manifest_remote_filemanager: ManifestRemoteFilemanager,
gcs_client_mock,
gcs_bucket_mock,
) -> None:
"""Test that the get_file method returns the FAIL code on failure."""
gcs_client_mock.get_bucket.side_effect = Exception("Test error")
Expand All @@ -64,15 +58,12 @@ def test_get_file_fail_validation_error(
gcs_client_mock,
gcs_bucket_mock,
gcs_blob_mock,
blob_json,
) -> None:
"""Test that the get_file method returns the FAIL code when a validation error occurs."""
mock_blob = gcs_blob_mock(blob_json, "manifest.json")

gcs_bucket_mock.get_blob.return_value = mock_blob
gcs_bucket_mock.get_blob.return_value = gcs_blob_mock
gcs_client_mock.get_bucket.return_value = gcs_bucket_mock

mock_blob.download_as_text.return_value = '{"invalid": "data"}'
gcs_blob_mock.download_as_text.return_value = '{"invalid": "data"}'

with patch(
"merino.providers.manifest.backends.filemanager.ManifestData.model_validate",
Expand All @@ -89,10 +80,9 @@ def test_get_file_fail_json_decoder_error(
gcs_client_mock,
gcs_bucket_mock,
gcs_blob_mock,
blob_json,
) -> None:
"""Test that the get_file method returns the FAIL code when a JSON decoder occurs."""
gcs_bucket_mock.get_blob.return_value = gcs_blob_mock(blob_json, "manifest.json")
gcs_bucket_mock.get_blob.return_value = gcs_blob_mock
gcs_client_mock.get_bucket.return_value = gcs_bucket_mock

with patch(
Expand Down
11 changes: 0 additions & 11 deletions tests/unit/providers/manifest/backends/test_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
def test_fetch_manifest_data(
manifest_remote_filemanager: ManifestRemoteFilemanager,
backend: ManifestBackend,
gcs_client_mock,
gcs_bucket_mock,
gcs_blob_mock,
blob_json,
) -> None:
"""Verify that the fetch_manifest_data method returns manifest data on success"""
manifest_remote_filemanager.gcs_client = MagicMock()
Expand All @@ -38,9 +35,6 @@ def test_fetch_manifest_data(

def test_fetch_manifest_data_skip(
manifest_remote_filemanager: ManifestRemoteFilemanager,
gcs_client_mock,
gcs_bucket_mock,
gcs_blob_mock,
backend: ManifestBackend,
) -> None:
"""Verify that the fetch_manifest_data method returns SKIP code for no new blob generation"""
Expand All @@ -59,8 +53,6 @@ def test_fetch_manifest_data_skip(

def test_fetch_manifest_data_fail(
manifest_remote_filemanager: ManifestRemoteFilemanager,
gcs_client_mock,
gcs_bucket_mock,
backend: ManifestBackend,
) -> None:
"""Verify that the fetch_manifest_data method returns FAIL code when an error occurs"""
Expand Down Expand Up @@ -88,10 +80,7 @@ def test_fetch_manifest_data_fail(
async def test_fetch(
manifest_remote_filemanager: ManifestRemoteFilemanager,
backend: ManifestBackend,
gcs_client_mock,
gcs_bucket_mock,
gcs_blob_mock,
blob_json,
) -> None:
"""Verify that the fetch method returns manifest data"""
manifest_remote_filemanager.gcs_client = MagicMock()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,13 @@ def test_create_gcs_client(
gcs_client_mock,
gcs_bucket_mock,
gcs_blob_mock,
blob_json,
) -> None:
"""Test that create_gcs_client returns the GCS client."""
mocker.patch(
"merino.configs.settings.providers.top_picks.domain_data_source"
).return_value = "remote"

gcs_bucket_mock.get_blob.return_value = gcs_blob_mock(
blob_json, "20220101120555_top_picks.json"
)
gcs_bucket_mock.get_blob.return_value = gcs_blob_mock
gcs_client_mock.get_bucket.return_value = gcs_bucket_mock

mocker.patch.object(
Expand Down
35 changes: 0 additions & 35 deletions tests/unit/providers/suggest/top_picks/backends/test_top_picks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
from typing import Any

import pytest
from google.cloud.storage import Blob, Bucket, Client
from pytest import LogCaptureFixture
from pytest_mock import MockerFixture

from merino.configs import settings
from merino.providers.suggest.top_picks.backends.filemanager import GetFileResultCode
Expand Down Expand Up @@ -45,38 +43,6 @@ def fixture_top_picks(top_picks_backend_parameters: dict[str, Any]) -> TopPicksB
return TopPicksBackend(**top_picks_backend_parameters)


@pytest.fixture(name="expected_timestamp")
def fixture_expected_timestamp() -> int:
"""Return a unix timestamp for metadata mocking."""
return 16818664520924621


@pytest.fixture(name="gcs_blob_mock", autouse=True)
def fixture_gcs_blob_mock(mocker: MockerFixture, expected_timestamp: int, blob_json: str) -> Any:
"""Create a GCS Blob mock object for testing."""
mock_blob = mocker.MagicMock(spec=Blob)
mock_blob.name = "20220101120555_top_picks.json"
mock_blob.generation = expected_timestamp
mock_blob.download_as_text.return_value = blob_json
return mock_blob


@pytest.fixture(name="gcs_bucket_mock", autouse=True)
def fixture_gcs_bucket_mock(mocker: MockerFixture, gcs_blob_mock) -> Any:
"""Create a GCS Bucket mock object for testing."""
mock_bucket = mocker.MagicMock(spec=Bucket)
mock_bucket.get_blob.return_value = gcs_blob_mock
return mock_bucket


@pytest.fixture(name="gcs_client_mock", autouse=True)
def mock_gcs_client(mocker: MockerFixture, gcs_bucket_mock):
"""Return a mock GCS Client instance"""
mock_client = mocker.MagicMock(spec=Client)
mock_client.get_bucket.return_value = gcs_bucket_mock
return mock_client


def test_init_failure_no_domain_file(
top_picks_backend_parameters: dict[str, Any],
) -> None:
Expand Down Expand Up @@ -164,7 +130,6 @@ def test_maybe_build_indicies_remote(
gcs_client_mock,
caplog: LogCaptureFixture,
filter_caplog: FilterCaplogFixture,
gcs_blob_mock,
) -> None:
"""Test remote build indicies method."""
caplog.set_level(logging.INFO)
Expand Down
68 changes: 0 additions & 68 deletions tests/unit/providers/suggest/top_picks/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

"""Module for test configurations for the Top Picks provider unit test directory."""

import json
from typing import Any

import pytest
Expand Down Expand Up @@ -54,70 +53,3 @@ def fixture_top_picks_parameters() -> dict[str, Any]:
def fixture_top_picks(backend: TopPicksBackend, top_picks_parameters: dict[str, Any]) -> Provider:
"""Create Top Pick Provider for test."""
return Provider(backend=backend, **top_picks_parameters) # type: ignore [arg-type]


@pytest.fixture(name="blob_json")
def fixture_blob_json() -> str:
"""Return a JSON string for mocking."""
return json.dumps(
{
"domains": [
{
"rank": 1,
"title": "Example",
"domain": "example",
"url": "https://example.com",
"icon": "",
"categories": ["web-browser"],
"serp_categories": [0],
"similars": ["exxample", "exampple", "eexample"],
},
{
"rank": 2,
"title": "Firefox",
"domain": "firefox",
"url": "https://firefox.com",
"icon": "",
"categories": ["web-browser"],
"serp_categories": [0],
"similars": [
"firefoxx",
"foyerfox",
"fiirefox",
"firesfox",
"firefoxes",
],
},
{
"rank": 3,
"title": "Mozilla",
"domain": "mozilla",
"url": "https://mozilla.org/en-US/",
"icon": "",
"categories": ["web-browser"],
"serp_categories": [0],
"similars": ["mozzilla", "mozila"],
},
{
"rank": 4,
"title": "Abc",
"domain": "abc",
"url": "https://abc.test",
"icon": "",
"categories": ["web-browser"],
"serp_categories": [0],
"similars": ["aa", "ab", "acb", "acbc", "aecbc"],
},
{
"rank": 5,
"title": "BadDomain",
"domain": "baddomain",
"url": "https://baddomain.test",
"icon": "",
"categories": ["web-browser"],
"serp_categories": [0],
"similars": ["bad", "badd"],
},
]
}
)

0 comments on commit c470a03

Please sign in to comment.