diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..875ce0c --- /dev/null +++ b/tests/conftest.py @@ -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 diff --git a/tests/test_history.py b/tests/test_history.py index 754dc35..c471c31 100644 --- a/tests/test_history.py +++ b/tests/test_history.py @@ -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): diff --git a/tests/test_typing.py b/tests/test_typing.py new file mode 100644 index 0000000..05b91e5 --- /dev/null +++ b/tests/test_typing.py @@ -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