예제 #1
0
    def setup_class(cls):
        super(TestAuthorisation, cls).setup_class()
        model.repo.new_revision()
        model.Session.commit()

        users = [{
            'name': 'test_sysadmin',
            'sysadmin': True,
            'apikey': 'test_sysadmin',
            'password': '******'
        }, {
            'name': 'test_user',
            'sysadmin': False,
            'apikey': 'test_user',
            'password': '******'
        }, {
            'name': 'test_user2',
            'sysadmin': False,
            'apikey': 'test_user2',
            'password': '******'
        }]
        CreateTestData.create_users(users)
        cls.test_user = user_model.User.get('test_user')
        cls.test_sysadmin = user_model.User.get('test_sysadmin')
        cls.api_test_sysadmin = ckanapi.TestAppCKAN(
            cls.app, apikey=cls.test_sysadmin.apikey)
        cls.api_test_user = ckanapi.TestAppCKAN(cls.app,
                                                apikey=cls.test_user.apikey)
        org_dict = {'name': 'test_organisation', 'title': 'Test Organisation'}
        cls.api_test_sysadmin.action.organization_create(**org_dict)
        cls.TEST_DATADICT = copy.deepcopy(TEST_DATADICT)
        cls.TEST_DATADICT['owner_org'] = 'test_organisation'
예제 #2
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']
예제 #3
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
예제 #4
0
    def test_resource_upload(self):
        '''Test a basic resource file upload'''
        factories.Sysadmin(apikey="my-test-key")

        app = self._get_test_app()
        demo = ckanapi.TestAppCKAN(app, apikey='my-test-key')
        factories.Dataset(name="my-dataset")

        file_path = os.path.join(os.path.dirname(__file__), 'data.csv')
        resource = demo.action.resource_create(package_id='my-dataset',
                                               upload=open(file_path),
                                               url='file.txt')

        key = '{1}/resources/{0}/data.csv' \
            .format(resource['id'],
                    config.get('ckanext.s3filestore.aws_storage_path'))

        s3 = self.botoSession.client('s3', endpoint_url=self.endpoint_url)

        # check whether the object exists in S3
        # will throw exception if not existing
        s3.head_object(Bucket='my-bucket', Key=key)

        #conn = boto.connect_s3()
        #bucket = conn.get_bucket('my-bucket')
        # test the key exists
        #assert_true(bucket.lookup(key))
        # test the file contains what's expected
        obj = s3.get_object(Bucket='my-bucket', Key=key)
        data = obj['Body'].read()
        assert_equal(data, open(file_path).read())
예제 #5
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
예제 #6
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_upload(self):
        '''Test a basic resource file upload'''
        factories.Sysadmin(apikey="my-test-key")

        app = helpers._get_test_app()
        demo = ckanapi.TestAppCKAN(app, apikey='my-test-key')
        factories.Dataset(name="my-dataset")

        file_path = os.path.join(os.path.dirname(__file__), 'data.csv')
        resource = demo.action.resource_create(package_id='my-dataset',
                                               upload=open(file_path),
                                               url='file.txt')

        key = '{1}/resources/{0}/data.csv' \
            .format(resource['id'],
                    config.get('ckanext.s3filestore.aws_storage_path'))

        # check whether the object exists in S3
        # will throw exception if not existing
        s3.head_object(Bucket=BUCKET_NAME, Key=key)

        # test the file contains what's expected
        obj = s3.get_object(Bucket=BUCKET_NAME, Key=key)
        data = obj['Body'].read()
        assert_equal(data, open(file_path).read())
예제 #8
0
    def test_resource_upload_then_clear(self):
        '''Test that clearing an upload removes the S3 key'''

        sysadmin = factories.Sysadmin(apikey="my-test-key")

        app = self._get_test_app()
        demo = ckanapi.TestAppCKAN(app, apikey='my-test-key')
        dataset = factories.Dataset(name="my-dataset")

        file_path = os.path.join(os.path.dirname(__file__), 'data.csv')
        resource = demo.action.resource_create(package_id='my-dataset',
                                               upload=open(file_path),
                                               url='file.txt')

        key = '{1}/resources/{0}/data.csv' \
            .format(resource['id'],
                    config.get('ckanext.s3filestore.aws_storage_path'))

        conn = boto.connect_s3()
        bucket = conn.get_bucket('my-bucket')
        # test the key exists
        assert_true(bucket.lookup(key))

        # clear upload
        url = toolkit.url_for(controller='package', action='resource_edit',
                              id=dataset['id'], resource_id=resource['id'])
        env = {'REMOTE_USER': sysadmin['name'].encode('ascii')}
        app.post(url, {'clear_upload': True,
                       'url': 'http://asdf', 'save': 'save'},
                 extra_environ=env)

        # key shouldn't exist
        assert_false(bucket.lookup(key))
예제 #9
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
예제 #10
0
 def setUp(self):
     try:
         import paste.fixture
     except ImportError:
         raise unittest.SkipTest('paste not importable')
     self.test_app = paste.fixture.TestApp(wsgi_app)
     self.ckan = ckanapi.TestAppCKAN(self.test_app)
예제 #11
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
예제 #12
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
예제 #13
0
    def test_author_check(self):

        context = {'user': self.user['name']}
        dataset1 = helpers.call_action('package_create',
                                       context=context,
                                       name='syndicated_dataset1',
                                       extras=[{
                                           'key': 'syndicate',
                                           'value': 'true'
                                       }])

        dataset2 = helpers.call_action('package_create',
                                       context=context,
                                       name='syndicated_dataset2',
                                       extras=[{
                                           'key': 'syndicate',
                                           'value': 'true'
                                       }])

        with patch('ckanext.syndicate.tasks.get_target') as mock_target:
            # Mock API

            mock_target.return_value = ckanapi.TestAppCKAN(
                self._get_test_app(), apikey=self.user['apikey'])

            # Syndicate to our Test CKAN instance
            ckan = mock_target()
            mock_user_show = mock.Mock()
            mock_user_show.return_value = self.user
            ckan.action.user_show = mock_user_show

            sync_package(dataset1['id'], 'dataset/create')
            helpers.call_action('package_patch',
                                id=dataset1['id'],
                                extras=[{
                                    'key': 'syndicate',
                                    'value': 'true'
                                }])

            sync_package(dataset1['id'], 'dataset/update')
            mock_user_show.assert_called_once_with(id='test_author')
            updated1 = helpers.call_action('package_show', id=dataset1['id'])
            assert_is_not_none(
                get_pkg_dict_extra(updated1, get_syndicated_id()))

            mock_user_show = mock.Mock()
            mock_user_show.return_value = {'name': 'random-name', 'id': ''}
            ckan.action.user_show = mock_user_show

            sync_package(dataset2['id'], 'dataset/create')
            helpers.call_action('package_patch',
                                id=dataset2['id'],
                                extras=[{
                                    'key': 'syndicate',
                                    'value': 'true'
                                }])
            sync_package(dataset2['id'], 'dataset/update')
            updated2 = helpers.call_action('package_show', id=dataset2['id'])
            assert_false(get_pkg_dict_extra(updated2, get_syndicated_id()))
            del Session.revision
예제 #14
0
    def test_resource_create_url_with_no_path(self):

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

        resource = api.action.resource_create(package_id=package['id'],
                                              url='http://example.com')

        resource = api.action.resource_show(id=resource['id'])
        assert resource['name'] == 'example.com'
예제 #15
0
 def _create_resources(self):
     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=0,
                                             name='foo')
     api.action.resource_schema_field_create(resource_id=resource['id'],
                                             index=1,
                                             name='bar')
     return resource, api
예제 #16
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
예제 #17
0
    def _upload_resource(self):
        factories.Sysadmin(apikey="my-test-key")

        app = self._get_test_app()
        demo = ckanapi.TestAppCKAN(app, apikey='my-test-key')
        factories.Dataset(name="my-dataset")

        file_path = os.path.join(os.path.dirname(__file__), 'data.csv')
        resource = demo.action.resource_create(package_id='my-dataset',
                                               upload=open(file_path),
                                               url='file.txt')
        return resource, demo, app
예제 #18
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')
예제 #19
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'])
예제 #20
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)
예제 #21
0
    def test_download_tdf(self):
        '''Test downloading a Tabular Data Format ZIP 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(dataset=dataset,
            url='http://test.com/test-url-1',
            schema='{"fields":[{"type":"string", "name":"col1"}]}')

        # Add a resource with an uploaded data file.
        csv_path = '../test-data/lahmans-baseball-database/AllstarFull.csv'
        csv_file = _get_csv_file(csv_path)
        api.action.resource_create(package_id=dataset['id'],
            name='AllstarFull.csv', upload=csv_file)

        # Download the package's SDF ZIP file.
        url = toolkit.url_for(
            controller='ckanext.datapackager.controllers.package:DataPackagerPackageController',
            action='download_tabular_data_format',
            package_id=dataset['name'])
        response = self.app.get(url)

        # Open the response as a ZIP file.
        zip_ = zipfile.ZipFile(StringIO.StringIO(response.body))

        # Check that the ZIP file contains the files we expect.
        nose.tools.assert_equals(zip_.namelist(),
                                 ['AllstarFull.csv', 'datapackage.json'])

        # Extract datapackage.json from the zip file and load it as json.
        datapackage = json.load(zip_.open('datapackage.json'))

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

        resources = datapackage['resources']
        nose.tools.assert_equals(linked_resource['url'], resources[0]['url'])
        schema = resources[0]['schema']
        nose.tools.assert_equals(
            {'fields': [{'type': 'string', 'name': 'col1'}]}, schema)

        nose.tools.assert_equals(resources[1]['path'], 'AllstarFull.csv')

        # Check the contenst of the AllstarFull.csv file.
        assert (zip_.open('AllstarFull.csv').read() ==
                _get_csv_file(csv_path).read())
예제 #22
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
예제 #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'
예제 #24
0
    def test_organization_replication(self):

        local_org = factories.Organization(user=self.user,
                                           name='local-org',
                                           title="Local Org")
        helpers.call_action('member_create',
                            id=local_org['id'],
                            object=self.user['id'],
                            object_type='user',
                            capacity='editor')

        context = {
            'user': self.user['name'],
        }

        dataset = helpers.call_action('package_create',
                                      context=context,
                                      name='syndicated_dataset',
                                      owner_org=local_org['id'],
                                      extras=[
                                          {
                                              'key': 'syndicate',
                                              'value': 'true'
                                          },
                                      ])
        assert_equal(dataset['name'], 'syndicated_dataset')

        with patch('ckanext.syndicate.tasks.get_target') as mock_target:
            # Mock API

            mock_target.return_value = ckanapi.TestAppCKAN(
                self._get_test_app(), apikey=self.user['apikey'])

            # Syndicate to our Test CKAN instance
            ckan = mock_target()
            mock_org_create = mock.Mock()
            mock_org_show = mock.Mock()
            mock_org_show.side_effect = tk.ObjectNotFound
            mock_org_create.return_value = local_org

            ckan.action.organization_create = mock_org_create
            ckan.action.organization_show = mock_org_show

            sync_package(dataset['id'], 'dataset/create')

            mock_org_show.assert_called_once_with(id=local_org['name'])

            assert_true(mock_org_create.called)
예제 #25
0
    def test_resource_create_with_url(self):
        '''If you create a resource giving a URL instead of uploading a file,
        and not specifying a resource name, the filename should be extracted
        from the URL.

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

        resource = api.action.resource_create(
            package_id=package['id'],
            url='http://example.com/resources/foo.csv')

        resource = api.action.resource_show(id=resource['id'])
        assert resource['name'] == 'foo.csv'
예제 #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'
예제 #27
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'
예제 #28
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'])
예제 #29
0
    def test_syndicate_existing_package(self):
        context = {
            'user': self.user['name'],
        }

        existing = helpers.call_action(
            'package_create',
            context=_get_context(context),
            name='existing-dataset',
            notes=
            'The MapAction PowerPoint Map Pack contains a set of country level reference maps'
        )

        existing['extras'] = [
            {
                'key': 'syndicate',
                'value': 'true'
            },
        ]

        helpers.call_action('package_update',
                            context=_get_context(context),
                            **existing)

        with patch('ckanext.syndicate.tasks.get_target') as mock_target:
            mock_target.return_value = ckanapi.TestAppCKAN(
                self._get_test_app(), apikey=self.user['apikey'])

            sync_package(existing['id'], 'dataset/update')

        updated = helpers.call_action(
            'package_show',
            context=_get_context(context),
            id=existing['id'],
        )

        syndicated_id = get_pkg_dict_extra(updated, 'syndicated_id')

        syndicated = helpers.call_action(
            'package_show',
            context=_get_context(context),
            id=syndicated_id,
        )

        # Expect the id of the syndicated package to match the metadata
        # syndicated_id in the source package.
        assert_equal(syndicated['notes'], updated['notes'])
예제 #30
0
    def test_download_tdf_with_three_files(self):
        '''Upload three CSV files to a package and test downloading the ZIP.'''

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

        def filename(path):
            return os.path.split(path)[1]

        csv_paths = ('../test-data/lahmans-baseball-database/AllstarFull.csv',
            '../test-data/lahmans-baseball-database/PitchingPost.csv',
            '../test-data/lahmans-baseball-database/TeamsHalf.csv')
        for path in csv_paths:
            csv_file = _get_csv_file(path)
            api.action.resource_create(package_id=dataset['id'],
                name=filename(path), upload=csv_file)

        # Download the package's SDF ZIP file.
        url = toolkit.url_for(
            controller='ckanext.datapackager.controllers.package:DataPackagerPackageController',
            action='download_tabular_data_format',
            package_id=dataset['name'])
        response = self.app.get(url)

        # Open the response as a ZIP file.
        zip_ = zipfile.ZipFile(StringIO.StringIO(response.body))

        # Check that the ZIP file contains the files we expect.
        nose.tools.assert_equals(zip_.namelist(),
            [filename(path) for path in csv_paths] + ['datapackage.json'])

        # Extract datapackage.json from the zip file and load it as json.
        datapackage = json.load(zip_.open('datapackage.json'))

        # Check the contents of the datapackage.json file.
        nose.tools.assert_equals(dataset['name'], datapackage['name'])
        resources = datapackage['resources']
        for csv_path, resource in zip(csv_paths, resources):
            nose.tools.assert_equals(resource['path'], filename(csv_path))
            assert 'schema' in resource

        # Check the contents of the CSV files.
        for csv_path in csv_paths:
            assert (zip_.open(filename(csv_path)).read() ==
                    _get_csv_file(csv_path).read())