From 190b2749450daea220a542f1097d778780d52d53 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Sun, 22 Mar 2026 11:22:38 +0000 Subject: [PATCH] Initial upload: snippet-manager with CI/CD workflow --- tests/test_cli.py | 99 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 tests/test_cli.py diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..ff62455 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,99 @@ +"""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