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 int_validator #1692

Merged
merged 12 commits into from May 2, 2014
Merged

fix int_validator #1692

merged 12 commits into from May 2, 2014

Conversation

wardi
Copy link
Contributor

@wardi wardi commented Apr 28, 2014

int_validator is currently broken in at least three cases:

  1. accepts long integers as strings, returning the long value, but an actual long passed raises Invalid
  2. floating point values raise Invalid even when int(value) == value
  3. when passed an empty string it returns None, but when passed None it raises Invalid

Tests to come

@wardi
Copy link
Contributor Author

wardi commented Apr 28, 2014

fixes #1678

@joetsoi joetsoi added this to the CKAN 2.3 milestone Apr 29, 2014
@seanh seanh self-assigned this Apr 29, 2014
@@ -16,6 +16,9 @@
import ckan.new_tests.lib.navl.test_validators as t


eq = nose.tools.eq_
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we agreed on using assert_equals and assert_not_equals in #1484

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh? I only saw assert_equals in one place in the new tests. Happy to change it.

@amercader amercader assigned vitorbaptista and unassigned seanh May 1, 2014
@amercader
Copy link
Member

@vitorbaptista can you have a look at this?

@vitorbaptista
Copy link
Contributor

@amercader sure!

@wardi Could you change your files to use single quotes? While you're at it, it would be good to change the few other methods in the files you touched to use single quotes as well, so we're a bit closer to our coding standards.

@@ -444,5 +447,55 @@ def call_validator(*args, **kwargs):
*args, **kwargs)
call_validator(key, data, errors, context={'model': mock_model})

def test_int_validator_idempotent(self):
import ckan.logic.validators as validators
Copy link
Contributor

Choose a reason for hiding this comment

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

I see that we use this pattern of importing what the tests need in the test itself everywhere in this file, but I haven't seen it used in other places. Do you know if it's something we should start doing? If not, I would prefer to move those imports to the beginning of the file, following the relative coding standards

1 + 0j, # int(complex) fails, so expect the same
]
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
Copy link
Contributor

Choose a reason for hiding this comment

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

Although I guess this deprecation warning isn't too bad, I still feel a bit nervous ignoring it.

Using divmod() on complex numbers was removed in Python 3. I know we won't be moving to Python 3 any time soon, but maybe it's simple enough to test if the number is complex and don't run divmod() in this case?

If you can't find an easy way to do so, I guess I'd rather see the deprecation message than ignore it (although I can be convinced otherwise).

inputs or non-whole values
'''
if value is None:
return None
Copy link
Contributor

Choose a reason for hiding this comment

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

I haven't tested it, but is this a new behavior? Just by looking at the diff, it seems that we used to raise Invalid if called with None.

Just making sure it was intended.

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, this is new behaviour.

The current implementation is not idempotent. If you call it once passing in an empty string it will convert that to None, but calling a second time will raise an Invalid exception. The same is true if you pass a long value as a string: it fails on the second call. This PR fixes those cases and some others.

I believe that all validators should be idempotent: it shouldn't matter if you call them once or twenty times in a row, you should always get the same result.

Copy link
Contributor

Choose a reason for hiding this comment

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

This made me realize that I had the wrong definition of idempotent. I thought it was simply that a function doesn't have side effects, and if I call it n times with the same parameters, it'll give me the same result. Basically, f(x) == f(x) == f(x), but checking Wikipedia tells me that it's actually f(f(f(x))) == f(x). Thanks!

vitorbaptista added a commit that referenced this pull request May 2, 2014
@vitorbaptista vitorbaptista merged commit 84da02f into ckan:master May 2, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants