|
| 1 | +# coding: spec |
| 2 | + |
| 3 | +import contextvars_for_test as ctxvars |
| 4 | +import pytest |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture(scope="module", autouse=True) |
| 8 | +def d_set_conftest_cm_module_autouse(): |
| 9 | + token = ctxvars.d.set("d_set_conftest_fixture_test") |
| 10 | + try: |
| 11 | + yield |
| 12 | + finally: |
| 13 | + ctxvars.d.reset(token) |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture(scope="module", autouse=True) |
| 17 | +async def e_set_conftest_cm_test(): |
| 18 | + assert ctxvars.f.get(ctxvars.Empty) is ctxvars.Empty |
| 19 | + ctxvars.e.set("e_set_conftest_cm_module") |
| 20 | + yield |
| 21 | + assert ctxvars.f.get() == "f_set_conftest_cm_module" |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(scope="module", autouse=True) |
| 25 | +async def f_set_conftest_cm_module(e_set_conftest_cm_test): |
| 26 | + assert ctxvars.e.get() == "e_set_conftest_cm_module" |
| 27 | + ctxvars.f.set("f_set_conftest_cm_module") |
| 28 | + yield |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.order(1) |
| 32 | +async it "gets session modified vars": |
| 33 | + assert ctxvars.a.get() == "a_set_conftest_fixture_session_autouse" |
| 34 | + assert ctxvars.b.get() == "b_set_conftest_cm_session_autouse" |
| 35 | + assert ctxvars.d.get() == "d_set_conftest_fixture_test" |
| 36 | + assert ctxvars.e.get() == "e_set_conftest_cm_module" |
| 37 | + assert ctxvars.f.get() == "f_set_conftest_cm_module" |
| 38 | + ctxvars.assertVarsEmpty(excluding=("a", "b", "d", "e", "f")) |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.order(2) |
| 42 | +async it "can use a fixture to change the var", c_set_conftest_fixture_test: |
| 43 | + assert ctxvars.a.get() == "a_set_conftest_fixture_session_autouse" |
| 44 | + assert ctxvars.b.get() == "b_set_conftest_cm_session_autouse" |
| 45 | + assert ctxvars.d.get() == "d_set_conftest_fixture_test" |
| 46 | + assert ctxvars.e.get() == "e_set_conftest_cm_module" |
| 47 | + assert ctxvars.f.get() == "f_set_conftest_cm_module" |
| 48 | + |
| 49 | + assert ctxvars.c.get() == "c_set_conftest_fixture_test" |
| 50 | + ctxvars.assertVarsEmpty(excluding=("a", "b", "c", "d", "e", "f")) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.order(3) |
| 54 | +async it "does not reset contextvars for you": |
| 55 | + """ |
| 56 | + It's too hard to know when a contextvar should be reset. It should |
| 57 | + be up to whatever sets the contextvar to know when it should be unset |
| 58 | + """ |
| 59 | + assert ctxvars.a.get() == "a_set_conftest_fixture_session_autouse" |
| 60 | + assert ctxvars.b.get() == "b_set_conftest_cm_session_autouse" |
| 61 | + assert ctxvars.d.get() == "d_set_conftest_fixture_test" |
| 62 | + assert ctxvars.e.get() == "e_set_conftest_cm_module" |
| 63 | + assert ctxvars.f.get() == "f_set_conftest_cm_module" |
| 64 | + |
| 65 | + assert ctxvars.c.get() == "c_set_conftest_fixture_test" |
| 66 | + ctxvars.assertVarsEmpty(excluding=("a", "b", "c", "d", "e", "f")) |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.order(4) |
| 70 | +async it "works in context manager fixtures", c_set_conftest_cm_test: |
| 71 | + """ |
| 72 | + It's too hard to know when a contextvar should be reset. It should |
| 73 | + be up to whatever sets the contextvar to know when it should be unset |
| 74 | + """ |
| 75 | + assert ctxvars.a.get() == "a_set_conftest_fixture_session_autouse" |
| 76 | + assert ctxvars.b.get() == "b_set_conftest_cm_session_autouse" |
| 77 | + assert ctxvars.d.get() == "d_set_conftest_fixture_test" |
| 78 | + assert ctxvars.e.get() == "e_set_conftest_cm_module" |
| 79 | + assert ctxvars.f.get() == "f_set_conftest_cm_module" |
| 80 | + |
| 81 | + assert ctxvars.c.get() == "c_set_conftest_cm_test" |
| 82 | + ctxvars.assertVarsEmpty(excluding=("a", "b", "c", "d", "e", "f")) |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.order(5) |
| 86 | +it "resets the contextvar successfully when cm attempts that": |
| 87 | + """ |
| 88 | + It's too hard to know when a contextvar should be reset. It should |
| 89 | + be up to whatever sets the contextvar to know when it should be unset |
| 90 | + """ |
| 91 | + assert ctxvars.a.get() == "a_set_conftest_fixture_session_autouse" |
| 92 | + assert ctxvars.b.get() == "b_set_conftest_cm_session_autouse" |
| 93 | + assert ctxvars.d.get() == "d_set_conftest_fixture_test" |
| 94 | + assert ctxvars.e.get() == "e_set_conftest_cm_module" |
| 95 | + assert ctxvars.f.get() == "f_set_conftest_cm_module" |
| 96 | + |
| 97 | + assert ctxvars.c.get() == "c_set_conftest_fixture_test" |
| 98 | + ctxvars.assertVarsEmpty(excluding=("a", "b", "c", "d", "e", "f")) |
0 commit comments