Esempio n. 1
0
    def test_fkey_create_across_datasets(self):
        '''test that creating across datapackages fails'''
        resource_0 = factories.Resource(dataset=factories.Dataset())
        helpers.call_action('resource_schema_field_create',
                            resource_id=resource_0['id'],
                            index=0,
                            name='zero')
        helpers.call_action('resource_schema_field_create',
                            resource_id=resource_0['id'],
                            index=1,
                            name='one')

        resource_1 = factories.Resource(dataset=factories.Dataset())
        helpers.call_action('resource_schema_field_create',
                            resource_id=resource_1['id'],
                            index=0,
                            name='zero_id')
        helpers.call_action('resource_schema_field_create',
                            resource_id=resource_1['id'],
                            index=1,
                            name='one_id')

        nose.tools.assert_raises(
            toolkit.ValidationError,
            helpers.call_action,
            'resource_schema_fkey_create',
            resource_id=resource_0['id'],
            field='zero',
            referenced_resource_id=resource_1['id'],
            referenced_field='zero_id',
        )
Esempio n. 2
0
    def test_file_switcher(self):
        '''Simple test that the contents of the file switcher dropdown are
        correct.

        '''
        dataset = factories.Dataset()
        resource_1 = factories.Resource(dataset=dataset)
        resource_2 = factories.Resource(dataset=dataset)
        resource_3 = factories.Resource(dataset=dataset)

        response = self.app.get(
            toolkit.url_for(controller='package', action='resource_read',
                            id=dataset['id'], resource_id=resource_1['id']))

        # The dropdown should contain links to the two other files in the
        # package.
        soup = response.html
        links = soup.find('h1', class_='dropdown').find('ul').find_all('a')
        assert len(links) == 2
        assert len([link for link in links if link['href'] ==
                   toolkit.url_for(controller='package', action='resource_read',
                                   id=dataset['id'],
                                   resource_id=resource_2['id'])]) == 1
        assert len([link for link in links if link['href'] ==
                   toolkit.url_for(controller='package', action='resource_read',
                                   id=dataset['id'],
                                   resource_id=resource_3['id'])]) == 1
    def _create_resource(self):
        dataset = factories.Dataset()

        resource_0 = factories.Resource(dataset=dataset)
        helpers.call_action('resource_schema_field_create',
                            resource_id=resource_0['id'],
                            index=0,
                            name='zero')
        helpers.call_action('resource_schema_field_create',
                            resource_id=resource_0['id'],
                            index=1,
                            name='one')
        helpers.call_action('resource_schema_field_create',
                            resource_id=resource_0['id'],
                            index=2,
                            name='two')

        resource_1 = factories.Resource(dataset=dataset)
        helpers.call_action('resource_schema_field_create',
                            resource_id=resource_1['id'],
                            index=0,
                            name='zero_id')

        schema = helpers.call_action(
            'resource_schema_fkey_create',
            resource_id=resource_0['id'],
            fkey_uid='uid',
            field='zero',
            referenced_resource_id=resource_1['id'],
            referenced_field='zero_id',
        )

        return resource_0['id'], resource_1['id'], schema
Esempio n. 4
0
    def test_get_all_without_views_accepts_list_of_formats_ignoring_case(self):
        factories.Resource(format='other_format')
        resource_id = factories.Resource(format='format')['id']

        resources = Resource.get_all_without_views(['FORMAT'])

        length = len(resources)
        assert length == 1, 'Expected 1 resource, but got %d' % length
        assert_equals([resources[0].id], [resource_id])
Esempio n. 5
0
    def test_get_all_resources_ids_in_datastore(self):
        resource_in_datastore = factories.Resource()
        resource_not_in_datastore = factories.Resource()
        data = {
            'resource_id': resource_in_datastore['id'],
            'force': True,
        }
        helpers.call_action('datastore_create', **data)

        resource_ids = db.get_all_resources_ids_in_datastore()

        assert resource_in_datastore['id'] in resource_ids
        assert resource_not_in_datastore['id'] not in resource_ids
Esempio n. 6
0
    def test_get_all_without_views_returns_all_resources_without_views(self):
        # Create resource with resource_view
        factories.ResourceView()

        expected_resources = [
            factories.Resource(format='format'),
            factories.Resource(format='other_format')
        ]

        resources = Resource.get_all_without_views()

        expected_resources_ids = [r['id'] for r in expected_resources]
        resources_ids = [r.id for r in resources]

        assert_equals(expected_resources_ids.sort(), resources_ids.sort())
Esempio n. 7
0
    def test_package_dictize_resource(self):
        dataset = factories.Dataset()
        resource = factories.Resource(package_id=dataset['id'],
                                      name='test_pkg_dictize')
        dataset_obj = model.Package.get(dataset['id'])
        context = {'model': model, 'session': model.Session}

        result = model_dictize.package_dictize(dataset_obj, context)

        assert_equal_for_keys(result['resources'][0], resource, 'name', 'url')
        expected_dict = {
            u'cache_last_updated': None,
            u'cache_url': None,
            u'description': u'Just another test resource.',
            u'format': u'res_format',
            u'hash': u'',
            u'last_modified': None,
            u'mimetype': None,
            u'mimetype_inner': None,
            u'name': u'test_pkg_dictize',
            u'position': 0,
            u'resource_type': None,
            u'size': None,
            u'state': u'active',
            u'url': u'http://link.to.some.data',
            u'url_type': None,
            u'webstore_last_updated': None,
            u'webstore_url': None
        }
        self.assert_equals_expected(expected_dict, result['resources'][0])
    def delete_pkey_when_there_is_no_pkey(self):
        '''Trying to delete the primary key from a resource that doesn't have
        one should be a no-op.

        '''
        resource = factories.Resource(dataset=factories.Dataset())
        helpers.call_action('resource_schema_field_create',
                            resource_id=resource['id'],
                            index=0,
                            name='zero')
        helpers.call_action('resource_schema_field_create',
                            resource_id=resource['id'],
                            index=1,
                            name='one')
        helpers.call_action('resource_schema_field_create',
                            resource_id=resource['id'],
                            index=2,
                            name='two')

        resource_before = helpers.call_action('resource_show',
                                              id=resource['id'])

        helpers.call_action('resource_schema_pkey_delete',
                            resource_id=resource['id'])

        resource_after = helpers.call_action('resource_show',
                                             id=resource['id'])

        assert resource_before == resource_after
    def test_resource_schema_field_delete(self):
        '''Test deleting a field from a resource schema.'''

        resource = factories.Resource(dataset=factories.Dataset())
        helpers.call_action('resource_schema_field_create',
                            resource_id=resource['id'],
                            index=0,
                            name='zero')
        helpers.call_action('resource_schema_field_create',
                            resource_id=resource['id'],
                            index=1,
                            name='one')
        helpers.call_action('resource_schema_field_create',
                            resource_id=resource['id'],
                            index=2,
                            name='two')

        helpers.call_action('resource_schema_field_delete',
                            resource_id=resource['id'],
                            index=1)

        schema = helpers.call_action('resource_schema_show',
                                     resource_id=resource['id'])
        fields = schema['fields']
        assert len(fields) == 2
        assert len([field for field in fields if field['index'] == 1]) == 0
        assert len([field for field in fields if field['name'] == 'one']) == 0

        nose.tools.assert_raises(toolkit.ValidationError,
                                 helpers.call_action,
                                 'resource_schema_field_show',
                                 resource_id=resource['id'],
                                 index=1)
    def test_resource_shema_field_delete_with_multiple_matching_indices(self):
        '''Deleting a field with an index that matches more than one field in
        the resource's schema should raise ValidationError.

        '''
        # resource_schema_field_create() won't let us create two fields with
        # the same index, but we can create them by passing the schema to
        # resource_create directly.
        schema = {
            'fields': [
                {
                    'name': 'foo',
                    'index': 3
                },
                {
                    'name': 'bar',
                    'index': 3
                },
            ],
        }
        resource = factories.Resource(dataset=factories.Dataset(),
                                      schema=json.dumps(schema))

        nose.tools.assert_raises(toolkit.ValidationError,
                                 helpers.call_action,
                                 'resource_schema_field_delete',
                                 resource_id=resource['id'],
                                 index=3)
Esempio n. 11
0
    def test_resource_schema_field_update_return_value(self):
        '''resource_schema_field_update should return the updated field.'''

        resource = factories.Resource(dataset=factories.Dataset())
        helpers.call_action('resource_schema_field_create',
                            resource_id=resource['id'],
                            index=0,
                            name='name',
                            title='title',
                            description='description',
                            type='string',
                            format='format')

        field = helpers.call_action('resource_schema_field_update',
                                    resource_id=resource['id'],
                                    index=0,
                                    name='new-name',
                                    title='new-title',
                                    description='new-description',
                                    type='string',
                                    format='format')

        assert field == helpers.call_action('resource_schema_field_show',
                                            resource_id=resource['id'],
                                            index=0)
Esempio n. 12
0
    def test_resource_schema_field_update_time_with_link_resource(self):
        '''If a resource schema field with type time is updated for a resource
        that has a URL to a remote file rather than an uploaded file, then no
        temporal extent should be added to the field.

        Since there's no uploaded file, the code for computing the temporal
        extent for the field won't work. This test is mostly here to make sure
        we don't crash in this case.

        '''
        user = factories.User()
        resource = factories.Resource(dataset=factories.Dataset(user=user))
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        api.action.resource_schema_field_create(resource_id=resource['id'],
                                                index=1,
                                                name='yearID',
                                                type='string')

        api.action.resource_schema_field_update(resource_id=resource['id'],
                                                index=1,
                                                name='yearID',
                                                type='time')

        field = helpers.call_action('resource_schema_field_show',
                                    resource_id=resource['id'],
                                    index=1)
        assert 'temporal_extent' not in field
Esempio n. 13
0
    def test_add_default_views_to_resource(self):

        # New resources have no views
        dataset_dict = factories.Dataset()
        resource_dict = factories.Resource(
            package_id=dataset_dict['id'],
            url='http://some.image.png',
            format='png',
        )

        # Change default views config setting
        config['ckan.views.default_views'] = 'image_view'

        context = {
            'user': helpers.call_action('get_site_user')['name']
        }
        created_views = helpers.call_action(
            'resource_create_default_resource_views',
            context,
            resource=resource_dict,
            package=dataset_dict)

        assert_equals(len(created_views), 1)

        assert_equals(created_views[0]['view_type'], 'image_view')
Esempio n. 14
0
    def test_resource_controller_plugin_show(self):
        """
        Before show gets called by the other methods but we test it
        separately here and make sure that it doesn't call the other
        methods.
        """
        user = factories.Sysadmin()
        package = factories.Dataset(user=user)
        resource = factories.Resource(user=user, package_id=package['id'])

        # Set up the plugin here because we don't want the resource creation
        # to affect it (because we will only check for changes to delete)
        ckan.plugins.load('example_iresourcecontroller')
        plugin = ckan.plugins.get_plugin('example_iresourcecontroller')

        res = helpers.call_action('package_show',
                                  name_or_id=package['id'])

        assert plugin.counter['before_create'] == 0, plugin.counter
        assert plugin.counter['after_create'] == 0, plugin.counter
        assert plugin.counter['before_update'] == 0, plugin.counter
        assert plugin.counter['after_update'] == 0, plugin.counter
        assert plugin.counter['before_delete'] == 0, plugin.counter
        assert plugin.counter['after_delete'] == 0, plugin.counter
        assert plugin.counter['before_show'] == 1, plugin.counter
Esempio n. 15
0
 def test_all_params_work_with_fields_with_whitespaces(self):
     resource = factories.Resource()
     data = {
         'resource_id': resource['id'],
         'force': True,
         'records': [
             {'the year': 2014},
             {'the year': 2013},
         ],
     }
     result = helpers.call_action('datastore_create', **data)
     search_data = {
         'resource_id': resource['id'],
         'fields': 'the year',
         'sort': 'the year',
         'filters': {
             'the year': 2013
         },
         'q': {
             'the year': '2013'
         },
     }
     result = helpers.call_action('datastore_search', **search_data)
     result_years = [r['the year'] for r in result['records']]
     assert_equals(result_years, [2013])
Esempio n. 16
0
    def test_resource_schema_field_create_when_schema_already_exists(self):
        '''Test creating a new schema field in a schema that already exists.

        (The tests above have been testing with resources that had no schema
        to begin with.)

        '''
        resource = factories.Resource(dataset=factories.Dataset())
        helpers.call_action('resource_schema_field_create',
                            resource_id=resource['id'],
                            index=0,
                            name='foo')

        # The field attributes we will pass.
        name = 'test-field'
        title = 'Test Field'

        helpers.call_action('resource_schema_field_create',
                            resource_id=resource['id'],
                            index=1,
                            name=name,
                            title=title)

        schema = helpers.call_action('resource_schema_show',
                                     resource_id=resource['id'])
        assert 'fields' in schema
        fields = schema['fields']
        assert len(fields) == 2
        assert fields[0]['name'] == 'foo'
        assert fields[1]['name'] == name
        assert fields[1]['title'] == title
Esempio n. 17
0
    def test_resource_schema_field_show_with_out_of_order_creation(self):
        '''Test that resource_schema_field_show works even if the fields were
        not created in index order.

        '''
        resource = factories.Resource(dataset=factories.Dataset())
        field_3 = helpers.call_action('resource_schema_field_create',
                                      resource_id=resource['id'],
                                      index=3,
                                      name='three')
        field_1 = helpers.call_action('resource_schema_field_create',
                                      resource_id=resource['id'],
                                      index=1,
                                      name='one')
        field_0 = helpers.call_action('resource_schema_field_create',
                                      resource_id=resource['id'],
                                      index=0,
                                      name='zero')
        field_2 = helpers.call_action('resource_schema_field_create',
                                      resource_id=resource['id'],
                                      index=2,
                                      name='two')

        assert helpers.call_action('resource_schema_field_show',
                                   resource_id=resource['id'],
                                   index=0) == field_0
        assert helpers.call_action('resource_schema_field_show',
                                   resource_id=resource['id'],
                                   index=1) == field_1
        assert helpers.call_action('resource_schema_field_show',
                                   resource_id=resource['id'],
                                   index=2) == field_2
        assert helpers.call_action('resource_schema_field_show',
                                   resource_id=resource['id'],
                                   index=3) == field_3
Esempio n. 18
0
def _create_resource_and_field():
    resource = factories.Resource(dataset=factories.Dataset())
    field = {'index': 0, 'name': 'name', 'title': 'title', 'foo': 'bar'}
    field = helpers.call_action('resource_schema_field_create',
                                resource_id=resource['id'],
                                **field)
    return resource, field
Esempio n. 19
0
    def test_resource_schema_field_create_with_custom_attributes(self):
        '''Test that custom schema field attributes are saved correctly.'''

        resource = factories.Resource(dataset=factories.Dataset())

        # The field attributes we will pass.
        name = 'test-field'
        title = 'Test Field'
        custom_attribute_1 = 'foo'
        custom_attribute_2 = 'bar'

        helpers.call_action('resource_schema_field_create',
                            resource_id=resource['id'],
                            index=0,
                            name=name,
                            title=title,
                            custom_attribute_1=custom_attribute_1,
                            custom_attribute_2=custom_attribute_2)

        schema = helpers.call_action('resource_schema_show',
                                     resource_id=resource['id'])
        assert 'fields' in schema
        fields = schema['fields']
        assert len(fields) == 1
        field = fields[0]
        assert field['name'] == name
        assert field['title'] == title
        assert field['custom_attribute_1'] == custom_attribute_1
        assert field['custom_attribute_2'] == custom_attribute_2
Esempio n. 20
0
    def test_resource_schema_field_create_complex(self):
        '''More complex test of creating a schema field.

        Pass all recommended params and test they all get created correctly.

        '''
        resource = factories.Resource(dataset=factories.Dataset())

        # The field attributes we will pass.
        name = 'test-field'
        title = 'Test Field'
        description = 'Just a simple test field.'
        type_ = 'string'
        format_ = 'foobar'

        helpers.call_action('resource_schema_field_create',
                            resource_id=resource['id'],
                            index=0,
                            name=name,
                            title=title,
                            description=description,
                            type=type_,
                            format=format_)

        schema = helpers.call_action('resource_schema_show',
                                     resource_id=resource['id'])
        assert 'fields' in schema
        fields = schema['fields']
        assert len(fields) == 1
        field = fields[0]
        assert field['name'] == name
        assert field['title'] == title
        assert field['description'] == description
        assert field['type'] == type_
        assert field['format'] == format_
Esempio n. 21
0
    def test_resource_schema_field_create_non_string_custom_attribute(self):
        '''Non-string custom attribute values should be converted to strings.

        '''
        resource = factories.Resource(dataset=factories.Dataset())

        # The field attributes we will pass.
        name = 'test-field'
        title = 'Test Field'
        custom_attribute_1 = 145
        custom_attribute_2 = True

        helpers.call_action('resource_schema_field_create',
                            resource_id=resource['id'],
                            index=0,
                            name=name,
                            title=title,
                            custom_attribute_1=custom_attribute_1,
                            custom_attribute_2=custom_attribute_2)

        schema = helpers.call_action('resource_schema_show',
                                     resource_id=resource['id'])
        assert 'fields' in schema
        fields = schema['fields']
        assert len(fields) == 1
        field = fields[0]
        assert field['custom_attribute_1'] == '145'
        assert field['custom_attribute_2'] == 'True'
Esempio n. 22
0
    def test_get_path_to_resource_file_with_linked_file(self):

        resource = factories.Resource(dataset=factories.Dataset(),
                                      url='http://example.com/foo.csv')

        nose.tools.assert_raises(exceptions.ResourceFileDoesNotExistException,
                                 util.get_path_to_resource_file, resource)
Esempio n. 23
0
 def test_fts_on_field_calculates_ranks_only_on_that_specific_field(self):
     resource = factories.Resource()
     data = {
         'resource_id':
         resource['id'],
         'force':
         True,
         'records': [{
             'from': 'Brazil',
             'to': 'Brazil'
         }, {
             'from': 'Brazil',
             'to': 'Italy'
         }],
     }
     result = helpers.call_action('datastore_create', **data)
     search_data = {
         'resource_id': resource['id'],
         'fields': 'from',
         'q': {
             'from': 'Brazil'
         },
     }
     result = helpers.call_action('datastore_search', **search_data)
     ranks = [r['rank from'] for r in result['records']]
     assert_equals(len(result['records']), 2)
     assert_equals(len(set(ranks)), 1)
Esempio n. 24
0
 def _resource_data(self, pkg_id, records = None):
     '''
     Returns a dictionary with some standard dictionary, including an
     associated package ID. Records are optional.
     :param pkg_id: The ID of the package to associate the resource to.
     :param records: Optionally, a list of records.
     :return: dict
     '''
     resource = factories.Resource()
     data = {
         'resource': {
             'id': resource['id'],
             'package_id': pkg_id,
             'name': 'Test records',
             'owner_org': self.org['id']
             },
         'fields': [{
             'id': 'common_name',
             'type': 'text'
             }, {
             'id': 'scientific_name',
             'type': 'text'
             }]
         }
     if records:
         data['records'] = records
     return data
Esempio n. 25
0
    def test_resource_schema_field_update_with_custom_attributes(self):
        '''Test updating custom attributes.'''

        resource = factories.Resource(dataset=factories.Dataset())
        helpers.call_action('resource_schema_field_create',
                            resource_id=resource['id'],
                            index=0,
                            name='original-name',
                            title='original-title',
                            custom_attr_1='foo',
                            custom_attr_2='bar')

        field = helpers.call_action('resource_schema_field_show',
                                    resource_id=resource['id'],
                                    index=0)
        field['custom_attr_1'] = 'foo2'
        field['custom_attr_2'] = 'bar2'
        helpers.call_action('resource_schema_field_update',
                            resource_id=resource['id'],
                            **field)

        field = helpers.call_action('resource_schema_field_show',
                                    resource_id=resource['id'],
                                    index=0)
        assert field['custom_attr_1'] == 'foo2'
        assert field['custom_attr_2'] == 'bar2'
Esempio n. 26
0
    def test_resource_schema_field_update_does_not_affect_other_fields(self):
        '''Updating a resource schema field should not affect any of the
        resource's other fields.

        '''
        resource_fields = {
            'url': 'http://www.example_resource.com',
            'description': 'Just a test resource',
            'format': 'CSV',
            'hash': 'xxx',
            'name': 'test-resource',
            'mimetype': 'text/csv',
            'size': '10',
        }
        resource = factories.Resource(dataset=factories.Dataset(),
                                      **resource_fields)
        field = helpers.call_action('resource_schema_field_create',
                                    resource_id=resource['id'],
                                    index=0,
                                    name='foo',
                                    title='bar')

        field['name'] = 'bar'
        field['title'] = 'foo'
        helpers.call_action('resource_schema_field_update',
                            resource_id=resource['id'],
                            **field)

        resource = helpers.call_action('resource_show', id=resource['id'])
        for key, value in resource_fields.items():
            assert resource[key] == value, (key, value)
Esempio n. 27
0
    def test_resource_schema_field_update_with_multiple_fields(self):
        '''Test updating a field when the schema has multiple fields.'''

        resource = factories.Resource(dataset=factories.Dataset())
        helpers.call_action('resource_schema_field_create',
                            resource_id=resource['id'],
                            index=0,
                            name='zero')
        field = helpers.call_action('resource_schema_field_create',
                                    resource_id=resource['id'],
                                    index=1,
                                    name='one')
        helpers.call_action('resource_schema_field_create',
                            resource_id=resource['id'],
                            index=2,
                            name='two')

        field['name'] = 'new-name'
        helpers.call_action('resource_schema_field_update',
                            resource_id=resource['id'],
                            **field)

        field = helpers.call_action('resource_schema_field_show',
                                    resource_id=resource['id'],
                                    index=field['index'])
        assert field == {'index': 1, 'name': 'new-name'}
Esempio n. 28
0
    def test_not_authorized_if_user_has_no_permissions_on_dataset(self):

        org = factories.Organization()

        user = factories.User()

        member = {'username': user['name'], 'role': 'admin', 'id': org['id']}
        helpers.call_action('organization_member_create', **member)

        user_2 = factories.User()

        dataset = factories.Dataset(owner_org=org['id'])

        resource = factories.Resource(package_id=dataset['id'])

        resource_view = {
            'resource_id': resource['id'],
            'title': u'Resource View',
            'view_type': u'image_view',
            'image_url': 'url'
        }

        context = {'user': user_2['name'], 'model': core_model}
        nose.tools.assert_raises(logic.NotAuthorized,
                                 helpers.call_auth,
                                 'resource_view_create',
                                 context=context,
                                 **resource_view)
Esempio n. 29
0
 def test_edit_url(self):
     res_dict = factories.Resource(url='http://first')
     res = Resource.get(res_dict['id'])
     res.url = 'http://second'
     model.repo.new_revision()
     model.repo.commit_and_remove()
     res = Resource.get(res_dict['id'])
     assert_equals(res.url, 'http://second')
Esempio n. 30
0
    def test_package_search_on_resource_name(self):
        '''
        package_search() should allow searching on resource name field.
        '''
        resource_name = 'resource_abc'
        package = factories.Resource(name=resource_name)

        search_result = helpers.call_action('package_search', q='resource_abc')
        eq(search_result['results'][0]['resources'][0]['name'], resource_name)