Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to modify extras, fix #78 #79

Merged
merged 2 commits into from
Jul 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion udata/core/dataset/api.py
Expand Up @@ -82,7 +82,7 @@ def put(self, dataset):
if dataset.deleted:
api.abort(410, 'Dataset has been deleted')
DatasetEditPermission(dataset).test()
form = api.validate(DatasetForm, dataset)
form = api.validate(DatasetFullForm, dataset)
return form.save()

@api.secure
Expand Down
2 changes: 1 addition & 1 deletion udata/forms/fields.py
Expand Up @@ -474,7 +474,7 @@ def process_formdata(self, valuelist):
else:
raise 'Unsupported datatype'
else:
self.data = {}
self.data = self.data or {}

def pre_validate(self, form):
if self.data:
Expand Down
69 changes: 69 additions & 0 deletions udata/tests/api/test_datasets_api.py
Expand Up @@ -130,6 +130,75 @@ def test_dataset_api_update(self):
self.assertEqual(Dataset.objects.count(), 1)
self.assertEqual(Dataset.objects.first().description, 'new description')

def test_dataset_api_update_with_extras(self):
'''It should update a dataset from the API with extras parameters'''
user = self.login()
dataset = DatasetFactory(owner=user)
data = dataset.to_dict()
data['extras'] = {
'integer': 42,
'float': 42.0,
'string': 'value',
}
response = self.put(url_for('api.dataset', dataset=dataset), data)
self.assert200(response)
self.assertEqual(Dataset.objects.count(), 1)

dataset = Dataset.objects.first()
self.assertEqual(dataset.extras['integer'], 42)
self.assertEqual(dataset.extras['float'], 42.0)
self.assertEqual(dataset.extras['string'], 'value')

def test_dataset_api_update_with_no_extras(self):
'''It should update a dataset from the API with no extras

In that case the extras parameters are kept.
'''
data = DatasetFactory.attributes()
data['extras'] = {
'integer': 42,
'float': 42.0,
'string': 'value',
}
with self.api_user():
response = self.post(url_for('api.datasets'), data)

dataset = Dataset.objects.first()
data = dataset.to_dict()
del data['extras']
response = self.put(url_for('api.dataset', dataset=dataset), data)
self.assert200(response)
self.assertEqual(Dataset.objects.count(), 1)

dataset = Dataset.objects.first()
self.assertEqual(dataset.extras['integer'], 42)
self.assertEqual(dataset.extras['float'], 42.0)
self.assertEqual(dataset.extras['string'], 'value')

def test_dataset_api_update_with_empty_extras(self):
'''It should update a dataset from the API with empty extras

In that case the extras parameters are set to an empty dict.
'''
data = DatasetFactory.attributes()
data['extras'] = {
'integer': 42,
'float': 42.0,
'string': 'value',
}
with self.api_user():
response = self.post(url_for('api.datasets'), data)

dataset = Dataset.objects.first()
data = dataset.to_dict()
data['extras'] = {}
response = self.put(url_for('api.dataset', dataset=dataset), data)
self.assert200(response)
self.assertEqual(Dataset.objects.count(), 1)

dataset = Dataset.objects.first()
self.assertEqual(dataset.extras, {})

def test_dataset_api_update_deleted(self):
'''It should not update a deleted dataset from the API and raise 401'''
user = self.login()
Expand Down