|
| 1 | +import pytest |
| 2 | +import os |
| 3 | +from readit.database import DatabaseConnection |
| 4 | + |
| 5 | +@pytest.fixture |
| 6 | +def db_connection(): |
| 7 | + # Setup: Create a temporary database file for testing |
| 8 | + db = DatabaseConnection() |
| 9 | + yield db |
| 10 | + # Teardown: Close the database and remove the temporary database file |
| 11 | + db.db.close() |
| 12 | + if os.path.exists(db.databasefile): |
| 13 | + os.remove(db.databasefile) |
| 14 | + |
| 15 | +def test_init_db_creates_tables(db_connection): |
| 16 | + # Test if the tables are created during initialization |
| 17 | + cursor = db_connection.cursor |
| 18 | + cursor.execute("SELECT name FROM sqlite_master WHERE type='table'") |
| 19 | + tables = cursor.fetchall() |
| 20 | + assert len(tables) == 4 # bookmarks, tags, url_tags and considering sqlite_sequence table too |
| 21 | + assert ('bookmarks',) in tables |
| 22 | + assert ('tags',) in tables |
| 23 | + assert ('url_tags',) in tables |
| 24 | + |
| 25 | +def test_add_url_success(db_connection): |
| 26 | + # Test successful addition of a URL |
| 27 | + url = "http://example.com" |
| 28 | + result = db_connection.add_url(url) |
| 29 | + assert result == True |
| 30 | + |
| 31 | + # Verify the URL was added to the database |
| 32 | + cursor = db_connection.cursor |
| 33 | + cursor.execute("SELECT url FROM bookmarks WHERE url=?", (url,)) |
| 34 | + assert cursor.fetchone()[0] == url |
| 35 | + |
| 36 | +def test_add_url_duplicate(db_connection): |
| 37 | + # Test adding a duplicate URL |
| 38 | + url = "http://example.com" |
| 39 | + db_connection.add_url(url) # First time |
| 40 | + result = db_connection.add_url(url) # Second time (should fail) |
| 41 | + assert result == False |
| 42 | + |
| 43 | +def test_tag_url_success(db_connection): |
| 44 | + # Test tagging a URL with a new tag |
| 45 | + url = "http://example.com" |
| 46 | + tag = "test" |
| 47 | + result = db_connection.tag_url(url, tag) |
| 48 | + assert result == True |
| 49 | + |
| 50 | + # Verify the tag was added to the database |
| 51 | + cursor = db_connection.cursor |
| 52 | + cursor.execute(""" |
| 53 | + SELECT b.url, t.tag_name |
| 54 | + FROM bookmarks b |
| 55 | + JOIN url_tags ut ON b.id = ut.url_id |
| 56 | + JOIN tags t ON ut.tag_id = t.id |
| 57 | + WHERE b.url=? AND t.tag_name=? |
| 58 | + """, (url, tag)) |
| 59 | + assert cursor.fetchone() == (url, tag) |
| 60 | + |
| 61 | +def test_list_all_tags(db_connection): |
| 62 | + # Test listing all tags |
| 63 | + db_connection.tag_url("http://example.com", "test") |
| 64 | + db_connection.tag_url("http://example.org", "example") |
| 65 | + |
| 66 | + tags = db_connection.list_all_tags() |
| 67 | + assert len(tags) == 2 |
| 68 | + assert "test" in tags |
| 69 | + assert "example" in tags |
| 70 | + |
| 71 | +def test_delete_url(db_connection): |
| 72 | + # Test deleting a URL and associated tags |
| 73 | + url = "http://example.com" |
| 74 | + db_connection.add_url(url) |
| 75 | + db_connection.tag_url(url, "test") |
| 76 | + |
| 77 | + # Get the URL ID |
| 78 | + cursor = db_connection.cursor |
| 79 | + cursor.execute("SELECT id FROM bookmarks WHERE url=?", (url,)) |
| 80 | + url_id = cursor.fetchone()[0] |
| 81 | + |
| 82 | + # Delete the URL |
| 83 | + result = db_connection.delete_url(url_id) |
| 84 | + assert result == True |
| 85 | + |
| 86 | + # Verify the URL and associated tags were deleted |
| 87 | + cursor.execute("SELECT url FROM bookmarks WHERE id=?", (url_id,)) |
| 88 | + assert cursor.fetchone() is None |
| 89 | + |
| 90 | +def test_update_url_success(db_connection): |
| 91 | + # Test updating an existing URL |
| 92 | + db_connection.add_url("http://example.com") |
| 93 | + |
| 94 | + # Get the URL ID |
| 95 | + cursor = db_connection.cursor |
| 96 | + cursor.execute("SELECT id FROM bookmarks WHERE url=?", ("http://example.com",)) |
| 97 | + url_id = cursor.fetchone()[0] |
| 98 | + |
| 99 | + # Update the URL |
| 100 | + result = db_connection.update_url(url_id, "http://updated.com") |
| 101 | + assert result == True |
| 102 | + |
| 103 | + # Verify the URL was updated in the database |
| 104 | + cursor.execute("SELECT url FROM bookmarks WHERE id=?", (url_id,)) |
| 105 | + assert cursor.fetchone()[0] == "http://updated.com" |
| 106 | + |
| 107 | +def test_search_url(db_connection): |
| 108 | + # Test searching for URLs by tag or substring |
| 109 | + db_connection.tag_url("http://example.com", "test") |
| 110 | + db_connection.tag_url("http://example-test.org", "mytag") |
| 111 | + |
| 112 | + # Search by tag |
| 113 | + results = db_connection.search_url("test") |
| 114 | + assert len(results) == 1 |
| 115 | + assert results[0][1] == "http://example.com" |
| 116 | + |
| 117 | + # Search by substring |
| 118 | + # (if given search value is tag and if it is available then it does not search in url substring) |
| 119 | + results = db_connection.search_url("example") |
| 120 | + assert len(results) == 2 |
| 121 | + |
| 122 | +def test_open_url(db_connection, mocker): |
| 123 | + # Test opening a URL in the browser |
| 124 | + mocker.patch('builtins.input', return_value='yes') # Mock user confirmation input |
| 125 | + mocker.patch('webbrowser.open', return_value=True) # Mock web browser opening |
| 126 | + |
| 127 | + db_connection.add_url("http://example.com") |
| 128 | + result = db_connection.open_url("example") |
| 129 | + assert result == True |
0 commit comments