From d6ad1301cbb1c31830bd7b43a5969f1b9e132562 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 19 Dec 2020 10:53:42 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=97=EF=B8=8F=20Fix=20the=20tests,=20even?= =?UTF-8?q?=20if=20i=20have=20no=20idea=20of=20how=20or=20why?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- royalnet/engineer/blueprints/blueprint.py | 4 ++-- royalnet/engineer/sentry/filter.py | 8 ++++---- royalnet/engineer/tests/test_sentry.py | 25 ++++++++++------------- royalnet/tests/test_pytest.py | 14 +++++++++++++ 4 files changed, 31 insertions(+), 20 deletions(-) create mode 100644 royalnet/tests/test_pytest.py diff --git a/royalnet/engineer/blueprints/blueprint.py b/royalnet/engineer/blueprints/blueprint.py index 70b47634..66a60f31 100644 --- a/royalnet/engineer/blueprints/blueprint.py +++ b/royalnet/engineer/blueprints/blueprint.py @@ -63,7 +63,7 @@ class Blueprint(metaclass=abc.ABCMeta): .. code-block:: def print_msg(message: Message): - message.requires(Message.text, Message.timestamp) + message.requires("text", "timestamp") print(f"{message.timestamp().isoformat()}: {message.text()}") :raises .exc.NeverAvailableError: If at least one of the fields raised a :exc:`.exc.NeverAvailableError`. @@ -75,7 +75,7 @@ class Blueprint(metaclass=abc.ABCMeta): for field in fields: try: - field(self) + self.__getattribute__(field)() except exc.NeverAvailableError as ex: exceptions.append(ex) except exc.NotAvailableError as ex: diff --git a/royalnet/engineer/sentry/filter.py b/royalnet/engineer/sentry/filter.py index 173fb363..200a20b8 100644 --- a/royalnet/engineer/sentry/filter.py +++ b/royalnet/engineer/sentry/filter.py @@ -8,7 +8,7 @@ from royalnet.royaltyping import * import functools import logging -from engineer import exc, blueprints +from .. import exc, blueprints log = logging.getLogger(__name__) @@ -155,7 +155,7 @@ class Filter: - the :class:`.blueprints.Blueprint` never has data for at least one of the fields, :exc:`.exc.NotAvailableError` is propagated upwards. - :param fields: The fields to test for. + :param fields: The fields to test for, as strings. :param propagate_not_available: If :exc:`.exc.NotAvailableError` should be propagated instead of discarding the errored object. :param propagate_never_available: If :exc:`.exc.NeverAvailableError` should be propagated @@ -166,11 +166,11 @@ class Filter: try: return obj.requires(*fields) except exc.NotAvailableError: - if not propagate_not_available: + if propagate_not_available: raise raise exc.Discard(obj, "Data is not available") except exc.NeverAvailableError: - if not propagate_never_available: + if propagate_never_available: raise raise exc.Discard(obj, "Data is never available") diff --git a/royalnet/engineer/tests/test_sentry.py b/royalnet/engineer/tests/test_sentry.py index a875cea0..47363fb4 100644 --- a/royalnet/engineer/tests/test_sentry.py +++ b/royalnet/engineer/tests/test_sentry.py @@ -2,7 +2,7 @@ import pytest import asyncio import async_timeout import re -from .. import sentry, exc, blueprints +from royalnet.engineer import sentry, exc, blueprints @pytest.fixture @@ -82,6 +82,7 @@ class TestFilter: async def test_filter(self, s: sentry.Sentry): await s.queue.put(None) await s.queue.put(None) + await s.queue.put(None) assert await s.f().filter(lambda x: x is None, "Is not None").get_single() is None @@ -147,28 +148,24 @@ class TestFilter: return 3 avmsg = AvailableMessage() - namsg = NotAvailableMessage() - nvmsg = NeverAvailableMessage() - await s.queue.put(avmsg) - await s.queue.put(namsg) - await s.queue.put(nvmsg) - await s.queue.put(namsg) - await s.queue.put(nvmsg) - - assert await s.f().requires(blueprints.Message.text).get_single() is avmsg + assert await s.f().requires("text").get_single() is avmsg + await s.queue.put(NotAvailableMessage()) with pytest.raises(exc.Discard): - await s.f().requires(blueprints.Message.text).get_single() + await s.f().requires("text").get_single() + await s.queue.put(NeverAvailableMessage()) with pytest.raises(exc.NeverAvailableError): - await s.f().requires(blueprints.Message.text).get_single() + await s.f().requires("text").get_single() + await s.queue.put(NotAvailableMessage()) with pytest.raises(exc.NotAvailableError): - await s.f().requires(blueprints.Message.text, propagate_not_available=True).get_single() + await s.f().requires("text", propagate_not_available=True).get_single() + await s.queue.put(NeverAvailableMessage()) with pytest.raises(exc.Discard): - await s.f().requires(blueprints.Message.text, propagate_never_available=False).get_single() + await s.f().requires("text", propagate_never_available=False).get_single() @pytest.mark.asyncio async def test_startswith(self, s: sentry.Sentry): diff --git a/royalnet/tests/test_pytest.py b/royalnet/tests/test_pytest.py new file mode 100644 index 00000000..0b43ce3d --- /dev/null +++ b/royalnet/tests/test_pytest.py @@ -0,0 +1,14 @@ +import asyncio +import pytest + + +async def sleep_and_raise(): + await asyncio.sleep(0.001) + raise Exception("Please except this gift!") + + +@pytest.mark.asyncio +class TestAsyncio: + async def test_exception(self): + with pytest.raises(Exception): + await sleep_and_raise()