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

Fix 500 errors caused by templates #395

Merged
merged 3 commits into from
May 10, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/logic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,26 @@ def generate_signed_url():
props=filedata[relative_path])
res_payload['filedata'][relative_path] = response.build_file_information()
return res_payload


#### helpers

def validate_for_template(descriptor):
'''
Cheks if descriptor fields have expected type for template Eg:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small typo: "Checks ..."

Copy link
Contributor

@anuveyatsu anuveyatsu May 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably it's better to rewrite:
"This function validates field types in the descriptor, e.g., licenses property should be a list."

licenses should be list
'''
licenses = descriptor.get('licenses')

if licenses is None or type(licenses) is list:
return descriptor

if type(licenses) is dict:
license = descriptor.pop('licenses')
license = license.get('type')
descriptor['license'] = license
return descriptor

descriptor.pop('licenses')

return descriptor
31 changes: 31 additions & 0 deletions tests/logic/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,34 @@ def tearDown(self):
db.session.remove()
db.drop_all()
db.engine.dispose()

class HelpersTest(unittest.TestCase):


def setUp(self):
self.descriptor = json.loads(open('fixtures/datapackage.json').read())


def test_validate_for_jinja_returns_descriptor_if_no_licenses(self):
descriptor = validate_for_template(self.descriptor)
self.assertEqual(self.descriptor, descriptor)

def test_validate_for_jinja_returns_descriptor_if_licenses_is_list(self):
self.descriptor['licenses'] = []
descriptor = validate_for_template(self.descriptor)
self.assertEqual(self.descriptor, descriptor)

def test_validate_for_jinja_modifies_descriptor_if_licenses_is_dict(self):
self.descriptor['licenses'] = {'url': 'test/url', 'type': 'Test'}
descriptor = validate_for_template(self.descriptor)
self.assertEqual(descriptor['license'], 'Test')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so licenses (plural) then it is a list, and if license singular then it is a string?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes


def test_validate_for_not_errors_if_licenses_is_dict_and_has_no_type_key(self):
self.descriptor['licenses'] = {'url': 'test/url'}
descriptor = validate_for_template(self.descriptor)
self.assertEqual(descriptor['license'], None)

def test_validate_for_jinja_removes_licenses_if_invalid_type(self):
self.descriptor['licenses'] = 1
descriptor = validate_for_template(self.descriptor)
self.assertEqual(descriptor.get('licenses'), None)