1
0
mirror of https://github.com/xgi/castero synced 2024-11-10 15:28:45 +01:00

add tests for subscriptions model

This commit is contained in:
Jake Robertson 2019-06-09 19:22:17 -04:00
parent 790b4151b5
commit 9c10626a91
No known key found for this signature in database
GPG Key ID: 15FF1380B7D5C715
8 changed files with 165 additions and 0 deletions

@ -48,6 +48,8 @@ class Subscriptions():
SubscriptionsParseError: unable to parse text as an XML document
SubscriptionsLoadError: an exception occurred when attempting to
load the file
SubscriptionsStructureError: the file data is not a properly
structured OPML document
"""
self._tree = None
try:
@ -118,10 +120,24 @@ class Subscriptions():
def _parse_feeds(self) -> None:
"""Parse the XML tree into a list of feeds.
Raises:
SubscriptionsStructureError: the file data is not a properly
structured OPML document
"""
error_msg = "The file data is not a properly structured OPML document"
if self._tree is None:
raise SubscriptionsStructureError(error_msg)
body = self._tree.find('body')
if body is None:
raise SubscriptionsStructureError(error_msg)
container = body.find('outline')
if container is None:
raise SubscriptionsStructureError(error_msg)
entries = container.findall('outline')
if entries is None:
raise SubscriptionsStructureError(error_msg)
self._feeds = []
for entry in entries:

@ -0,0 +1,3 @@
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<opml version="1.0">
</opml>

@ -0,0 +1,10 @@
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<opml version="1.0">
<head>
<title>my feeds</title>
</head>
<body>
<outline type="rss" text="feed1" xmlUrl="http://feed1" />
<outline type="rss" text="feed2" xmlUrl="http://feed2" />
</body>
</opml>

@ -0,0 +1,12 @@
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<opml version="1.0">
<head>
<title>my feeds</title>
</head>
<body>
<outline text="feeds">
<outline type="rss" text="feed1" xmlUrl="http://feed1" />
<outline type="rss" text="feed2" xmlUrl="http://feed2" />
</outline>
</body>
</bad>

@ -0,0 +1,12 @@
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<opml version="1.0">
<head>
<title>my feeds</title>
</head>
<body>
<outline text="feeds">
<outline type="rss" text="feed1" xmlUrl="http://feed1" />
<outline type="rss" text="feed2" xmlUrl="http://feed2" />
</outline>
</body>
</opml>

@ -0,0 +1,7 @@
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<opml version="1.0">
<body>
<outline text="feeds">
</outline>
</body>
</opml>

@ -0,0 +1,9 @@
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<opml version="1.0">
<body>
<outline text="feeds">
<outline type="rss" text="feed1" xmlUrl="http://feed1" />
<outline type="rss" text="feed2" xmlUrl="http://feed2" />
</outline>
</body>
</opml>

@ -0,0 +1,96 @@
import os
from unittest import mock
import xml.etree.ElementTree as ElementTree
import pytest
from castero.feed import Feed
from castero.subscriptions import Subscriptions, SubscriptionsLoadError, \
SubscriptionsParseError, SubscriptionsStructureError, SubscriptionsError
my_dir = os.path.dirname(os.path.realpath(__file__))
def test_subscriptions_valid_complete():
mysubscriptions = Subscriptions()
Feed.__init__ = mock.MagicMock(return_value=None)
mysubscriptions.load(my_dir + "/subscriptions/valid_complete.xml")
assert isinstance(mysubscriptions, Subscriptions)
Feed.__init__.assert_any_call(url="http://feed1")
Feed.__init__.assert_any_call(url="http://feed2")
assert Feed.__init__.call_count == 2
assert len(mysubscriptions.feeds) == 2
def test_subscriptions_valid_no_head():
mysubscriptions = Subscriptions()
Feed.__init__ = mock.MagicMock(return_value=None)
mysubscriptions.load(my_dir + "/subscriptions/valid_no_head.xml")
assert isinstance(mysubscriptions, Subscriptions)
Feed.__init__.assert_any_call(url="http://feed1")
Feed.__init__.assert_any_call(url="http://feed2")
assert Feed.__init__.call_count == 2
assert len(mysubscriptions.feeds) == 2
def test_subscriptions_valid_minimal():
mysubscriptions = Subscriptions()
Feed.__init__ = mock.MagicMock(return_value=None)
mysubscriptions.load(my_dir + "/subscriptions/valid_minimal.xml")
assert isinstance(mysubscriptions, Subscriptions)
assert len(mysubscriptions.feeds) == 0
def test_subscriptions_broken_nonexistant():
mysubscriptions = Subscriptions()
Feed.__init__ = mock.MagicMock(return_value=None)
with pytest.raises(SubscriptionsLoadError):
mysubscriptions.load(my_dir + "/subscriptions/doesnt_exist")
def test_subscriptions_broken_parse():
mysubscriptions = Subscriptions()
Feed.__init__ = mock.MagicMock(return_value=None)
with pytest.raises(SubscriptionsParseError):
mysubscriptions.load(my_dir + "/subscriptions/broken_parse.xml")
def test_subscriptions_broken_no_body():
mysubscriptions = Subscriptions()
Feed.__init__ = mock.MagicMock(return_value=None)
with pytest.raises(SubscriptionsStructureError):
mysubscriptions.load(my_dir + "/subscriptions/broken_no_body.xml")
def test_subscriptions_generate():
feed1 = mock.MagicMock()
feed2 = mock.MagicMock()
mysubscriptions = Subscriptions()
mysubscriptions.generate([feed1, feed2])
Feed.__init__ = mock.MagicMock(return_value=None)
mysubscriptions._parse_feeds()
assert len(mysubscriptions.feeds) == 2
def test_subscriptions_save():
temp_fname = my_dir + "/subscriptions/saved_temp.xml"
Feed.__init__ = mock.MagicMock(return_value=None)
mysubscriptions1 = Subscriptions()
mysubscriptions1.load(my_dir + "/subscriptions/valid_complete.xml")
mysubscriptions1.save(temp_fname)
mysubscriptions2 = Subscriptions()
mysubscriptions2.load(my_dir + "/subscriptions/saved_temp.xml")
os.remove(temp_fname)
tree1 = ElementTree.tostring(mysubscriptions1._tree.getroot())
tree2 = ElementTree.tostring(mysubscriptions2._tree.getroot())
assert tree1 == tree2
def test_subscriptions_save_before_create():
mysubscriptions = Subscriptions()
with pytest.raises(SubscriptionsError):
mysubscriptions.save(my_dir + "/subscriptions/saved_bad_temp.xml")