|
| 1 | +import random |
| 2 | +import threading |
| 3 | +import time |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from taskbadger import create_task, init |
| 8 | +from taskbadger.mug import GLOBAL_MUG, Badger |
| 9 | +from tests.test_sdk_primatives import _json_task_response, _verify_task |
| 10 | + |
| 11 | + |
| 12 | +def test_scope_singleton(): |
| 13 | + assert Badger.current == GLOBAL_MUG |
| 14 | + scope = Badger.current.scope() |
| 15 | + assert scope.context == {} |
| 16 | + assert scope.stack == [] |
| 17 | + assert scope == Badger.current.scope() |
| 18 | + |
| 19 | + |
| 20 | +def test_scope_context(): |
| 21 | + scope = Badger.current.scope() |
| 22 | + assert scope.context == {} |
| 23 | + assert scope.stack == [] |
| 24 | + with scope: |
| 25 | + assert scope.stack == [{}] |
| 26 | + scope.context["foo"] = "bar" |
| 27 | + with scope: |
| 28 | + assert scope.stack == [{}, {"foo": "bar"}] |
| 29 | + assert scope.context == {"foo": "bar"} |
| 30 | + scope.context["bar"] = "bazz" |
| 31 | + with scope: |
| 32 | + assert scope.context == {"foo": "bar", "bar": "bazz"} |
| 33 | + scope.context.clear() |
| 34 | + assert scope.context == {"foo": "bar"} |
| 35 | + assert scope.stack == [{}] |
| 36 | + assert scope.context == {} |
| 37 | + assert scope.stack == [] |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture(autouse=True) |
| 41 | +def init_skd(): |
| 42 | + init("org", "project", "token") |
| 43 | + |
| 44 | + |
| 45 | +def test_create_task_with_scope(httpx_mock): |
| 46 | + with Badger.current.scope() as scope: |
| 47 | + scope["foo"] = "bar" |
| 48 | + scope["bar"] = "bazz" |
| 49 | + httpx_mock.add_response( |
| 50 | + url="https://taskbadger.net/api/org/project/tasks/", |
| 51 | + method="POST", |
| 52 | + match_headers={"Authorization": "Bearer token"}, |
| 53 | + match_content=b'{"name": "name", "status": "pending", "data": {"foo": "bar", "bar": "buzzer"}}', |
| 54 | + json=_json_task_response(), |
| 55 | + status_code=201, |
| 56 | + ) |
| 57 | + task = create_task("name", data={"bar": "buzzer"}) |
| 58 | + _verify_task(task) |
0 commit comments