def test_dataset_displays_custom_fields(self, app):
        user = Sysadmin()
        Dataset(
            user=user,
            type="test-schema",
            name="set-one",
            humps=3,
            resources=[{
                "url": "http://example.com/camel.txt",
                "camels_in_photo": 2
            }],
        )

        response = app.get("/dataset/set-one")
        assert "Humps" in response.body
Exemplo n.º 2
0
    def test_choice_field_shows_list_if_multiple_options(self, app):
        user = Sysadmin()
        Dataset(
            user=user,
            type="test-schema",
            name="with-multiple-choice-n",
            personality=["friendly", "spits"],
        )

        response = app.get("/dataset/with-multiple-choice-n")

        assert (
            "<ul><li>Often friendly</li><li>Tends to spit</li></ul>"
            in response.body
        )
    def test_dataset_displays_custom_fields(self):
        user = Sysadmin()
        Dataset(user=user,
                type='test-schema',
                name='set-one',
                humps=3,
                photographer='John Newton',
                photographer_email='*****@*****.**',
                resources=[{
                    'url': "http://example.com/camel.txt",
                    'camels_in_photo': 2
                }])

        app = self._get_test_app()
        response = app.get(url='/dataset/set-one')
        assert_true('Humps' in response.body)
Exemplo n.º 4
0
    def test_resource_form_create_url(self):
        dataset = Dataset()

        app = self._get_test_app()
        env, response = _get_resource_new_page_as_sysadmin(app, dataset['id'])
        form = response.forms['resource-edit']

        value = 'https://example.com/schemas.json'

        form['url'] = 'https://example.com/data.csv'
        form['schema_json'] = value

        submit_and_follow(app, form, env, 'save')

        dataset = call_action('package_show', id=dataset['id'])

        assert_equals(dataset['resources'][0]['schema'], value)
Exemplo n.º 5
0
    def test_resource_form_update(self, app):
        value = {"a": 1, "b": 2}
        dataset = Dataset(
            type="test-schema",
            resources=[{
                "url": "http://example.com/data.csv",
                "a_resource_json_field": value,
            }],
        )

        env, response = _get_resource_update_page_as_sysadmin(
            app, dataset["id"], dataset["resources"][0]["id"])
        form = BeautifulSoup(response.body).select_one("#resource-edit")
        assert form.select_one(
            "textarea[name=a_resource_json_field]").text == json.dumps(
                value, indent=2)

        url = ckantoolkit.h.url_for(
            "test-schema_resource.edit",
            id=dataset["id"],
            resource_id=dataset["resources"][0]["id"],
        )
        if not url.startswith('/'):  # ckan < 2.9
            url = '/dataset/{ds}/resource_edit/{rs}'.format(
                ds=dataset["id"], rs=dataset["resources"][0]["id"])

        value = {"a": 1, "b": 2, "c": 3}
        json_value = json.dumps(value)

        data = {
            "id": dataset["resources"][0]["id"],
            "save": "",
            "a_resource_json_field": json_value,
            "name": dataset["name"],
        }
        try:
            app.post(url,
                     environ_overrides=env,
                     data=data,
                     follow_redirects=False)
        except TypeError:
            app.post(url.encode('ascii'), params=data, extra_environ=env)

        dataset = call_action("package_show", id=dataset["id"])

        assert dataset["resources"][0]["a_resource_json_field"] == value
    def test_resource_displays_custom_fields(self):
        user = Sysadmin()
        d = Dataset(user=user,
                    type='camel-photos',
                    name='set-two',
                    humps=3,
                    resources=[{
                        'url': "http://example.com/camel.txt",
                        'camels_in_photo': 2,
                        'date': '2015-01-01'
                    }])

        app = self._get_test_app()
        response = app.get(url='/dataset/set-two/resource/' +
                           d['resources'][0]['id'])
        assert_true('Camels in Photo' in response.body)
        assert_true('Date' in response.body)
    def test_resource_displays_custom_fields(self, app):
        user = Sysadmin()
        d = Dataset(
            user=user,
            type="test-schema",
            name="set-two",
            humps=3,
            resources=[{
                "url": "http://example.com/camel.txt",
                "camels_in_photo": 2,
                "date": "2015-01-01",
            }],
        )

        response = app.get("/dataset/set-two/resource/" +
                           d["resources"][0]["id"])
        assert "Camels in Photo" in response.body
        assert "Date" in response.body
Exemplo n.º 8
0
    def test_resource_form_create_upload(self, mock_open):
        dataset = Dataset()

        app = self._get_test_app()
        env, response = _get_resource_new_page_as_sysadmin(app, dataset['id'])
        form = response.forms['resource-edit']

        value = {'fields': [{'name': 'code'}, {'name': 'department'}]}
        json_value = json.dumps(value)

        upload = ('schema_upload', 'schema.json', json_value)
        form['url'] = 'https://example.com/data.csv'

        webtest_submit(form, 'save', upload_files=[upload], extra_environ=env)

        dataset = call_action('package_show', id=dataset['id'])

        assert_equals(dataset['resources'][0]['schema'], value)
Exemplo n.º 9
0
    def test_resource_form_create_json(self):
        dataset = Dataset()

        app = self._get_test_app()
        env, response = _get_resource_new_page_as_sysadmin(app, dataset['id'])
        form = response.forms['resource-edit']

        value = {'fields': [{'name': 'code'}, {'name': 'department'}]}
        json_value = json.dumps(value)

        form['url'] = 'https://example.com/data.csv'
        form['schema_json'] = json_value

        submit_and_follow(app, form, env, 'save')

        dataset = call_action('package_show', id=dataset['id'])

        assert_equals(dataset['resources'][0]['schema'], value)
Exemplo n.º 10
0
    def test_resource_form_create_valid(self, mock_open):
        dataset = Dataset()

        app = self._get_test_app()
        env, response = _get_resource_new_page_as_sysadmin(app, dataset['id'])
        form = response.forms['resource-edit']

        upload = ('upload', 'valid.csv', VALID_CSV)

        valid_stream = io.BufferedReader(io.BytesIO(VALID_CSV))

        with mock.patch('io.open', return_value=valid_stream):

            submit_and_follow(app, form, env, 'save', upload_files=[upload])

        dataset = call_action('package_show', id=dataset['id'])

        assert_equals(dataset['resources'][0]['validation_status'], 'success')
        assert 'validation_timestamp' in dataset['resources'][0]
Exemplo n.º 11
0
    def test_json_field_displayed(self, app):
        user = Sysadmin()
        Dataset(
            user=user,
            type="test-schema",
            name="plain-json",
            a_json_field={"a": "1", "b": "2"},
        )
        response = app.get("/dataset/plain-json")

        if six.PY3:
            expected = """{\n  "a": "1",\n  "b": "2"\n}"""
        else:
            expected = """{\n  "a": "1", \n  "b": "2"\n}"""
        expected = expected.replace(
            '"', "&#34;"
        )  # Ask webhelpers

        assert expected in response.body
        assert "Example JSON" in response.body
    def test_json_field_displayed(self):
        user = Sysadmin()
        d = Dataset(
            user=user,
            type='test-schema',
            name='plain-json',
            a_json_field={
                'a': '1',
                'b': '2'
            },
        )
        app = self._get_test_app()
        response = app.get(url='/dataset/plain-json')

        expected = '''{
  "a": "1", 
  "b": "2"
}'''.replace('"', '&#34;')  # Ask webhelpers

        assert_in(expected, response.body)
        assert_in('Example JSON', response.body)
Exemplo n.º 13
0
    def test_resource_form_create(self):
        dataset = Dataset(type='test-schema')

        app = self._get_test_app()
        env, response = _get_resource_new_page_as_sysadmin(app, dataset['id'])
        form = response.forms['resource-edit']

        value = {
            'a': 1,
            'b': 2,
        }
        json_value = json.dumps(value)

        form['url'] = 'http://example.com/data.csv'
        form['a_resource_json_field'] = json_value

        submit_and_follow(app, form, env, 'save')

        dataset = call_action('package_show', id=dataset['id'])

        assert_equals(dataset['resources'][0]['a_resource_json_field'], value)
Exemplo n.º 14
0
    def test_resource_form_create_invalid(self, mock_open):
        dataset = Dataset()

        app = self._get_test_app()
        env, response = _get_resource_new_page_as_sysadmin(app, dataset['id'])
        form = response.forms['resource-edit']

        upload = ('upload', 'invalid.csv', INVALID_CSV)

        invalid_stream = io.BufferedReader(io.BytesIO(INVALID_CSV))

        with mock.patch('io.open', return_value=invalid_stream):

            response = webtest_submit(form,
                                      'save',
                                      upload_files=[upload],
                                      extra_environ=env)

        assert_in('validation', response.body)
        assert_in('missing-value', response.body)
        assert_in('Row 2 has a missing value in column 4', response.body)
Exemplo n.º 15
0
    def test_dataset_form_update(self, app):
        dataset = Dataset(type="test-subfields",
                          citation=[{
                              'originator': ['mei']
                          }, {
                              'originator': ['ahmed']
                          }],
                          contact_address=[{
                              'address': 'anyplace'
                          }])

        env, response = _get_package_update_page_as_sysadmin(
            app, dataset["id"])
        form = BeautifulSoup(response.body).select_one("#dataset-edit")
        assert form.select_one(
            "input[name=citation-1-originator]").attrs['value'] == 'ahmed'

        data = {"save": ""}
        data["citation-0-originator"] = ['ling']
        data["citation-1-originator"] = ['umet']
        data["contact_address-0-address"] = 'home'
        data["name"] = dataset["name"]

        url = '/test-subfields/edit/' + dataset["id"]
        try:
            app.post(url,
                     environ_overrides=env,
                     data=data,
                     follow_redirects=False)
        except TypeError:
            app.post(url.encode('ascii'), params=data, extra_environ=env)

        dataset = call_action("package_show", id=dataset["id"])

        assert dataset["citation"] == [{
            'originator': ['ling']
        }, {
            'originator': ['umet']
        }]
        assert dataset["contact_address"] == [{'address': 'home'}]
Exemplo n.º 16
0
    def test_resource_form_create(self, app):
        dataset = Dataset(type="test-schema")

        env, response = _get_resource_new_page_as_sysadmin(app, dataset["id"])

        url = ckantoolkit.h.url_for("test-schema_resource.new",
                                    id=dataset["id"])

        value = {"a": 1, "b": 2}
        json_value = json.dumps(value)

        data = {
            "id": "",
            "save": "",
            "url": "http://example.com/data.csv",
            "a_resource_json_field": json_value,
            "name": dataset["name"],
        }
        app.post(url, environ_overrides=env, data=data, follow_redirects=False)
        dataset = call_action("package_show", id=dataset["id"])

        assert dataset["resources"][0]["a_resource_json_field"] == value
Exemplo n.º 17
0
    def test_resource_form_create(self):
        dataset = Dataset()

        app = self._get_test_app()
        env, response = _get_resource_new_page_as_sysadmin(app, dataset['id'])
        form = response.forms['resource-edit']

        value = {
            'delimiter': ';',
            'headers': 2,
            'skip_rows': ['#'],
        }
        json_value = json.dumps(value)

        form['url'] = 'https://example.com/data.csv'
        form['validation_options'] = json_value

        submit_and_follow(app, form, env, 'save')

        dataset = call_action('package_show', id=dataset['id'])

        assert_equals(dataset['resources'][0]['validation_options'], value)
Exemplo n.º 18
0
    def test_resource_form_update_url(self):
        value = {'fields': [{'name': 'code'}, {'name': 'department'}]}
        dataset = Dataset(resources=[{
            'url': 'https://example.com/data.csv',
            'schema': value
        }])

        app = self._get_test_app()
        env, response = _get_resource_update_page_as_sysadmin(
            app, dataset['id'], dataset['resources'][0]['id'])
        form = response.forms['resource-edit']

        assert_equals(form['schema_json'].value, json.dumps(value, indent=2))

        value = 'https://example.com/schema.json'

        form['schema_url'] = value

        submit_and_follow(app, form, env, 'save')

        dataset = call_action('package_show', id=dataset['id'])

        assert_equals(dataset['resources'][0]['schema'], value)
Exemplo n.º 19
0
    def test_resource_form_update(self):
        value = {
            'delimiter': ';',
            'headers': 2,
            'skip_rows': ['#'],
        }

        dataset = Dataset(resources=[{
            'url': 'https://example.com/data.csv',
            'validation_options': value
        }])

        app = self._get_test_app()
        env, response = _get_resource_update_page_as_sysadmin(
            app, dataset['id'], dataset['resources'][0]['id'])
        form = response.forms['resource-edit']

        assert_equals(form['validation_options'].value,
                      json.dumps(value, indent=2, sort_keys=True))

        value = {
            'delimiter': ';',
            'headers': 2,
            'skip_rows': ['#'],
            'skip_tests': ['blank-rows'],
        }

        json_value = json.dumps(value)

        form['url'] = 'https://example.com/data.csv'
        form['validation_options'] = json_value

        submit_and_follow(app, form, env, 'save')

        dataset = call_action('package_show', id=dataset['id'])

        assert_equals(dataset['resources'][0]['validation_options'], value)
Exemplo n.º 20
0
    def test_resource_form_update(self):
        value = {
            'a': 1,
            'b': 2,
        }
        dataset = Dataset(
            type='test-schema',
            resources=[{
                'url': 'http://example.com/data.csv',
                'a_resource_json_field': value
            }]
        )

        app = self._get_test_app()
        env, response = _get_resource_update_page_as_sysadmin(
            app, dataset['id'], dataset['resources'][0]['id'])
        form = response.forms['resource-edit']

        assert_equals(
            form['a_resource_json_field'].value,
            json.dumps(value, indent=2))

        value = {
            'a': 1,
            'b': 2,
            'c': 3,
        }
        json_value = json.dumps(value)

        form['a_resource_json_field'] = json_value

        submit_and_follow(app, form, env, 'save')

        dataset = call_action('package_show', id=dataset['id'])

        assert_equals(dataset['resources'][0]['a_resource_json_field'], value)
Exemplo n.º 21
0
    def test_resource_form_update(self, app):
        value = {"a": 1, "b": 2}
        dataset = Dataset(
            type="test-schema",
            resources=[{
                "url": "http://example.com/data.csv",
                "a_resource_json_field": value,
            }],
        )

        env, response = _get_resource_update_page_as_sysadmin(
            app, dataset["id"], dataset["resources"][0]["id"])
        form = BeautifulSoup(response.body).select_one("#resource-edit")
        assert form.select_one(
            "textarea[name=a_resource_json_field]").text == json.dumps(
                value, indent=2)
        url = ckantoolkit.h.url_for(
            "test-schema_resource.edit",
            id=dataset["id"],
            resource_id=dataset["resources"][0]["id"],
        )

        value = {"a": 1, "b": 2, "c": 3}
        json_value = json.dumps(value)

        data = {
            "id": dataset["resources"][0]["id"],
            "save": "",
            "a_resource_json_field": json_value,
            "name": dataset["name"],
        }
        app.post(url, environ_overrides=env, data=data, follow_redirects=False)

        dataset = call_action("package_show", id=dataset["id"])

        assert dataset["resources"][0]["a_resource_json_field"] == value
Exemplo n.º 22
0
    def test_resource_form_fields_are_persisted(self):

        dataset = Dataset(resources=[
            {
                'url': 'https://example.com/data.csv',
                'validation_status': 'success',
                'validation_timestamp': datetime.datetime.now().isoformat()
            }
        ])

        app = self._get_test_app()
        env, response = _get_resource_update_page_as_sysadmin(
            app, dataset['id'], dataset['resources'][0]['id'])
        form = response.forms['resource-edit']

        form['description'] = 'test desc'

        submit_and_follow(app, form, env, 'save')

        dataset = call_action('package_show', id=dataset['id'])

        assert_equals(dataset['resources'][0]['validation_status'], 'success')
        assert 'validation_timestamp' in dataset['resources'][0]
        assert_equals(dataset['resources'][0]['description'], 'test desc')
Exemplo n.º 23
0
    def test_resource_form_update(self, app):
        dataset = Dataset(
            type="test-subfields",
            citation=[{
                'originator': 'na'
            }],
            resources=[{
                "url":
                "http://example.com/data.csv",
                "schedule": [
                    {
                        "impact": "A",
                        "frequency": "1m"
                    },
                    {
                        "impact": "P",
                        "frequency": "7d"
                    },
                ]
            }],
        )

        env, response = _get_resource_update_page_as_sysadmin(
            app, dataset["id"], dataset["resources"][0]["id"])
        form = BeautifulSoup(response.body).select_one("#resource-edit")
        opt7d = form.find_all('option', {'value': '7d'})
        assert 'selected' not in opt7d[0].attrs
        assert 'selected' in opt7d[1].attrs
        assert 'selected' not in opt7d[2].attrs  # blank subfields

        url = ckantoolkit.h.url_for(
            "test-schema_resource.edit",
            id=dataset["id"],
            resource_id=dataset["resources"][0]["id"],
        )
        if not url.startswith('/'):  # ckan < 2.9
            url = '/dataset/{ds}/resource_edit/{rs}'.format(
                ds=dataset["id"], rs=dataset["resources"][0]["id"])

        data = {"id": dataset["resources"][0]["id"], "save": ""}
        data["schedule-0-frequency"] = '1y'
        data["schedule-0-impact"] = 'A'
        data["schedule-1-frequency"] = '1m'
        data["schedule-1-impact"] = 'P'

        try:
            app.post(url,
                     environ_overrides=env,
                     data=data,
                     follow_redirects=False)
        except TypeError:
            app.post(url.encode('ascii'), params=data, extra_environ=env)

        dataset = call_action("package_show", id=dataset["id"])

        assert dataset["resources"][0]["schedule"] == [
            {
                "frequency": '1y',
                "impact": 'A'
            },
            {
                "frequency": '1m',
                "impact": 'P'
            },
        ]
Exemplo n.º 24
0
    def test_resource_form_includes_json_fields(self, app):
        dataset = Dataset(type="test-schema")

        env, response = _get_resource_new_page_as_sysadmin(app, dataset["id"])
        form = BeautifulSoup(response.body).select_one("#resource-edit")
        assert form.select("textarea[name=a_resource_json_field]")