From aabb480ff45705bd6113ada9458d26438228f439 Mon Sep 17 00:00:00 2001 From: Simon Kelly Date: Thu, 13 Feb 2025 17:32:03 +0200 Subject: [PATCH 1/2] update integration tests with tags --- integration_tests/__init__.py | 3 +++ integration_tests/test_basics.py | 2 ++ integration_tests/test_celery.py | 1 + 3 files changed, 6 insertions(+) diff --git a/integration_tests/__init__.py b/integration_tests/__init__.py index a4863a7..f0d6e12 100644 --- a/integration_tests/__init__.py +++ b/integration_tests/__init__.py @@ -32,5 +32,8 @@ def _load_config(): os.environ.get("TASKBADGER_PROJECT", ""), os.environ.get("TASKBADGER_API_KEY", ""), systems=[CelerySystemIntegration()], + tags={ + "env": "integration", + }, ) print(f"\nIntegration tests configuration:\n {badger.mug.Badger.current.settings}\n") diff --git a/integration_tests/test_basics.py b/integration_tests/test_basics.py index 3248b2a..8beec1e 100644 --- a/integration_tests/test_basics.py +++ b/integration_tests/test_basics.py @@ -9,8 +9,10 @@ def test_basics(): task = badger.create_task("test basics", data=data) task.success(100) assert task.status == StatusEnum.SUCCESS + assert task.tags == {"env": "integration"} fresh = badger.get_task(task.id) assert fresh.status == StatusEnum.SUCCESS assert fresh.value == 100 assert fresh.data == data + assert fresh.tags == {"env": "integration"} diff --git a/integration_tests/test_celery.py b/integration_tests/test_celery.py index d24cec0..4bd24d8 100644 --- a/integration_tests/test_celery.py +++ b/integration_tests/test_celery.py @@ -34,6 +34,7 @@ def test_celery(celery_session_app, celery_session_worker): assert tb_task.status == StatusEnum.SUCCESS assert tb_task.value == 100 assert tb_task.data == {"result": a + b} + assert tb_task.tags == {"env": "integration"} def test_celery_auto_track(celery_session_app, celery_session_worker): From b3d9fcba761e173e0c6902ef77f0b37ec698c098 Mon Sep 17 00:00:00 2001 From: Simon Kelly Date: Thu, 13 Feb 2025 18:38:18 +0200 Subject: [PATCH 2/2] deepcopy to isolate changes --- taskbadger/mug.py | 15 +++++++++------ tests/{test_session.py => test_mug.py} | 11 ++++++++++- tests/test_scope.py | 10 ++++++---- 3 files changed, 25 insertions(+), 11 deletions(-) rename tests/{test_session.py => test_mug.py} (86%) diff --git a/taskbadger/mug.py b/taskbadger/mug.py index f9b7a75..1dacf76 100644 --- a/taskbadger/mug.py +++ b/taskbadger/mug.py @@ -1,6 +1,7 @@ import dataclasses from contextlib import ContextDecorator from contextvars import ContextVar +from copy import deepcopy from typing import Union from taskbadger.internal import AuthenticatedClient @@ -87,8 +88,8 @@ def __init__(self): def __enter__(self): self.stack.append((self.context, self.tags)) - self.context = self.context.copy() - self.tags = self.tags.copy() + self.context = deepcopy(self.context) + self.tags = deepcopy(self.tags) return self def __exit__(self, *args): @@ -116,17 +117,19 @@ def current(cls): class Badger(metaclass=MugMeta): def __init__(self, settings_or_mug=None): + self._session = ReentrantSession() + self._scope = Scope() + if isinstance(settings_or_mug, Badger): self.settings = settings_or_mug.settings + self._scope.context = deepcopy(settings_or_mug._scope.context) + self._scope.tags = deepcopy(settings_or_mug._scope.tags) else: self.settings = settings_or_mug - self._session = ReentrantSession() - self._scope = Scope() - def bind(self, settings, tags=None): self.settings = settings - self.scope().tags = tags or {} + self._scope.tags = tags or {} def session(self) -> ReentrantSession: return self._session diff --git a/tests/test_session.py b/tests/test_mug.py similarity index 86% rename from tests/test_session.py rename to tests/test_mug.py index 2cdcb5d..7a7287c 100644 --- a/tests/test_session.py +++ b/tests/test_mug.py @@ -9,7 +9,7 @@ @pytest.fixture(autouse=True) def _bind_settings(): - Badger.current.bind(Settings("https://taskbadger.net", "token", "org", "proj")) + Badger.current.bind(Settings("https://taskbadger.net", "token", "org", "proj"), tags={"env": "test"}) def test_session_singleton(): @@ -19,6 +19,8 @@ def test_session_singleton(): assert session.stack == [] assert session == Badger.current.session() + assert Badger.current.scope().tags == {"env": "test"} + def test_session_global(): session = Session() @@ -90,3 +92,10 @@ def run(self): with session as client: assert client is not None self.clients.append(client) + + assert Badger.current.scope().tags == {"env": "test"} + with Badger.current.scope() as scope: + scope.tag({"thread": self.name}) + assert scope.tags == {"env": "test", "thread": self.name} + + assert Badger.current.scope().tags == {"env": "test"} diff --git a/tests/test_scope.py b/tests/test_scope.py index 5950826..c73d97f 100644 --- a/tests/test_scope.py +++ b/tests/test_scope.py @@ -22,19 +22,21 @@ def test_scope_context(): with scope: assert scope.stack == [({}, {})] scope.context["foo"] = "bar" + scope.context["nit"] = [1] scope.tags["name"] = "value" with scope: - assert scope.stack == [({}, {}), ({"foo": "bar"}, {"name": "value"})] - assert scope.context == {"foo": "bar"} + assert scope.stack == [({}, {}), ({"foo": "bar", "nit": [1]}, {"name": "value"})] + assert scope.context == {"foo": "bar", "nit": [1]} assert scope.tags == {"name": "value"} scope.context["bar"] = "bazz" + scope.context["nit"].append(2) scope.tags["bar"] = "bazz" with scope: - assert scope.context == {"foo": "bar", "bar": "bazz"} + assert scope.context == {"foo": "bar", "bar": "bazz", "nit": [1, 2]} assert scope.tags == {"name": "value", "bar": "bazz"} scope.context.clear() scope.tags.clear() - assert scope.context == {"foo": "bar"} + assert scope.context == {"foo": "bar", "nit": [1]} assert scope.tags == {"name": "value"} assert scope.stack == [({}, {})] assert scope.context == {}