Download tests/test_database.py from smlflg/BookmarkBrain: direct link, hf CLI and curl.
- Browser
- Download file 4.14 kB
-
https://huggingface.co/smlflg/BookmarkBrain/resolve/main/tests/test_database.py
- Command line
-
hf download hf://smlflg/BookmarkBrain/tests/test_database.py
-
curl -L -o test_database.py https://huggingface.co/smlflg/BookmarkBrain/resolve/main/tests/test_database.py
4.14 kB
| 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() | |