Search view and tests added, along with rudimentary control

This commit is contained in:
asdf 2019-12-19 15:29:21 +11:00
parent 696b594f1e
commit 279c6a0f87
2 changed files with 63 additions and 22 deletions

View File

@ -120,9 +120,32 @@ def print_thread_details(post_id) -> tuple:
return parent_id, post_url
def print_search_results(keyword: str, search_results: list):
"""a view for the search results - prints results to screen"""
print(
"\nShowing results for {}\n\n{:>4s} {:<15s}{:<12s}{:<13s}".format(
keyword, "ID#", "DATE", "AUTHOR", "DESC"
)
)
for display_index, record in enumerate(search_results, start=1):
date = datetime.fromtimestamp(float(record[2])).strftime("%Y-%m-%d")
author = record[1]
desc = record[6]
print(
"{:4d} {:<15s}{:<12s}{:<13s}\n".format(display_index, date, author, desc)
)
## CONTROLS
def do_search():
keyword = input("\nEnter your search\n")
search_results = LinkData.search(keyword)
print_search_results(keyword, search_results)
def view_link_in_browser(url):
"""Attempts to view the specified URL in the configured browser"""
if which(config.USER.browser) is None:
@ -241,28 +264,6 @@ def post_link() -> int:
return LinkData.add(record)
def search(keyword):
"""Search function - not yet complete"""
raise NotImplementedError
## PSEUDOCODE:
## results_found = ""
## for line in link_data:
## if keyword in link_data[title] or keyword in link_data[category] or
## keyword in link_data[comment]: ## results_found = "yes"
## if line is parent post:
## print line
## elif line is reply:
## get parentID
## print(line[parentID])
## if results_found == "":
## print("No results found")
## else:
## next_step = input("Enter ID to view thread, "M" for main menu, or [Enter] to quit: ")
## if next_step...
def menu_view_categories():
"""Displays list of categories, takes keyboard input and
executes corresponding functions."""
@ -282,6 +283,9 @@ def menu_view_categories():
if post_id >= 0:
menu_view_thread_details(post_id)
continue
if option == "s":
do_search()
continue
try:
cat_index = categories[int(option) - 1]
menu_view_category_details(cat_index)

37
tests/view_tests.py Normal file
View File

@ -0,0 +1,37 @@
#!/usr/bin/env python3
"""Tests for Linkulator views"""
import unittest
from unittest.mock import patch, call
import linkulator
class TestPrintSearchResults(unittest.TestCase):
"""Tests covering the print_search_results function"""
@patch("builtins.print")
def test_print_search_results(self, mock_print):
test_keyword = "keyword"
test_search_results = [
(66, "keyword", "1576461366.5580268", "", "c", "c", "c"),
(65, "poster6", "1576461367.5580268", "", "keyword", "c", "c"),
(64, "poster7", "1576461368.5580268", "", "c", "keyword", "c"),
(63, "poster8", "1576461369.5580268", "", "c", "c", "keyword"),
]
test_print_calls = [
call(
"\nShowing results for keyword\n\n ID# DATE AUTHOR DESC "
),
call(" 1 2019-12-16 keyword c \n"),
call(" 2 2019-12-16 poster6 c \n"),
call(" 3 2019-12-16 poster7 c \n"),
call(" 4 2019-12-16 poster8 keyword \n"),
]
linkulator.print_search_results(test_keyword, test_search_results)
self.assertEqual(
mock_print.call_count, 5
) # one count for title, 4 for the items)
self.assertListEqual(test_print_calls, mock_print.call_args_list)