Skip to content

Commit

Permalink
[#1349] Add dataset and group test factories
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Hammond committed Nov 29, 2013
1 parent 19b1792 commit b6d05a7
Showing 1 changed file with 74 additions and 1 deletion.
75 changes: 74 additions & 1 deletion ckan/new_tests/factories.py
Expand Up @@ -65,7 +65,7 @@ def _generate_user_id(user):
class User(factory.Factory):
'''A factory class for creating CKAN users.'''

# This is the class that UserFactory will create and return instances
# This is the class that User factory will create and return instances
# of.
FACTORY_FOR = ckan.model.User

Expand Down Expand Up @@ -126,6 +126,79 @@ def _create(cls, target_class, *args, **kwargs):
return mock_user


def _generate_dataset_title(dataset):
'''Return a title for the given Dataset factory stub object.'''

return dataset.name.replace('_', ' ').title()


class Dataset(factory.Factory):
'''A factory class for creating CKAN datasets.'''

FACTORY_FOR = ckan.model.Package

name = factory.Sequence(lambda n: 'test_dataset_{n}'.format(n=n))
title = factory.LazyAttribute(_generate_dataset_title)
author = 'test dataset author'
author_email = 'test_dataset_author@test_dataset.io'
maintainer = 'test dataset maintainer'
maintainer_email = 'test_dataset_maintainer@test_dataset.io'
license_id = 'cc-by'
notes = 'Some test notes about this test dataset'
url = 'test_dataset.io'
version = '0.1 beta'

@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."
if 'user' in kwargs:
user = kwargs.pop('user')
context = {'user': user['name']}
else:
context = {}
dataset_dict = helpers.call_action('dataset_create', context=context,
**kwargs)
return dataset_dict


def _generate_group_title(group):
'''Return a title for the given Group factory stub object.'''

return group.name.replace('_', ' ').title()


class Group(factory.Factory):
'''A factory class for creating CKAN groups.'''

FACTORY_FOR = ckan.model.Group

name = factory.Sequence(lambda n: 'test_group_{n}'.format(n=n))
title = factory.LazyAttribute(_generate_group_title)
description = 'A test description for this test group.'

@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."
assert 'user' in kwargs, ('The Group factory requires an extra '
'user=user_dict keyword argument (the user '
'who will create the group)')
user_dict = kwargs.pop('user')
context = {'user': user_dict['name']}
group_dict = helpers.call_action('group_create', context=context,
**kwargs)
return group_dict


def validator_data_dict():
'''Return a data dict with some arbitrary data in it, suitable to be passed
to validator functions for testing.
Expand Down

0 comments on commit b6d05a7

Please sign in to comment.