Skip to content

Commit 8ebd273

Browse files
tarekbadrshafrittoli
authored andcommitted
add test for core
1 parent 6752638 commit 8ebd273

File tree

11 files changed

+216
-2
lines changed

11 files changed

+216
-2
lines changed

cli/cdevents/cli/cdevents_command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
class CDeventsCommand(ABC):
15-
"""Abstract base class for all Hostlog commands."""
15+
"""Abstract base class for all CDevents commands."""
1616

1717
def __init__(self, config_handler: ConfigurationHandler = None):
1818
"""Initializes base class.

cli/setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ packages = find_namespace:
3030
zip_safe = False
3131
include_package_data = True
3232
install_requires =
33+
pytest
3334
click>=8.0.4
3435
pyyaml>=6.0
3536
click-option-group

cli/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Tests for core package."""

cli/tests/test_utils.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
"""Testing for module utils."""
2+
from datetime import datetime
3+
from unittest import mock
4+
5+
import pytest
6+
7+
from cdevents.cli.utils import DictUtils, time_stamp
8+
9+
# pylint: disable=missing-function-docstring, protected-access, missing-class-docstring
10+
@pytest.mark.unit
11+
def test_merge_dicts_1():
12+
source = {"a": 66}
13+
target = {"b": 77, "c": 88}
14+
expected = {"a": 66, "b": 77, "c": 88}
15+
merge_and_test(source, target, expected)
16+
17+
18+
@pytest.mark.unit
19+
def test_merge_dicts_2():
20+
source = {"a": 66}
21+
target = {"a": 77}
22+
expected = {"a": 77}
23+
merge_and_test(source, target, expected)
24+
25+
26+
@pytest.mark.unit
27+
def test_merge_dicts_3():
28+
source = {}
29+
target = {"a": 77}
30+
expected = {"a": 77}
31+
merge_and_test(source, target, expected)
32+
33+
34+
@pytest.mark.unit
35+
def test_merge_dicts_3_1():
36+
target = {}
37+
source = {"a": 77}
38+
expected = {"a": 77}
39+
merge_and_test(source, target, expected)
40+
41+
42+
@pytest.mark.unit
43+
def test_merge_dicts_4():
44+
source = {"a": {"aa": 11, "aaa": {"aaaa": 111}}}
45+
target = {"b": {"bb": 22, "bbb": {"bbbb": 222}}}
46+
expected = {
47+
"a": {"aa": 11, "aaa": {"aaaa": 111}},
48+
"b": {"bb": 22, "bbb": {"bbbb": 222}},
49+
}
50+
merge_and_test(source, target, expected)
51+
52+
53+
@pytest.mark.unit
54+
def test_merge_dicts_5():
55+
source = {
56+
"a": {"aa": 12, "aaa": {"aaaa": 222, "bbb": 333}},
57+
"b": 4,
58+
"c": [8, 9],
59+
"d": [1],
60+
}
61+
target = {
62+
"a": {"aa": 11, "aaa": {"aaaa": 111}},
63+
"b": 5,
64+
"c": [1, 2, 3],
65+
"d": {"v": 1},
66+
}
67+
expected = {
68+
"a": {"aa": 11, "aaa": {"aaaa": 111, "bbb": 333}},
69+
"b": 5,
70+
"c": [1, 2, 3, 8, 9],
71+
"d": {"v": 1},
72+
}
73+
merge_and_test(source, target, expected)
74+
75+
76+
def merge_and_test(source, target, expected):
77+
DictUtils.merge_dicts(source, target)
78+
assert target == expected
79+
80+
81+
# FIXME: add test for times_tamep
82+
# VALID_TIME_STAMP_REGEX=
83+
EXPECTED_DATETIME = datetime(
84+
year=2022,
85+
month=1,
86+
day=12,
87+
hour=10,
88+
minute=44,
89+
second=23,
90+
)
91+
EXPECTED_TIMESTAMP = "20220112T104423000"
92+
93+
94+
@pytest.mark.unit
95+
@mock.patch("cdevents.cli.utils._utcnow")
96+
def test_time_stamp(mock_utcnow):
97+
mock_utcnow.return_value = EXPECTED_DATETIME
98+
actual = time_stamp("T")
99+
assert actual == EXPECTED_TIMESTAMP

cli/tests/test_version.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import re
2+
3+
import pytest
4+
5+
from cdevents.cli import __version__
6+
7+
# From https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
8+
VALID_SEMVER_REGEX = (
9+
r"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)"
10+
r"(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"
11+
)
12+
13+
14+
@pytest.mark.unit
15+
def test_version_is_semantic():
16+
assert re.fullmatch(VALID_SEMVER_REGEX, __version__)

core/setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ packages = find_namespace:
3030
zip_safe = False
3131
include_package_data = True
3232
install_requires =
33+
pytest
3334
PyYAML
3435
requests
3536
cloudevents

core/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Tests for core package."""

core/tests/test_events.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
3+
from pickle import NONE
4+
from cdevents.core import event_type
5+
import pytest
6+
7+
from cdevents.core.events import Events
8+
9+
10+
@pytest.mark.unit
11+
def test_create_event():
12+
event = Events().create_event(event_type="event_type", extensions={"test": "test"})
13+
assert event is not None
14+
assert event._attributes["type"] == "event_type"
15+
assert event._attributes["extensions"] == {"test": "test"}
16+
17+
@pytest.mark.unit
18+
def test_create_artifact_event():
19+
event = Events().create_artifact_event(event_type.ArtifactPackagedEventV1, id="_id", name="_name", version="_version", data={"artifact": "_artifact"})
20+
assert event is not None
21+
assert event._attributes["type"] == event_type.ArtifactPackagedEventV1
22+
assert event._attributes["extensions"] == {"artifactid": "_id", "artifactname": "_name", "artifactversion": "_version"}
23+
assert event.data == {"artifact": "_artifact"}
24+
25+
@pytest.mark.unit
26+
def test_create_branch_event():
27+
event = Events().create_branch_event(event_type.BranchCreatedEventV1, id="_id", name="_name", repoid="_repoid", data={"branch": "_branch"})
28+
assert event is not None
29+
assert event._attributes["type"] == event_type.BranchCreatedEventV1
30+
assert event._attributes["extensions"] == {"branchid": "_id", "branchname": "_name", "branchrepositoryid": "_repoid"}
31+
assert event.data == {"branch": "_branch"}
32+
33+
@pytest.mark.unit
34+
def test_create_build_event():
35+
event = Events().create_build_event(event_type.BuildFinishedEventV1, id="_id", name="_name", artifact="_artifact", data={"build": "_build"})
36+
assert event is not None
37+
assert event._attributes["type"] == event_type.BuildFinishedEventV1
38+
assert event._attributes["extensions"] == {"buildid": "_id", "buildname": "_name", "buildartifactid": "_artifact"}
39+
assert event.data == {"build": "_build"}
40+
41+
@pytest.mark.unit
42+
def test_create_environment_event():
43+
event = Events().create_environment_event(event_type.EnvironmentCreatedEventV1, id="_id", name="_name", repo="_repo", data={"environment": "_environment"})
44+
assert event is not None
45+
assert event._attributes["type"] == event_type.EnvironmentCreatedEventV1
46+
assert event._attributes["extensions"] == {"envId": "_id", "envname": "_name", "envrepourl": "_repo"}
47+
assert event.data == {"environment": "_environment"}
48+
49+
@pytest.mark.unit
50+
def test_create_pipelinerun_event():
51+
event = Events().create_pipelinerun_event(event_type.PipelineRunFinishedEventV1, id="_id", name="_name", status="_status", url="_url", errors="_errors", data={"pipelinerun": "_pipelinerun"})
52+
assert event is not None
53+
assert event._attributes["type"] == event_type.PipelineRunFinishedEventV1
54+
assert event._attributes["extensions"] == {"pipelinerunid": "_id", "pipelinerunname": "_name", "pipelinerunstatus": "_status", "pipelinerunurl": "_url", "pipelinerunerrors": "_errors"}
55+
assert event.data == {"pipelinerun": "_pipelinerun"}
56+
57+
@pytest.mark.unit
58+
def test_create_service_event():
59+
event = Events().create_service_event(event_type.ServiceDeployedEventV1, envid="_envid", name="_name", version="_version", data={"service": "_service"})
60+
assert event is not None
61+
assert event._attributes["type"] == event_type.ServiceDeployedEventV1
62+
assert event._attributes["extensions"] == {"serviceenvid": "_envid", "servicename": "_name", "serviceversion": "_version"}
63+
assert event.data == {"service": "_service"}
64+
65+
66+
@pytest.mark.unit
67+
def test_create_taskrun_event():
68+
event = Events().create_taskrun_event(event_type.TaskRunFinishedEventV1, id="_id", name="_name", pipelineid="_pipelineid", data={"taskrun": "_taskrun"})
69+
assert event is not None
70+
assert event._attributes["type"] == event_type.TaskRunFinishedEventV1
71+
assert event._attributes["extensions"] == {"taskrunid": "_id", "taskrunname": "_name", "taskrunpipelineid": "_pipelineid"}
72+
assert event.data == {"taskrun": "_taskrun"}

core/tests/test_version.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import re
2+
3+
import pytest
4+
5+
from cdevents.core import __version__
6+
7+
# From https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
8+
VALID_SEMVER_REGEX = (
9+
r"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)"
10+
r"(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"
11+
)
12+
13+
14+
@pytest.mark.unit
15+
def test_version_is_semantic():
16+
assert re.fullmatch(VALID_SEMVER_REGEX, __version__)

pytest.ini

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[pytest]
2+
testpaths =
3+
cli/tests
4+
core/tests
5+
markers =
6+
unit: mark for unit tests
7+
integration: integration tests

0 commit comments

Comments
 (0)