Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
zain-sohail committed Jan 12, 2025
1 parent d66d34d commit 9a7a8b0
Showing 1 changed file with 49 additions and 7 deletions.
56 changes: 49 additions & 7 deletions tests/loader/flash/test_flash_metadata.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Tests for FlashLoader metadata functionality"""
from __future__ import annotations

from pathlib import Path

import pytest

from sed.loader.flash.metadata import MetadataRetriever
Expand All @@ -13,22 +15,63 @@ def mock_requests(requests_mock) -> None:
requests_mock.get(dataset_url, json={"fake": "data"}, status_code=200)


# Test cases for MetadataRetriever
def test_get_metadata(mock_requests: None) -> None: # noqa: ARG001
@pytest.fixture
def mock_env_token(monkeypatch, tmp_path) -> None:
# Create a temporary .env file
env_path = tmp_path / ".env"
env_path.write_text("SCICAT_TOKEN=env_test_token")
monkeypatch.setattr(Path, "home", lambda: tmp_path)


def test_get_metadata_with_explicit_token(mock_requests: None) -> None: # noqa: ARG001
metadata_config = {
"archiver_url": "https://example.com",
}
retriever = MetadataRetriever(metadata_config, token="explicit_test_token")
metadata = retriever.get_metadata("11013410", ["43878"])
assert isinstance(metadata, dict)
assert metadata == {"fake": "data"}


def test_get_metadata_with_config_token(mock_requests: None) -> None: # noqa: ARG001
metadata_config = {
"archiver_url": "https://example.com",
"token": "config_test_token",
}
retriever = MetadataRetriever(metadata_config)
metadata = retriever.get_metadata("11013410", ["43878"])
assert isinstance(metadata, dict)
assert metadata == {"fake": "data"}


def test_get_metadata_with_env_token(mock_requests: None, mock_env_token: None) -> None: # noqa: ARG001
metadata_config = {
"archiver_url": "https://example.com",
"token": "fake_token",
}
retriever = MetadataRetriever(metadata_config)
metadata = retriever.get_metadata("11013410", ["43878"])
assert isinstance(metadata, dict)
assert metadata == {"fake": "data"}


def test_get_metadata_no_token() -> None:
metadata_config = {
"archiver_url": "https://example.com",
}
with pytest.raises(ValueError, match="Token is required for metadata collection"):
MetadataRetriever(metadata_config)


def test_get_metadata_no_url() -> None:
metadata_config: dict = {}
with pytest.raises(ValueError, match="No URL provided for fetching metadata"):
MetadataRetriever(metadata_config, token="test_token")


def test_get_metadata_with_existing_metadata(mock_requests: None) -> None: # noqa: ARG001
metadata_config = {
"archiver_url": "https://example.com",
"token": "fake_token",
"token": "test_token",
}
retriever = MetadataRetriever(metadata_config)
existing_metadata = {"existing": "metadata"}
Expand All @@ -40,7 +83,7 @@ def test_get_metadata_with_existing_metadata(mock_requests: None) -> None: # no
def test_get_metadata_per_run(mock_requests: None) -> None: # noqa: ARG001
metadata_config = {
"archiver_url": "https://example.com",
"token": "fake_token",
"token": "test_token",
}
retriever = MetadataRetriever(metadata_config)
metadata = retriever._get_metadata_per_run("11013410/43878")
Expand All @@ -51,10 +94,9 @@ def test_get_metadata_per_run(mock_requests: None) -> None: # noqa: ARG001
def test_create_dataset_url_by_PID() -> None:
metadata_config = {
"archiver_url": "https://example.com",
"token": "fake_token",
"token": "test_token",
}
retriever = MetadataRetriever(metadata_config)
# Assuming the dataset follows the new format
pid = "11013410/43878"
url = retriever._create_new_dataset_url(pid)
expected_url = "https://example.com/Datasets/11013410%2F43878"
Expand Down

0 comments on commit 9a7a8b0

Please sign in to comment.