|
| 1 | +import pytest |
| 2 | + |
| 3 | +from labthings.server import labthing |
| 4 | + |
| 5 | +from labthings.server.representations import LabThingsJSONEncoder |
| 6 | +from labthings.server.names import EXTENSION_NAME |
| 7 | +from labthings.server.extensions import BaseExtension |
| 8 | + |
| 9 | + |
| 10 | +def test_init_types(): |
| 11 | + types = ["org.labthings.test"] |
| 12 | + thing = labthing.LabThing(types=types) |
| 13 | + assert thing.types == types |
| 14 | + |
| 15 | + |
| 16 | +def test_init_types_invalid(): |
| 17 | + types = ["org;labthings;test"] |
| 18 | + with pytest.raises(ValueError): |
| 19 | + labthing.LabThing(types=types) |
| 20 | + |
| 21 | + |
| 22 | +def test_init_app(app): |
| 23 | + thing = labthing.LabThing() |
| 24 | + thing.init_app(app) |
| 25 | + |
| 26 | + assert app.extensions.get(EXTENSION_NAME) == thing |
| 27 | + assert app.json_encoder == LabThingsJSONEncoder |
| 28 | + |
| 29 | + |
| 30 | +def test_init_app_early_views(app, view_cls, client): |
| 31 | + thing = labthing.LabThing() |
| 32 | + thing.add_view(view_cls, "/", endpoint="index") |
| 33 | + |
| 34 | + thing.init_app(app) |
| 35 | + |
| 36 | + with client as c: |
| 37 | + assert c.get("/").status_code == 200 |
| 38 | + |
| 39 | + |
| 40 | +def test_register_extension(thing): |
| 41 | + extension = BaseExtension("org.labthings.tests.extension") |
| 42 | + thing.register_extension(extension) |
| 43 | + assert thing.extensions.get("org.labthings.tests.extension") == extension |
| 44 | + |
| 45 | + |
| 46 | +def test_register_extension_type_error(thing): |
| 47 | + extension = object() |
| 48 | + with pytest.raises(TypeError): |
| 49 | + thing.register_extension(extension) |
| 50 | + |
| 51 | + |
| 52 | +def test_add_component(thing): |
| 53 | + component = type("component", (object,), {}) |
| 54 | + |
| 55 | + thing.add_component(component, "org.labthings.tests.component") |
| 56 | + assert "org.labthings.tests.component" in thing.components |
| 57 | + |
| 58 | + |
| 59 | +def test_on_component_callback(thing): |
| 60 | + # Build extension |
| 61 | + def f(component): |
| 62 | + component.callback_called = True |
| 63 | + |
| 64 | + extension = BaseExtension("org.labthings.tests.extension") |
| 65 | + extension.on_component("org.labthings.tests.component", f) |
| 66 | + # Add extension |
| 67 | + thing.register_extension(extension) |
| 68 | + |
| 69 | + # Build component |
| 70 | + component = type("component", (object,), {"callback_called": False}) |
| 71 | + |
| 72 | + # Add component |
| 73 | + thing.add_component(component, "org.labthings.tests.component") |
| 74 | + # Check callback |
| 75 | + assert component.callback_called |
| 76 | + |
| 77 | + |
| 78 | +def test_on_component_callback_component_already_added(thing): |
| 79 | + # Build component |
| 80 | + component = type("component", (object,), {"callback_called": False}) |
| 81 | + # Add component |
| 82 | + thing.add_component(component, "org.labthings.tests.component") |
| 83 | + |
| 84 | + # Build extension |
| 85 | + def f(component): |
| 86 | + component.callback_called = True |
| 87 | + |
| 88 | + extension = BaseExtension("org.labthings.tests.extension") |
| 89 | + extension.on_component("org.labthings.tests.component", f) |
| 90 | + # Add extension |
| 91 | + thing.register_extension(extension) |
| 92 | + |
| 93 | + # Check callback |
| 94 | + assert component.callback_called |
| 95 | + |
| 96 | + |
| 97 | +def test_on_component_callback_wrong_component(thing): |
| 98 | + def f(component): |
| 99 | + component.callback_called = True |
| 100 | + |
| 101 | + extension = BaseExtension("org.labthings.tests.extension") |
| 102 | + extension.on_component("org.labthings.tests.component", f) |
| 103 | + thing.register_extension(extension) |
| 104 | + |
| 105 | + component = type("component", (object,), {"callback_called": False}) |
| 106 | + thing.add_component(component, "org.labthings.tests.wrong_component") |
| 107 | + assert not component.callback_called |
| 108 | + |
| 109 | + |
| 110 | +def test_on_register_callback(thing): |
| 111 | + # Build extension |
| 112 | + def f(extension): |
| 113 | + extension.callback_called = True |
| 114 | + |
| 115 | + extension = BaseExtension("org.labthings.tests.extension") |
| 116 | + extension.callback_called = False |
| 117 | + extension.on_register(f, args=(extension,)) |
| 118 | + # Add extension |
| 119 | + thing.register_extension(extension) |
| 120 | + |
| 121 | + # Check callback |
| 122 | + assert extension.callback_called |
| 123 | + |
| 124 | + |
| 125 | +def test_complete_url(thing): |
| 126 | + thing.url_prefix = "" |
| 127 | + assert thing._complete_url("", "") == "/" |
| 128 | + assert thing._complete_url("", "api") == "/api" |
| 129 | + assert thing._complete_url("/method", "api") == "/api/method" |
| 130 | + |
| 131 | + thing.url_prefix = "prefix" |
| 132 | + assert thing._complete_url("", "") == "/prefix" |
| 133 | + assert thing._complete_url("", "api") == "/prefix/api" |
| 134 | + assert thing._complete_url("/method", "api") == "/prefix/api/method" |
0 commit comments