Can you do fuzzy comparison between integers, floats, etc?

15 views
Skip to first unread message

Asaf Peleg

unread,
Mar 31, 2016, 11:21:40 AM3/31/16
to Simplistix Open Source Software
Can I use testfixtures to ensure a dictionary/json object contains the correct type versus doing an exact match, or should i look elsewhere?

Chris Withers

unread,
Mar 31, 2016, 11:24:23 AM3/31/16
to simpl...@googlegroups.com
On 31/03/2016 16:11, Asaf Peleg wrote:
Can I use testfixtures to ensure a dictionary/json object contains the correct type versus doing an exact match, or should i look elsewhere?
Why would you not want to make explicit assertions about the values?

You *can* do want you're asking for, it's just not a great idea:

from testfixtures import Comparison as C, compare

compare(actual, expected=dict(thing=C(int), other_thing=C(str)))


cheers,

Chris

Daniel Fortunov

unread,
Mar 31, 2016, 6:15:42 PM3/31/16
to simpl...@googlegroups.com
If you're trying to do schema validation of JSON input you may want to look at schema.py: https://github.com/keleshev/schema

--
You received this message because you are subscribed to the Google Groups "Simplistix Open Source Software" group.
To unsubscribe from this group and stop receiving emails from it, send an email to simplistix+...@googlegroups.com.
To post to this group, send email to simpl...@googlegroups.com.
Visit this group at https://groups.google.com/group/simplistix.
For more options, visit https://groups.google.com/d/optout.

Chris Withers

unread,
Mar 31, 2016, 6:16:56 PM3/31/16
to simpl...@googlegroups.com
For that sort of thing, I've been massively impressed with Voluptuous:

https://github.com/alecthomas/voluptuous

cheers,

Chris
______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________

Asaf Peleg

unread,
Apr 1, 2016, 3:17:54 PM4/1/16
to Simplistix Open Source Software
We generally uses testfixtures.compare to confirm that the responses from our RESTful api matches up with an expected response.  Usually we want to compare an exact value but sometimes we just want to do general validation like you mentioned via your links.

Take the following json response for example...
{
   'message': 'Your report was successfully created'
   'time-elapsed': 23
}

We'd like to confirm that key time-elapsed was present in the response and that the value of it was an integer but it's not necessary to test exactly how long it took to generate the report.  
As for the message, we want to confirm that the message matches exactly as expected.

All this being said I will take a closer look at the libraries you provided to determine if they would be a better general fit for our use case.
Reply all
Reply to author
Forward
0 new messages