Skip to content
Snippets Groups Projects
Commit 74cc4f50 authored by Benjamin Becker's avatar Benjamin Becker
Browse files

adds tests for gather_collection_info, refactors for testability

parent 8930bf50
Branches
Tags
No related merge requests found
......@@ -62,9 +62,6 @@ def get_datasets_from_solr(dataset_names):
def gather_collection_info(collection_dict, datasets_in_collection, dataset_dict=None):
url_from_id = lambda id: helpers.url_for(controller='package', action='read', id=id)
name_first_dataset = datasets_in_collection[0].get('name')
url_first_dataset = url_from_id(name_first_dataset)
......@@ -72,25 +69,27 @@ def gather_collection_info(collection_dict, datasets_in_collection, dataset_dict
url_last_dataset = url_from_id(name_last_dataset)
name_collection = collection_dict.get('name')
persistent_link_last_member = helpers.url_for(
controller='ckanext.odsh.collection.controller:LatestDatasetController',
action='latest',
id=name_collection
)
persistent_link_last_member = url_last_member(name_collection)
if dataset_dict:
name_current_dataset = dataset_dict.get('name')
dataset_names = [d.get('name') for d in datasets_in_collection]
def get_predecessor():
try:
id_current = dataset_names.index(name_current_dataset)
if id_current and id_current > 0:
except ValueError:
return None
if id_current > 0:
return dataset_names[id_current - 1]
return None
def get_successor():
try:
id_current = dataset_names.index(name_current_dataset)
if id_current and id_current < len(dataset_names) - 1:
except ValueError:
return None
if id_current < len(dataset_names) - 1:
return dataset_names[id_current + 1]
return None
......@@ -122,6 +121,16 @@ def gather_collection_info(collection_dict, datasets_in_collection, dataset_dict
'persistent_link_last_member': persistent_link_last_member,
}
def url_from_id(package_id):
return helpers.url_for(controller='package', action='read', id=package_id)
def url_last_member(name_collection):
return helpers.url_for(
controller='ckanext.odsh.collection.controller:LatestDatasetController',
action='latest',
id=name_collection
)
def get_latest_dataset(collection_name):
collection_info = get_collection_info(collection_name)
......
import nose.tools as nt
from mock import patch
import ckanext.odsh.collection.helpers as helpers_collection
class TestGatherCollectionInfo(object):
def setUp(self):
# construct datasets that shall be in following order:
# name date
# public2 2014-07-01
# public3 2014-07-01
# public4 2014-07-02
# public1 2014-07-03
self.names_collection_members = [
u'public3', u'public2', u'public1', u'public4',
u'private3', u'private2', u'private1']
self.dates_collection_members = [
u'2014-07-01T00:00:00', # public3
u'2014-07-01T00:00:00', # public2
u'2014-07-03T00:00:00', # public1
u'2014-07-02T00:00:00', # public4
u'2014-07-05T00:00:00', # private3
u'2014-07-06T00:00:00', # private2
u'2014-07-07T00:00:00', # private1
]
self.pkg_dicts_collection_members = [
{
u'name': name,
u'extras': {u'issued': date}
}
for (name, date) in zip(self.names_collection_members, self.dates_collection_members)
]
self.patch_url_from_id = patch.object(
helpers_collection,
'url_from_id',
new=lambda id: 'http://{}'.format(id)
)
self.patch_url_from_id.start()
self.patch_url_last_member = patch.object(
helpers_collection,
'url_last_member',
new=lambda collection_name: 'http://{}/last'.format(
collection_name)
)
self.patch_url_last_member.start()
self.collection_dict = {
'name': 'collection name',
'title': 'collection title',
}
self.datasets_in_collection = [
{
'name': 'dataset 1',
},
{
'name': 'dataset 2',
},
{
'name': 'dataset 3',
},
]
def tearDown(self):
self.patch_url_from_id.stop()
self.patch_url_last_member.stop()
def test_patch_url_from_id(self):
nt.assert_equal(helpers_collection.url_from_id(
'some_id'), 'http://some_id')
def test_patch_url_last_member(self):
nt.assert_equal(helpers_collection.url_last_member(
'some_collection'), 'http://some_collection/last')
def test_gather_collection_info_without_reference_package(self):
collection_info = helpers_collection.gather_collection_info(
self.collection_dict, self.datasets_in_collection, dataset_dict=None)
collection_info_expected = {
'first_member': {'name': 'dataset 1', 'url': 'http://dataset 1'},
'last_member': {'name': 'dataset 3', 'url': 'http://dataset 3'},
'members': [
{'name': 'dataset 1'},
{'name': 'dataset 2'},
{'name': 'dataset 3'}],
'persistent_link_last_member': 'http://collection name/last',
'predecessor': {'url': None},
'successor': {'url': None},
'title': 'collection title'
}
nt.assert_equal(collection_info, collection_info_expected)
def test_gather_collection_info_with_reference_package_1(self):
collection_info = helpers_collection.gather_collection_info(
self.collection_dict, self.datasets_in_collection, dataset_dict={'name': 'dataset 1'})
collection_info_expected = {
'first_member': {'name': 'dataset 1', 'url': 'http://dataset 1'},
'last_member': {'name': 'dataset 3', 'url': 'http://dataset 3'},
'members': [
{'name': 'dataset 1'},
{'name': 'dataset 2'},
{'name': 'dataset 3'}],
'persistent_link_last_member': 'http://collection name/last',
'predecessor': {'url': None},
'successor': {'url': 'http://dataset 2'},
'title': 'collection title'
}
nt.assert_equal(collection_info, collection_info_expected)
def test_gather_collection_info_with_reference_package_2(self):
collection_info = helpers_collection.gather_collection_info(
self.collection_dict, self.datasets_in_collection, dataset_dict={'name': 'dataset 2'})
collection_info_expected = {
'first_member': {'name': 'dataset 1', 'url': 'http://dataset 1'},
'last_member': {'name': 'dataset 3', 'url': 'http://dataset 3'},
'members': [
{'name': 'dataset 1'},
{'name': 'dataset 2'},
{'name': 'dataset 3'}],
'persistent_link_last_member': 'http://collection name/last',
'predecessor': {'url': 'http://dataset 1'},
'successor': {'url': 'http://dataset 3'},
'title': 'collection title'
}
nt.assert_equal(collection_info, collection_info_expected)
def test_gather_collection_info_with_reference_package_3(self):
collection_info = helpers_collection.gather_collection_info(
self.collection_dict, self.datasets_in_collection, dataset_dict={'name': 'dataset 3'})
collection_info_expected = {
'first_member': {'name': 'dataset 1', 'url': 'http://dataset 1'},
'last_member': {'name': 'dataset 3', 'url': 'http://dataset 3'},
'members': [
{'name': 'dataset 1'},
{'name': 'dataset 2'},
{'name': 'dataset 3'}],
'persistent_link_last_member': 'http://collection name/last',
'predecessor': {'url': 'http://dataset 2'},
'successor': {'url': None},
'title': 'collection title'
}
nt.assert_equal(collection_info, collection_info_expected)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment