summaryrefslogtreecommitdiff
path: root/tests_feedgenerator/test_feedgenerator.py
blob: 95fa0cd13d6daa6ebdcb34b6663637d62a02c71d (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- encoding: utf-8 -*-

from __future__ import unicode_literals

try:
    import unittest2 as unittest
except ImportError:
    import unittest

import six
import datetime

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.",
    content="Full content of our testing entry.",
    pubdate=datetime.datetime(2016,8,11,0,0,0,0),
)


EXPECTED_RESULT_RSS = """<?xml version="1.0" encoding="utf-8"?>
<rss 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><pubDate>Thu, 11 Aug 2016 00:00:00 -0000</pubDate></item></channel></rss>"""

EXPECTED_RESULT_ATOM = """<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Poynter E-Media Tidbits</title><link href="http://www.poynter.org/column.asp?id=31" rel="alternate"></link><id>http://www.poynter.org/column.asp?id=31</id><updated>%DATE%</updated><entry><title>Hello</title><link href="http://www.holovaty.com/test/" rel="alternate"></link><published>2016-08-11T00:00:00Z</published><updated>2016-08-11T00:00:00Z</updated><id>tag:www.holovaty.com,2016-08-11:/test/</id><summary type="html">Testing.</summary><content type="html">Full content of our testing entry.</content></entry></feed>"""

ENCODING = 'utf-8'

def build_expected_rss_result(feed, expected_result, encoding):
    # Result's date is of course different from the date in the fixture.
    # So make them equal!
    d = feedgenerator.rfc2822_date(feed.latest_post_date())
    s = expected_result.replace('%DATE%', d)
    if encoding:
        return s.encode(encoding)
    else:
        return s

def build_expected_atom_result(feed, expected_result, encoding):
    # Result's date is of course different from the date in the fixture.
    # So make them equal!
    d = feedgenerator.rfc3339_date(feed.latest_post_date())
    s = expected_result.replace('%DATE%', d)
    if encoding:
        return s.encode(encoding)
    else:
        return s

class TestFeedGenerator(unittest.TestCase):

    def setUp(self):
        self.maxDiff = None

    def test_000_types(self):
        ty = str if six.PY3 else unicode
        for k, v in FIXT_FEED.items():
            self.assertEqual(type(v), ty)
        for k, v in FIXT_ITEM.items():
            if k == "pubdate" or k == "updateddate":
                self.assertEqual(type(v), datetime.datetime)
            else:
                self.assertEqual(type(v), ty)
        self.assertEqual(type(EXPECTED_RESULT_RSS), ty)

    def test_001_string_results_rss(self):
        #import ipdb; ipdb.set_trace()
        feed = feedgenerator.Rss201rev2Feed(**FIXT_FEED)
        feed.add_item(**FIXT_ITEM)
        result = feed.writeString(ENCODING)
        if six.PY3:
            # On Python 3, result of feedgenerator is a unicode string!
            # So do not encode our expected_result.
            expected_result = build_expected_rss_result(feed, EXPECTED_RESULT_RSS, None)
        else:
            # On Python 2, result of feedgenerator is a str string!
            # Expected_result must therefore encoded likewise.
            expected_result = build_expected_rss_result(feed, EXPECTED_RESULT_RSS, ENCODING)
        # The different result types of Python 2 (str=bytes) and Python 3
        # (str=text=unicode) stems from a different implementation of StringIO.
        # As I understand it, getvalue() in Python 2 returns the type you
        # originally wrote into the buffer. In Python 3 getvalue() always
        # returns a str (=text=unicode).
        # See other test: test_stringio.py
        #print type(result), type(expected_result)
        self.assertEqual(type(result), type(expected_result))
        self.assertEqual(result, expected_result)

    def OFF_test_002_file_results(self):
        pass
        # DO-IT_YOURSELF: Run usage_example with python2 and python3.
        #                 Each will create a feed file.
        #                 Compare the files, they must be equal!
        #                 XXX  Argh -- No, the lastBuildDate will differ.
        #                              But this is allowed.

    def test_003_string_results_atom(self):
        #import ipdb; ipdb.set_trace()
        feed = feedgenerator.Atom1Feed(**FIXT_FEED)
        feed.add_item(**FIXT_ITEM)
        result = feed.writeString(ENCODING)
        if six.PY3:
            # On Python 3, result of feedgenerator is a unicode string!
            # So do not encode our expected_result.
            expected_result = build_expected_atom_result(feed, EXPECTED_RESULT_ATOM, None)
        else:
            # On Python 2, result of feedgenerator is a str string!
            # Expected_result must therefore encoded likewise.
            expected_result = build_expected_atom_result(feed, EXPECTED_RESULT_ATOM, ENCODING)
        # The different result types of Python 2 (str=bytes) and Python 3
        # (str=text=unicode) stems from a different implementation of StringIO.
        # As I understand it, getvalue() in Python 2 returns the type you
        # originally wrote into the buffer. In Python 3 getvalue() always
        # returns a str (=text=unicode).
        # See other test: test_stringio.py
        #print type(result), type(expected_result)
        self.assertEqual(type(result), type(expected_result))
        self.assertEqual(result, expected_result)