From aab84d1a006da797576cf9182cd53b9084c72304 Mon Sep 17 00:00:00 2001 From: amercader Date: Wed, 11 Dec 2013 17:47:23 +0000 Subject: [PATCH] [#1208] Add new tests for group/org list/show Added a couple of factories for groups and orgs. Not clear about the user that should be creating them. --- ckan/new_tests/factories.py | 73 ++++++++++++ ckan/new_tests/logic/action/test_get.py | 145 +++++++++++++++++++++++- 2 files changed, 217 insertions(+), 1 deletion(-) diff --git a/ckan/new_tests/factories.py b/ckan/new_tests/factories.py index 6e8385eeacf..79bbdda426f 100644 --- a/ckan/new_tests/factories.py +++ b/ckan/new_tests/factories.py @@ -99,6 +99,79 @@ def _create(cls, target_class, *args, **kwargs): return user_dict +class Group(factory.Factory): + '''A factory class for creating CKAN groups.''' + + # This is the class that GroupFactory will create and return instances + # of. + FACTORY_FOR = ckan.model.Group + + # These are the default params that will be used to create new groups. + type = 'group' + is_organization = False + + title = 'Test Group' + description = 'Just another test group.' + image_url = 'http://placekitten.com/g/200/200' + + # Generate a different group name param for each user that gets created. + name = factory.Sequence(lambda n: 'test_group_{n}'.format(n=n)) + + @classmethod + def _build(cls, target_class, *args, **kwargs): + raise NotImplementedError(".build() isn't supported in CKAN") + + @classmethod + def _create(cls, target_class, *args, **kwargs): + if args: + assert False, "Positional args aren't supported, use keyword args." + + #TODO: we will need to be able to define this when creating the instance + # perhaps passing a 'user' param? + context = { + 'user': helpers.call_action('get_site_user')['name'] + } + + group_dict = helpers.call_action('group_create', context=context, **kwargs) + return group_dict + + +class Organization(factory.Factory): + '''A factory class for creating CKAN organizations.''' + + # This is the class that OrganizationFactory will create and return instances + # of. + FACTORY_FOR = ckan.model.Group + + # These are the default params that will be used to create new organizations. + type = 'organization' + is_organization = True + + title = 'Test Organization' + description = 'Just another test organization.' + image_url = 'http://placekitten.com/g/200/100' + + # Generate a different group name param for each user that gets created. + name = factory.Sequence(lambda n: 'test_org_{n}'.format(n=n)) + + @classmethod + def _build(cls, target_class, *args, **kwargs): + raise NotImplementedError(".build() isn't supported in CKAN") + + @classmethod + def _create(cls, target_class, *args, **kwargs): + if args: + assert False, "Positional args aren't supported, use keyword args." + + #TODO: we will need to be able to define this when creating the instance + # perhaps passing a 'user' param? + context = { + 'user': helpers.call_action('get_site_user')['name'] + } + + group_dict = helpers.call_action('organization_create', context=context, **kwargs) + return group_dict + class MockUser(factory.Factory): '''A factory class for creating mock CKAN users using the mock library.''' diff --git a/ckan/new_tests/logic/action/test_get.py b/ckan/new_tests/logic/action/test_get.py index 2ef68491f9d..f2207d2a6d0 100644 --- a/ckan/new_tests/logic/action/test_get.py +++ b/ckan/new_tests/logic/action/test_get.py @@ -1,10 +1,153 @@ import nose.tools -import nose.case + import ckan.logic as logic +import ckan.lib.search as search import ckan.new_tests.helpers as helpers import ckan.new_tests.factories as factories +class TestGet(object): + + @classmethod + def setup_class(cls): + helpers.reset_db() + + def setup(self): + import ckan.model as model + + # Reset the db before each test method. + model.repo.rebuild_db() + + # Clear the search index + search.clear() + + def test_group_list(self): + + group1 = factories.Group() + group2 = factories.Group() + + group_list = helpers.call_action('group_list') + + assert (sorted(group_list) == + sorted([g['name'] for g in [group1, group2]])) + + def test_group_show(self): + + group = factories.Group() + + group_dict = helpers.call_action('group_show', id=group['id']) + + # FIXME: Should this be returned by group_create? + group_dict.pop('num_followers', None) + assert group_dict == group + + def test_group_show_packages_returned(self): + + user_name = helpers.call_action('get_site_user')['name'] + + group = factories.Group() + + datasets = [ + {'name': 'dataset_1', 'groups': [{'name': group['name']}]}, + {'name': 'dataset_2', 'groups': [{'name': group['name']}]}, + ] + + for dataset in datasets: + helpers.call_action('package_create', + context={'user': user_name}, + **dataset) + + group_dict = helpers.call_action('group_show', id=group['id']) + + assert len(group_dict['packages']) == 2 + assert group_dict['package_count'] == 2 + + def test_group_show_no_packages_returned(self): + + user_name = helpers.call_action('get_site_user')['name'] + + group = factories.Group() + + datasets = [ + {'name': 'dataset_1', 'groups': [{'name': group['name']}]}, + {'name': 'dataset_2', 'groups': [{'name': group['name']}]}, + ] + + for dataset in datasets: + helpers.call_action('package_create', + context={'user': user_name}, + **dataset) + + group_dict = helpers.call_action('group_show', id=group['id'], + include_datasets=False) + + assert not 'packages' in group_dict + assert group_dict['package_count'] == 2 + + def test_organization_list(self): + + org1 = factories.Organization() + org2 = factories.Organization() + + org_list = helpers.call_action('organization_list') + + assert (sorted(org_list) == + sorted([g['name'] for g in [org1, org2]])) + + def test_organization_show(self): + + org = factories.Organization() + + org_dict = helpers.call_action('organization_show', id=org['id']) + + # FIXME: Should this be returned by organization_create? + org_dict.pop('num_followers', None) + assert org_dict == org + + def test_organization_show_packages_returned(self): + + user_name = helpers.call_action('get_site_user')['name'] + + org = factories.Organization() + + datasets = [ + {'name': 'dataset_1', 'owner_org': org['name']}, + {'name': 'dataset_2', 'owner_org': org['name']}, + ] + + for dataset in datasets: + helpers.call_action('package_create', + context={'user': user_name}, + **dataset) + + org_dict = helpers.call_action('organization_show', id=org['id']) + + assert len(org_dict['packages']) == 2 + assert org_dict['package_count'] == 2 + + def test_organization_show_private_packages_not_returned(self): + + user_name = helpers.call_action('get_site_user')['name'] + + org = factories.Organization() + + datasets = [ + {'name': 'dataset_1', 'owner_org': org['name']}, + {'name': 'dataset_2', 'owner_org': org['name'], 'private': True}, + ] + + for dataset in datasets: + helpers.call_action('package_create', + context={'user': user_name}, + **dataset) + + org_dict = helpers.call_action('organization_show', id=org['id']) + + assert len(org_dict['packages']) == 1 + assert org_dict['packages'][0]['name'] == 'dataset_1' + assert org_dict['package_count'] == 1 + + class TestBadLimitQueryParameters(object): '''test class for #1258 non-int query parameters cause 500 errors