Skip to content

Commit 3c88d06

Browse files
authored
Merge pull request #170 from pythonpune/testcases
Added test cases
2 parents 6454daa + ad19ae8 commit 3c88d06

File tree

5 files changed

+152
-30
lines changed

5 files changed

+152
-30
lines changed

readit/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
@click.option("--add", "-a", help="Add urls --> readit -a <url1> <url2>")
3030
@click.option("--tag", "-t", help="Use to tag url --> readit -a <url1> -t <tag1>")
3131
@click.option("--delete", "-d", help="Remove a URL of particular ID --> readit -d <url_id>")
32-
@click.option("--clear", "-c", help="Clear bookmarks --> readit -c")
32+
@click.option("--clear", "-c", nargs=0, help="Clear bookmarks --> readit -c")
3333
@click.option("--update", "-u", help="Update a URL for specific ID --> readit -u <existing_id> <new_url>")
3434
@click.option("--search", "-s", help="Search for bookmarks using either a tag or a substring of the URL --> readit -s <tag> or <substring>")
3535
@click.option("--view", "-v", multiple=True, nargs=0, help="Show bookmarks --> readit -v")

readit/database.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def show_urls(self):
310310

311311
def search_url(self, search_value):
312312
"""
313-
Displays a group of URLs associated with the provided search_value (tag).
313+
Displays a group of URLs associated with the provided search_value (tag or url substring).
314314
"""
315315
try:
316316
self.search = search_value.lower()
@@ -325,6 +325,7 @@ def search_url(self, search_value):
325325
WHERE t.tag_name = ?
326326
""", (self.search,)) # Use a tuple with a trailing comma
327327
all_bookmarks = self.cursor.fetchall()
328+
328329
if not all_bookmarks:
329330
# If no bookmarks found with the tag, search in all URLs by given value as substring
330331
bookmarks = self.show_urls() # Fetch all URls

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ dev =
4444
readme_renderer
4545
test =
4646
pytest
47+
pytest-mock
4748

4849
[flake8]
4950
max_line_length = 100

tests/test_db.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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

tests/test_readit.py

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,34 @@ def test_version(runner):
1414
result = runner.invoke(c.main, ["--version"])
1515
assert not result.exception
1616
assert result.exit_code == 0
17-
expected = "readit v0.3\n"
18-
assert result.output == expected
17+
# Check if version number is correctly outputted
18+
assert "readit v0.3" in result.output
1919

2020

2121
# Testing -V option
2222
def test_by_version(runner):
2323
result = runner.invoke(c.main, ["-V"])
2424
assert not result.exception
2525
assert result.exit_code == 0
26-
expected = "readit v0.3\n"
27-
assert result.output == expected
28-
26+
# Check if version number is correctly outputted
27+
assert "readit v0.3" in result.output
2928

3029
# Testing --help option
3130
def test_help_option(runner):
32-
"""testing the help of readit"""
31+
"""Test the help output of the readit CLI tool"""
3332
result = runner.invoke(c.main, ["--help"])
34-
assert not result.exception
3533
assert result.exit_code == 0
36-
expected_output = (
37-
"Usage: main [OPTIONS] [INSERT]...\n"
38-
"\n"
39-
" Readit - Command-line bookmark manager tool."
40-
"\n\n"
41-
"Options:\n"
42-
" -a, --add TEXT... Add URLs with space-separated\n"
43-
" -t, --tag TEXT... Add Tag with space-separated URL\n"
44-
" -d, --delete TEXT Remove a URL of particular ID\n"
45-
" -c, --clear TEXT... Clear bookmarks\n"
46-
" -u, --update TEXT... Update a URL for specific ID\n"
47-
" -s, --search TEXT Search all bookmarks by Tag\n"
48-
" -v, --view TEXT... Show bookmarks\n"
49-
" -o, --openurl TEXT Open URL in Browser\n"
50-
" -V, --version Check latest version\n"
51-
" -e, --export TEXT... Export URLs in csv file\n"
52-
" -tl, --taglist TEXT... Show all Tags\n"
53-
" -ui, --urlinfo TEXT Check particular URL information\n"
54-
" --help Show this message and exit.\n"
55-
)
56-
assert result.output == expected_output
34+
assert not result.exception
35+
36+
# Asserting key parts of the help message are present
37+
assert "Usage: main [OPTIONS]" in result.output
38+
assert "Readit - Command-line bookmark manager tool." in result.output
39+
assert "-a, --add TEXT" in result.output
40+
assert "-t, --tag TEXT" in result.output
41+
assert "-V, --version" in result.output
42+
assert "--help" in result.output
43+
44+
# Asserting information
45+
assert "Options:" in result.output
46+
assert "Show bookmarks" in result.output
47+
assert "Add urls" in result.output

0 commit comments

Comments
 (0)