From e19f854bdcd5eaff0bf077b102e08f8384211a2a Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Sun, 22 Mar 2026 11:22:35 +0000 Subject: [PATCH] Initial upload: snippet-manager with CI/CD workflow --- tests/test_db.py | 105 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 tests/test_db.py diff --git a/tests/test_db.py b/tests/test_db.py new file mode 100644 index 0000000..e334f81 --- /dev/null +++ b/tests/test_db.py @@ -0,0 +1,105 @@ +"""Tests for database operations.""" + +import os +import tempfile + +import pytest + +from snip.db.database import Database + + +@pytest.fixture +def db(): + with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f: + db_path = f.name + database = Database(db_path) + database.init_db() + yield database + os.unlink(db_path) + + +def test_init_db(db): + """Test database initialization.""" + result = db.list_snippets() + assert result == [] + + +def test_add_snippet(db): + """Test adding a snippet.""" + snippet_id = db.add_snippet( + title="Test Snippet", + code="print('hello')", + language="python", + tags=["test"], + ) + assert snippet_id > 0 + + snippet = db.get_snippet(snippet_id) + assert snippet is not None + assert snippet["title"] == "Test Snippet" + assert snippet["code"] == "print('hello')" + assert snippet["language"] == "python" + + +def test_list_snippets(db): + """Test listing snippets.""" + db.add_snippet(title="Snippet 1", code="code1") + db.add_snippet(title="Snippet 2", code="code2") + + snippets = db.list_snippets() + assert len(snippets) == 2 + + +def test_update_snippet(db): + """Test updating a snippet.""" + snippet_id = db.add_snippet(title="Original", code="original") + db.update_snippet(snippet_id, title="Updated", code="updated") + + snippet = db.get_snippet(snippet_id) + assert snippet["title"] == "Updated" + assert snippet["code"] == "updated" + + +def test_delete_snippet(db): + """Test deleting a snippet.""" + snippet_id = db.add_snippet(title="To Delete", code="delete me") + assert db.delete_snippet(snippet_id) is True + assert db.get_snippet(snippet_id) is None + + +def test_add_tag(db): + """Test adding a tag.""" + snippet_id = db.add_snippet(title="Tagged", code="code") + db.add_tag(snippet_id, "python") + + snippet = db.get_snippet(snippet_id) + tags = eval(snippet["tags"]) + assert "python" in tags + + +def test_collection(db): + """Test collections.""" + collection_id = db.create_collection("Test Collection", "A test collection") + assert collection_id > 0 + + snippet_id = db.add_snippet(title="In Collection", code="code") + db.add_snippet_to_collection(snippet_id, collection_id) + + snippets = db.get_collection_snippets(collection_id) + assert len(snippets) == 1 + assert snippets[0]["title"] == "In Collection" + + +def test_export_import(db): + """Test export and import.""" + db.add_snippet(title="Export Me", code="export this", tags=["test"]) + + snippets = db.export_all() + assert len(snippets) == 1 + + db.add_snippet(title="Existing", code="existing") + db.import_snippet({"title": "Import 1", "code": "import1"}, strategy="skip") + db.import_snippet({"title": "Import 2", "code": "import2"}, strategy="duplicate") + + all_snippets = db.list_snippets(limit=100) + assert len(all_snippets) == 4