forked from cmccabe/linkulator2
239 lines
8.3 KiB
Python
239 lines
8.3 KiB
Python
#!/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):
|
|
# """tests that the search results are printed correctly"""
|
|
# 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 "),
|
|
# call(" 2 2019-12-16 poster6 c "),
|
|
# call(" 3 2019-12-16 poster7 c "),
|
|
# call(" 4 2019-12-16 poster8 keyword "),
|
|
# call(""),
|
|
# ]
|
|
#
|
|
# linkulator.print_search_results(test_keyword, test_search_results)
|
|
#
|
|
# self.assertEqual(
|
|
# mock_print.call_count, 6
|
|
# ) # one count for title, 4 for the items and a blank line for formatting
|
|
#
|
|
# self.assertListEqual(test_print_calls, mock_print.call_args_list)
|
|
|
|
|
|
class TestPrintCategories(unittest.TestCase):
|
|
def test_print_categories(self):
|
|
"""Test general output of print_categories"""
|
|
|
|
categories = [
|
|
{
|
|
"name": "category 1",
|
|
"count": 1,
|
|
"last_updated": "10",
|
|
},
|
|
{
|
|
"name": "category 2",
|
|
"count": 2,
|
|
"last_updated": "100",
|
|
},
|
|
{
|
|
"name": "long category name that will be truncated because it's a long line, longer than the terminal width. that's for sure.",
|
|
"count": 20,
|
|
"last_updated": "1000",
|
|
},
|
|
]
|
|
cols = 80
|
|
|
|
expected_header = " ID# New Category"
|
|
|
|
expected_content = [
|
|
" 1 * category 1 (1)",
|
|
" 2 * category 2 (2)",
|
|
" 3 * long category name that will be truncated because it's a long... (20)",
|
|
]
|
|
|
|
actual_header, actual_content = linkulator.print_categories(categories, cols)
|
|
|
|
# confirm expected is equal to actual
|
|
self.assertEqual(expected_header, actual_header)
|
|
self.assertListEqual(expected_content, actual_content)
|
|
|
|
# confirm actual does not exceed cols
|
|
header_max_cols = max([len(line) for line in actual_header])
|
|
self.assertTrue(header_max_cols <= cols)
|
|
content_max_cols = max([len(line) for line in actual_content])
|
|
self.assertTrue(content_max_cols <= cols)
|
|
|
|
def test_empty_categories(self):
|
|
"""Test output when no categories data"""
|
|
empty_categories = []
|
|
cols = 80
|
|
expected_header = ""
|
|
expected_content = [
|
|
"",
|
|
"There are no posts yet - enter p to post a new link",
|
|
]
|
|
actual_header, actual_content = linkulator.print_categories(
|
|
empty_categories, cols
|
|
)
|
|
|
|
# confirm expected is equal to actual
|
|
self.assertEqual(expected_header, actual_header)
|
|
self.assertListEqual(expected_content, actual_content)
|
|
|
|
# confirm actual does not exceed cols
|
|
content_max_cols = max([len(line) for line in actual_content])
|
|
self.assertTrue(content_max_cols <= cols)
|
|
|
|
|
|
class TestPrintLinks(unittest.TestCase):
|
|
def test_print_links(self):
|
|
"""Test general output of print_links"""
|
|
|
|
links = [
|
|
{
|
|
"post_id": 1,
|
|
"link_timestamp": "1627549445.044661",
|
|
"link_author": "auth 1",
|
|
"reply_count": 0,
|
|
"description": "description 1",
|
|
"has_new_replies": False,
|
|
"last_modified_timestamp": "1627549445.044661",
|
|
},
|
|
{
|
|
"post_id": 2,
|
|
"link_timestamp": "1627549445.044661",
|
|
"link_author": "author 2 with a long name",
|
|
"reply_count": 250,
|
|
"description": "a long description for the second post that should wrap i guess",
|
|
"has_new_replies": True,
|
|
"last_modified_timestamp": "1627549445.044661",
|
|
},
|
|
]
|
|
|
|
cols = 80
|
|
expected_header = (
|
|
" ID# Date Author #Repl Description"
|
|
)
|
|
expected_content = [
|
|
" 1 2021-07-29 auth 1 [ 0] description 1",
|
|
" 2 2021-07-29 author 2 [ 25] a long description for the second post...*",
|
|
]
|
|
|
|
actual_header, actual_content = linkulator.print_links(links, cols)
|
|
|
|
# confirm expected is equal to actual
|
|
self.assertEqual(expected_header, actual_header)
|
|
self.assertListEqual(expected_content, actual_content)
|
|
|
|
# confirm actual does not exceed cols
|
|
header_max_cols = max([len(line) for line in actual_header])
|
|
self.assertTrue(header_max_cols <= cols)
|
|
content_max_cols = max([len(line) for line in actual_content])
|
|
self.assertTrue(content_max_cols <= cols)
|
|
|
|
|
|
class TestPrintPost(unittest.TestCase):
|
|
def test_post_without_reply(self):
|
|
"""Test print_post where the post has no reply"""
|
|
|
|
post = {
|
|
"author": "post author 1",
|
|
"category": "test category 1",
|
|
"timestamp": "100",
|
|
"parent_id": "author+timestamp",
|
|
"replies": [],
|
|
"title": "A cool website",
|
|
"url": "http://asdflkjasdf",
|
|
}
|
|
|
|
cols = 80
|
|
expected_content = [
|
|
" Title : A cool website",
|
|
" Link : http://asdflkjasdf",
|
|
" Category : test category 1",
|
|
" User : post author 1",
|
|
" Date : Thu 01 Jan 1970 10:01:40",
|
|
"\n No replies yet. Be the first!",
|
|
]
|
|
|
|
actual_content = linkulator.print_post(post, cols)
|
|
|
|
# confirm expected is equal to actual
|
|
self.assertListEqual(actual_content, expected_content)
|
|
|
|
# confirm actual does not exceed cols
|
|
content_max_cols = max([len(line) for line in actual_content])
|
|
self.assertTrue(content_max_cols <= cols)
|
|
|
|
def test_post_with_reply(self):
|
|
"""Test print_post where the post has a reply"""
|
|
|
|
post = {
|
|
"author": "author2",
|
|
"category": "category 2",
|
|
"timestamp": "1000",
|
|
"parent_id": "xxxxxxxxx",
|
|
"replies": [
|
|
[
|
|
"",
|
|
"reply author 1",
|
|
"1001",
|
|
"",
|
|
"",
|
|
"",
|
|
"a reply",
|
|
],
|
|
[
|
|
"",
|
|
"reply author 2 with a long long long name, a very long name",
|
|
"1002",
|
|
"",
|
|
"",
|
|
"",
|
|
"a reply with a lot of words in it, too many to read, not going to read this",
|
|
],
|
|
],
|
|
"title": "Website 2",
|
|
"url": "asdflkjasdf",
|
|
}
|
|
|
|
cols = 80
|
|
expected_content = [
|
|
" Title : Website 2",
|
|
" Link : asdflkjasdf",
|
|
" Category : category 2",
|
|
" User : author2",
|
|
" Date : Thu 01 Jan 1970 10:16:40",
|
|
"\n Replies:\n",
|
|
" 1970-01-01 10:16 reply author 1: a reply",
|
|
" 1970-01-01 10:16 reply author 2 with a long long long name, a very long name: a reply with a lot of words in it, too many to read, not going to read this",
|
|
]
|
|
|
|
actual_content = linkulator.print_post(post, cols)
|
|
|
|
# confirm expected is equal to actual
|
|
self.assertListEqual(actual_content, expected_content)
|
|
|
|
# confirm actual does not exceed cols
|
|
content_max_cols = max([len(line) for line in actual_content])
|
|
self.assertTrue(content_max_cols <= cols)
|