From 6fae6dae058bdc234f65d31919f148502df06a67 Mon Sep 17 00:00:00 2001 From: Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com> Date: Tue, 12 Sep 2023 21:31:50 +0200 Subject: [PATCH] test_fetcher abstraction --- .../openbb_provider/abstract/fetcher.py | 58 +++++++++++++++++-- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/openbb_sdk/sdk/provider/openbb_provider/abstract/fetcher.py b/openbb_sdk/sdk/provider/openbb_provider/abstract/fetcher.py index d045cd57d19..7f2052af8ea 100644 --- a/openbb_sdk/sdk/provider/openbb_provider/abstract/fetcher.py +++ b/openbb_sdk/sdk/provider/openbb_provider/abstract/fetcher.py @@ -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], + )