summaryrefslogtreecommitdiff
path: root/tests/test_feedgenerator.py
blob: 5b8bc42fa0f02bbf72a79da0fbc48394e5aae59b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- encoding: utf-8 -*-

from __future__ import unicode_literals

try:
    import unittest2 as unittest
except ImportError:
    import unittest

import six

import feedgenerator

FIXT_FEED = dict(
    title="Poynter E-Media Tidbits",
    link="http://www.poynter.org/column.asp?id=31",
    description="""A group Weblog by the sharpest minds in online media/journalism/publishing.
    Umlauts: äöüßÄÖÜ
    Chinese: 老师是四十四,是不是?
    Finnish: Mustan kissan paksut posket. (ah, no special chars) Kärpänen sanoi kärpäselle: tuu kattoon kattoon ku kaveri tapettiin tapettiin.
    """,
    language="en"
)
FIXT_ITEM = dict(
    title="Hello",
    link="http://www.holovaty.com/test/",
    description="Testing."
)

# Cannot use unicode chars here. six.b() encodes string to latin-1, which would fail if
# we have unicode chars in these literals.
FIXT_FEED_BYTES = dict(
    title=six.b("Poynter E-Media Tidbits"),
    link=six.b("http://www.poynter.org/column.asp?id=31"),
    description=six.b("A group Weblog by the sharpest minds in online media/journalism/publishing."),
    language=six.b("en")
)
FIXT_ITEM_BYTES = dict(
    title=six.b("Hello"),
    link=six.b("http://www.holovaty.com/test/"),
    description=six.b("Testing.")
)

EXPECTED_RESULT = """<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Poynter E-Media Tidbits</title><link>http://www.poynter.org/column.asp?id=31</link><description>A group Weblog by the sharpest minds in online media/journalism/publishing.
    Umlauts: äöüßÄÖÜ
    Chinese: 老师是四十四,是不是?
    Finnish: Mustan kissan paksut posket. (ah, no special chars) Kärpänen sanoi kärpäselle: tuu kattoon kattoon ku kaveri tapettiin tapettiin.
    </description><language>en</language><lastBuildDate>%DATE%</lastBuildDate><item><title>Hello</title><link>http://www.holovaty.com/test/</link><description>Testing.</description></item></channel></rss>"""

EXPECTED_RESULT_BYTES = six.b("""<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Poynter E-Media Tidbits</title><link>http://www.poynter.org/column.asp?id=31</link><description>A group Weblog by the sharpest minds in online media/journalism/publishing.</description><language>en</language><lastBuildDate>%DATE%</lastBuildDate><item><title>Hello</title><link>http://www.holovaty.com/test/</link><description>Testing.</description></item></channel></rss>""")


ENCODING = 'utf-8'


def build_expected_result(feed, expected_result, encoding):
    d = feedgenerator.rfc2822_date(feed.latest_post_date())
    return expected_result.replace('%DATE%', d)


class TestFeedGenerator(unittest.TestCase):

    def test_001_default_encoding(self):
        # Default encoding is unicode since we have imported unicode_literals!
        feed = feedgenerator.Rss201rev2Feed(**FIXT_FEED)
        feed.add_item(**FIXT_ITEM)
        result = feed.writeString(ENCODING)
        expected_result = build_expected_result(feed, EXPECTED_RESULT, ENCODING)
        self.assertEqual(result, expected_result)

    def OFF_test_002_bytes(self):
        feed = feedgenerator.Rss201rev2Feed(**FIXT_FEED_BYTES)
        feed.add_item(**FIXT_ITEM_BYTES)
        result = feed.writeString(ENCODING)
        expected_result = build_expected_result(feed, EXPECTED_RESULT_BYTES, ENCODING)
        self.assertEqual(result, expected_result)