100 lines
2.6 KiB
Python
100 lines
2.6 KiB
Python
"""Tests for CLI commands."""
|
|
|
|
import os
|
|
import tempfile
|
|
|
|
import pytest
|
|
from click.testing import CliRunner
|
|
|
|
from snip.cli.commands import cli
|
|
from snip.db.database import Database
|
|
|
|
|
|
@pytest.fixture
|
|
def runner():
|
|
return CliRunner()
|
|
|
|
|
|
@pytest.fixture
|
|
def test_db():
|
|
with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
|
|
db_path = f.name
|
|
os.environ["SNIP_DB_PATH"] = db_path
|
|
database = Database(db_path)
|
|
database.init_db()
|
|
yield database
|
|
os.unlink(db_path)
|
|
if "SNIP_DB_PATH" in os.environ:
|
|
del os.environ["SNIP_DB_PATH"]
|
|
|
|
|
|
def test_init_command(runner, test_db):
|
|
"""Test init command."""
|
|
result = runner.invoke(cli, ["init"])
|
|
assert result.exit_code == 0
|
|
assert "initialized" in result.output.lower()
|
|
|
|
|
|
def test_add_command(runner, test_db):
|
|
"""Test add command."""
|
|
result = runner.invoke(cli, [
|
|
"add",
|
|
"--title", "Test Snippet",
|
|
"--code", "print('test')",
|
|
"--language", "python",
|
|
])
|
|
assert result.exit_code == 0
|
|
assert "added" in result.output.lower()
|
|
|
|
|
|
def test_list_command(runner, test_db):
|
|
"""Test list command."""
|
|
test_db.add_snippet(title="Test 1", code="code1")
|
|
test_db.add_snippet(title="Test 2", code="code2")
|
|
|
|
result = runner.invoke(cli, ["list"])
|
|
assert result.exit_code == 0
|
|
assert "Test 1" in result.output
|
|
assert "Test 2" in result.output
|
|
|
|
|
|
def test_get_command(runner, test_db):
|
|
"""Test get command."""
|
|
snippet_id = test_db.add_snippet(title="Get Me", code="print('get')", language="python")
|
|
|
|
result = runner.invoke(cli, ["get", str(snippet_id)])
|
|
assert result.exit_code == 0
|
|
assert "Get Me" in result.output
|
|
|
|
|
|
def test_delete_command(runner, test_db):
|
|
"""Test delete command."""
|
|
snippet_id = test_db.add_snippet(title="Delete Me", code="code")
|
|
|
|
result = runner.invoke(cli, ["delete", str(snippet_id)], input="y\n")
|
|
assert result.exit_code == 0
|
|
|
|
assert test_db.get_snippet(snippet_id) is None
|
|
|
|
|
|
def test_tag_commands(runner, test_db):
|
|
"""Test tag commands."""
|
|
snippet_id = test_db.add_snippet(title="Tagged", code="code")
|
|
|
|
result = runner.invoke(cli, ["tag", "add", str(snippet_id), "python"])
|
|
assert result.exit_code == 0
|
|
|
|
result = runner.invoke(cli, ["tag", "list"])
|
|
assert result.exit_code == 0
|
|
assert "python" in result.output
|
|
|
|
|
|
def test_collection_commands(runner, test_db):
|
|
"""Test collection commands."""
|
|
result = runner.invoke(cli, ["collection", "create", "Test Collection"])
|
|
assert result.exit_code == 0
|
|
|
|
result = runner.invoke(cli, ["collection", "list"])
|
|
assert result.exit_code == 0
|
|
assert "Test Collection" in result.output
|