Skip to content

Commit 7f5feb0

Browse files
committed
fix: inconsistent use of to_dict and to_json
1 parent 2d459b9 commit 7f5feb0

8 files changed

Lines changed: 169 additions & 21 deletions

File tree

openproficiency/ProficiencyScore.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""ProficiencyScore module for OpenProficiency library."""
22

3+
import json
34
from enum import Enum
45
from typing import Union
56

@@ -84,13 +85,17 @@ def _get_name_from_score(self, score: float) -> ProficiencyScoreName:
8485
else:
8586
raise ValueError(f"Invalid score value: {score}")
8687

87-
def to_json(self) -> dict:
88+
def to_dict(self) -> dict:
8889
"""Convert to a JSON-serializable dictionary."""
8990
return {
9091
"topic_id": self.topic_id,
9192
"score": self._score
9293
}
9394

95+
def to_json(self) -> str:
96+
"""Convert to a JSON string."""
97+
return json.dumps(self.to_dict())
98+
9499
# Debugging
95100

96101
def __repr__(self) -> str:

openproficiency/Topic.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Topic module for OpenProficiency library."""
22

3+
import json
34
from typing import List, Union
45

56

@@ -77,7 +78,7 @@ def add_pretopics(self, pretopics: List[Union[str, "Topic"]]) -> None:
7778
for pretopic in pretopics:
7879
self.add_pretopic(pretopic)
7980

80-
def to_json(self) -> dict:
81+
def to_dict(self) -> dict:
8182
"""Convert Topic to JSON-serializable dictionary."""
8283
return {
8384
"id": self.id,
@@ -86,6 +87,10 @@ def to_json(self) -> dict:
8687
"pretopics": self.pretopics
8788
}
8889

90+
def to_json(self) -> str:
91+
"""Convert Topic to JSON string."""
92+
return json.dumps(self.to_dict())
93+
8994
# Debugging
9095
def __repr__(self) -> str:
9196
"""String representation of Topic."""

openproficiency/TopicList.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def __init__(
1515
owner: str,
1616
name: str,
1717
# Optional
18-
description: str = "",
18+
description: str = ""
1919
):
2020
# Required
2121
self.owner = owner
@@ -204,7 +204,7 @@ def _add_pretopics_recursive(
204204
topic_list.add_topic(pretopic)
205205
current_child.add_pretopic(pretopic)
206206

207-
def to_json(self) -> str:
207+
def to_dict(self) -> dict:
208208
"""
209209
Export the TopicList to a JSON string.
210210
"""
@@ -234,4 +234,8 @@ def to_json(self) -> str:
234234
# Store in data
235235
data["topics"][topic_id] = topic_data
236236

237-
return json.dumps(data, indent=2)
237+
return data
238+
239+
def to_json(self) -> str:
240+
"""Convert TopicList to JSON string."""
241+
return json.dumps(self.to_dict())

openproficiency/TranscriptEntry.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""TranscriptEntry module for OpenProficiency library."""
22

3+
import json
34
from datetime import datetime
45
from typing import Optional
56
from .ProficiencyScore import ProficiencyScore
@@ -38,7 +39,7 @@ def proficiency_score(self) -> ProficiencyScore:
3839
return self._proficiency_score
3940

4041
# Methods
41-
def to_json(self) -> dict:
42+
def to_dict(self) -> dict:
4243
"""Convert Topic to JSON-serializable dictionary."""
4344
return {
4445
"user_id": self.user_id,
@@ -48,6 +49,10 @@ def to_json(self) -> dict:
4849
"timestamp": self.timestamp.isoformat()
4950
}
5051

52+
def to_json(self) -> str:
53+
"""Convert Topic to JSON string."""
54+
return json.dumps(self.to_dict())
55+
5156
# Debugging
5257
def __repr__(self) -> str:
5358
"""String representation of TranscriptEntry."""

tests/ProficiencyScore_test.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,7 @@ def test_score_name_setter_invalid_type(self):
183183
assert "ProficiencyScoreName enum" in str(e)
184184

185185
# Methods
186-
187-
def test_to_json(self):
186+
def test_to_dict(self):
188187
"""Test conversion to JSON-serializable dictionary."""
189188

190189
# Arrange
@@ -193,14 +192,29 @@ def test_to_json(self):
193192
ps = ProficiencyScore(topic_id=topic_id, score=score)
194193

195194
# Act
196-
json_dict = ps.to_json()
195+
json_dict = ps.to_dict()
197196

198197
# Assert
199198
assert json_dict == {
200199
"topic_id": topic_id,
201200
"score": score
202201
}
203202

203+
def test_to_json(self):
204+
"""Test conversion to JSON string."""
205+
206+
# Arrange
207+
topic_id = "git-commit"
208+
score = 0.8
209+
ps = ProficiencyScore(topic_id=topic_id, score=score)
210+
211+
# Act
212+
json_str = ps.to_json()
213+
214+
# Assert
215+
expected_json = '{"topic_id": "git-commit", "score": 0.8}'
216+
assert json_str == expected_json
217+
204218
# Debugging
205219
def test_repr(self):
206220
"""Test string representation of ProficiencyScore."""

tests/TopicList_test.py

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -374,25 +374,69 @@ def test_load_from_json_prepretopics(self):
374374
assert "git-pull" in topic_list.topics
375375
assert topic_list.topics["git-pull"].description == "Versioning code with Git repositories"
376376

377-
def test_to_json_simple(self):
378-
"""Exporting a simple TopicList to JSON."""
377+
def test_to_dict_simple(self):
378+
"""Exporting a simple TopicList to dictionary."""
379379

380380
# Arrange
381381
topic_list = TopicList(
382382
owner="github",
383383
name="github-features",
384384
description="Features of the GitHub platform",
385385
)
386-
topic1 = Topic(id="actions", description="Storing changes to the Git history")
387-
topic1.add_subtopic("automation")
388-
topic1.add_pretopic("yaml")
386+
topic_list.add_topic(Topic(
387+
id="actions",
388+
description="Storing changes to the Git history",
389+
subtopics=["automation"],
390+
pretopics=["yaml"]
391+
))
392+
topic_list.add_topic(Topic(
393+
id="repositories",
394+
description="Versioning code with Git repositories",
395+
subtopics=["git-clone"],
396+
pretopics=["git-push"]
397+
))
389398

390-
topic2 = Topic(id="repositories", description="Versioning code with Git repositories")
391-
topic2.add_subtopic("git-clone")
392-
topic2.add_pretopic("git-push")
399+
# Act
400+
data = topic_list.to_dict()
393401

394-
topic_list.add_topic(topic1)
395-
topic_list.add_topic(topic2)
402+
# Assert - List Info
403+
assert data["owner"] == "github"
404+
assert data["name"] == "github-features"
405+
assert data["description"] == "Features of the GitHub platform"
406+
407+
# Assert - Topic 1
408+
assert "actions" in data["topics"]
409+
assert data["topics"]["actions"]["description"] == "Storing changes to the Git history"
410+
assert "automation" in data["topics"]["actions"]["subtopics"]
411+
assert "yaml" in data["topics"]["actions"]["pretopics"]
412+
413+
# Assert - Topic 2
414+
assert "repositories" in data["topics"]
415+
assert data["topics"]["repositories"]["description"] == "Versioning code with Git repositories"
416+
assert "git-clone" in data["topics"]["repositories"]["subtopics"]
417+
assert "git-push" in data["topics"]["repositories"]["pretopics"]
418+
419+
def test_to_json_simple(self):
420+
"""Exporting a simple TopicList to JSON string."""
421+
422+
# Arrange
423+
topic_list = TopicList(
424+
owner="github",
425+
name="github-features",
426+
description="Features of the GitHub platform",
427+
)
428+
topic_list.add_topic(Topic(
429+
id="actions",
430+
description="Storing changes to the Git history",
431+
subtopics=["automation"],
432+
pretopics=["yaml"]
433+
))
434+
topic_list.add_topic(Topic(
435+
id="repositories",
436+
description="Versioning code with Git repositories",
437+
subtopics=["git-clone"],
438+
pretopics=["git-push"]
439+
))
396440

397441
# Act
398442
json_data = topic_list.to_json()

tests/Topic_test.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from openproficiency import Topic
23

34

@@ -141,7 +142,7 @@ def test_add_pretopics_mixed(self):
141142
assert pretopic1 in topic.pretopics
142143
assert pretopic2.id in topic.pretopics
143144

144-
def test_to_json(self):
145+
def test_to_dict(self):
145146
"""Test converting a Topic to JSON."""
146147

147148
# Arrange
@@ -153,7 +154,29 @@ def test_to_json(self):
153154
)
154155

155156
# Act
156-
topic_json = topic.to_json()
157+
topic_json = topic.to_dict()
158+
159+
# Assert
160+
assert topic_json["id"] == "git-merge"
161+
assert topic_json["description"] == "Combining branches in Git"
162+
assert "git-branch" in topic_json["subtopics"]
163+
assert "git-commit" in topic_json["subtopics"]
164+
assert "cli" in topic_json["pretopics"]
165+
166+
def test_to_json(self):
167+
"""Test converting a Topic to JSON string."""
168+
169+
# Arrange
170+
topic = Topic(
171+
id="git-merge",
172+
description="Combining branches in Git",
173+
subtopics=["git-branch", "git-commit"],
174+
pretopics=["cli"]
175+
)
176+
177+
# Act
178+
topic_json_str = topic.to_json()
179+
topic_json = json.loads(topic_json_str)
157180

158181
# Assert
159182
assert topic_json["id"] == "git-merge"

tests/TranscriptEntry_test.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,54 @@ def test_proficiency_score(self):
9696
assert topic_id == "git-commit"
9797
assert score == 0.8
9898

99+
# Methods
100+
def test_to_dict(self):
101+
"""Test conversion of TranscriptEntry to dictionary."""
102+
103+
# Arrange
104+
entry = TranscriptEntry(
105+
user_id="user-123",
106+
topic_id="git-commit",
107+
score=0.8,
108+
issuer="github-learn",
109+
timestamp=datetime(2024, 1, 15, 10, 30, 0)
110+
)
111+
112+
# Act
113+
entry_dict = entry.to_dict()
114+
115+
# Assert
116+
assert entry_dict == {
117+
"user_id": "user-123",
118+
"topic_id": "git-commit",
119+
"score": 0.8,
120+
"issuer": "github-learn",
121+
"timestamp": "2024-01-15T10:30:00"
122+
}
123+
124+
def test_to_json(self):
125+
"""Test conversion of TranscriptEntry to JSON string."""
126+
127+
# Arrange
128+
entry = TranscriptEntry(
129+
user_id="user-123",
130+
topic_id="git-commit",
131+
score=0.8,
132+
issuer="github-learn",
133+
timestamp=datetime(2024, 1, 15, 10, 30, 0)
134+
)
135+
136+
# Act
137+
json_str = entry.to_json()
138+
139+
# Assert
140+
expected_json = (
141+
'{"user_id": "user-123", "topic_id": "git-commit", '
142+
'"score": 0.8, "issuer": "github-learn", '
143+
'"timestamp": "2024-01-15T10:30:00"}'
144+
)
145+
assert json_str == expected_json
146+
99147
# Debugging
100148
def test_repr(self):
101149
"""Test string representation of TranscriptEntry."""

0 commit comments

Comments
 (0)