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 | |
parent | a9f544666f0146110a58c6b46fa60f4eb78f7a46 (diff) | |
parent | 8bcc4d9119ed36c45ed7c2c68ac9aa5ab9c53ec2 (diff) | |
download | feedgenerator-db49b9b1b5bafd59dcf547c2ab66da8f5cc730cd.tar.gz |
Merge pull request #32 from venthur/update_testsmaster
Modernize tests
-rw-r--r-- | MANIFEST.in | 2 | ||||
-rw-r--r-- | setup.cfg | 2 | ||||
-rw-r--r-- | setup.py | 2 | ||||
-rw-r--r-- | tests/__init__.py (renamed from tests_feedgenerator/__init__.py) | 0 | ||||
-rw-r--r-- | tests/test_feedgenerator.py (renamed from tests_feedgenerator/test_feedgenerator.py) | 77 | ||||
-rw-r--r-- | tests/test_stringio.py | 61 | ||||
-rw-r--r-- | tests/usage_example.py (renamed from tests_feedgenerator/usage_example.py) | 0 | ||||
-rw-r--r-- | tests_feedgenerator/test_stringio.py | 64 |
8 files changed, 101 insertions, 107 deletions
diff --git a/MANIFEST.in b/MANIFEST.in index 7fecac1..9720425 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,2 +1,2 @@ include README.rst LICENSE tox.ini -recursive-include tests_feedgenerator * +recursive-include tests * @@ -4,6 +4,6 @@ force_manifest = 1 [tool:pytest] addopts = --cov=feedgenerator - --cov=tests_feedgenerator + --cov=tests --cov-report=html --cov-report=term-missing:skip-covered @@ -36,7 +36,7 @@ MAINTAINER_EMAIL = 'authors@getpelican.com' KEYWORDS = "feed atom rss".split(' ') VERSION = '2.0.0' -TEST_SUITE = 'tests_feedgenerator' +TEST_SUITE = 'tests' REQUIRES = ['pytz >= 0a'] diff --git a/tests_feedgenerator/__init__.py b/tests/__init__.py index e69de29..e69de29 100644 --- a/tests_feedgenerator/__init__.py +++ b/tests/__init__.py diff --git a/tests_feedgenerator/test_feedgenerator.py b/tests/test_feedgenerator.py index fd80237..c8a5b34 100644 --- a/tests_feedgenerator/test_feedgenerator.py +++ b/tests/test_feedgenerator.py @@ -1,10 +1,9 @@ -import unittest - import datetime +import pytest + import feedgenerator -import pytest FIXT_FEED = dict( title="Poynter E-Media Tidbits", @@ -51,6 +50,7 @@ def build_expected_rss_result(feed, expected_result, 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! @@ -61,43 +61,40 @@ def build_expected_atom_result(feed, expected_result, encoding): else: return s -class TestFeedGenerator(unittest.TestCase): - - def setUp(self): - self.maxDiff = None - - def test_000_types(self): - ty = str - 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) - # 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) - self.assertEqual(type(result), type(expected_result)) - self.assertEqual(result, expected_result) - - def test_002_string_results_atom(self): - #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) - self.assertEqual(type(result), type(expected_result)) - self.assertEqual(result, expected_result) + +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", [ 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_feedgenerator/usage_example.py b/tests/usage_example.py index 043f8ce..043f8ce 100644 --- a/tests_feedgenerator/usage_example.py +++ b/tests/usage_example.py diff --git a/tests_feedgenerator/test_stringio.py b/tests_feedgenerator/test_stringio.py deleted file mode 100644 index 3791035..0000000 --- a/tests_feedgenerator/test_stringio.py +++ /dev/null @@ -1,64 +0,0 @@ -import unittest - -from io 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. - self.assertEqual(type(S0), str) - buf = StringIO() - print(S0, file=buf, end="") - s1 = buf.getvalue() - self.assertEqual(type(S0), type(s1)) - self.assertEqual(S0, s1) - self.assertEqual(type(s1), str) - - def test_002_bytes(self): - 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 - 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. |