|
| 1 | +"""Module provides context classes for testing purposes in the CMEM plugin environment. |
| 2 | +
|
| 3 | +Classes included in this module: |
| 4 | +- TestUserContext: testing user context with token management |
| 5 | +- TestPluginContext: testing plugin context |
| 6 | +- TestTaskContext: testing task context |
| 7 | +- TestExecutionContext: testing execution context with task and user linkage |
| 8 | +- TestSystemContext: testing system context with encryption/decryption placeholders |
| 9 | +
|
| 10 | +These classes are intended for use in unit tests and other testing scenarios where real |
| 11 | +context objects are unavailable or unnecessary. |
| 12 | +""" |
| 13 | + |
| 14 | +from typing import ClassVar |
| 15 | + |
| 16 | +from cmem.cmempy.api import get_token |
| 17 | +from cmem.cmempy.config import get_oauth_default_credentials |
| 18 | + |
| 19 | +from cmem_plugin_base.dataintegration.context import ( |
| 20 | + ExecutionContext, |
| 21 | + PluginContext, |
| 22 | + ReportContext, |
| 23 | + SystemContext, |
| 24 | + TaskContext, |
| 25 | + UserContext, |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +class TestUserContext(UserContext): |
| 30 | + """Testing user context""" |
| 31 | + |
| 32 | + __test__ = False |
| 33 | + default_credential: ClassVar[dict] = {} |
| 34 | + |
| 35 | + def __init__(self): |
| 36 | + if not TestUserContext.default_credential: |
| 37 | + TestUserContext.default_credential = get_oauth_default_credentials() |
| 38 | + self.access_token = get_token(_oauth_credentials=TestUserContext.default_credential)[ |
| 39 | + "access_token" |
| 40 | + ] |
| 41 | + |
| 42 | + def token(self) -> str: |
| 43 | + """Get an access token""" |
| 44 | + return f"{self.access_token}" |
| 45 | + |
| 46 | + |
| 47 | +class TestPluginContext(PluginContext): |
| 48 | + """Testing plugin context""" |
| 49 | + |
| 50 | + __test__ = False |
| 51 | + |
| 52 | + def __init__(self, project_id: str = "TestProject"): |
| 53 | + self.project_id = project_id |
| 54 | + self.user = TestUserContext() |
| 55 | + |
| 56 | + |
| 57 | +class TestTaskContext(TaskContext): |
| 58 | + """Testing task context""" |
| 59 | + |
| 60 | + __test__ = False |
| 61 | + |
| 62 | + def __init__(self, project_id: str = "TestProject", task_id: str = "TestTask"): |
| 63 | + self._project_id = project_id |
| 64 | + self._task_id = task_id |
| 65 | + |
| 66 | + def project_id(self) -> str: |
| 67 | + """Get the project ID.""" |
| 68 | + return self._project_id |
| 69 | + |
| 70 | + def task_id(self) -> str: |
| 71 | + """Get the task ID.""" |
| 72 | + return self._task_id |
| 73 | + |
| 74 | + |
| 75 | +class TestExecutionContext(ExecutionContext): |
| 76 | + """Testing execution context""" |
| 77 | + |
| 78 | + __test__ = False |
| 79 | + |
| 80 | + def __init__(self, project_id: str = "TestProject", task_id: str = "TestTask"): |
| 81 | + self.report = ReportContext() |
| 82 | + self.task = TestTaskContext(project_id=project_id, task_id=task_id) |
| 83 | + self.user = TestUserContext() |
| 84 | + |
| 85 | + |
| 86 | +class TestSystemContext(SystemContext): |
| 87 | + """Testing system context""" |
| 88 | + |
| 89 | + __test__ = False |
| 90 | + |
| 91 | + def __init__(self) -> None: |
| 92 | + self._version: str = "1.0.0" |
| 93 | + |
| 94 | + def di_version(self) -> str: |
| 95 | + """Get data integration version.""" |
| 96 | + return self._version |
| 97 | + |
| 98 | + def encrypt(self, value: str) -> str: |
| 99 | + """Encrypt a value.""" |
| 100 | + return value |
| 101 | + |
| 102 | + def decrypt(self, value: str) -> str: |
| 103 | + """Decrypt a value.""" |
| 104 | + return value |
0 commit comments