test: add tests for enclosure object

This commit is contained in:
Dmitriy Pleshevskiy 2019-07-26 19:14:19 +03:00
parent 3a7708a3f9
commit 933154a5a1
5 changed files with 67 additions and 5 deletions

View file

@ -5,7 +5,7 @@ from datetime import datetime
import pytz
__all__ = ('GenRSS', 'Enclosure',)
__all__ = ('GenRSS', 'Item', 'Enclosure')
ElementT = TypeVar('ElementT')
RSS_DEFAULT_GENERATOR = f'Generated by genrss for python'
@ -116,10 +116,8 @@ class Item:
for category in self.categories:
item.append(create_element('category', CDATA(category)))
if self.enclosure:
item.append(self.enclosure.to_element())
if self.pub_date:
item.append(create_element('pubDate', self.pub_date))

23
tests/conftest.py Normal file
View file

@ -0,0 +1,23 @@
import pytest
IMAGE_URL = 'http://s3.smartfridge.me/image.jpg'
@pytest.fixture(params=[
pytest.param((IMAGE_URL, None, None), id='+/-/-'),
pytest.param((IMAGE_URL, 1000, None), id='+/+/-'),
pytest.param((IMAGE_URL, 1000, 'image/png'), id='+/+/+'),
pytest.param((IMAGE_URL, None, 'image/png'), id='+/-/+'),
])
def enclosure_tuple(request):
return request.param
@pytest.fixture(params=[
pytest.param(dict(url=IMAGE_URL), id='+/-/-'),
pytest.param(dict(url=IMAGE_URL, size=1000), id='+/+/-'),
pytest.param(dict(url=IMAGE_URL, size=1000, type='image/png'), id='+/+/+'),
pytest.param(dict(url=IMAGE_URL, type='image/png'), id='+/-/+'),
])
def enclosure_dict(request):
return request.param

23
tests/test_enclosure.py Normal file
View file

@ -0,0 +1,23 @@
import pytest
from genrss import Enclosure
def test_init_fails():
with pytest.raises(TypeError):
Enclosure()
assert False
def test_init(enclosure_tuple):
url, size, type = enclosure_tuple
enclosure = Enclosure(url, size, type)
assert enclosure.url == url
assert enclosure.size == (size or 0)
assert enclosure.type == (type or 'image/jpeg')
def test_init_from_dict(enclosure_dict):
enclosure = Enclosure.from_dict(enclosure_dict)
assert enclosure.url == enclosure_dict.get('url')
assert enclosure.size == enclosure_dict.get('size', 0)
assert enclosure.type == enclosure_dict.get('type', 'image/jpeg')

View file

@ -3,7 +3,7 @@ from textwrap import dedent
import pytz
import pytest
from genrss import RSS_DEFAULT_GENERATOR
from genrss import RSS_DEFAULT_GENERATOR, Item
from tests.support import create_rss
@ -109,3 +109,11 @@ def test_feed_categories():
assert xml
assert '<category><![CDATA[Category 1]]></category>' \
'<category><![CDATA[Category 2]]></category>' in xml
def test_feed_bad_items():
feed = create_rss(items=['item'])
xml = feed.xml()
assert xml
assert '<item>' not in xml

View file

@ -94,3 +94,13 @@ def test_item_enclosure(feed):
assert xml
assert '<enclosure length="{}" type="{}" url="{}"/>' \
'</item>'.format(0, 'image/jpeg', enclosure.url) in xml
def test_item_enclosure_from_dict(feed, enclosure_dict):
create_item(feed, enclosure=enclosure_dict)
xml = feed.xml()
assert xml
assert '<enclosure length="{}" type="{}" url="{}"/>' \
'</item>'.format(enclosure_dict.get('size', 0),
enclosure_dict.get('type', 'image/jpeg'),
enclosure_dict.get('url')) in xml