Skip to content

Commit 20f64e4

Browse files
committed
feat: add class TranscriptEntry
1 parent ce2d299 commit 20f64e4

3 files changed

Lines changed: 174 additions & 0 deletions

File tree

openproficiency/TranscriptEntry.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""TranscriptEntry module for OpenProficiency library."""
2+
3+
from datetime import datetime
4+
from typing import Optional
5+
from .ProficiencyScore import ProficiencyScore
6+
7+
8+
class TranscriptEntry:
9+
"""A user's proficiency score, validated by a particular issuer."""
10+
11+
# Initializers
12+
def __init__(
13+
self,
14+
# Required
15+
user_id: str,
16+
topic_id: str,
17+
score: float,
18+
issuer: str,
19+
20+
# Optional
21+
timestamp: Optional[datetime] = None,
22+
):
23+
# Required
24+
self.user_id = user_id
25+
self._proficiency_score = ProficiencyScore(
26+
topic_id=topic_id,
27+
score=score
28+
)
29+
self.issuer = issuer
30+
31+
# Optional
32+
self.timestamp = timestamp or datetime.now()
33+
34+
# Properties
35+
@property
36+
def proficiency_score(self) -> ProficiencyScore:
37+
"""Get the topic ID from the proficiency score."""
38+
return self._proficiency_score
39+
40+
# Methods
41+
def to_json(self) -> dict:
42+
"""Convert Topic to JSON-serializable dictionary."""
43+
return {
44+
"user_id": self.user_id,
45+
"topic_id": self._proficiency_score.topic_id,
46+
"score": self._proficiency_score.score,
47+
"issuer": self.issuer,
48+
"timestamp": self.timestamp.isoformat()
49+
}
50+
51+
# Debugging
52+
def __repr__(self) -> str:
53+
"""String representation of TranscriptEntry."""
54+
return f"TranscriptEntry(user_id='{self.user_id}', topic_id='{self._proficiency_score.topic_id}', score={self._proficiency_score.score}, issuer='{self.issuer}')"

openproficiency/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
from .Topic import Topic
44
from .TopicList import TopicList
55
from .ProficiencyScore import ProficiencyScore, ProficiencyScoreName
6+
from .TranscriptEntry import TranscriptEntry

tests/TranscriptEntry_test.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
"""Tests for the TranscriptEntry class."""
2+
3+
from datetime import datetime
4+
from openproficiency import TranscriptEntry, ProficiencyScore, ProficiencyScoreName
5+
6+
7+
class TestTranscriptEntry:
8+
9+
# Initializers
10+
def test_init_required_params(self):
11+
"""Create a transcript entry with required fields."""
12+
13+
# Arrange
14+
user_id = "user-123"
15+
topic_id = "git-commit"
16+
score = 0.8
17+
issuer = "github-learn"
18+
19+
# Act
20+
entry = TranscriptEntry(
21+
user_id=user_id,
22+
topic_id=topic_id,
23+
score=score,
24+
issuer=issuer
25+
)
26+
27+
# Assert
28+
assert entry.user_id == user_id
29+
assert entry._proficiency_score.topic_id == topic_id
30+
assert entry._proficiency_score.score == score
31+
assert entry.issuer == issuer
32+
assert entry.timestamp is not None
33+
34+
def test_init_with_optional_params(self):
35+
"""Create a transcript entry with optional parameters."""
36+
37+
# Arrange
38+
user_id = "user-123"
39+
topic_id = "git-commit"
40+
score = 0.8
41+
issuer = "github-learn"
42+
timestamp = datetime(2024, 1, 15, 10, 30, 0)
43+
44+
# Act
45+
entry = TranscriptEntry(
46+
user_id=user_id,
47+
topic_id=topic_id,
48+
score=score,
49+
issuer=issuer,
50+
timestamp=timestamp
51+
)
52+
53+
# Assert
54+
assert entry.timestamp == timestamp
55+
56+
def test_init_default_timestamp(self):
57+
"""Test that timestamp defaults to current time."""
58+
59+
# Arrange
60+
user_id = "user-123"
61+
topic_id = "git-commit"
62+
score = 0.8
63+
issuer = "github-learn"
64+
65+
# Act
66+
before = datetime.now()
67+
entry = TranscriptEntry(
68+
user_id=user_id,
69+
topic_id=topic_id,
70+
score=score,
71+
issuer=issuer
72+
)
73+
after = datetime.now()
74+
75+
# Assert
76+
assert entry.timestamp >= before
77+
assert entry.timestamp <= after
78+
79+
# Properties
80+
def test_proficiency_score(self):
81+
"""Test that proficiency_score topic and score."""
82+
83+
# Arrange
84+
entry = TranscriptEntry(
85+
user_id="user-123",
86+
topic_id="git-commit",
87+
score=0.8,
88+
issuer="github-learn"
89+
)
90+
91+
# Act
92+
topic_id = entry.proficiency_score.topic_id
93+
score = entry.proficiency_score.score
94+
95+
# Assert
96+
assert topic_id == "git-commit"
97+
assert score == 0.8
98+
99+
# Debugging
100+
def test_repr(self):
101+
"""Test string representation of TranscriptEntry."""
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+
)
110+
111+
# Act
112+
repr_str = repr(entry)
113+
114+
# Assert
115+
assert "TranscriptEntry" in repr_str
116+
assert "user-123" in repr_str
117+
assert "git-commit" in repr_str
118+
assert "0.8" in repr_str
119+
assert "github-learn" in repr_str

0 commit comments

Comments
 (0)