Skip to content

Commit cca049c

Browse files
authored
Merge pull request #19 from eccenca/feature/testingModule
Move integrated cmem-plugin-template testing code in to cmem-plugin-base
2 parents 066ffc3 + 830d3a5 commit cca049c

11 files changed

+123
-53
lines changed

.idea/cmem-plugin-base.iml

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmem_plugin_base/dataintegration/description.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def retrieve_parameters(self, plugin_class: type) -> list[PluginParameter]:
304304
# Special handling of PluginContext parameter
305305
if isinstance(param.param_type, PluginContextParameterType):
306306
param.visible = False # Should never be visible in the UI
307-
param.default_value = "" # dummy value
307+
param.default_value = "" # default value
308308

309309
if param.default_value is None and sig_param.default != _empty:
310310
param.default_value = sig_param.default

cmem_plugin_base/testing.py

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

tests/parameter_types/test_choice.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import collections
44

55
from cmem_plugin_base.dataintegration.parameter.choice import ChoiceParameterType
6-
from tests.utils import TestPluginContext
6+
from cmem_plugin_base.testing import TestPluginContext
77

88
CHOICE_LIST = collections.OrderedDict({"ONE": "First Option", "TWO": "Second Option"})
99

tests/parameter_types/test_code.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
YamlCode,
1717
)
1818
from cmem_plugin_base.dataintegration.plugins import TransformPlugin
19-
from tests.utils import TestPluginContext
19+
from cmem_plugin_base.testing import TestPluginContext
2020

2121

2222
class CodeParameterTest(unittest.TestCase):
@@ -80,7 +80,7 @@ def test_serialization(self) -> None:
8080
jinja_type = CodeParameterType[JinjaCode]("jinja2")
8181

8282
# Create a jinja code instance from a string
83-
jinja_code = jinja_type.from_string("my code", TestPluginContext(user=None))
83+
jinja_code = jinja_type.from_string("my code", TestPluginContext())
8484
assert jinja_code.code == "my code"
8585

8686
# Convert jinja code instance to a string

tests/parameter_types/test_dataset.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""graph parameter type tests"""
22

33
from cmem_plugin_base.dataintegration.parameter.dataset import DatasetParameterType
4-
from tests.utils import TestPluginContext, get_autocomplete_values, needs_cmem
4+
from cmem_plugin_base.testing import TestPluginContext
5+
from tests.utils import get_autocomplete_values, needs_cmem
56

67

78
@needs_cmem

tests/parameter_types/test_graph.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""graph parameter type tests"""
22

33
from cmem_plugin_base.dataintegration.parameter.graph import GraphParameterType
4-
from tests.utils import TestPluginContext, needs_cmem
4+
from cmem_plugin_base.testing import TestPluginContext
5+
from tests.utils import needs_cmem
56

67

78
@needs_cmem

tests/parameter_types/test_resource.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"""resource parameter type tests"""
22

33
from cmem_plugin_base.dataintegration.parameter.resource import ResourceParameterType
4+
from cmem_plugin_base.testing import TestPluginContext
45
from tests.conftest import JSONResourceFixtureDate
5-
from tests.utils import TestPluginContext, get_autocomplete_values, needs_cmem
6+
from tests.utils import get_autocomplete_values, needs_cmem
67

78

89
@needs_cmem

tests/test_types.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
EnumParameterType,
99
ParameterTypes,
1010
)
11-
from tests.utils import TestPluginContext
11+
from cmem_plugin_base.testing import TestPluginContext
1212

13-
# dummy plugin context to be used in tests
13+
# testing plugin context to be used in tests
1414
context = TestPluginContext()
1515

1616

tests/test_utils_write_to_dataset.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
from cmem_plugin_base.dataintegration.parameter.dataset import DatasetParameterType
1111
from cmem_plugin_base.dataintegration.utils import write_to_dataset
12-
from tests.utils import TestPluginContext, get_autocomplete_values, needs_cmem
12+
from cmem_plugin_base.testing import TestPluginContext
13+
from tests.utils import get_autocomplete_values, needs_cmem
1314

1415

1516
@needs_cmem

tests/utils.py

+4-41
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,16 @@
44

55
import pytest
66

7-
# check for cmem environment and skip if not present
8-
from cmem.cmempy.api import get_token
9-
from cmem.cmempy.config import get_oauth_grant_type
10-
11-
from cmem_plugin_base.dataintegration.context import PluginContext, UserContext
7+
from cmem_plugin_base.dataintegration.context import PluginContext
128
from cmem_plugin_base.dataintegration.types import ParameterType
139

1410
needs_cmem = pytest.mark.skipif(
15-
"CMEM_BASE_URI" not in os.environ, reason="Needs CMEM configuration"
11+
# check for cmem environment and skip if not present
12+
"CMEM_BASE_URI" not in os.environ,
13+
reason="Needs CMEM configuration",
1614
)
1715

1816

19-
class TestUserContext(UserContext):
20-
"""dummy user context that can be used in tests"""
21-
22-
__test__ = False
23-
24-
def __init__(self) -> None:
25-
# get access token from default service account
26-
if get_oauth_grant_type() == "prefetched_token":
27-
access_token = os.environ.get("OAUTH_ACCESS_TOKEN")
28-
else:
29-
access_token = get_token()["access_token"] # type : ignore[annotation-unchecked]
30-
self.access_token = str(access_token)
31-
32-
def token(self) -> str:
33-
"""Get access token."""
34-
return self.access_token
35-
36-
37-
class TestPluginContext(PluginContext):
38-
"""dummy plugin context that can be used in tests"""
39-
40-
__test__ = False
41-
42-
def __init__(
43-
self,
44-
project_id: str = "dummyProject",
45-
user: UserContext | None = None,
46-
):
47-
self.project_id = project_id
48-
if user is None:
49-
self.user = TestUserContext()
50-
else:
51-
self.user = user
52-
53-
5417
def get_autocomplete_values(
5518
parameter: ParameterType, query_terms: list[str], context: PluginContext
5619
) -> list[str]:

0 commit comments

Comments
 (0)