Skip to content

Commit 612f296

Browse files
107 otelevent as pydantic model (#108)
* 107 - Add test for pydantic model validation * 107 - update tests to handle new implmentation of OTelEvent as pydantic model * 107 - update OTelEvent to pydantic model * 107 - Fix mypy erros with plugin
1 parent d15d113 commit 612f296

File tree

5 files changed

+81
-11
lines changed

5 files changed

+81
-11
lines changed

.mypy.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[mypy]
2-
plugins = sqlalchemy.ext.mypy.plugin
2+
plugins = sqlalchemy.ext.mypy.plugin, pydantic.mypy
33
disable_error_code = import-untyped
44
exclude = proof_of_concepts

tel2puml/otel_to_pv/otel_to_pv_types.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""This module contains types required for otel_to_pv package."""
2-
from typing import NamedTuple, Optional
2+
from typing import Optional
33

4+
from pydantic import BaseModel
45

5-
class OTelEvent(NamedTuple):
6+
7+
class OTelEvent(BaseModel):
68
"""Named tuple for OTel event.
79
810
:param job_name: The name of the job.

tests/tel2puml/otel_to_pv/conftest.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -1036,15 +1036,18 @@ def otel_jobs_multiple_job_names(
10361036
for i, otel_events in enumerate(otel_jobs.values()):
10371037
otel_jobs_updated[f"{i+5}"] = []
10381038
for j, event in enumerate(reversed(otel_events)):
1039-
event = (
1040-
event._replace(job_name="test_name_1")
1041-
._replace(job_id=f"test_id_{i+5}")
1042-
._replace(event_id=f"{i+5}_{j}")
1043-
)
1039+
event = event.model_copy(update={
1040+
"job_name": "test_name_1", "job_id": f"test_id_{i+5}",
1041+
"event_id": f"{i+5}_{j}"
1042+
})
10441043
if j == 0:
1045-
event = event._replace(child_event_ids=[f"{i+5}_{j+1}"])
1044+
event = event.model_copy(
1045+
update={"child_event_ids": [f"{i+5}_{j+1}"]}
1046+
)
10461047
else:
1047-
event = event._replace(parent_event_id=f"{i+5}_{j-1}")
1048+
event = event.model_copy(
1049+
update={"parent_event_id": f"{i+5}_{j-1}"}
1050+
)
10481051
otel_jobs_updated[f"{i+5}"].append(event)
10491052

10501053
updated_otel_jobs = dict(otel_jobs_copy, **otel_jobs_updated)

tests/tel2puml/otel_to_pv/data_holders/sql_data_holder/test_sql_dataholder.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ def test_stream_data(
753753
)
754754

755755
num_jobs = 10
756-
events_per_job = 10000 # Total of 100,000 events
756+
events_per_job = 1000 # Total of 10,000 events
757757

758758
with large_sql_data_holder:
759759
for job_index in range(num_jobs):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""Tests for otel_to_pv types."""
2+
3+
import pytest
4+
from pydantic import ValidationError
5+
6+
from tel2puml.otel_to_pv.otel_to_pv_types import OTelEvent
7+
8+
9+
def test_otel_event() -> None:
10+
"""Test OTelEvent."""
11+
# test that setting with correct types works does not raise an error
12+
input_dict = {
13+
"job_name": "job_name",
14+
"job_id": "job_id",
15+
"event_type": "event_type",
16+
"event_id": "event_id",
17+
"start_timestamp": 1,
18+
"end_timestamp": 2,
19+
"application_name": "application_name",
20+
"parent_event_id": "parent_event_id",
21+
"child_event_ids": ["child_event_id"],
22+
}
23+
OTelEvent(**input_dict)
24+
# test that setting with incorrect types raises a validation error
25+
input_dict = {
26+
"job_name": 1,
27+
"job_id": 1,
28+
"event_type": 1,
29+
"event_id": 1,
30+
"start_timestamp": "1",
31+
"end_timestamp": "2",
32+
"application_name": 1,
33+
"parent_event_id": 1,
34+
"child_event_ids": [1],
35+
}
36+
with pytest.raises(ValidationError):
37+
OTelEvent(**input_dict)
38+
# test that setting timestamp fields with a type that can be coerced works
39+
input_dict = {
40+
"job_name": "job_name",
41+
"job_id": "job_id",
42+
"event_type": "event_type",
43+
"event_id": "event_id",
44+
"start_timestamp": "1",
45+
"end_timestamp": "2",
46+
"application_name": "application_name",
47+
"parent_event_id": "parent_event_id",
48+
"child_event_ids": ["child_event_id"],
49+
}
50+
OTelEvent(**input_dict)
51+
# test that setting timestamp fields with a type that cannot be coerced
52+
# raises a validation error
53+
input_dict = {
54+
"job_name": "job_name",
55+
"job_id": "job_id",
56+
"event_type": "event_type",
57+
"event_id": "event_id",
58+
"start_timestamp": "a",
59+
"end_timestamp": "b",
60+
"application_name": "application_name",
61+
"parent_event_id": "parent_event_id",
62+
"child_event_ids": ["child_event_id"],
63+
}
64+
with pytest.raises(ValidationError):
65+
OTelEvent(**input_dict)

0 commit comments

Comments
 (0)