Skip to content

Commit

Permalink
grangier#188 - tests refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavier Grangier committed Dec 31, 2014
1 parent 49f50b0 commit 6009d44
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 112 deletions.
82 changes: 0 additions & 82 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,85 +20,3 @@
See the License for the specific language governing permissions and
limitations under the License.
"""

import urllib2
import unittest
import socket

from StringIO import StringIO


# Response
class MockResponse():
"""\
Base mock response class
"""
code = 200
msg = "OK"

def __init__(self, cls):
self.cls = cls

def content(self):
return "response"

def response(self, req):
data = self.content(req)
url = req.get_full_url()
resp = urllib2.addinfourl(StringIO(data), data, url)
resp.code = self.code
resp.msg = self.msg
return resp


class MockHTTPHandler(urllib2.HTTPHandler, urllib2.HTTPSHandler):
"""\
Mocked HTTPHandler in order to query APIs locally
"""
cls = None

def https_open(self, req):
return self.http_open(req)

def http_open(self, req):
r = self.cls.callback(self.cls)
return r.response(req)

@staticmethod
def patch(cls):
opener = urllib2.build_opener(MockHTTPHandler)
urllib2.install_opener(opener)
# dirty !
for h in opener.handlers:
if isinstance(h, MockHTTPHandler):
h.cls = cls
return [h for h in opener.handlers if isinstance(h, MockHTTPHandler)][0]

@staticmethod
def unpatch():
# urllib2
urllib2._opener = None


class BaseMockTests(unittest.TestCase):
"""\
Base Mock test case
"""
callback = MockResponse

def setUp(self):
# patch DNS
self.original_getaddrinfo = socket.getaddrinfo
socket.getaddrinfo = self.new_getaddrinfo
MockHTTPHandler.patch(self)

def tearDown(self):
MockHTTPHandler.unpatch()
# DNS
socket.getaddrinfo = self.original_getaddrinfo

def new_getaddrinfo(self, *args):
return [(2, 1, 6, '', ('127.0.0.1', 0))]

def _get_current_testname(self):
return self.id().split('.')[-1:][0]
32 changes: 17 additions & 15 deletions tests/extractors/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,34 @@
import os
import json

from base import BaseMockTests, MockResponse
from base import BaseMockTests
from base import MockResponseExtractors

from goose import Goose
from goose.utils import FileHelper
from goose.configuration import Configuration
from goose.text import StopWordsChinese
from goose.text import StopWordsArabic
from goose.text import StopWordsKorean
from goose.utils import FileHelper


CURRENT_PATH = os.path.dirname(os.path.abspath(__file__))


class MockResponseExtractors(MockResponse):
def content(self, req):
current_test = self.cls._get_current_testname()
path = os.path.join(CURRENT_PATH, "data", "extractors", "%s.html" % current_test)
path = os.path.abspath(path)
content = FileHelper.loadResourceFile(path)
return content


class TestExtractionBase(BaseMockTests):
"""\
Extraction test case
"""
callback = MockResponseExtractors

def getRawHtml(self):
suite, module, cls, func = self.id().split('.')
path = os.path.join(CURRENT_PATH, "data", module, "%s.html" % func)
test, suite, module, cls, func = self.id().split('.')
path = os.path.join(
os.path.dirname(CURRENT_PATH),
"data",
suite,
module,
"%s.html" % func)
path = os.path.abspath(path)
content = FileHelper.loadResourceFile(path)
return content
Expand All @@ -62,8 +59,13 @@ def loadData(self):
"""\
"""
suite, module, cls, func = self.id().split('.')
path = os.path.join(CURRENT_PATH, "data", module, "%s.json" % func)
test, suite, module, cls, func = self.id().split('.')
path = os.path.join(
os.path.dirname(CURRENT_PATH),
"data",
suite,
module,
"%s.json" % func)
path = os.path.abspath(path)
content = FileHelper.loadResourceFile(path)
self.data = json.loads(content)
Expand Down
2 changes: 1 addition & 1 deletion tests/extractors/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import unittest

from base import MockResponse
from extractors import TestExtractionBase
from base import TestExtractionBase

from goose.configuration import Configuration
from goose.image import Image
Expand Down
22 changes: 8 additions & 14 deletions tests/extractors/videos.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
limitations under the License.
"""
import os
import json

from .base import MockResponse
from .extractors import TestExtractionBase
from base import MockResponse
from base import TestExtractionBase

from goose.utils import FileHelper

Expand All @@ -34,7 +33,12 @@
class MockResponseVideos(MockResponse):
def content(self, req):
current_test = self.cls._get_current_testname()
path = os.path.join(CURRENT_PATH, "data", "videos", "%s.html" % current_test)
path = os.path.join(
os.path.dirname(CURRENT_PATH),
"data",
"extractors",
"videos",
"%s.html" % current_test)
path = os.path.abspath(path)
content = FileHelper.loadResourceFile(path)
return content
Expand All @@ -59,16 +63,6 @@ def assert_movies(self, field, expected_value, result_value):
r = getattr(video, k)
self.assertEqual(r, v)

def loadData(self):
"""\
"""
suite, module, cls, func = self.id().split('.')
path = os.path.join(CURRENT_PATH, "data", module, "%s.json" % func)
path = os.path.abspath(path)
content = FileHelper.loadResourceFile(path)
self.data = json.loads(content)

def test_embed(self):
article = self.getArticle()
fields = ['movies']
Expand Down

0 comments on commit 6009d44

Please sign in to comment.