Skip to content
This repository has been archived by the owner on May 30, 2020. It is now read-only.

Commit

Permalink
Add tests to fully cover initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikOssipoff committed Jun 13, 2015
1 parent 84a297f commit 0f868d7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
__versionstr__ = '.'.join([str(v) for v in VERSION])

install_requires = ['beautifulsoup4>=4.3', 'requests>=2.7']
test_require = install_requires + ['coverage>=3.7', 'nose>=1.3']
test_require = install_requires + ['coverage>=3.7', 'nose>=1.3', 'responses']

setup(
name='python-opengraph',
Expand Down
31 changes: 31 additions & 0 deletions tests/test_opengraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
from unittest import TestCase

from opengraph import OpenGraph
from threading import local
import responses

data = local()


class TestOpenGraph(TestCase):
Expand All @@ -14,6 +18,15 @@ def setUp(self): # NOQA
</head><body></body></html>
"""

@responses.activate
def test_loading_from_url(self):
url = 'http://foo.bar.com/'
responses.add(
responses.GET, url, body=self.test_document,
status=200, content_type='text/html')
og = OpenGraph(url=url)
self.assertEqual(og.title, 'Test title')

def test_get_attr(self):
og = OpenGraph(html=self.test_document)
self.assertEqual(og.title, 'Test title')
Expand All @@ -28,3 +41,21 @@ def test_str_repr(self):
og = OpenGraph(html=self.test_document)
text_of_data = og.__data__.__str__()
self.assertEqual(str(og), text_of_data)

@responses.activate
def test_loading_from_url(self):
def http_callback(request):
# Ugly, but using thread locals in order to capture the request
# headers in the callback, to assert that it's being set correctly
data.headers = request.headers
return (200, {'content-type': 'text/html'}, self.test_document)

url = 'http://foo.bar.com/'
useragent = 'python-opengraph/0.0'
responses.add_callback(
responses.GET, url, callback=http_callback,
content_type='text/html')
og = OpenGraph(url=url, useragent=useragent)
headers = data.headers
self.assertEqual(og.title, 'Test title')
self.assertEqual(headers['user-agent'], useragent)

0 comments on commit 0f868d7

Please sign in to comment.