import io import pytest import oxrlib.errors import oxrlib.loaders from . import any_date SUCCESS_S = '"success"\n' ERROR = oxrlib.errors.LoaderNoDataError("test") class FakeLoader: def __init__(self, result): self.result = result def _respond(self, *args, **kwargs): return io.StringIO(self.result) def __getattr__(self, name): return self._respond class FakeErrorLoader(FakeLoader): def __init__(self, error): self.error = error def _respond(self, *args, **kwargs): raise self.error @pytest.fixture def lchain(): return oxrlib.loaders.LoaderChain() @pytest.fixture def good_loader(): return FakeLoader(SUCCESS_S) @pytest.fixture def error_loader(): return FakeErrorLoader(ERROR) def test_no_loaders(lchain, any_date): try: lchain.historical(any_date, 'USD') except oxrlib.errors.NoLoadersError: pass else: assert False, "expected NoLoadersError not raised" def test_one_with_success(lchain, any_date, good_loader): lchain.add_loader(good_loader) response = lchain.historical(any_date, 'USD') assert response.read(32) == SUCCESS_S def test_two_with_success(lchain, any_date, good_loader, error_loader): lchain.add_loader(error_loader) lchain.add_loader(good_loader) response = lchain.historical(any_date, 'USD') assert response.read(32) == SUCCESS_S @pytest.mark.parametrize('count', [1, 2]) def test_no_success(lchain, any_date, error_loader, count): for _ in range(count): lchain.add_loader(error_loader) try: lchain.historical(any_date, 'USD') except type(ERROR) as error: assert error is ERROR else: assert False, "{} not raised".format(type(ERROR).__name__)