diff options
author | Eddy Pedroni <epedroni@pm.me> | 2024-09-25 22:19:41 +0200 |
---|---|---|
committer | Eddy Pedroni <epedroni@pm.me> | 2024-09-25 22:19:41 +0200 |
commit | d4f33e9a886363008385b4d76b475e53c5689cdb (patch) | |
tree | efb080b5d1873c3cde339c09e6cba5a686f14dc8 | |
parent | 7f6ab3e4c535eb0cc8b8dfdaa591e1cd7131e537 (diff) |
Add session class with basic test coverage
-rw-r--r-- | src/cli.py | 0 | ||||
-rw-r--r-- | src/session.py | 29 | ||||
-rw-r--r-- | src/session_integrationtest.py | 46 |
3 files changed, 75 insertions, 0 deletions
diff --git a/src/cli.py b/src/cli.py deleted file mode 100644 index e69de29..0000000 --- a/src/cli.py +++ /dev/null diff --git a/src/session.py b/src/session.py new file mode 100644 index 0000000..33870aa --- /dev/null +++ b/src/session.py @@ -0,0 +1,29 @@ +from typing import Iterator, Callable + +from card import Card +from scheduler import getSchedulerClass +from parser import parseFiles +from state_json import load, save + +class Session: + def __init__(self, scheduler_name: str, card_files: list[str], state_file: str): + self._cards = parseFiles(card_files) + self._state_file = state_file + self._scheduler = getSchedulerClass(scheduler_name)(self._cards, load(state_file)) + + def practice(self, size: int) -> Iterator[Card]: + ids = self._scheduler.practice(size) + for id in ids: + yield self._cards[id] + + def test(self, size: int) -> Iterator[tuple[Card, Callable]]: + ids = self._scheduler.practice(size) + results = {} + + for id in ids: + def result(correct: bool) -> None: + results[id] = int(correct) + yield self._cards[id], result + + self._scheduler.update(results) + save(self._state_file, self._scheduler.getState()) diff --git a/src/session_integrationtest.py b/src/session_integrationtest.py new file mode 100644 index 0000000..2c5d608 --- /dev/null +++ b/src/session_integrationtest.py @@ -0,0 +1,46 @@ +import pytest +import json +from session import Session + +@pytest.fixture +def cardFiles(tmp_path): + card_files = [ + tmp_path / "test1.fcard", + tmp_path / "test2.fcard", + ] + + for i, c in enumerate(card_files): + with open(c, "w") as f: + for j in range(0, 3): + f.write(f"FRONT\nFile {i}, card {j} front\nBACK\nback\n") + + return card_files + +@pytest.fixture +def stateFile(tmp_path): + return tmp_path / "state.json" + +def test_practiceSession(cardFiles, stateFile): + session = Session("brutal", cardFiles, stateFile) + + c = 0 + for card in session.practice(5): + c += 1 + + assert c == 5 + +def test_testSession(cardFiles, stateFile): + session = Session("brutal", cardFiles, stateFile) + + c = 0 + for i, (card, correct) in enumerate(session.test(5)): + c += 1 + correct(i % 2 == 0) + + assert c == 5 + + with open(stateFile, "r") as f: + state = json.load(f) + latest_scores = [history[-1] for id, history in state.items()] + assert latest_scores.count(0) == 2 + assert latest_scores.count(1) == 3 |