From f57b45afb2c80850ec669c80e0cb08cd52e6d6ac Mon Sep 17 00:00:00 2001 From: Mattias Roback Date: Thu, 18 Feb 2016 13:07:42 +0100 Subject: [PATCH 1/4] Add fixture helper to ParserTest DRY --- tests/test_parser.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/tests/test_parser.py b/tests/test_parser.py index 570bedf..3aa4a13 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -4,13 +4,18 @@ import twingly_search class ParserTest(unittest.TestCase): + def get_fixture(self, fixture_name): + file_path = "./tests/fixtures/%s.xml" % fixture_name + fixture = open(file_path, 'r').read() + return fixture + def test_with_valid_result(self): - data = open("./tests/fixtures/valid_result.xml", 'r').read() + data = self.get_fixture("valid_result") r = twingly_search.Parser().parse(data) self.assertIsInstance(r, twingly_search.Result) def test_with_valid_result_containing_non_blogs(self): - data = open("./tests/fixtures/valid_non_blog_result.xml", 'r').read() + data = self.get_fixture("valid_non_blog_result") if hasattr(data, 'decode'): data = data.decode("utf-8") r = twingly_search.Parser().parse(data) @@ -19,35 +24,35 @@ def test_with_valid_result_containing_non_blogs(self): def test_with_nonexistent_api_key_result(self): with self.assertRaises(twingly_search.TwinglyAuthException): - data = open("./tests/fixtures/nonexistent_api_key_result.xml", 'r').read() + data = self.get_fixture("nonexistent_api_key_result") if hasattr(data, 'decode'): data = data.decode("utf-8") r = twingly_search.Parser().parse(data) def test_with_unauthorized_api_key_result(self): with self.assertRaises(twingly_search.TwinglyAuthException): - data = open("./tests/fixtures/unauthorized_api_key_result.xml", 'r').read() + data = self.get_fixture("unauthorized_api_key_result") if hasattr(data, 'decode'): data = data.decode("utf-8") r = twingly_search.Parser().parse(data) def test_with_service_unavailable_result(self): with self.assertRaises(twingly_search.TwinglyServerException): - data = open("./tests/fixtures/service_unavailable_result.xml", 'r').read() + data = self.get_fixture("service_unavailable_result") if hasattr(data, 'decode'): data = data.decode("utf-8") r = twingly_search.Parser().parse(data) def test_with_undefined_error_result(self): with self.assertRaises(twingly_search.TwinglyServerException): - data = open("./tests/fixtures/undefined_error_result.xml", 'r').read() + data = self.get_fixture("undefined_error_result") if hasattr(data, 'decode'): data = data.decode("utf-8") r = twingly_search.Parser().parse(data) def test_with_undefined_error_result(self): with self.assertRaises(twingly_search.TwinglyServerException): - data = open("./tests/fixtures/non_xml_result.xml", 'r').read() + data = self.get_fixture("non_xml_result") if hasattr(data, 'decode'): data = data.decode("utf-8") r = twingly_search.Parser().parse(data) From 54c832817df495790be9789f252630f2f79d38f3 Mon Sep 17 00:00:00 2001 From: Mattias Roback Date: Thu, 18 Feb 2016 14:01:46 +0100 Subject: [PATCH 2/4] Add tests for parser close #17 --- tests/fixtures/minimal_valid_result.xml | 52 ++++++++++++++ tests/test_parser.py | 92 +++++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 tests/fixtures/minimal_valid_result.xml diff --git a/tests/fixtures/minimal_valid_result.xml b/tests/fixtures/minimal_valid_result.xml new file mode 100644 index 0000000..95d5382 --- /dev/null +++ b/tests/fixtures/minimal_valid_result.xml @@ -0,0 +1,52 @@ + + + + http://oppogner.blogg.no/1409602010_bare_m_ha.html + <![CDATA[Bare MÅ ha!]]> + + no + 2014-09-02 06:53:26Z + 2014-09-02 09:00:53Z + http://oppogner.blogg.no/ + + 1 + 1 + + + + + http://www.skvallernytt.se/hardtraning-da-galler-swedish-house-mafia + <![CDATA[Hårdträning – då gäller Swedish House Mafia]]> + + sv + 2013-01-29 15:21:56Z + 2013-01-29 15:22:52Z + http://www.skvallernytt.se/ + + 38 + 4 + + + + + + + + + http://didriksinspesielleverden.blogg.no/1359472349_justin_bieber.html + <![CDATA[Justin Bieber]]> + + no + 2013-01-29 15:12:29Z + 2013-01-29 15:14:37Z + http://didriksinspesielleverden.blogg.no/ + + 0 + 1 + + diff --git a/tests/test_parser.py b/tests/test_parser.py index 3aa4a13..c34e814 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -1,3 +1,5 @@ +# coding: utf-8 + from __future__ import unicode_literals import unittest @@ -9,6 +11,19 @@ def get_fixture(self, fixture_name): fixture = open(file_path, 'r').read() return fixture + def assert_blog_posts_equal(self, actual_post, expected_post): + self.assertEqual(actual_post.url, expected_post.url) + self.assertEqual(actual_post.title, expected_post.title) + self.assertEqual(actual_post.summary, expected_post.summary) + self.assertEqual(actual_post.language_code, expected_post.language_code) + self.assertEqual(actual_post.published, expected_post.published) + self.assertEqual(actual_post.indexed, expected_post.indexed) + self.assertEqual(actual_post.blog_url, expected_post.blog_url) + self.assertEqual(actual_post.blog_name, expected_post.blog_name) + self.assertEqual(actual_post.blog_rank, expected_post.blog_rank) + self.assertEqual(actual_post.authority, expected_post.authority) + self.assertEqual(actual_post.tags, expected_post.tags) + def test_with_valid_result(self): data = self.get_fixture("valid_result") r = twingly_search.Parser().parse(data) @@ -56,3 +71,80 @@ def test_with_undefined_error_result(self): if hasattr(data, 'decode'): data = data.decode("utf-8") r = twingly_search.Parser().parse(data) + + def test_with_post_containing_one_tag(self): + fixture = self.get_fixture("minimal_valid_result") + result = twingly_search.Parser().parse(fixture) + + expected_values = { + "url": "http://oppogner.blogg.no/1409602010_bare_m_ha.html", + "title": "Bare MÅ ha!", + "summary": "Ja, velkommen til høsten ...", + "languageCode": "no", + "published": "2014-09-02 06:53:26Z", + "indexed": "2014-09-02 09:00:53Z", + "blogUrl": "http://oppogner.blogg.no/", + "blogName": "oppogner", + "authority": "1", + "blogRank": "1", + "tags": ["Blogg"], + } + + expected_post = twingly_search.Post() + expected_post.set_values(expected_values) + actual_post = result.posts[0] + + self.assert_blog_posts_equal(actual_post, expected_post) + + def test_with_post_containing_multiple_tags(self): + fixture = self.get_fixture("minimal_valid_result") + result = twingly_search.Parser().parse(fixture) + + expected_values = { + "url": "http://www.skvallernytt.se/hardtraning-da-galler-swedish-house-mafia", + "title": "Hårdträning – då gäller Swedish House Mafia", + "summary": """Träning. Och Swedish House Mafia. Det verkar vara ett lyckat koncept. "Don't you worry child" och "Greyhound" är nämligen de två mest spelade träningslåtarna under januari 2013 på Spotify. + +Relaterade inlägg: +Swedish House Mafia – ny låt! +Ny knivattack på Swedish House Mafia-konsert +Swedish House Mafia gör succé i USA""", + "languageCode": "sv", + "published": "2013-01-29 15:21:56Z", + "indexed": "2013-01-29 15:22:52Z", + "blogUrl": "http://www.skvallernytt.se/", + "blogName": "Skvallernytt.se", + "authority": "38", + "blogRank": "4", + "tags": ["Okategoriserat", "Träning", "greyhound", "koncept", "mafia"], + } + + expected_post = twingly_search.Post() + expected_post.set_values(expected_values) + actual_post = result.posts[1] + + self.assert_blog_posts_equal(actual_post, expected_post) + + def test_with_post_containing_no_tags(self): + fixture = self.get_fixture("minimal_valid_result") + result = twingly_search.Parser().parse(fixture) + + expected_values = { + "url": "http://didriksinspesielleverden.blogg.no/1359472349_justin_bieber.html", + "title": "Justin Bieber", + "summary": """OMG! Justin Bieber Believe acoustic albumet er nå ute på spotify. Han er helt super. Love him. Personlig liker jeg best beauty and a beat og as long as you love me, kommenter gjerne hva dere synes! <3 #sus YOLO""", + "languageCode": "no", + "published": "2013-01-29 15:12:29Z", + "indexed": "2013-01-29 15:14:37Z", + "blogUrl": "http://didriksinspesielleverden.blogg.no/", + "blogName": "Didriksinspesielleverden", + "authority": "0", + "blogRank": "1", + "tags": [], + } + + expected_post = twingly_search.Post() + expected_post.set_values(expected_values) + actual_post = result.posts[2] + + self.assert_blog_posts_equal(actual_post, expected_post) From cd91be8af3c287b5270a60a1f9b744107a6095f9 Mon Sep 17 00:00:00 2001 From: Mattias Roback Date: Thu, 18 Feb 2016 14:03:20 +0100 Subject: [PATCH 3/4] Parse tags correctly Make the tests in the previous commit pass. fix #18 --- twingly_search/parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/twingly_search/parser.py b/twingly_search/parser.py index 2e4eb6f..e43b09b 100644 --- a/twingly_search/parser.py +++ b/twingly_search/parser.py @@ -63,7 +63,7 @@ def _parse_post(self, element): def _parse_tags(self, element): tags = [] - for tag in element.find('tag'): + for tag in element.findall('tag'): tags.append(tag.text) return tags From fe8407021d982cef853b68e022b7bf12ebe1d5c8 Mon Sep 17 00:00:00 2001 From: Mattias Roback Date: Thu, 18 Feb 2016 14:23:45 +0100 Subject: [PATCH 4/4] Remove unused code in Parser tests At least I think its not needed. It works in python 2.7 without them. Lets see what Travis has to say about it :). --- tests/test_parser.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/test_parser.py b/tests/test_parser.py index c34e814..0cc3c1d 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -31,8 +31,6 @@ def test_with_valid_result(self): def test_with_valid_result_containing_non_blogs(self): data = self.get_fixture("valid_non_blog_result") - if hasattr(data, 'decode'): - data = data.decode("utf-8") r = twingly_search.Parser().parse(data) self.assertIsInstance(r, twingly_search.Result) self.assertEqual(len(r.posts), 1) @@ -40,36 +38,26 @@ def test_with_valid_result_containing_non_blogs(self): def test_with_nonexistent_api_key_result(self): with self.assertRaises(twingly_search.TwinglyAuthException): data = self.get_fixture("nonexistent_api_key_result") - if hasattr(data, 'decode'): - data = data.decode("utf-8") r = twingly_search.Parser().parse(data) def test_with_unauthorized_api_key_result(self): with self.assertRaises(twingly_search.TwinglyAuthException): data = self.get_fixture("unauthorized_api_key_result") - if hasattr(data, 'decode'): - data = data.decode("utf-8") r = twingly_search.Parser().parse(data) def test_with_service_unavailable_result(self): with self.assertRaises(twingly_search.TwinglyServerException): data = self.get_fixture("service_unavailable_result") - if hasattr(data, 'decode'): - data = data.decode("utf-8") r = twingly_search.Parser().parse(data) def test_with_undefined_error_result(self): with self.assertRaises(twingly_search.TwinglyServerException): data = self.get_fixture("undefined_error_result") - if hasattr(data, 'decode'): - data = data.decode("utf-8") r = twingly_search.Parser().parse(data) def test_with_undefined_error_result(self): with self.assertRaises(twingly_search.TwinglyServerException): data = self.get_fixture("non_xml_result") - if hasattr(data, 'decode'): - data = data.decode("utf-8") r = twingly_search.Parser().parse(data) def test_with_post_containing_one_tag(self):