Files
snippet-manager/tests/test_db.py
7000pctAUTO e19f854bdc
Some checks failed
CI / test (push) Has been cancelled
CI / lint (push) Has been cancelled
Initial upload: snippet-manager with CI/CD workflow
2026-03-22 11:22:35 +00:00

106 lines
2.8 KiB
Python

"""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