Added tests for model search functions
This commit is contained in:
parent
e10c919b07
commit
696b594f1e
24
data.py
24
data.py
|
@ -70,13 +70,14 @@ def parse_ignore_file() -> list:
|
|||
return ignore_names
|
||||
|
||||
|
||||
def get_id_from_parent(parent_id: str, link_data: list) -> int:
|
||||
def get_parent_record(parent_id: str, link_data: list) -> list:
|
||||
"""given a parent ID, return the ID for the parent record or -1"""
|
||||
assert parent_id != ""
|
||||
if parent_id == "":
|
||||
raise ValueError("parent_id cannot be empty")
|
||||
for record in link_data:
|
||||
if record[0] == parent_id.partition("+")[2]:
|
||||
return record[0]
|
||||
return -1
|
||||
return record
|
||||
raise KeyError("there's no parent record for the specified parent_id")
|
||||
|
||||
|
||||
class LinkData:
|
||||
|
@ -186,7 +187,8 @@ class LinkData:
|
|||
def search(self, keyword: str) -> list:
|
||||
"""returns a unique list of link_data record IDs for posts that contain
|
||||
the specified keyword"""
|
||||
assert keyword != ""
|
||||
if keyword == "":
|
||||
raise ValueError("a search keyword must be specified")
|
||||
|
||||
query = (record for record in self.link_data if keyword in record)
|
||||
|
||||
|
@ -196,10 +198,12 @@ class LinkData:
|
|||
post_id = record[0]
|
||||
parent_id = record[3]
|
||||
if post_id:
|
||||
search_results.add(record[0])
|
||||
search_results.add(tuple(record))
|
||||
else:
|
||||
parent_post_id = get_id_from_parent(parent_id, self.link_data)
|
||||
if parent_post_id >= 0:
|
||||
search_results.add(parent_post_id)
|
||||
try:
|
||||
parent_record = get_parent_record(parent_id, self.link_data)
|
||||
except KeyError:
|
||||
continue
|
||||
search_results.add(tuple(parent_record))
|
||||
|
||||
return sorted(search_results)
|
||||
return sorted(search_results, key=lambda x: x[0], reverse=True)
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
"""unit tests for the data module"""
|
||||
import unittest
|
||||
import unittest.mock
|
||||
from time import time
|
||||
import data
|
||||
|
||||
|
||||
class TestDataHelperFunctions(unittest.TestCase):
|
||||
"""Tests that cover helper functions, mostly handling string validation"""
|
||||
|
||||
def test_wash_line(self):
|
||||
"""tests the data.wash_line function"""
|
||||
teststrings = [
|
||||
|
@ -104,6 +106,108 @@ class TestDataHelperFunctions(unittest.TestCase):
|
|||
data.process(item, "username{}".format(i)), teststrings_output[i]
|
||||
)
|
||||
|
||||
def test_get_parent_record(self):
|
||||
test_link_data = []
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
data.get_parent_record("", test_link_data)
|
||||
|
||||
|
||||
class TestLinkDataSearch(unittest.TestCase):
|
||||
@unittest.mock.patch.object(data.LinkData, "get")
|
||||
def test_search_exceptions(self, mock_get):
|
||||
link_data = data.LinkData()
|
||||
mock_get.assert_called()
|
||||
with self.assertRaises(ValueError):
|
||||
link_data.search("")
|
||||
|
||||
@unittest.mock.patch.object(data.LinkData, "get")
|
||||
def test_search(self, mock_get):
|
||||
link_data = data.LinkData()
|
||||
mock_get.assert_called()
|
||||
|
||||
link_data.link_data = [
|
||||
[
|
||||
"",
|
||||
"user1",
|
||||
"1576486443.8539028",
|
||||
"poster1+1576289662.7914467",
|
||||
"",
|
||||
"",
|
||||
"this comment contains a keyword regarding the keyword website",
|
||||
],
|
||||
[
|
||||
"",
|
||||
"user2",
|
||||
"1576486440.65404",
|
||||
"poster1+1576289662.7914467",
|
||||
"",
|
||||
"",
|
||||
"this is a reply to the site but does not contain the word",
|
||||
],
|
||||
[
|
||||
70,
|
||||
"poster1",
|
||||
"1576289662.7914467",
|
||||
"",
|
||||
"keyword",
|
||||
"http://keyword.com",
|
||||
"the keyword website",
|
||||
],
|
||||
[
|
||||
69,
|
||||
"poster2",
|
||||
"1576462584.2307518",
|
||||
"",
|
||||
"keyword",
|
||||
"gemini://keyword",
|
||||
"keyword site with no replies",
|
||||
],
|
||||
[68, "poster3", "1576462007.9509487", "", "a", "a", "key word"],
|
||||
[67, "poster4", "1576461366.5580268", "", "b", "b", "key.word"],
|
||||
[
|
||||
"",
|
||||
"user3",
|
||||
"1576376868.284987",
|
||||
"poster3+1576376644.2783155",
|
||||
"",
|
||||
"",
|
||||
"this is an orphaned reply but it contains the keyword",
|
||||
],
|
||||
[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"],
|
||||
[63, "poster9", "1576461370.5580268", "", "c", "c", "z8keyworddui3"],
|
||||
]
|
||||
|
||||
test_results = [
|
||||
(
|
||||
70,
|
||||
"poster1",
|
||||
"1576289662.7914467",
|
||||
"",
|
||||
"keyword",
|
||||
"http://keyword.com",
|
||||
"the keyword website",
|
||||
),
|
||||
(
|
||||
69,
|
||||
"poster2",
|
||||
"1576462584.2307518",
|
||||
"",
|
||||
"keyword",
|
||||
"gemini://keyword",
|
||||
"keyword site with no replies",
|
||||
),
|
||||
(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"),
|
||||
]
|
||||
|
||||
self.assertEqual(link_data.search("keyword"), test_results)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
Loading…
Reference in New Issue