def check_non_dict(self): """Valid JSON which does not parse as a dict should raise a ValueError""" # Should be able to parse this as JSON json.loads(valid_json_not_dict) with pytest.raises(ValueError): get_user_defined_globals(valid_json_not_dict)
def check_non_dict_from_file(self): """Validate behavior when given file containing valid JSON which does not parse as a dict""" _, fname = tempfile.mkstemp() try: with open(fname, "w") as fh: # Write valid JSON which does not parse as a dict fh.write(valid_json_not_dict) with pytest.raises(ValueError, match=fname): get_user_defined_globals(fname) finally: os.remove(fname)
def check_bad_parse_from_file(self): """Validate behavior when given file containing invalid JSON""" _, fname = tempfile.mkstemp() try: with open(fname, "w") as fh: # Write invalid JSON fh.write(invalid_globals_json) with pytest.raises(ValueError): get_user_defined_globals(fname) finally: os.remove(fname)
def check_immutable(self): """Expect the user defined dict object to be immutable.""" global_dict = get_user_defined_globals(globals_json) with pytest.raises(NotImplementedError): global_dict["x"] = -1 with pytest.raises(NotImplementedError): global_dict["y"] = 3
def check_parse_from_file(self): """Validate that, given a filename of a file containing valid JSON, we correctly parse the file contents.""" _, fname = tempfile.mkstemp() try: with open(fname, "w") as fh: fh.write(globals_json) global_dict = get_user_defined_globals(fname) assert global_dict == json.loads(globals_json) assert global_dict["x"] == 200 finally: os.remove(fname)
def check_unparseable(self): """If globals string is not a path to a file, and not parseable as JSON we want to raise a ValueError """ with pytest.raises(ValueError): get_user_defined_globals(invalid_globals_json)
def check_parseable_json_string(self): """Check if globals_json is parseable as JSON, we get back a dictionary view of parsed JSON.""" globals_dict = get_user_defined_globals(globals_json) assert globals_dict == json.loads(globals_json)
def check_pickleable(self): """Expect the user defined dict object to be pickleable""" globals_dict = get_user_defined_globals(globals_json) assert globals_dict # Need to test non-empty dict, to ensure py3 compatibility assert pickle.loads(pickle.dumps(globals_dict)) == globals_dict