Example #1
0
    def test_resource_create_upload_same_file_with_no_name(self):
        '''If you create multiple resources, uploading files with the same name
        and not giving names for the resources, the resource names should be
        set to the file name but with _2, _3, etc. appended.

        '''
        user = factories.User()
        package = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])

        resource_1 = api.action.resource_create(
            package_id=package['id'],
            upload=custom_helpers.get_csv_file(
                'test-data/lahmans-baseball-database/AllstarFull.csv'))
        resource_2 = api.action.resource_create(
            package_id=package['id'],
            upload=custom_helpers.get_csv_file(
                'test-data/lahmans-baseball-database/AllstarFull.csv'))
        resource_3 = api.action.resource_create(
            package_id=package['id'],
            upload=custom_helpers.get_csv_file(
                'test-data/lahmans-baseball-database/AllstarFull.csv'))
        assert resource_1['name'] == 'AllstarFull.csv', resource_1['name']
        assert resource_2['name'] == 'AllstarFull_2.csv', resource_2['name']
        assert resource_3['name'] == 'AllstarFull_3.csv', resource_3['name']
Example #2
0
    def test_resource_schema_field_update_time(self):
        '''Test that a temporal extent is added to the field, when updating a
        field and setting its type to time.

        '''
        user = factories.User()
        package = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        csv_file = custom_helpers.get_csv_file('test-data/times.csv')
        resource = api.action.resource_create(package_id=package['id'],
                                              upload=csv_file)

        # Re-create the field with time type.
        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)

        # Unit tests elsewhere check that the temporal extent value is correct,
        # here we're just testing that it's present.
        assert 'temporal_extent' in field
Example #3
0
    def test_resource_schema_field_create_date(self):
        '''Test that a temporal extent is added to the field, when creating
        a field of type date.

        '''
        user = factories.User()
        package = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        csv_file = custom_helpers.get_csv_file(
            'test-data/lahmans-baseball-database/AllstarFull.csv')
        resource = api.action.resource_create(package_id=package['id'],
                                              upload=csv_file)

        # Delete the field from the schema so we can re-create it.
        api.action.resource_schema_field_delete(resource_id=resource['id'],
                                                index=1)

        # Re-create the field with date type.
        api.action.resource_schema_field_create(resource_id=resource['id'],
                                                index=1,
                                                name='yearID',
                                                type='date')

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

        # Unit tests elsewhere check that the temporal extent value is correct,
        # here we're just testing that it's present.
        assert 'temporal_extent' in field
    def test_resource_schema_field_update_date_with_invalid_value(self):
        '''Test calling resource_schema_field_update() and setting the type of
        the field to date, when the corresponding column in the CSV file
        contains non-date values.

        The code for computing the temporal extent of the field can't work if
        the column doesn't contain temporal data. This test is mostly here to
        make sure we don't crash in this case.

        '''
        user = factories.User()
        package = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        csv_file = custom_helpers.get_csv_file('test-data/test.csv')
        resource = api.action.resource_create(package_id=package['id'],
                                              upload=csv_file)

        api.action.resource_schema_field_update(resource_id=resource['id'],
                                                index=6, name='object',
                                                type='date')

        field = helpers.call_action('resource_schema_field_show',
                                     resource_id=resource['id'], index=6)

        assert 'temporal_extent' not in field
Example #5
0
    def test_resource_schema_field_update_with_naive_and_aware_dates(self):
        '''Test calling resource_schema_field_update() when the corresponding
        column in the CSV file contains a mix of naive and aware date strings.

        The temporal extent of the column cannot be computed in this case.
        This test is mostly here to make sure we don't crash in this case.

        '''
        user = factories.User()
        package = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        csv_file = custom_helpers.get_csv_file(
            'test-data/naive-and-aware-dates.csv')
        resource = api.action.resource_create(package_id=package['id'],
                                              upload=csv_file)

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

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

        assert 'temporal_extent' not in field
    def test_resource_create(self):
        '''Test that a schema is added to a resource when it's created.

        Unit tests elsewhere test that the schemas inferred from CSV files are
        correct, here we're just testing that the schema gets added to the
        resource on resource_create().

        '''
        user = factories.Sysadmin()
        package = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        csv_file = custom_helpers.get_csv_file(
            'test-data/lahmans-baseball-database/AllstarFull.csv')

        api.action.resource_create(package_id=package['id'],
                                   name='test-resource', upload=csv_file)

        # Apparently resource_create doesn't return the resource dict when
        # called via ckanapi, so we need another call to get it.
        package = api.action.package_show(id=package['id'])

        # The package should have just one resource, the one we just created.
        assert len(package['resources']) == 1
        resource = package['resources'][0]

        # There should be a schema in the resource.
        assert 'schema' in resource
        assert 'fields' in resource['schema']
    def test_resource_schema_field_create_with_naive_and_aware_dates(self):
        '''Test calling resource_schema_field_create() when the corresponding
        column in the CSV file contains a mix of naive and aware date strings.

        The temporal extent of the column cannot be computed in this case.
        This test is mostly here to make sure we don't crash in this case.

        '''
        user = factories.User()
        package = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        csv_file = custom_helpers.get_csv_file(
            'test-data/naive-and-aware-dates.csv')
        resource = api.action.resource_create(package_id=package['id'],
                                              upload=csv_file)

        # Delete the field from the schema so we can re-create it.
        api.action.resource_schema_field_delete(resource_id=resource['id'],
                                                index=1)

        # Re-create the field with date type.
        api.action.resource_schema_field_create(resource_id=resource['id'],
                                                index=1, name='object',
                                                type='date')

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

        assert 'temporal_extent' not in field
Example #8
0
    def test_resource_create(self):
        '''Test that a schema is added to a resource when it's created.

        Unit tests elsewhere test that the schemas inferred from CSV files are
        correct, here we're just testing that the schema gets added to the
        resource on resource_create().

        '''
        user = factories.Sysadmin()
        package = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        csv_file = custom_helpers.get_csv_file(
            'test-data/lahmans-baseball-database/AllstarFull.csv')

        api.action.resource_create(package_id=package['id'],
                                   name='test-resource',
                                   upload=csv_file)

        # Apparently resource_create doesn't return the resource dict when
        # called via ckanapi, so we need another call to get it.
        package = api.action.package_show(id=package['id'])

        # The package should have just one resource, the one we just created.
        assert len(package['resources']) == 1
        resource = package['resources'][0]

        # There should be a schema in the resource.
        assert 'schema' in resource
        assert 'fields' in resource['schema']
    def test_resource_schema_field_create_time(self):
        '''Test that a temporal extent is added to the field, when creating
        a field of type time.

        '''
        user = factories.User()
        package = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        csv_file = custom_helpers.get_csv_file('test-data/times.csv')
        resource = api.action.resource_create(package_id=package['id'],
                                              upload=csv_file)

        # Delete the field from the schema so we can re-create it.
        api.action.resource_schema_field_delete(resource_id=resource['id'],
                                                index=1)

        # Re-create the field with time type.
        api.action.resource_schema_field_create(resource_id=resource['id'],
                                                index=1, name='yearID',
                                                type='time')

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

        # Unit tests elsewhere check that the temporal extent value is correct,
        # here we're just testing that it's present.
        assert 'temporal_extent' in field
Example #10
0
    def test_resource_schema_field_update_date_with_invalid_value(self):
        '''Test calling resource_schema_field_update() and setting the type of
        the field to date, when the corresponding column in the CSV file
        contains non-date values.

        The code for computing the temporal extent of the field can't work if
        the column doesn't contain temporal data. This test is mostly here to
        make sure we don't crash in this case.

        '''
        user = factories.User()
        package = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        csv_file = custom_helpers.get_csv_file('test-data/test.csv')
        resource = api.action.resource_create(package_id=package['id'],
                                              upload=csv_file)

        api.action.resource_schema_field_update(resource_id=resource['id'],
                                                index=6,
                                                name='object',
                                                type='date')

        field = helpers.call_action('resource_schema_field_show',
                                    resource_id=resource['id'],
                                    index=6)

        assert 'temporal_extent' not in field
Example #11
0
    def test_resource_update(self):
        '''Test that a resource's schema is updated when the resource is
        updated with a new uploaded CSV file.

        Unit tests elsewhere test that the schemas inferred from CSV files are
        correct, here we're just testing that the schema gets added to the
        resource on resource_create().

        '''
        user = factories.Sysadmin()
        package = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])

        # Upload a first CSV file to the package.
        csv_file = custom_helpers.get_csv_file(
            'test-data/lahmans-baseball-database/AllstarFull.csv')
        api.action.resource_create(package_id=package['id'],
                                   name='test-package',
                                   upload=csv_file)

        # Get the generated schema from the resource.
        package = api.action.package_show(id=package['id'])
        assert len(package['resources']) == 1
        resource = package['resources'][0]
        assert 'schema' in resource
        original_schema = resource['schema']

        # Now update the resource with a new file.
        csv_file = custom_helpers.get_csv_file(
            'test-data/lahmans-baseball-database/Appearances.csv')
        api.action.resource_update(id=resource['id'],
                                   name=resource['name'],
                                   upload=csv_file)

        # Check that the schema has been changed.
        package = api.action.package_show(id=package['id'])
        assert len(package['resources']) == 1
        resource = package['resources'][0]
        assert 'schema' in resource
        schema = resource['schema']
        assert 'fields' in schema
        assert schema != original_schema
Example #12
0
    def _create_resource(self, file='test-data/lahmans-baseball-database/ManagersHalf.csv'):
        '''Return a test dataset, resource and resource schema.'''

        user = factories.User()
        dataset = factories.Dataset(user=user)
        csv_file = custom_helpers.get_csv_file(file)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        resource = api.action.resource_create(package_id=dataset['id'],
                                              upload=csv_file)
        schema = api.action.resource_schema_show(resource_id=resource['id'])

        return dataset, resource, schema
Example #13
0
    def test_foreign_key_is_on_page_string(self):
        user = factories.User()
        dataset = factories.Dataset(user=user)
        csv_file0 = custom_helpers.get_csv_file(
            'test-data/lahmans-baseball-database/Master.csv')
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        resource0 = api.action.resource_create(package_id=dataset['id'],
                                              upload=csv_file0)
        schema0 = api.action.resource_schema_show(resource_id=resource0['id'])


        csv_file1 = custom_helpers.get_csv_file(
            'test-data/lahmans-baseball-database/ManagersHalf.csv')
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        resource1 = api.action.resource_create(package_id=dataset['id'],
                                              upload=csv_file1)
        schema1 = api.action.resource_schema_show(resource_id=resource1['id'])

        helpers.call_action(
            'resource_schema_fkey_create',
            field="playerID",
            resource_id=resource1['id'],
            referenced_resource_id=resource0['id'],
            referenced_field="playerID",
        )

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

        soup = response.html
        nose.tools.assert_true(soup.find(text='Foreign keys'))
        pkey = soup.find(id='foreign-key')
        nose.tools.assert_true(
            pkey.li.a.attrs['href'],
            '/package/{0}/file/{1}/schema/0'.format(
                dataset['id'], resource0['id'])
        )

        nose.tools.assert_true(pkey.li.a.text, 'playerID')
    def test_resource_update(self):
        '''Test that a resource's schema is updated when the resource is
        updated with a new uploaded CSV file.

        Unit tests elsewhere test that the schemas inferred from CSV files are
        correct, here we're just testing that the schema gets added to the
        resource on resource_create().

        '''
        user = factories.Sysadmin()
        package = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])

        # Upload a first CSV file to the package.
        csv_file = custom_helpers.get_csv_file(
            'test-data/lahmans-baseball-database/AllstarFull.csv')
        api.action.resource_create(package_id=package['id'],
                                   name='test-package', upload=csv_file)

        # Get the generated schema from the resource.
        package = api.action.package_show(id=package['id'])
        assert len(package['resources']) == 1
        resource = package['resources'][0]
        assert 'schema' in resource
        original_schema = resource['schema']

        # Now update the resource with a new file.
        csv_file = custom_helpers.get_csv_file(
            'test-data/lahmans-baseball-database/Appearances.csv')
        api.action.resource_update(id=resource['id'], name=resource['name'],
                                   upload=csv_file)

        # Check that the schema has been changed.
        package = api.action.package_show(id=package['id'])
        assert len(package['resources']) == 1
        resource = package['resources'][0]
        assert 'schema' in resource
        schema = resource['schema']
        assert 'fields' in schema
        assert schema != original_schema
    def test_resource_update_to_duplicate_name(self):
        user = factories.User()
        dataset = factories.Dataset(user=user)
        resource_1 = factories.Resource(dataset=dataset)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        resource_2 = api.action.resource_create(package_id=dataset['id'],
            name='test-resource',
            upload=custom_helpers.get_csv_file(
                'test-data/lahmans-baseball-database/AllstarFull.csv'))

        resource_2['name'] = resource_1['name']
        nose.tools.assert_raises(toolkit.ValidationError,
            helpers.call_action, 'resource_update', **resource_2)
    def test_resource_create_upload_same_file_with_no_name(self):
        '''If you create multiple resources, uploading files with the same name
        and not giving names for the resources, the resource names should be
        set to the file name but with _2, _3, etc. appended.

        '''
        user = factories.User()
        package = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])

        resource_1 = api.action.resource_create(package_id=package['id'],
            upload=custom_helpers.get_csv_file(
                'test-data/lahmans-baseball-database/AllstarFull.csv'))
        resource_2 = api.action.resource_create(package_id=package['id'],
            upload=custom_helpers.get_csv_file(
                'test-data/lahmans-baseball-database/AllstarFull.csv'))
        resource_3 = api.action.resource_create(package_id=package['id'],
            upload=custom_helpers.get_csv_file(
                'test-data/lahmans-baseball-database/AllstarFull.csv'))
        assert resource_1['name'] == 'AllstarFull.csv', resource_1['name']
        assert resource_2['name'] == 'AllstarFull_2.csv', resource_2['name']
        assert resource_3['name'] == 'AllstarFull_3.csv', resource_3['name']
Example #17
0
    def test_download_datapackage(self):
        '''Test downloading a DataPackage file of a package.

        '''
        user = factories.Sysadmin()
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        dataset = factories.Dataset()

        # Add a resource with a linked-to, not uploaded, data file.
        linked_resource = factories.Resource(
            package_id=dataset['id'],
            url='http://www.foo.com/data.csv',
            schema='{"fields":[{"type":"string", "name":"col1"}]}',
        )

        # Add a resource with an uploaded data file.
        csv_path = 'lahmans-baseball-database/AllstarFull.csv'
        csv_file = custom_helpers.get_csv_file(csv_path)
        uploaded_resource = api.action.resource_create(
            package_id=dataset['id'],
            name='AllstarFull',
            url='_needed_for_ckan<2.6',
            upload=csv_file,
        )

        # Download the package's JSON file.
        url = toolkit.url_for('export_datapackage',
                              package_id=dataset['name'])
        response = self.app.get(url)

        # Open and validate the response as a JSON.
        dp = datapackage.DataPackage(json.loads(response.body))
        dp.validate()

        # Check the contents of the datapackage.json file.
        nose.tools.assert_equals(dataset['name'], dp.descriptor['name'])

        resources = dp.resources
        nose.tools.assert_equals(len(resources), 2)
        nose.tools.assert_equals(linked_resource['url'],
                                 resources[0].descriptor['path'])
        nose.tools.assert_equals(linked_resource['url'],
                                 resources[0].source)
        schema = resources[0].schema
        nose.tools.assert_equals(
            schema.descriptor['fields'][0]['name'], 'col1')
        nose.tools.assert_equals(
            schema.descriptor['fields'][0]['type'], 'string')

        nose.tools.assert_equals(uploaded_resource['url'],
                                 resources[1].descriptor['path'])
Example #18
0
    def test_resource_update_to_duplicate_name(self):
        user = factories.User()
        dataset = factories.Dataset(user=user)
        resource_1 = factories.Resource(dataset=dataset)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        resource_2 = api.action.resource_create(
            package_id=dataset['id'],
            name='test-resource',
            upload=custom_helpers.get_csv_file(
                'test-data/lahmans-baseball-database/AllstarFull.csv'))

        resource_2['name'] = resource_1['name']
        nose.tools.assert_raises(toolkit.ValidationError, helpers.call_action,
                                 'resource_update', **resource_2)
    def test_resource_update_to_url(self):
        user = factories.User()
        dataset = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        resource = api.action.resource_create(package_id=dataset['id'],
            upload=custom_helpers.get_csv_file(
                'test-data/lahmans-baseball-database/AllstarFull.csv'))
        original_name = resource['name']

        resource['url'] = 'http://example-resource.com/foo.csv'
        helpers.call_action('resource_update', **resource)

        resource = helpers.call_action('resource_show', id=resource['id'])
        assert resource['name'] == original_name
    def test_resource_update_to_good_name(self):
        user = factories.User()
        dataset = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        resource = api.action.resource_create(package_id=dataset['id'],
            name='test-resource',
            upload=custom_helpers.get_csv_file(
                'test-data/lahmans-baseball-database/AllstarFull.csv'))

        resource['name'] = 'new-name'
        helpers.call_action('resource_update', **resource)

        resource = helpers.call_action('resource_show', id=resource['id'])
        assert resource['name'] == 'new-name'
    def test_resource_create_with_custom_name(self):
        '''If you create a resource and given a unique custom name, that name
        should be taken as the resource's name.

        '''
        user = factories.User()
        dataset = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])

        resource = api.action.resource_create(package_id=dataset['id'],
            name='test-resource',
            upload=custom_helpers.get_csv_file(
                'test-data/lahmans-baseball-database/AllstarFull.csv'))

        assert resource['name'] == 'test-resource'
    def test_resource_create_with_no_format(self):
        '''If no format is given when creating a resource, the resource's
        format should be set to CSV.

        '''
        user = factories.User()
        dataset = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])

        resource = api.action.resource_create(package_id=dataset['id'],
            upload=custom_helpers.get_csv_file(
                'test-data/lahmans-baseball-database/AllstarFull.csv'))

        resource = api.action.resource_show(id=resource['id'])
        assert resource['format'] == 'CSV'
Example #23
0
    def test_resource_update_to_good_name(self):
        user = factories.User()
        dataset = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        resource = api.action.resource_create(
            package_id=dataset['id'],
            name='test-resource',
            upload=custom_helpers.get_csv_file(
                'test-data/lahmans-baseball-database/AllstarFull.csv'))

        resource['name'] = 'new-name'
        helpers.call_action('resource_update', **resource)

        resource = helpers.call_action('resource_show', id=resource['id'])
        assert resource['name'] == 'new-name'
Example #24
0
    def test_resource_update_to_url(self):
        user = factories.User()
        dataset = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        resource = api.action.resource_create(
            package_id=dataset['id'],
            upload=custom_helpers.get_csv_file(
                'test-data/lahmans-baseball-database/AllstarFull.csv'))
        original_name = resource['name']

        resource['url'] = 'http://example-resource.com/foo.csv'
        helpers.call_action('resource_update', **resource)

        resource = helpers.call_action('resource_show', id=resource['id'])
        assert resource['name'] == original_name
Example #25
0
    def test_resource_create_with_custom_name(self):
        '''If you create a resource and given a unique custom name, that name
        should be taken as the resource's name.

        '''
        user = factories.User()
        dataset = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])

        resource = api.action.resource_create(
            package_id=dataset['id'],
            name='test-resource',
            upload=custom_helpers.get_csv_file(
                'test-data/lahmans-baseball-database/AllstarFull.csv'))

        assert resource['name'] == 'test-resource'
Example #26
0
    def test_resource_create_with_no_format(self):
        '''If no format is given when creating a resource, the resource's
        format should be set to CSV.

        '''
        user = factories.User()
        dataset = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])

        resource = api.action.resource_create(
            package_id=dataset['id'],
            upload=custom_helpers.get_csv_file(
                'test-data/lahmans-baseball-database/AllstarFull.csv'))

        resource = api.action.resource_show(id=resource['id'])
        assert resource['format'] == 'CSV'
    def test_get_path_to_resource_file_with_uploaded_file(self):

        user = factories.User()
        package = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        csv_file = custom_helpers.get_csv_file('test-data/datetimes.csv')
        resource = api.action.resource_create(package_id=package['id'],
                                              upload=csv_file)

        path = util.get_path_to_resource_file(resource)

        # Check that the path is correct by comparing the contents of the
        # uploaded copy of the file to the original file.
        assert open(path).read() == (
            open(os.path.join(os.path.split(__file__)[0],
                '../test-data/datetimes.csv')).read())
    def test_schema_output_is_json_string(self):
        '''Test that a resource's schema output is a valid json string'''
        user = factories.Sysadmin()
        package = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])

        csv_file = custom_helpers.get_csv_file(
            'test-data/lahmans-baseball-database/AllstarFull.csv')
        resource = api.action.resource_create(package_id=package['id'],
                                              name='test-resource',
                                              upload=csv_file)
        resource_show = api.action.resource_show(id=resource['id'])

        #try and load the schema as json string
        print resource_show['schema']
        common.json.loads(resource_show['schema'])
Example #29
0
    def test_schema_output_is_json_string(self):
        '''Test that a resource's schema output is a valid json string'''
        user = factories.Sysadmin()
        package = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])

        csv_file = custom_helpers.get_csv_file(
            'test-data/lahmans-baseball-database/AllstarFull.csv')
        resource = api.action.resource_create(package_id=package['id'],
                                              name='test-resource',
                                              upload=csv_file)
        resource_show = api.action.resource_show(id=resource['id'])

        #try and load the schema as json string
        print resource_show['schema']
        common.json.loads(resource_show['schema'])
    def test_resource_create_with_no_name(self):
        '''If you create a new resource and upload a data file but don't give
        a name for the resource, the resource name should be set to the name
        of the data file.

        '''
        user = factories.User()
        package = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])

        csv_file = custom_helpers.get_csv_file(
            'test-data/lahmans-baseball-database/AllstarFull.csv')
        resource = api.action.resource_create(package_id=package['id'],
                                              upload=csv_file)

        resource = api.action.resource_show(id=resource['id'])
        assert resource['name'] == 'AllstarFull.csv', resource['name']
    def test_resource_create_with_custom_format(self):
        '''If a format other than CSV is given when creating a resource, it
        should be accepted.

        '''
        user = factories.User()
        dataset = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])

        for format_ in ('jpeg', 'image/png', 'foobar'):
            resource = api.action.resource_create(package_id=dataset['id'],
                format=format_,
                upload=custom_helpers.get_csv_file(
                    'test-data/lahmans-baseball-database/AllstarFull.csv'))

            resource = api.action.resource_show(id=resource['id'])
            assert resource['format'] == format_
Example #32
0
    def test_resource_create_with_no_name(self):
        '''If you create a new resource and upload a data file but don't give
        a name for the resource, the resource name should be set to the name
        of the data file.

        '''
        user = factories.User()
        package = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])

        csv_file = custom_helpers.get_csv_file(
            'test-data/lahmans-baseball-database/AllstarFull.csv')
        resource = api.action.resource_create(package_id=package['id'],
                                              upload=csv_file)

        resource = api.action.resource_show(id=resource['id'])
        assert resource['name'] == 'AllstarFull.csv', resource['name']
Example #33
0
    def test_get_path_to_resource_file_with_uploaded_file(self):

        user = factories.User()
        package = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        csv_file = custom_helpers.get_csv_file('test-data/datetimes.csv')
        resource = api.action.resource_create(package_id=package['id'],
                                              upload=csv_file)

        path = util.get_path_to_resource_file(resource)

        # Check that the path is correct by comparing the contents of the
        # uploaded copy of the file to the original file.
        assert open(path).read() == (open(
            os.path.join(
                os.path.split(__file__)[0],
                '../test-data/datetimes.csv')).read())
Example #34
0
    def test_resource_create_with_custom_format(self):
        '''If a format other than CSV is given when creating a resource, it
        should be accepted.

        '''
        user = factories.User()
        dataset = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])

        for format_ in ('jpeg', 'image/png', 'foobar'):
            resource = api.action.resource_create(
                package_id=dataset['id'],
                format=format_,
                upload=custom_helpers.get_csv_file(
                    'test-data/lahmans-baseball-database/AllstarFull.csv'))

            resource = api.action.resource_show(id=resource['id'])
            assert resource['format'] == format_
    def test_resource_update_with_missing_name(self):
        '''If a user updates a resource and does not pass a name field, the
        name should be changed to the name of the resource's uploaded file.

        '''
        user = factories.User()
        dataset = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        resource = api.action.resource_create(package_id=dataset['id'],
            name='test-resource',
            upload=custom_helpers.get_csv_file(
                'test-data/lahmans-baseball-database/AllstarFull.csv'))

        del resource['name']
        helpers.call_action('resource_update', **resource)

        resource = helpers.call_action('resource_show', id=resource['id'])
        assert resource['name'] == 'AllstarFull.csv'
Example #36
0
    def test_resource_update_with_missing_name(self):
        '''If a user updates a resource and does not pass a name field, the
        name should be changed to the name of the resource's uploaded file.

        '''
        user = factories.User()
        dataset = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        resource = api.action.resource_create(
            package_id=dataset['id'],
            name='test-resource',
            upload=custom_helpers.get_csv_file(
                'test-data/lahmans-baseball-database/AllstarFull.csv'))

        del resource['name']
        helpers.call_action('resource_update', **resource)

        resource = helpers.call_action('resource_show', id=resource['id'])
        assert resource['name'] == 'AllstarFull.csv'
    def test_resource_update_with_empty_format(self):
        '''If an empty format is given when updating a resource, the resource's
        format should not be changed.

        '''
        user = factories.User()
        dataset = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        resource = api.action.resource_create(package_id=dataset['id'],
            upload=custom_helpers.get_csv_file(
                'test-data/lahmans-baseball-database/AllstarFull.csv'))
        old_format = api.action.resource_show(id=resource['id'])['format']

        for format_ in ('', ' '):
            api.action.resource_update(id=resource['id'], format=format_,
                url=resource['url'])

            resource = api.action.resource_show(id=resource['id'])
            assert resource['format'] == old_format
Example #38
0
    def test_resource_update_with_empty_format(self):
        '''If an empty format is given when updating a resource, the resource's
        format should not be changed.

        '''
        user = factories.User()
        dataset = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        resource = api.action.resource_create(
            package_id=dataset['id'],
            upload=custom_helpers.get_csv_file(
                'test-data/lahmans-baseball-database/AllstarFull.csv'))
        old_format = api.action.resource_show(id=resource['id'])['format']

        for format_ in ('', ' '):
            api.action.resource_update(id=resource['id'],
                                       format=format_,
                                       url=resource['url'])

            resource = api.action.resource_show(id=resource['id'])
            assert resource['format'] == old_format
Example #39
0
    def test_non_csv_file(self):
        '''When viewing the page of a resource whose file is not a CSV file,
        an error should be shown instead of the CSV preview.

        '''
        user = factories.User()
        dataset = factories.Dataset(user=user)
        non_csv_file = custom_helpers.get_csv_file('test-data/not-a-csv.png')
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        resource = api.action.resource_create(package_id=dataset['id'],
                                              upload=non_csv_file)

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

        soup = response.html
        divs = soup('div', class_='ckanext-datapreview')
        assert len(divs) == 1
        div = divs[0]
        assert div.text.strip().startswith('Error: ')
    def test_resource_schema_field_update_date(self):
        '''Test that a temporal extent is added to the field, when updating
        a field and settings its type to date.

        '''
        user = factories.User()
        package = factories.Dataset(user=user)
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        csv_file = custom_helpers.get_csv_file(
            'test-data/lahmans-baseball-database/AllstarFull.csv')
        resource = api.action.resource_create(package_id=package['id'],
                                              upload=csv_file)

        # Re-create the field with date type.
        api.action.resource_schema_field_update(resource_id=resource['id'],
                                                index=1, name='yearID',
                                                type='date')

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

        # Unit tests elsewhere check that the temporal extent value is correct,
        # here we're just testing that it's present.
        assert 'temporal_extent' in field
Example #41
0
    def test_foreign_key_is_on_page_list(self):
        user = factories.User()
        dataset = factories.Dataset(user=user)
        csv_file0 = custom_helpers.get_csv_file(
            'test-data/lahmans-baseball-database/BattingPost.csv')
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        resource0 = api.action.resource_create(package_id=dataset['id'],
                                              upload=csv_file0)
        schema0 = api.action.resource_schema_show(resource_id=resource0['id'])


        csv_file1 = custom_helpers.get_csv_file(
            'test-data/lahmans-baseball-database/AllstarFull.csv')
        api = ckanapi.TestAppCKAN(self.app, apikey=user['apikey'])
        resource1 = api.action.resource_create(package_id=dataset['id'],
                                              upload=csv_file1)
        schema1 = api.action.resource_schema_show(resource_id=resource1['id'])

        helpers.call_action(
            'resource_schema_fkey_create',
            field=["playerID", "yearID"],
            resource_id=resource1['id'],
            referenced_resource_id=resource0['id'],
            referenced_field=["playerID", "yearID"],
        )

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

        soup = response.html
        nose.tools.assert_true(soup.find(text='Foreign keys'))

        fkey = soup.find(id='foreign-key')
        #check that the name of the referned csv file appears
        nose.tools.assert_true('BattingPost.csv' in fkey.text)

        fkeys = fkey.find_all('li')

        #check the source link
        nose.tools.assert_equals(
            fkeys[0].a.attrs['href'],
            '/package/{0}/file/{1}/schema/0'.format(
                dataset['id'], resource1['id'])
        )
        nose.tools.assert_equals(fkeys[0].a.text, 'playerID')

        nose.tools.assert_equals(
            fkeys[1].a.attrs['href'],
            '/package/{0}/file/{1}/schema/1'.format(
                dataset['id'], resource1['id'])
        )
        nose.tools.assert_equals(fkeys[1].a.text, 'yearID')

        #check the destination link
        nose.tools.assert_equals(
            fkeys[2].a.attrs['href'],
            '/package/{0}/file/{1}/schema/2'.format(
                dataset['id'], resource0['id'])
        )
        nose.tools.assert_equals(fkeys[2].a.text, 'playerID')

        #check the destination link
        nose.tools.assert_equals(
            fkeys[3].a.attrs['href'],
            '/package/{0}/file/{1}/schema/0'.format(
                dataset['id'], resource0['id'])
        )
        nose.tools.assert_equals(fkeys[3].a.text, 'yearID')