|
| 1 | +from email.contentmanager import ContentManager |
| 2 | +from unittest.mock import MagicMock, Mock |
| 3 | + |
| 4 | +import pytest |
| 5 | +import requests |
| 6 | +import responses |
| 7 | + |
| 8 | +from posit.connect.context import Context, requires |
| 9 | +from posit.connect.urls import Url |
| 10 | + |
| 11 | + |
| 12 | +class TestRequires: |
| 13 | + def test_version_unsupported(self): |
| 14 | + class Stub(ContentManager): |
| 15 | + def __init__(self, ctx): |
| 16 | + self.ctx = ctx |
| 17 | + |
| 18 | + @requires("1.0.0") |
| 19 | + def fail(self): |
| 20 | + pass |
| 21 | + |
| 22 | + ctx = MagicMock() |
| 23 | + ctx.version = "0.0.0" |
| 24 | + instance = Stub(ctx) |
| 25 | + |
| 26 | + with pytest.raises(RuntimeError): |
| 27 | + instance.fail() |
| 28 | + |
| 29 | + def test_version_supported(self): |
| 30 | + class Stub(ContentManager): |
| 31 | + def __init__(self, ctx): |
| 32 | + self.ctx = ctx |
| 33 | + |
| 34 | + @requires("1.0.0") |
| 35 | + def success(self): |
| 36 | + pass |
| 37 | + |
| 38 | + ctx = MagicMock() |
| 39 | + ctx.version = "1.0.0" |
| 40 | + instance = Stub(ctx) |
| 41 | + |
| 42 | + instance.success() |
| 43 | + |
| 44 | + def test_version_missing(self): |
| 45 | + class Stub(ContentManager): |
| 46 | + def __init__(self, ctx): |
| 47 | + self.ctx = ctx |
| 48 | + |
| 49 | + @requires("1.0.0") |
| 50 | + def success(self): |
| 51 | + pass |
| 52 | + |
| 53 | + ctx = MagicMock() |
| 54 | + ctx.version = None |
| 55 | + instance = Stub(ctx) |
| 56 | + |
| 57 | + instance.success() |
| 58 | + |
| 59 | + |
| 60 | +class TestContextVersion: |
| 61 | + @responses.activate |
| 62 | + def test_unknown(self): |
| 63 | + responses.get( |
| 64 | + f"http://connect.example/__api__/server_settings", |
| 65 | + json={}, |
| 66 | + ) |
| 67 | + |
| 68 | + session = requests.Session() |
| 69 | + url = Url("http://connect.example") |
| 70 | + ctx = Context(session, url) |
| 71 | + |
| 72 | + assert ctx.version is None |
| 73 | + |
| 74 | + @responses.activate |
| 75 | + def test_known(self): |
| 76 | + responses.get( |
| 77 | + f"http://connect.example/__api__/server_settings", |
| 78 | + json={"version": "2024.09.24"}, |
| 79 | + ) |
| 80 | + |
| 81 | + session = requests.Session() |
| 82 | + url = Url("http://connect.example") |
| 83 | + ctx = Context(session, url) |
| 84 | + |
| 85 | + assert ctx.version == "2024.09.24" |
| 86 | + |
| 87 | + def test_setter(self): |
| 88 | + ctx = Context(Mock(), Mock()) |
| 89 | + ctx.version = "2024.09.24" |
| 90 | + assert ctx.version == "2024.09.24" |
0 commit comments