|
9 | 9 | from typing import Any
|
10 | 10 |
|
11 | 11 | import pytest
|
12 |
| -from common_library.errors_classes import OsparcErrorMixin |
| 12 | +from common_library.errors_classes import ( |
| 13 | + ForbiddenError, |
| 14 | + NotFoundError, |
| 15 | + OsparcErrorMixin, |
| 16 | + make_resource_error, |
| 17 | +) |
13 | 18 |
|
14 | 19 |
|
15 | 20 | def test_get_full_class_name():
|
16 |
| - class A(OsparcErrorMixin): |
17 |
| - ... |
| 21 | + class A(OsparcErrorMixin): ... |
18 | 22 |
|
19 |
| - class B1(A): |
20 |
| - ... |
| 23 | + class B1(A): ... |
21 | 24 |
|
22 |
| - class B2(A): |
23 |
| - ... |
| 25 | + class B2(A): ... |
24 | 26 |
|
25 |
| - class C(B2): |
26 |
| - ... |
| 27 | + class C(B2): ... |
27 | 28 |
|
28 |
| - class B12(B1, ValueError): |
29 |
| - ... |
| 29 | + class B12(B1, ValueError): ... |
30 | 30 |
|
31 | 31 | assert B1._get_full_class_name() == "A.B1"
|
32 | 32 | assert C._get_full_class_name() == "A.B2.C"
|
@@ -159,3 +159,47 @@ class MyError(OsparcErrorMixin, ValueError):
|
159 | 159 | "message": "42 and 'missing=?'",
|
160 | 160 | "value": 42,
|
161 | 161 | }
|
| 162 | + |
| 163 | + |
| 164 | +def test_resource_error_factory(): |
| 165 | + ProjectNotFoundError = make_resource_error("project", NotFoundError) |
| 166 | + |
| 167 | + error_1 = ProjectNotFoundError(resource_id="abc123") |
| 168 | + assert "resource_id" in error_1.error_context() |
| 169 | + assert error_1.resource_id in error_1.message # type: ignore |
| 170 | + |
| 171 | + |
| 172 | +def test_resource_error_factory_auto_detect_resource_id(): |
| 173 | + ProjectForbiddenError = make_resource_error("project", ForbiddenError) |
| 174 | + error_2 = ProjectForbiddenError(project_id="abc123", other_id="foo") |
| 175 | + assert ( |
| 176 | + error_2.resource_id == error_2.project_id # type: ignore |
| 177 | + ), "auto-detects project ids as resourceid" |
| 178 | + assert error_2.other_id # type: ignore |
| 179 | + assert error_2.code == "BaseOsparcError.ForbiddenError.ProjectForbiddenError" |
| 180 | + |
| 181 | + assert error_2.error_context() == { |
| 182 | + "project_id": "abc123", |
| 183 | + "other_id": "foo", |
| 184 | + "resource": "project", |
| 185 | + "resource_id": "abc123", |
| 186 | + "message": "Access to project is forbidden: id='abc123'", |
| 187 | + "code": "BaseOsparcError.ForbiddenError.ProjectForbiddenError", |
| 188 | + } |
| 189 | + |
| 190 | + |
| 191 | +def test_resource_error_factory_different_base_exception(): |
| 192 | + |
| 193 | + class MyServiceError(Exception): ... |
| 194 | + |
| 195 | + OtherProjectForbiddenError = make_resource_error( |
| 196 | + "other_project", ForbiddenError, MyServiceError |
| 197 | + ) |
| 198 | + |
| 199 | + assert issubclass(OtherProjectForbiddenError, MyServiceError) |
| 200 | + |
| 201 | + error_3 = OtherProjectForbiddenError(project_id="abc123") |
| 202 | + assert ( |
| 203 | + error_3.code |
| 204 | + == "MyServiceError.BaseOsparcError.ForbiddenError.OtherProjectForbiddenError" |
| 205 | + ) |
0 commit comments