Skip to content

Commit

Permalink
Merge pull request #825 from jfunez/626_completar_tests_balalio
Browse files Browse the repository at this point in the history
626 completar tests balalio
  • Loading branch information
gustavofonseca committed Jun 6, 2014
2 parents a051c6a + f600710 commit 6af08e1
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 4 deletions.
11 changes: 8 additions & 3 deletions scielomanager/articletrack/balaio.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,21 @@ def list_files_members_by_attempt(self, attempt_id):
return self._process_response_as_json(response)

def get_file_member_by_attempt(self, attempt_id, target_name, file_member):
url = self.get_fullpath() + 'files/%s/%s.zip/?file=%s' % (attempt_id, target_name, file_member)
if '&file=' not in file_member: # single file_member
qs = {'file': file_member}
else: # a list of params &files=A&files=B...
list_of_members = file_member.split('&file=') # ['file=A', file=B, ]
qs = [('file', param) for param in list_of_members] # [('file', 'A'), ('file', 'B'), ]
qs = urlencode(qs)
url = self.get_fullpath() + 'files/%s/%s.zip?%s' % (attempt_id, target_name, qs)
return self._open(url)

def get_files_members_by_attempt(self, attempt_id, target_name, files_members):
files_members = '&file='.join(files_members)
return self.get_file_member_by_attempt(attempt_id, target_name, files_members)

def get_full_package(self, attempt_id, target_name):
url = self.get_fullpath() + 'files/%s/%s.zip/?full=true' % (attempt_id, target_name)
url = self.get_fullpath() + 'files/%s/%s.zip?full=true' % (attempt_id, target_name)
return self._open(url)

def get_xml_uri(self, attempt_id, target_name):
Expand Down Expand Up @@ -151,4 +157,3 @@ def call(self, method, args=()):
rpc_server = self.get_server(uri)

return getattr(rpc_server, method)(*args)

21 changes: 21 additions & 0 deletions scielomanager/articletrack/tests/doubles.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
# -*- coding: utf-8 -*-
from os import path
from articletrack.balaio import BalaioAPI
from django.conf import settings
from urllib2 import urlopen


def make_expected_generator(file_uri):
# function to return a generator with the expected result
response = urlopen(file_uri, timeout=settings.API_BALAIO_DEFAULT_TIMEOUT)
while True:
response_chunk = response.read(settings.API_BALAIO_DEFAULT_CHUNK_SIZE)
if response_chunk:
yield response_chunk
else:
raise StopIteration()


class BalaioAPIDouble(BalaioAPI):
Expand All @@ -26,3 +39,11 @@ def list_files_members_by_attempt(self, attempt_id):
def get_xml_uri(self, attempt_id, target_name):
tests_xmls_dirs = path.abspath(path.join(path.dirname(__file__), 'xml_tests_files'))
return "file://%s" % path.join(tests_xmls_dirs, "valid.xml")


class BalaioAPIDoubleDisabled(BalaioAPI):
def is_up(self):
return False

def list_files_members_by_attempt(self, attempt_id):
raise ValueError
142 changes: 142 additions & 0 deletions scielomanager/articletrack/tests/tests_balaio_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import json
import urlparse
from urllib import urlencode
from os import path
from urllib2 import urlopen
from itertools import izip_longest

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
import mocker

from articletrack.balaio import BalaioAPI, BalaioRPC, SettingsMixin
from . import doubles


class BalaioCheckSettingsTests(unittest.TestCase):
Expand Down Expand Up @@ -176,6 +180,144 @@ def test_list_files_unexistent_attempt_raise_error(self):
expected_json_response
)

def test_get_file_member_by_attempt(self):
"""
Test that the method ``get_file_member_by_attempt`` returns a generator by chunks
Test that the method ``get_file_member_by_attempt`` request the correct URL
Test that the query string is url_encoded
"""
balaio_api = BalaioAPI()
attempt_id = '25'
target_name = '0034-8910-rsp-48-2-0206'
file_member = '%s.xml' % target_name
expected_filename = '%s.zip' % file_member
zip_tests_files_dir = path.abspath(path.join(
path.dirname(__file__),
'zip_tests_files'
))
zip_tests_file_abs_path = path.abspath(path.join(zip_tests_files_dir, expected_filename))
expected_file_uri = 'file://%s' % zip_tests_file_abs_path

qs = {'file': file_member}
qs = urlencode(qs)
remote_url = balaio_api.get_fullpath() + 'files/%s/%s.zip?%s' % (attempt_id, target_name, qs)

# Override Balaio's method
class BalaioTest(doubles.BalaioAPIDouble):

def _open(self, url):
# to avoid the request, override de result of _open to return a local
# zip file as a generator, same behavior as expected
if url != remote_url:
raise AssertionError('The requested URL is different to the expected')
return doubles.make_expected_generator(expected_file_uri)

# get the results
balaio_test = BalaioTest()

balaio_mock = self.mocker.proxy(balaio_test)
balaio_mock.get_file_member_by_attempt(attempt_id, target_name, file_member)
self.mocker.passthrough()

self.mocker.replay()

balaio_response = balaio_mock.get_file_member_by_attempt(attempt_id, target_name, file_member)
expected_response = doubles.make_expected_generator(expected_file_uri)

# compare the 2 responses - http://stackoverflow.com/a/9983596/1503
compare_equal = all(a == b for a, b in izip_longest(balaio_response, expected_response))
self.assertTrue(compare_equal)

def test_get_files_members_by_attempt(self):
"""
Test that the method ``get_files_members_by_attempt`` returns a generator by chunks
Test that the method ``get_files_members_by_attempt`` request the correct URL
Test that the query string is url_encoded
"""
balaio_api = BalaioAPI()
attempt_id = '25'
target_name1 = 'foo'
target_name2 = 'bar'
target_name = 'foo_bar'
file_members = ['foo.xml', 'bar.xml']
expected_filename = '%s.zip' % target_name
zip_tests_files_dir = path.abspath(path.join(
path.dirname(__file__),
'zip_tests_files'
))
zip_tests_file_abs_path = path.abspath(path.join(zip_tests_files_dir, expected_filename))
expected_file_uri = 'file://%s' % zip_tests_file_abs_path

qs = [('file', member) for member in file_members]
qs = urlencode(qs)
remote_url = balaio_api.get_fullpath() + 'files/%s/%s.zip?%s' % (attempt_id, target_name, qs)

# Override Balaio's method
class BalaioTest(doubles.BalaioAPIDouble):

def _open(self, url):
# to avoid the request, override de result of _open to return a local
# zip file as a generator, same behavior as expected
if url != remote_url:
raise AssertionError('The requested URL is different to the expected')
return doubles.make_expected_generator(expected_file_uri)

# get the results
balaio_test = BalaioTest()

balaio_mock = self.mocker.proxy(balaio_test)
balaio_mock.get_files_members_by_attempt(attempt_id, target_name, file_members)
self.mocker.passthrough()

self.mocker.replay()

balaio_response = balaio_mock.get_files_members_by_attempt(attempt_id, target_name, file_members)
expected_response = doubles.make_expected_generator(expected_file_uri)

# compare the 2 responses - http://stackoverflow.com/a/9983596/1503
compare_equal = all(a == b for a, b in izip_longest(balaio_response, expected_response))
self.assertTrue(compare_equal)

def test_get_full_package(self):
balaio_api = BalaioAPI()
attempt_id = '25'
target_name = 'test_full_package_file'
expected_filename = 'full_package.zip'
zip_tests_files_dir = path.abspath(path.join(
path.dirname(__file__),
'zip_tests_files'
))
zip_tests_file_abs_path = path.abspath(path.join(zip_tests_files_dir, expected_filename))
expected_file_uri = 'file://%s' % zip_tests_file_abs_path

remote_url = balaio_api.get_fullpath() + 'files/%s/%s.zip?full=true' % (attempt_id, target_name)

# Override Balaio's method
class BalaioTest(doubles.BalaioAPIDouble):

def _open(self, url):
# to avoid the request, override de result of _open to return a local
# zip file as a generator, same behavior as expected
if url != remote_url:
raise AssertionError('The requested URL is different to the expected')
return doubles.make_expected_generator(expected_file_uri)

# get the results
balaio_test = BalaioTest()

balaio_mock = self.mocker.proxy(balaio_test)
balaio_mock.get_full_package(attempt_id, target_name)
self.mocker.passthrough()

self.mocker.replay()

balaio_response = balaio_mock.get_full_package(attempt_id, target_name)
expected_response = doubles.make_expected_generator(expected_file_uri)

# compare the 2 responses - http://stackoverflow.com/a/9983596/1503
compare_equal = all(a == b for a, b in izip_longest(balaio_response, expected_response))
self.assertTrue(compare_equal)

def test_get_xml_uri(self):
balaio_api = BalaioAPI()
attempt_id = 25
Expand Down
20 changes: 19 additions & 1 deletion scielomanager/articletrack/tests/tests_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def _makePermission(perm, model, app_label='articletrack'):
return auth_models.Permission.objects.get(codename=perm, content_type=ct)


class CheckinListTests(WebTest):
class CheckinListTests(WebTest, mocker.MockerTestCase):

def setUp(self):
self.user = auth.UserF(is_active=True)
Expand Down Expand Up @@ -186,6 +186,12 @@ def test_status_code_notice_list(self):
self._addWaffleFlag()
notice = self._makeOne()

# to avoid making a request will replace it with a double
balaio = self.mocker.replace('articletrack.balaio.BalaioAPI')
balaio()
self.mocker.result(doubles.BalaioAPIDoubleDisabled())
self.mocker.replay()

response = self.app.get(reverse('notice_detail',
args=[notice.checkin.pk]), user=self.user)

Expand All @@ -203,6 +209,12 @@ def test_notice_list_with_itens(self):
self._addWaffleFlag()
notice = self._makeOne()

# to avoid making a request will replace it with a double
balaio = self.mocker.replace('articletrack.balaio.BalaioAPI')
balaio()
self.mocker.result(doubles.BalaioAPIDoubleDisabled())
self.mocker.replay()

response = self.app.get(reverse('notice_detail',
args=[notice.checkin.pk]), user=self.user)

Expand All @@ -212,6 +224,12 @@ def test_notice_list_must_have_link_to_checkin_list(self):
self._addWaffleFlag()
notice = self._makeOne()

# to avoid making a request will replace it with a double
balaio = self.mocker.replace('articletrack.balaio.BalaioAPI')
balaio()
self.mocker.result(doubles.BalaioAPIDoubleDisabled())
self.mocker.replay()

response = self.app.get(reverse('notice_detail',
args=[notice.checkin.pk]), user=self.user)

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 6af08e1

Please sign in to comment.