|
| 1 | +from labthings.server.spec import utilities |
| 2 | +import json |
| 3 | +from apispec import APISpec |
| 4 | +from apispec.ext.marshmallow import MarshmallowPlugin |
| 5 | +from marshmallow import fields |
| 6 | +import pytest |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def view_class(): |
| 11 | + class ViewClass: |
| 12 | + def get(self): |
| 13 | + pass |
| 14 | + |
| 15 | + def post(self): |
| 16 | + pass |
| 17 | + |
| 18 | + def put(self): |
| 19 | + pass |
| 20 | + |
| 21 | + def delete(self): |
| 22 | + pass |
| 23 | + |
| 24 | + return ViewClass |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture |
| 28 | +def spec(): |
| 29 | + return APISpec( |
| 30 | + title="Python-LabThings PyTest", |
| 31 | + version="1.0.0", |
| 32 | + openapi_version="3.0.2", |
| 33 | + plugins=[MarshmallowPlugin()], |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +def test_initial_update_spec(view_class): |
| 38 | + initial_spec = {"key": "value"} |
| 39 | + utilities.update_spec(view_class, initial_spec) |
| 40 | + |
| 41 | + assert view_class.__apispec__ == initial_spec |
| 42 | + |
| 43 | + |
| 44 | +def test_update_spec(view_class): |
| 45 | + initial_spec = {"key": {"subkey": "value"}} |
| 46 | + utilities.update_spec(view_class, initial_spec) |
| 47 | + |
| 48 | + new_spec = {"key": {"new_subkey": "new_value"}} |
| 49 | + utilities.update_spec(view_class, new_spec) |
| 50 | + |
| 51 | + assert view_class.__apispec__ == { |
| 52 | + "key": {"subkey": "value", "new_subkey": "new_value"} |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | +def test_get_spec(view_class): |
| 57 | + assert utilities.get_spec(None) == {} |
| 58 | + assert utilities.get_spec(view_class) == {} |
| 59 | + |
| 60 | + initial_spec = {"key": {"subkey": "value"}} |
| 61 | + view_class.__apispec__ = initial_spec |
| 62 | + |
| 63 | + assert utilities.get_spec(view_class) == initial_spec |
| 64 | + |
| 65 | + |
| 66 | +def test_get_topmost_spec_attr(view_class): |
| 67 | + assert not utilities.get_topmost_spec_attr(view_class, "key") |
| 68 | + |
| 69 | + # Root value missing, fall back to GET |
| 70 | + view_class.get.__apispec__ = {"key": "get_value"} |
| 71 | + assert utilities.get_topmost_spec_attr(view_class, "key") == "get_value" |
| 72 | + |
| 73 | + # Root value present, return root value |
| 74 | + view_class.__apispec__ = {"key": "class_value"} |
| 75 | + assert utilities.get_topmost_spec_attr(view_class, "key") == "class_value" |
| 76 | + |
| 77 | + |
| 78 | +def test_convert_schema_none(spec): |
| 79 | + assert not utilities.convert_schema(None, spec) |
| 80 | + |
| 81 | + |
| 82 | +def test_convert_schema_schema(spec): |
| 83 | + from marshmallow import Schema |
| 84 | + |
| 85 | + schema = Schema() |
| 86 | + assert utilities.convert_schema(schema, spec) is schema |
| 87 | + |
| 88 | + |
| 89 | +def test_convert_schema_map(spec): |
| 90 | + schema = {"integer": fields.Int()} |
| 91 | + assert utilities.convert_schema(schema, spec) == { |
| 92 | + "type": "object", |
| 93 | + "properties": {"integer": {"type": "integer", "format": "int32"}}, |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | +def test_convert_schema_field(spec): |
| 98 | + schema = fields.Int() |
| 99 | + assert utilities.convert_schema(schema, spec) == { |
| 100 | + "type": "integer", |
| 101 | + "format": "int32", |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | +def test_convert_schema_invalid(spec): |
| 106 | + schema = object() |
| 107 | + |
| 108 | + with pytest.raises(TypeError): |
| 109 | + utilities.convert_schema(schema, spec) |
| 110 | + |
| 111 | + |
| 112 | +def test_map_to_schema_nested(spec): |
| 113 | + schema = {"submap": {"integer": fields.Int()}} |
| 114 | + |
| 115 | + assert utilities.map_to_properties(schema, spec) == { |
| 116 | + "type": "object", |
| 117 | + "properties": { |
| 118 | + "submap": { |
| 119 | + "type": "object", |
| 120 | + "properties": {"integer": {"type": "integer", "format": "int32"}}, |
| 121 | + } |
| 122 | + }, |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | +def test_map_to_schema_json(spec): |
| 127 | + schema = {"key": "value"} |
| 128 | + |
| 129 | + assert utilities.map_to_properties(schema, spec) == { |
| 130 | + "type": "object", |
| 131 | + "properties": {"key": "value"}, |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | +def test_schema_to_json(spec): |
| 136 | + from marshmallow import Schema |
| 137 | + |
| 138 | + UserSchema = Schema.from_dict({"name": fields.Str(), "email": fields.Email()}) |
| 139 | + |
| 140 | + assert utilities.schema_to_json(UserSchema(), spec) == { |
| 141 | + "type": "object", |
| 142 | + "properties": { |
| 143 | + "name": {"type": "string"}, |
| 144 | + "email": {"type": "string", "format": "email"}, |
| 145 | + }, |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | +def test_schema_to_json_json_in(spec): |
| 150 | + from marshmallow import Schema |
| 151 | + |
| 152 | + input_dict = { |
| 153 | + "type": "object", |
| 154 | + "properties": { |
| 155 | + "name": {"type": "string"}, |
| 156 | + "email": {"type": "string", "format": "email"}, |
| 157 | + }, |
| 158 | + } |
| 159 | + |
| 160 | + assert utilities.schema_to_json(input_dict, spec) == input_dict |
| 161 | + |
| 162 | + |
| 163 | +def test_recursive_expand_refs(spec): |
| 164 | + from marshmallow import Schema |
| 165 | + |
| 166 | + UserSchema = Schema.from_dict({"name": fields.Str(), "email": fields.Email()}) |
| 167 | + TestParentSchema = Schema.from_dict({"author": fields.Nested(UserSchema)}) |
| 168 | + |
| 169 | + assert utilities.schema_to_json(TestParentSchema(), spec) == { |
| 170 | + "type": "object", |
| 171 | + "properties": { |
| 172 | + "author": { |
| 173 | + "type": "object", |
| 174 | + "properties": { |
| 175 | + "email": {"type": "string", "format": "email"}, |
| 176 | + "name": {"type": "string"}, |
| 177 | + }, |
| 178 | + } |
| 179 | + }, |
| 180 | + } |
| 181 | + |
| 182 | + |
| 183 | +def test_recursive_expand_refs_schema_in(spec): |
| 184 | + from marshmallow import Schema |
| 185 | + |
| 186 | + UserSchema = Schema.from_dict({"name": fields.Str(), "email": fields.Email()}) |
| 187 | + |
| 188 | + user_schema_instance = UserSchema() |
| 189 | + assert ( |
| 190 | + utilities.recursive_expand_refs(user_schema_instance, spec) |
| 191 | + == user_schema_instance |
| 192 | + ) |
| 193 | + |
| 194 | + |
| 195 | +### TODO: Test expand_refs |
0 commit comments