Skip to content

Commit

Permalink
Update reports wrong field names.
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Sep 7, 2012
1 parent ebd57d8 commit 3fd0d90
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
11 changes: 10 additions & 1 deletion ckanext/datastore/db.py
Expand Up @@ -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:
Expand Down Expand Up @@ -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]
Expand All @@ -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))
Expand Down
61 changes: 58 additions & 3 deletions ckanext/datastore/tests/test_datastore.py
Expand Up @@ -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']))
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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'],
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3fd0d90

Please sign in to comment.