Corrected functionality, identified and fixed search bug
This commit is contained in:
parent
4760777e07
commit
29e7e3addd
62
data.py
62
data.py
|
@ -75,7 +75,8 @@ def get_parent_record(parent_id: str, link_data: list) -> list:
|
||||||
if parent_id == "":
|
if parent_id == "":
|
||||||
raise ValueError("parent_id cannot be empty")
|
raise ValueError("parent_id cannot be empty")
|
||||||
for record in link_data:
|
for record in link_data:
|
||||||
if record[0] == parent_id.partition("+")[2]:
|
timestamp = record[2]
|
||||||
|
if timestamp == parent_id.partition("+")[2]:
|
||||||
return record
|
return record
|
||||||
raise KeyError("there's no parent record for the specified parent_id")
|
raise KeyError("there's no parent record for the specified parent_id")
|
||||||
|
|
||||||
|
@ -167,47 +168,44 @@ class LinkData:
|
||||||
new_post_id = 1
|
new_post_id = 1
|
||||||
record = record._replace(ID_if_parent=new_post_id)
|
record = record._replace(ID_if_parent=new_post_id)
|
||||||
self.link_data.insert(0, list(record))
|
self.link_data.insert(0, list(record))
|
||||||
self.generate_category_data()
|
|
||||||
else:
|
else:
|
||||||
self.link_data.insert(0, list(record))
|
self.link_data.insert(0, list(record))
|
||||||
|
self.generate_category_data()
|
||||||
return new_post_id
|
return new_post_id
|
||||||
|
|
||||||
def generate_category_data(self):
|
def generate_category_data(self):
|
||||||
"""generate categories list and category count from sorted link data"""
|
"""generate categories list and category count from sorted link data"""
|
||||||
self.categories.clear()
|
self.categories.clear()
|
||||||
i = (record for record in self.link_data if record[4] != "")
|
for record in self.link_data:
|
||||||
for record in i:
|
|
||||||
name = record[4]
|
name = record[4]
|
||||||
timestamp = record[2]
|
timestamp = record[2]
|
||||||
if name not in [cat_record["name"] for cat_record in self.categories]:
|
if name != "":
|
||||||
self.categories.append(
|
if name not in [cat_record["name"] for cat_record in self.categories]:
|
||||||
{"name": name, "count": 1, "last_updated": timestamp}
|
self.categories.append(
|
||||||
)
|
{"name": name, "count": 1, "last_updated": timestamp}
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
for cat_record in self.categories:
|
||||||
|
if cat_record["name"] == name:
|
||||||
|
cat_record["count"] += 1
|
||||||
|
if cat_record["last_updated"] < timestamp:
|
||||||
|
cat_record["last_updated"] = timestamp
|
||||||
else:
|
else:
|
||||||
for cat_record in self.categories:
|
parent_id = record[3]
|
||||||
if cat_record["name"] == name:
|
try:
|
||||||
cat_record["count"] += 1
|
parent_record = get_parent_record(parent_id, self.link_data)
|
||||||
if cat_record["last_updated"] < timestamp:
|
except KeyError:
|
||||||
cat_record["last_updated"] = timestamp
|
continue
|
||||||
|
parent_cat_name = parent_record[4]
|
||||||
for record in self.link_data:
|
if parent_cat_name not in [cat_record["name"] for cat_record in self.categories]:
|
||||||
if record[4] == "":
|
self.categories.append(
|
||||||
parent_id = self.get_parent_record(record[3])
|
{"name": parent_cat_name, "count": 1, "last_updated": timestamp}
|
||||||
timestamp = record[2]
|
)
|
||||||
if parent_id != -1:
|
else:
|
||||||
for line in self.link_data:
|
for cat_record in self.categories:
|
||||||
if line[0] == parent_id:
|
if cat_record["name"] == parent_cat_name:
|
||||||
name = record[4]
|
if cat_record["last_updated"] < timestamp:
|
||||||
for cat_record in self.categories:
|
cat_record["last_updated"] = timestamp
|
||||||
if cat_record["name"] == name:
|
|
||||||
if cat_record["last_updated"] < timestamp:
|
|
||||||
cat_record["last_updated"] = timestamp
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def search(self, keyword: str) -> list:
|
def search(self, keyword: str) -> list:
|
||||||
"""returns a unique list of link_data records for posts that contain
|
"""returns a unique list of link_data records for posts that contain
|
||||||
|
|
|
@ -108,10 +108,48 @@ class TestDataHelperFunctions(unittest.TestCase):
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_get_parent_record(self):
|
def test_get_parent_record(self):
|
||||||
|
# test that an empty parent_id throws a value error
|
||||||
test_link_data = []
|
test_link_data = []
|
||||||
|
parent_id = ""
|
||||||
|
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
data.get_parent_record("", test_link_data)
|
data.get_parent_record(parent_id, test_link_data)
|
||||||
|
|
||||||
|
# confirm the function returns a valid parent record
|
||||||
|
test_parent_record = [
|
||||||
|
1,
|
||||||
|
"testuser",
|
||||||
|
"1000",
|
||||||
|
"",
|
||||||
|
"test_category",
|
||||||
|
"test_url",
|
||||||
|
"test_title",
|
||||||
|
]
|
||||||
|
test_link_data = (
|
||||||
|
[
|
||||||
|
[
|
||||||
|
1,
|
||||||
|
"testuser",
|
||||||
|
"1000",
|
||||||
|
"",
|
||||||
|
"test_category",
|
||||||
|
"test_url",
|
||||||
|
"test_title",
|
||||||
|
]
|
||||||
|
],
|
||||||
|
)
|
||||||
|
parent_id = 1000
|
||||||
|
|
||||||
|
self.assertListEqual(
|
||||||
|
test_parent_record, data.get_parent_record(parent_id, test_link_data)
|
||||||
|
)
|
||||||
|
|
||||||
|
# confirm key error raised if no results found
|
||||||
|
|
||||||
|
parent_id = 69
|
||||||
|
|
||||||
|
with self.assertRaises(KeyError):
|
||||||
|
data.get_parent_record(parent_id, test_link_data)
|
||||||
|
|
||||||
|
|
||||||
class TestLinkDataSearch(unittest.TestCase):
|
class TestLinkDataSearch(unittest.TestCase):
|
||||||
|
@ -177,12 +215,22 @@ class TestLinkDataSearch(unittest.TestCase):
|
||||||
"",
|
"",
|
||||||
"this is an orphaned reply but it contains the keyword",
|
"this is an orphaned reply but it contains the keyword",
|
||||||
],
|
],
|
||||||
[66, "keyword", "1576461366.5580268", "", "c", "c", "c"],
|
[66, "keyword", "1576461366.5580261", "", "c", "c", "c"],
|
||||||
[65, "poster6", "1576461367.5580268", "", "keyword", "c", "c"],
|
[65, "poster6", "1576461367.5580268", "", "keyword", "c", "c"],
|
||||||
[64, "poster7", "1576461368.5580268", "", "c", "keyword", "c"],
|
[64, "poster7", "1576461368.5580268", "", "c", "keyword", "c"],
|
||||||
[63, "poster8", "1576461369.5580268", "", "c", "c", "keyword"],
|
[63, "poster8", "1576461369.5580268", "", "c", "c", "keyword"],
|
||||||
[62, "poster9", "1576461370.5580268", "", "c", "c", "ssskeywordsubstring"],
|
[62, "poster9", "1576461370.5580268", "", "c", "c", "ssskeywordsubstring"],
|
||||||
[61, "poster0", "1576461370.5580268", "", "c", "c", "KEYWORD capital"],
|
[61, "poster0", "1576461371.5580268", "", "c", "c", "KEYWORD capital"],
|
||||||
|
[60, "poste99", "1576461372.5580268", "", "c", "c", "c"],
|
||||||
|
[
|
||||||
|
"",
|
||||||
|
"user99",
|
||||||
|
"1576461372.6680268",
|
||||||
|
"poste99+1576461372.5580268",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"the post doesn't contain the keyword but this reply does",
|
||||||
|
],
|
||||||
]
|
]
|
||||||
|
|
||||||
test_results = [
|
test_results = [
|
||||||
|
@ -204,12 +252,13 @@ class TestLinkDataSearch(unittest.TestCase):
|
||||||
"gemini://keyword",
|
"gemini://keyword",
|
||||||
"keyword site with no replies",
|
"keyword site with no replies",
|
||||||
),
|
),
|
||||||
(66, "keyword", "1576461366.5580268", "", "c", "c", "c"),
|
(66, "keyword", "1576461366.5580261", "", "c", "c", "c"),
|
||||||
(65, "poster6", "1576461367.5580268", "", "keyword", "c", "c"),
|
(65, "poster6", "1576461367.5580268", "", "keyword", "c", "c"),
|
||||||
(64, "poster7", "1576461368.5580268", "", "c", "keyword", "c"),
|
(64, "poster7", "1576461368.5580268", "", "c", "keyword", "c"),
|
||||||
(63, "poster8", "1576461369.5580268", "", "c", "c", "keyword"),
|
(63, "poster8", "1576461369.5580268", "", "c", "c", "keyword"),
|
||||||
(62, "poster9", "1576461370.5580268", "", "c", "c", "ssskeywordsubstring"),
|
(62, "poster9", "1576461370.5580268", "", "c", "c", "ssskeywordsubstring"),
|
||||||
(61, "poster0", "1576461370.5580268", "", "c", "c", "KEYWORD capital"),
|
(61, "poster0", "1576461371.5580268", "", "c", "c", "KEYWORD capital"),
|
||||||
|
(60, "poste99", "1576461372.5580268", "", "c", "c", "c"),
|
||||||
]
|
]
|
||||||
|
|
||||||
self.assertEqual(link_data.search("keyword"), test_results)
|
self.assertEqual(link_data.search("keyword"), test_results)
|
||||||
|
|
Loading…
Reference in New Issue