From 3fd0d90ca90367c419a9b9ccce4179aaaad80ed1 Mon Sep 17 00:00:00 2001 From: Dominik Moritz Date: Fri, 7 Sep 2012 15:08:34 +0100 Subject: [PATCH] Update reports wrong field names. --- ckanext/datastore/db.py | 11 +++- ckanext/datastore/tests/test_datastore.py | 61 +++++++++++++++++++++-- 2 files changed, 68 insertions(+), 4 deletions(-) diff --git a/ckanext/datastore/db.py b/ckanext/datastore/db.py index 74cb981ea56..953acb5cd50 100644 --- a/ckanext/datastore/db.py +++ b/ckanext/datastore/db.py @@ -418,6 +418,7 @@ def upsert_data(context, data_dict): records = data_dict['records'] sql_columns = ", ".join(['"%s"' % name for name in field_names] + ['"_full_text"']) + if method in [UPDATE, UPSERT]: unique_keys = _get_unique_key(context, data_dict) if len(unique_keys) < 1: @@ -455,7 +456,7 @@ def upsert_data(context, data_dict): if field not in record] if missing_fields: raise p.toolkit.ValidationError({ - 'key': [u'rows "{0}" are missing but needed as key'.format( + 'key': [u'fields "{0}" are missing but needed as key'.format( ', '.join(missing_fields))] }) unique_values = [record[key] for key in unique_keys] @@ -464,6 +465,14 @@ def upsert_data(context, data_dict): used_values = [record[field] for field in used_field_names] full_text = _to_full_text(fields, record) + non_existing_filed_names = [field for field in used_field_names + if field not in field_names] + if non_existing_filed_names: + raise p.toolkit.ValidationError({ + 'fields': [u'fields "{0}" do not exist'.format( + ', '.join(missing_fields))] + }) + sql_string = u''' update "{res_id}" set ({columns}, "_full_text") = ({values}, to_tsvector(%s)) diff --git a/ckanext/datastore/tests/test_datastore.py b/ckanext/datastore/tests/test_datastore.py index f1c80463766..a83b33feefa 100644 --- a/ckanext/datastore/tests/test_datastore.py +++ b/ckanext/datastore/tests/test_datastore.py @@ -535,6 +535,21 @@ def test_insert(self): assert results.rowcount == 3 + def test_insert_non_existing_field(self): + data = { + 'resource_id': self.data['resource_id'], + 'method': 'insert', + 'records': [{u'b\xfck': 'annakarenina', 'dummy': 'tolkien'}] + } + + postparams = '%s=1' % json.dumps(data) + auth = {'Authorization': str(self.sysadmin_user.apikey)} + res = self.app.post('/api/action/datastore_upsert', params=postparams, + extra_environ=auth, status=409) + res_dict = json.loads(res.body) + + assert res_dict['success'] is False + def test_update(self): c = model.Session.connection() results = c.execute('select 1 from "{0}"'.format(self.data['resource_id'])) @@ -546,7 +561,7 @@ def test_update(self): data = { 'resource_id': self.data['resource_id'], 'method': 'update', - 'records': [{u'b\xfck': hhguide, 'author': 'adams'}] + 'records': [{'author': 'adams', u'b\xfck': hhguide}] } postparams = '%s=1' % json.dumps(data) @@ -571,11 +586,11 @@ def test_update(self): assert results.rowcount == 1 model.Session.remove() - #update only the publish date + # update only the publish date data = { 'resource_id': self.data['resource_id'], 'method': 'update', - 'records': [{u'b\xfck': hhguide, 'published': '1979-1-1'}] + 'records': [{'published': '1979-1-1', u'b\xfck': hhguide}] } postparams = '%s=1' % json.dumps(data) @@ -596,6 +611,31 @@ def test_update(self): assert records[2].published == datetime.datetime(1979, 1, 1) model.Session.remove() + # delete publish date + data = { + 'resource_id': self.data['resource_id'], + 'method': 'update', + 'records': [{u'b\xfck': hhguide, 'published': None}] + } + + postparams = '%s=1' % json.dumps(data) + auth = {'Authorization': str(self.sysadmin_user.apikey)} + res = self.app.post('/api/action/datastore_upsert', params=postparams, + extra_environ=auth) + res_dict = json.loads(res.body) + + assert res_dict['success'] is True + + c = model.Session.connection() + results = c.execute('select * from "{0}"'.format(self.data['resource_id'])) + assert results.rowcount == 3 + + records = results.fetchall() + assert records[2][u'b\xfck'] == hhguide + assert records[2].author == 'adams' + assert records[2].published == None + model.Session.remove() + def test_update_missing_key(self): data = { 'resource_id': self.data['resource_id'], @@ -626,6 +666,21 @@ def test_update_non_existing_key(self): assert res_dict['success'] is False + def test_update_non_existing_field(self): + data = { + 'resource_id': self.data['resource_id'], + 'method': 'update', + 'records': [{u'b\xfck': 'annakarenina', 'dummy': 'tolkien'}] + } + + postparams = '%s=1' % json.dumps(data) + auth = {'Authorization': str(self.sysadmin_user.apikey)} + res = self.app.post('/api/action/datastore_upsert', params=postparams, + extra_environ=auth, status=409) + res_dict = json.loads(res.body) + + assert res_dict['success'] is False + class TestDatastoreDelete(tests.WsgiAppCase): sysadmin_user = None