mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-30 14:02:06 +00:00
test_fetcher abstraction
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
"""Abstract class for the fetcher."""
|
||||
# ruff: noqa: S101
|
||||
# pylint: disable=E1101
|
||||
|
||||
|
||||
from typing import Any, Dict, Generic, Optional, TypeVar, get_args, get_origin
|
||||
@@ -77,8 +79,54 @@ class Fetcher(Generic[Q, R]):
|
||||
return data
|
||||
|
||||
@classmethod
|
||||
def test_fetcher(cls, params: Dict[str, Any], credentials: Dict[str, str]) -> R:
|
||||
"""Test the fetcher."""
|
||||
query = cls.transform_query(params=params)
|
||||
data = cls.extract_data(query=query, credentials=credentials)
|
||||
return cls.transform_data(data=data)
|
||||
def test_fetcher(
|
||||
cls,
|
||||
test_params: Dict[str, Any],
|
||||
credentials: Optional[Dict[str, str]] = None,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
"""Test the fetcher.
|
||||
|
||||
This method will test each stage of the fetcher TET (Transform, Extract, Transform).
|
||||
|
||||
Args
|
||||
----
|
||||
test_params: The params to test the fetcher with.
|
||||
credentials: The credentials to test the fetcher with.
|
||||
|
||||
Raises
|
||||
------
|
||||
AssertionError: If any of the tests fail.
|
||||
"""
|
||||
query = cls.transform_query(params=test_params)
|
||||
data = cls.extract_data(query=query, credentials=credentials, **kwargs)
|
||||
transformed_data = cls.transform_data(data=data)
|
||||
|
||||
# Query Assertions
|
||||
assert query
|
||||
assert issubclass(type(query), cls.query_params_type)
|
||||
assert all([getattr(query, key) == value for key, value in test_params.items()])
|
||||
|
||||
# Data Assertions
|
||||
assert data
|
||||
assert all([field in data[0] for field in cls.data_type.__fields__])
|
||||
assert len(data) > 0
|
||||
# This makes sure that the data is not transformed yet so that the
|
||||
# pipeline is implemented correctly. We can remove this assertion if we
|
||||
# want to be less strict.
|
||||
assert issubclass(type(data[0]), cls.data_type) is False
|
||||
|
||||
# Transformed Data Assertions
|
||||
assert transformed_data
|
||||
assert all(
|
||||
[
|
||||
field in transformed_data[0].__dict__ # type: ignore
|
||||
for field in cls.return_type.__args__[0].__fields__
|
||||
]
|
||||
)
|
||||
assert len(transformed_data) > 0 # type: ignore
|
||||
assert issubclass(type(transformed_data[0]), cls.data_type) # type: ignore
|
||||
assert issubclass(
|
||||
type(transformed_data[0]), # type: ignore
|
||||
cls.return_type.__args__[0],
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user