From a0de0eef48d5e84ab85bf2bdadadb0aa5c35dcd1 Mon Sep 17 00:00:00 2001 From: evilchili Date: Thu, 1 Dec 2022 00:32:48 -0800 Subject: [PATCH] adding tests --- groove/shell/help.py | 2 +- test/conftest.py | 12 ++++++--- test/test_shell.py | 60 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 test/test_shell.py diff --git a/groove/shell/help.py b/groove/shell/help.py index ed6328d..6c6daa6 100644 --- a/groove/shell/help.py +++ b/groove/shell/help.py @@ -23,4 +23,4 @@ class help(BasePrompt): table.add_row(getattr(obj, 'usage', name), obj.__doc__) print(table) else: - print(f"Help for {parts}:") + print(f"Help for {' '.join(parts)}:") diff --git a/test/conftest.py b/test/conftest.py index 5a7fa7a..c906c61 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -7,14 +7,18 @@ from sqlalchemy.orm import sessionmaker @pytest.fixture(scope='function') -def in_memory_db(): +def in_memory_engine(): + return create_engine('sqlite:///:memory:', future=True) + + +@pytest.fixture(scope='function') +def in_memory_db(in_memory_engine): """ An (empty) in-memory SQLite3 database """ - engine = create_engine('sqlite:///:memory:', future=True) - Session = sessionmaker(bind=engine, future=True) + Session = sessionmaker(bind=in_memory_engine, future=True) session = Session() - groove.db.metadata.create_all(bind=engine) + groove.db.metadata.create_all(bind=in_memory_engine) yield session session.close() diff --git a/test/test_shell.py b/test/test_shell.py new file mode 100644 index 0000000..d808a96 --- /dev/null +++ b/test/test_shell.py @@ -0,0 +1,60 @@ +import pytest + +from groove.db.manager import database_manager +from groove.shell import interactive_shell +from unittest.mock import MagicMock + + +@pytest.fixture +def cmd_prompt(in_memory_engine, db): + with database_manager() as manager: + manager._session = db + yield interactive_shell.CommandPrompt(manager) + + +def response_factory(responses): + return MagicMock(side_effect=responses + ['']) + + +@pytest.mark.parametrize('inputs, expected', [ + (['stats'], 'Database contains 4 playlists'), # match the db fixture +]) +def test_stats(monkeypatch, capsys, cmd_prompt, inputs, expected): + monkeypatch.setattr('groove.shell.base.prompt', response_factory(inputs)) + cmd_prompt.start() + output = capsys.readouterr() + assert expected in output.out + + +@pytest.mark.parametrize('inputs, expected', [ + (['quit'], SystemExit), +]) +def test_quit(monkeypatch, capsys, cmd_prompt, inputs, expected): + monkeypatch.setattr('groove.shell.base.prompt', response_factory(inputs)) + with pytest.raises(expected): + cmd_prompt.start() + + +def test_browse(monkeypatch, capsys, cmd_prompt): + monkeypatch.setattr('groove.shell.base.prompt', response_factory(['browse'])) + cmd_prompt.start() + output = capsys.readouterr() + assert 'Displaying 4 playlists' in output.out + assert 'playlist one' in output.out + assert 'the first one' in output.out + assert 'playlist-one' in output.out + assert 'the second one' in output.out + assert 'the threerd one' in output.out + assert 'empty playlist' in output.out + + +@pytest.mark.parametrize('inputs, expected', [ + ('help', ['Available Commands', ' help ', ' stats ', ' browse ']), + ('help browse', ['Help for browse']), +]) +def test_help(monkeypatch, capsys, cmd_prompt, inputs, expected): + monkeypatch.setattr('groove.shell.base.prompt', response_factory([inputs])) + cmd_prompt.start() + output = capsys.readouterr() + for txt in expected: + assert txt in output.out