summaryrefslogtreecommitdiff
path: root/tests_feedgenerator
diff options
context:
space:
mode:
authorDirk Makowski <dmakowski@gmx.net>2012-08-29 13:03:49 +0200
committerDirk Makowski <dmakowski@gmx.net>2012-08-29 13:03:49 +0200
commitcdfc63bd7908069571528ed62c70fccdd452c93f (patch)
treea812c47ab0e6d378276fa91ab32dc62dc5bc886e /tests_feedgenerator
parent1056f14fd2f19b2e1db78af1bcc04cebe3bb6c8d (diff)
downloadfeedgenerator-cdfc63bd7908069571528ed62c70fccdd452c93f.tar.gz
Renamed tests dir to avoid conflict by UnitTest
Diffstat (limited to 'tests_feedgenerator')
-rw-r--r--tests_feedgenerator/__init__.py0
-rw-r--r--tests_feedgenerator/test_feedgenerator.py90
-rw-r--r--tests_feedgenerator/test_stringio.py89
-rw-r--r--tests_feedgenerator/usage_example.py38
4 files changed, 217 insertions, 0 deletions
diff --git a/tests_feedgenerator/__init__.py b/tests_feedgenerator/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests_feedgenerator/__init__.py
diff --git a/tests_feedgenerator/test_feedgenerator.py b/tests_feedgenerator/test_feedgenerator.py
new file mode 100644
index 0000000..ccbf111
--- /dev/null
+++ b/tests_feedgenerator/test_feedgenerator.py
@@ -0,0 +1,90 @@
+# -*- 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."
+)
+
+
+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>"""
+
+
+ENCODING = 'utf-8'
+
+def build_expected_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
+
+
+class TestFeedGenerator(unittest.TestCase):
+
+ 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():
+ self.assertEqual(type(v), ty)
+ self.assertEqual(type(EXPECTED_RESULT), ty)
+
+ def test_001_string_results(self):
+ 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_result(feed, EXPECTED_RESULT, None)
+ else:
+ # On Python 2, result of feedgenerator is a str string!
+ # Expected_result must therefore encoded likewise.
+ expected_result = build_expected_result(feed, EXPECTED_RESULT, 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.
diff --git a/tests_feedgenerator/test_stringio.py b/tests_feedgenerator/test_stringio.py
new file mode 100644
index 0000000..5ba56a8
--- /dev/null
+++ b/tests_feedgenerator/test_stringio.py
@@ -0,0 +1,89 @@
+# -*- encoding: utf-8 -*-
+
+from __future__ import unicode_literals, print_function
+
+try:
+ import unittest2 as unittest
+except ImportError:
+ import unittest
+
+import six
+from six import StringIO
+
+ENCODING = 'utf-8'
+
+S0 = 'hello world, Umlauts: äöüßÄÖÜ, Chinese: 四是四,十是十,十四是十四,四十是四十,四十四隻不識字之石獅子是死的'
+S0_BYTES = 'fe fi foe fam'.encode(ENCODING)
+
+print("###", StringIO, "###")
+
+class TestStringIO(unittest.TestCase):
+
+ def test_001_text(self):
+ # If we throw unicode into the StringIO buffer, we'll
+ # get unicode out of it.
+ # Thank god this is the same in Python 2 and 3.
+ if six.PY3:
+ self.assertEqual(type(S0), str)
+ else:
+ self.assertEqual(type(S0), unicode)
+ buf = StringIO()
+ print(S0, file=buf, end="")
+ s1 = buf.getvalue()
+ self.assertEqual(type(S0), type(s1))
+ self.assertEqual(S0, s1)
+ if six.PY3:
+ self.assertEqual(type(s1), str)
+ else:
+ self.assertEqual(type(s1), unicode)
+
+ def test_002_bytes(self):
+ buf = StringIO()
+ print(S0_BYTES, file=buf, end="")
+ s1 = buf.getvalue()
+
+ if six.PY3:
+ # In Python 3 StringIO *ALWAYS* returns str (=text=unicode) !
+ # Even if we originally write bytes into the buffer, the value
+ # we get out of it has type str!
+
+ # Input is bytes
+ self.assertEqual(type(S0_BYTES), bytes)
+ # Output is NOT bytes...
+ self.assertNotEqual(type(S0_BYTES), type(s1))
+ self.assertNotEqual(type(s1), bytes)
+ # ...but str!
+ self.assertEqual(type(s1), str)
+ # So the contents are not equal!
+ self.assertNotEqual(S0_BYTES, s1)
+ # StringIO coerced bytes into str:
+ # b'xyz' ---> "b'xyz'"
+ self.assertEqual(str(S0_BYTES), s1)
+ # See, the type info is literally present in the output str!
+ self.assertEqual("b'" + str(S0_BYTES, encoding=ENCODING) + "'", s1)
+ # Coercion is NOT decoding!
+ self.assertNotEqual(S0_BYTES.decode(ENCODING), s1)
+ self.assertNotEqual(str(S0_BYTES, encoding=ENCODING), s1)
+ # These are the same
+ self.assertEqual(S0_BYTES.decode(ENCODING),
+ str(S0_BYTES, encoding=ENCODING))
+ # Additional note:
+ # If we do not specify an encoding when we create a StringIO
+ # buffer, Python 3 automatically uses the locale's preferred
+ # encoding: locale.getpreferredencoding()
+ # Cf. http://docs.python.org/release/3.0.1/library/io.html#io.TextIOWrapper
+ # In my case this is the same encoding as the encoding of this source file,
+ # namely UTF-8. If on your system both encodings are different, you may
+ # encounter other results than the above.
+ #
+ # In Python 3.2 the signature of StringIO() has changed. It is no more
+ # possible to specify an encoding here.
+ else:
+ # In Python 2 StringIO returns the type that we originally
+ # wrote into the buffer.
+ # Here we see that if we write bytes into the buffer, we'll get
+ # bytes out of it.
+ self.assertEqual(type(S0_BYTES), str)
+ self.assertEqual(type(S0_BYTES), type(s1))
+ self.assertEqual(type(s1), str)
+ self.assertEqual(S0_BYTES, s1)
diff --git a/tests_feedgenerator/usage_example.py b/tests_feedgenerator/usage_example.py
new file mode 100644
index 0000000..69f258b
--- /dev/null
+++ b/tests_feedgenerator/usage_example.py
@@ -0,0 +1,38 @@
+# -*- encoding: utf-8 -*-
+
+from __future__ import unicode_literals
+
+import os
+import tempfile
+import six
+import feedgenerator
+
+feed = feedgenerator.Rss201rev2Feed(
+ 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",
+)
+feed.add_item(
+ title="Hello",
+ link="http://www.holovaty.com/test/",
+ description="Testing."
+)
+
+if six.PY3:
+ FN_PREFIX = 'feed_py3-'
+else:
+ FN_PREFIX = 'feed_py2-'
+
+# Usage example in feedgenerator docs opens the file in text mode, not binary.
+# So we do this here likewise.
+fd, filename = tempfile.mkstemp(prefix=FN_PREFIX, suffix='.txt', text=True)
+try:
+ fh = os.fdopen(fd, 'w')
+ feed.write(fh, 'utf-8')
+finally:
+ fh.close()