Skip to content

Commit

Permalink
Add tests on typing accuracy (#122)
Browse files Browse the repository at this point in the history
Co-authored-by: Romain Pesche <[email protected]>
  • Loading branch information
rpesche and Romain Pesche authored Oct 26, 2021
1 parent 6097255 commit 25500c4
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 27 deletions.
53 changes: 53 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from unittest.mock import MagicMock

import pytest

from mitype import app, commandline, history
from mitype.database import fetch_text_from_id


@pytest.fixture()
def empty_history_file(monkeypatch, tmp_path):
history_file = tmp_path / ".mitype_history.csv"
history_file.touch()

monkeypatch.setattr(history, "history_file_absolute_path", lambda: history_file)


@pytest.fixture()
def header_only_history_file(monkeypatch, tmp_path):
history_file = tmp_path / ".mitype_history.csv"

with history_file.open("w+") as fd:
fd.write("ID,WPM,DATE,TIME,ACCURACY\n")

monkeypatch.setattr(history, "history_file_absolute_path", lambda: history_file)


@pytest.fixture()
def history_file(monkeypatch, tmp_path):
history_file = tmp_path / ".mitype_history.csv"
monkeypatch.setattr(history, "history_file_absolute_path", lambda: history_file)

for i in range(100):
history.save_history(i, 65.3, 89)


@pytest.fixture()
def mocked_curses(monkeypatch):
monkeypatch.setattr(app, "curses", MagicMock())


@pytest.fixture()
def mocked_app(mocked_curses, monkeypatch):
text_id = 1758
text = fetch_text_from_id(text_id)

monkeypatch.setattr(app, "resolve_commandline_arguments", lambda: (text, text_id))

myapp = app.App()
myapp.Color = MagicMock()
myapp.window_width = 274
myapp.window_height = 75

return myapp
27 changes: 0 additions & 27 deletions tests/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,6 @@
from mitype import history


@pytest.fixture()
def empty_history_file(monkeypatch, tmp_path):
history_file = tmp_path / ".mitype_history.csv"
history_file.touch()

monkeypatch.setattr(history, "history_file_absolute_path", lambda: history_file)


@pytest.fixture()
def header_only_history_file(monkeypatch, tmp_path):
history_file = tmp_path / ".mitype_history.csv"

with history_file.open("w+") as fd:
fd.write("ID,WPM,DATE,TIME,ACCURACY\n")

monkeypatch.setattr(history, "history_file_absolute_path", lambda: history_file)


@pytest.fixture()
def history_file(monkeypatch, tmp_path):
history_file = tmp_path / ".mitype_history.csv"
monkeypatch.setattr(history, "history_file_absolute_path", lambda: history_file)

for i in range(100):
history.save_history(i, 65.3, 89)


class TestShowHistory:
def test_show_history_no_file(self, monkeypatch, tmp_path):

Expand Down
30 changes: 30 additions & 0 deletions tests/test_typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from copy import copy
from unittest.mock import MagicMock


def test_perfect_accuracy(mocked_app):
win = MagicMock()

for character in copy(mocked_app.text):
mocked_app.typing_mode(win, character)
assert mocked_app.accuracy == 100


def test_error_during_typing(mocked_app):
win = MagicMock()

text = ["z", "KEY_BACKSPACE"] + list(copy(mocked_app.text))

for character in copy(text):
mocked_app.typing_mode(win, character)
assert mocked_app.accuracy == 99.2


def test_same_character_after_typing(mocked_app):
win = MagicMock()

text = list(copy(mocked_app.text)) + list(mocked_app.text[-1] * 10)

for character in copy(text):
mocked_app.typing_mode(win, character)
assert mocked_app.accuracy == 100

0 comments on commit 25500c4

Please sign in to comment.