diff options
author | Dirk Makowski <dmakowski@gmx.net> | 2012-08-26 23:07:51 +0200 |
---|---|---|
committer | Dirk Makowski <dmakowski@gmx.net> | 2012-08-26 23:07:51 +0200 |
commit | 4de4e021ee82111c300f18582caafe420a0f49fe (patch) | |
tree | d7c0d96b53380c6ac521e77e30d13923e6c8038f /tests | |
parent | 6d4d097ede94cd5c7d8bac542827870a1104a709 (diff) | |
download | feedgenerator-4de4e021ee82111c300f18582caafe420a0f49fe.tar.gz |
Improved tests
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_feedgenerator.py | 67 | ||||
-rw-r--r-- | tests/test_stringio.py | 89 | ||||
-rw-r--r-- | tests/usage_example.py | 22 |
3 files changed, 149 insertions, 29 deletions
diff --git a/tests/test_feedgenerator.py b/tests/test_feedgenerator.py index 5b8bc42..dbc90b3 100644 --- a/tests/test_feedgenerator.py +++ b/tests/test_feedgenerator.py @@ -27,19 +27,6 @@ FIXT_ITEM = dict( 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. @@ -48,31 +35,57 @@ EXPECTED_RESULT = """<?xml version="1.0" encoding="utf-8"?> 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): + # 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()) - return expected_result.replace('%DATE%', d) + s = expected_result.replace('%DATE%', d) + if encoding: + return s.encode(encoding) + else: + return s class TestFeedGenerator(unittest.TestCase): - def test_001_default_encoding(self): - # Default encoding is unicode since we have imported unicode_literals! + 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) - expected_result = build_expected_result(feed, EXPECTED_RESULT, 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) + # XXX Is the following resoning true? XXX + # 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_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) + 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/test_stringio.py b/tests/test_stringio.py new file mode 100644 index 0000000..5ba56a8 --- /dev/null +++ b/tests/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/usage_example.py b/tests/usage_example.py index e074148..69f258b 100644 --- a/tests/usage_example.py +++ b/tests/usage_example.py @@ -1,5 +1,12 @@ # -*- 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", @@ -15,6 +22,17 @@ feed.add_item( link="http://www.holovaty.com/test/", description="Testing." ) -with open('test.rss', 'w') as fp: - feed.write(fp, 'utf-8') +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() |