Skip to content

Commit 5a4a0ce

Browse files
committed
feat: add method to export Topic as json object
1 parent fe736f7 commit 5a4a0ce

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

openproficiency/Topic.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ def add_pretopics(self, pretopics: List[Union[str, "Topic"]]) -> None:
7777
for pretopic in pretopics:
7878
self.add_pretopic(pretopic)
7979

80+
def to_json(self) -> dict:
81+
"""Convert Topic to JSON-serializable dictionary."""
82+
return {
83+
"id": self.id,
84+
"description": self.description,
85+
"subtopics": self.subtopics,
86+
"pretopics": self.pretopics
87+
}
88+
8089
# Debugging
8190
def __repr__(self) -> str:
8291
"""String representation of Topic."""

tests/Topic_test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,27 @@ def test_add_pretopics_mixed(self):
141141
assert pretopic1 in topic.pretopics
142142
assert pretopic2.id in topic.pretopics
143143

144+
def test_to_json(self):
145+
"""Test converting a Topic to JSON."""
146+
147+
# Arrange
148+
topic = Topic(
149+
id="git-merge",
150+
description="Combining branches in Git",
151+
subtopics=["git-branch", "git-commit"],
152+
pretopics=["cli"]
153+
)
154+
155+
# Act
156+
topic_json = topic.to_json()
157+
158+
# Assert
159+
assert topic_json["id"] == "git-merge"
160+
assert topic_json["description"] == "Combining branches in Git"
161+
assert "git-branch" in topic_json["subtopics"]
162+
assert "git-commit" in topic_json["subtopics"]
163+
assert "cli" in topic_json["pretopics"]
164+
144165
# Debugging
145166
def test_topic_repr(self):
146167
"""Check string representation of a Topic"""

0 commit comments

Comments
 (0)