File size: 4,143 Bytes
3ac10cb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | import unittest
import sqlite3
import json
import sys
import os
from unittest.mock import patch, MagicMock
# Add the parent directory to the Python path so we can import from backend
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
# Create a simple test file to test import without sqlite_vec
class TestDatabaseImport(unittest.TestCase):
def test_import_without_sqlite_vec(self):
"""Test that backend.database can be imported without sqlite_vec installed."""
# Save the original sys.modules
original_modules = sys.modules.copy()
# Remove sqlite_vec from sys.modules if it exists
if 'sqlite_vec' in sys.modules:
del sys.modules['sqlite_vec']
try:
import backend.database
self.assertTrue(True, "Import succeeded without sqlite_vec")
except Exception as e:
self.fail(f"Import failed without sqlite_vec: {e}")
finally:
# Restore original modules
sys.modules.clear()
sys.modules.update(original_modules)
class TestUpdateBookmark(unittest.TestCase):
def setUp(self):
"""Set up a test database connection."""
self.conn = sqlite3.connect(':memory:')
# Create a simple bookmarks table for testing
self.conn.execute('''
CREATE TABLE bookmarks (
id INTEGER PRIMARY KEY,
url TEXT,
title TEXT,
description TEXT,
auto_tags TEXT,
user_tags TEXT,
updated_at TIMESTAMP
)
''')
# Insert a test bookmark
self.conn.execute(
"INSERT INTO bookmarks (id, url, title) VALUES (1, 'https://example.com', 'Test Title')"
)
def tearDown(self):
"""Clean up after tests."""
self.conn.close()
def test_update_no_kwargs(self):
"""Test that update_bookmark returns without executing SQL when no kwargs are supplied."""
from backend.database import update_bookmark
# Get the initial state of the bookmark
initial_row = self.conn.execute("SELECT * FROM bookmarks WHERE id = 1").fetchone()
# Call update_bookmark with no kwargs
update_bookmark(self.conn, 1)
# Get the final state of the bookmark
final_row = self.conn.execute("SELECT * FROM bookmarks WHERE id = 1").fetchone()
# Verify that the bookmark was not updated (no SQL was executed)
self.assertEqual(initial_row, final_row, "update_bookmark should not modify the bookmark when no kwargs are provided")
def test_update_with_tags(self):
"""Test that update_bookmark serializes list tag values and emits expected parameterized SQL for allowed fields."""
from backend.database import update_bookmark
# Call update_bookmark with tag lists
update_bookmark(
self.conn,
1,
auto_tags=["tag1", "tag2"],
user_tags=["user_tag1", "user_tag2"],
title="Updated Title"
)
# Verify the bookmark was updated
row = self.conn.execute("SELECT * FROM bookmarks WHERE id = 1").fetchone()
# Convert row to dict for easier access
columns = [description[1] for description in self.conn.execute("PRAGMA table_info(bookmarks)").fetchall()]
row_dict = dict(zip(columns, row))
self.assertEqual(row_dict['title'], "Updated Title")
self.assertEqual(json.loads(row_dict['auto_tags']), ["tag1", "tag2"])
self.assertEqual(json.loads(row_dict['user_tags']), ["user_tag1", "user_tag2"])
def test_update_with_unknown_field(self):
"""Test that update_bookmark rejects an unknown field."""
from backend.database import update_bookmark
# Try to update with an unknown field
with self.assertRaises(ValueError) as context:
update_bookmark(self.conn, 1, unknown_field="value")
# Verify the error message
self.assertIn("Unknown field 'unknown_field'", str(context.exception))
if __name__ == '__main__':
unittest.main()
|