Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions tests/fixtures/minimal_valid_result.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<twinglydata numberOfMatchesReturned="1" secondsElapsed="0.148" numberOfMatchesTotal="1">
<post contentType="blog">
<url>http://oppogner.blogg.no/1409602010_bare_m_ha.html</url>
<title><![CDATA[Bare MÅ ha!]]></title>
<summary><![CDATA[Ja, velkommen til høsten ...]]></summary>
<languageCode>no</languageCode>
<published>2014-09-02 06:53:26Z</published>
<indexed>2014-09-02 09:00:53Z</indexed>
<blogUrl>http://oppogner.blogg.no/</blogUrl>
<blogName><![CDATA[oppogner]]></blogName>
<authority>1</authority>
<blogRank>1</blogRank>
<tags>
<tag><![CDATA[Blogg]]></tag>
</tags>
</post><post contentType="blog">
<url>http://www.skvallernytt.se/hardtraning-da-galler-swedish-house-mafia</url>
<title><![CDATA[Hårdträning – då gäller Swedish House Mafia]]></title>
<summary><![CDATA[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]]></summary>
<languageCode>sv</languageCode>
<published>2013-01-29 15:21:56Z</published>
<indexed>2013-01-29 15:22:52Z</indexed>
<blogUrl>http://www.skvallernytt.se/</blogUrl>
<blogName><![CDATA[Skvallernytt.se]]></blogName>
<authority>38</authority>
<blogRank>4</blogRank>
<tags>
<tag><![CDATA[Okategoriserat]]></tag>
<tag><![CDATA[Träning]]></tag>
<tag><![CDATA[greyhound]]></tag>
<tag><![CDATA[koncept]]></tag>
<tag><![CDATA[mafia]]></tag>
</tags>
</post><post contentType="blog">
<url>http://didriksinspesielleverden.blogg.no/1359472349_justin_bieber.html</url>
<title><![CDATA[Justin Bieber]]></title>
<summary><![CDATA[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]]></summary>
<languageCode>no</languageCode>
<published>2013-01-29 15:12:29Z</published>
<indexed>2013-01-29 15:14:37Z</indexed>
<blogUrl>http://didriksinspesielleverden.blogg.no/</blogUrl>
<blogName><![CDATA[Didriksinspesielleverden]]></blogName>
<authority>0</authority>
<blogRank>1</blogRank>
</post>
</twinglydata>
123 changes: 104 additions & 19 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,138 @@
# coding: utf-8

from __future__ import unicode_literals
import unittest

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 assert_blog_posts_equal(self, actual_post, expected_post):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about implementing __eq__ on post instead. Seems to be pretty straight-foward:

def __eq__(self, other):
        return (isinstance(other, self.__class__)
            and self.__dict__ == other.__dict__)

See: http://stackoverflow.com/questions/390250/elegant-ways-to-support-equivalence-equality-in-python-classes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about implementing eq on post instead.

TIL about __dict__ :)

One disadvantage is that it becomes impossible to find out whats wrong by looking at the test output:

AssertionError: 'Skvallernytt.se' != u'FEL'

# becomes:

AssertionError: <twingly_search.post.Post object at 0x10a8b76d0> != <twingly_search.post.Post object at 0x10a8b75d0>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true.

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 = 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()
if hasattr(data, 'decode'):
data = data.decode("utf-8")
data = self.get_fixture("valid_non_blog_result")
r = twingly_search.Parser().parse(data)
self.assertIsInstance(r, twingly_search.Result)
self.assertEqual(len(r.posts), 1)

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()
if hasattr(data, 'decode'):
data = data.decode("utf-8")
data = self.get_fixture("nonexistent_api_key_result")
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()
if hasattr(data, 'decode'):
data = data.decode("utf-8")
data = self.get_fixture("unauthorized_api_key_result")
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()
if hasattr(data, 'decode'):
data = data.decode("utf-8")
data = self.get_fixture("service_unavailable_result")
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()
if hasattr(data, 'decode'):
data = data.decode("utf-8")
data = self.get_fixture("undefined_error_result")
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()
if hasattr(data, 'decode'):
data = data.decode("utf-8")
data = self.get_fixture("non_xml_result")
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)
2 changes: 1 addition & 1 deletion twingly_search/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down