51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
import sys
|
|
import unittest
|
|
import xml.etree.ElementTree as ET
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
|
|
from merge import FeedItem, build_rss, sort_and_limit
|
|
|
|
|
|
def item(title, link, hour, description=""):
|
|
return FeedItem(
|
|
title=title,
|
|
link=link,
|
|
guid=link,
|
|
published=datetime(2026, 1, 1, hour, tzinfo=timezone.utc),
|
|
description=description,
|
|
source_title="Test Feed",
|
|
source_url="https://example.com/feed.xml",
|
|
)
|
|
|
|
|
|
class MergeTests(unittest.TestCase):
|
|
def test_sorts_keeps_duplicates_and_limits(self):
|
|
values = [
|
|
item("Older", "https://example.com/old", 1),
|
|
item("Newest", "https://example.com/new", 3),
|
|
item("Duplicate title", "https://example.com/new#comments", 2),
|
|
]
|
|
merged = sort_and_limit(values, 2)
|
|
self.assertEqual([value.title for value in merged], ["Newest", "Duplicate title"])
|
|
|
|
def test_builds_valid_rss(self):
|
|
group = {
|
|
"title": "Merged",
|
|
"sources": ["https://example.com/feed.xml"],
|
|
"self_url": "https://example.com/all.xml",
|
|
}
|
|
document = build_rss("all", group, [item("Hello", "https://example.com/hello", 1, "<p>Body</p>")])
|
|
root = ET.fromstring(document)
|
|
self.assertEqual(root.tag, "rss")
|
|
self.assertEqual(root.findtext("channel/title"), "Merged")
|
|
self.assertEqual(root.findtext("channel/item/title"), "Hello")
|
|
self.assertEqual(root.findtext("channel/item/description"), "<p>Body</p>")
|
|
self.assertEqual(root.find("channel/item/source").get("url"), "https://example.com/feed.xml")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|