diff options
| author | Justin Mayer <entroP@gmail.com> | 2021-11-03 11:06:42 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-03 11:06:42 -0500 |
| commit | db49b9b1b5bafd59dcf547c2ab66da8f5cc730cd (patch) | |
| tree | 0d23c52f7a8cacbea6ceb91b4ef4ba2d12005cd8 /tests | |
| parent | a9f544666f0146110a58c6b46fa60f4eb78f7a46 (diff) | |
| parent | 8bcc4d9119ed36c45ed7c2c68ac9aa5ab9c53ec2 (diff) | |
| download | feedgenerator-master.tar.gz | |
Merge pull request #32 from venthur/update_testsmaster
Modernize tests
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/__init__.py | 0 | ||||
| -rw-r--r-- | tests/test_feedgenerator.py | 137 | ||||
| -rw-r--r-- | tests/test_stringio.py | 61 | ||||
| -rw-r--r-- | tests/usage_example.py | 30 |
4 files changed, 228 insertions, 0 deletions
diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/__init__.py diff --git a/tests/test_feedgenerator.py b/tests/test_feedgenerator.py new file mode 100644 index 0000000..c8a5b34 --- /dev/null +++ b/tests/test_feedgenerator.py @@ -0,0 +1,137 @@ +import datetime + +import pytest + +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><subtitle>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. + </subtitle><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 + + +def test_000_types(): + for k, v in FIXT_FEED.items(): + assert isinstance(v, str) + for k, v in FIXT_ITEM.items(): + if k == "pubdate" or k == "updateddate": + assert isinstance(v, datetime.datetime) + else: + assert isinstance(v, str) + assert isinstance(EXPECTED_RESULT_RSS, str) + + +def test_001_string_results_rss(): + #import ipdb; ipdb.set_trace() + feed = feedgenerator.Rss201rev2Feed(**FIXT_FEED) + feed.add_item(**FIXT_ITEM) + result = feed.writeString(ENCODING) + # 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) + assert isinstance(result, type(expected_result)) + assert result == expected_result + + +def test_002_string_results_atom(): + #import ipdb; ipdb.set_trace() + feed = feedgenerator.Atom1Feed(**FIXT_FEED) + feed.add_item(**FIXT_ITEM) + result = feed.writeString(ENCODING) + # 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) + assert isinstance(result, type(expected_result)) + assert result == expected_result + + +@pytest.mark.parametrize("description, subtitle, fragment, nonfragment", [ + # Neither description nor subtitle are provided + (None, None, None, "<subtitle></subtitle>"), + ("", "", None, "<subtitle></subtitle>"), + # Description is provided + ("description", None, "<subtitle>description</subtitle>", None), + ("description", "", "<subtitle>description</subtitle>", None), + # Subtitle is provided + (None, "subtitle", "<subtitle>subtitle</subtitle>", None), + ("", "subtitle", "<subtitle>subtitle</subtitle>", None), + # Both description & subtitle are provided; subtitle takes precedence + ("description", "subtitle", "<subtitle>subtitle</subtitle>", "<subtitle>description</subtitle>"), +]) +def test_subtitle(description, subtitle, fragment, nonfragment): + """Test regression for https://github.com/getpelican/feedgenerator/issues/30. + + We test against all four possible combinations of description x + subtitle parameters and additionally for None and "". + + description, subtitle are the values for the respective + feed-parameters. + + fragment and nonfragment are text fragments that should be in the + expected result or not. + + """ + FIXT_FEED = dict( + title="title", + link="https://example.com", + description=description, + subtitle=subtitle, + ) + feed = feedgenerator.Atom1Feed(**FIXT_FEED) + result = feed.writeString(ENCODING) + if fragment: + assert fragment in result + if nonfragment: + assert nonfragment not in result diff --git a/tests/test_stringio.py b/tests/test_stringio.py new file mode 100644 index 0000000..89e784f --- /dev/null +++ b/tests/test_stringio.py @@ -0,0 +1,61 @@ +from io import StringIO + +ENCODING = 'utf-8' + +S0 = 'hello world, Umlauts: äöüßÄÖÜ, Chinese: 四是四,十是十,十四是十四,四十是四十,四十四隻不識字之石獅子是死的' +S0_BYTES = 'fe fi foe fam'.encode(ENCODING) + +#print("###", StringIO, "###") + + +def test_001_text(): + # If we throw unicode into the StringIO buffer, we'll + # get unicode out of it. + assert isinstance(S0, str) + buf = StringIO() + print(S0, file=buf, end="") + s1 = buf.getvalue() + assert isinstance(S0, type(s1)) + assert S0 == s1 + assert isinstance(s1, str) + + +def test_002_bytes(): + buf = StringIO() + print(S0_BYTES, file=buf, end="") + s1 = buf.getvalue() + + # 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 + assert isinstance(S0_BYTES, bytes) + # Output is NOT bytes... + assert not isinstance(S0_BYTES, type(s1)) + assert not isinstance(s1, bytes) + # ...but str! + assert isinstance(s1, str) + # So the contents are not equal! + assert S0_BYTES != s1 + # StringIO coerced bytes into str: + # b'xyz' ---> "b'xyz'" + assert str(S0_BYTES) == s1 + # See, the type info is literally present in the output str! + assert "b'" + str(S0_BYTES, encoding=ENCODING) + "'" == s1 + # Coercion is NOT decoding! + assert S0_BYTES.decode(ENCODING) != s1 + assert str(S0_BYTES, encoding=ENCODING) != s1 + # These are the same + assert 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. diff --git a/tests/usage_example.py b/tests/usage_example.py new file mode 100644 index 0000000..043f8ce --- /dev/null +++ b/tests/usage_example.py @@ -0,0 +1,30 @@ +import os +import tempfile +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." +) + +FN_PREFIX = 'feed_py3-' + +# 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() |
