def testEncoders(self): for class_, fake_func in TEST_CASES: original_object = fake_func() # We can skip metrics and runners; the encoders will automatically # handle the addition of new fields to these classes if isinstance(original_object, Metric) or isinstance( original_object, Runner ): continue json_object = object_to_json(original_object) object_keys = { remove_prefix(key, "_") for key in original_object.__dict__.keys() } json_keys = {key for key in json_object.keys() if key != "__type"} # Account for fields that appear in the Python object but not the JSON # and for fields that appear in both places but with different names if class_ in ENCODE_DECODE_FIELD_MAPS: map = ENCODE_DECODE_FIELD_MAPS[class_] for field in map.python_only: json_keys.add(field) for field in map.encoded_only: object_keys.add(field) for python, encoded in map.python_to_encoded.items(): json_keys.remove(encoded) json_keys.add(python) self.assertEqual( object_keys, json_keys, msg=f"Mismatch between Python and JSON representation in {class_}.", )
def testEncoders(self): for class_, fake_func, unbound_encode_func, _ in TEST_CASES: original_object = fake_func() # We can skip metrics and runners; the encoders will automatically # handle the addition of new fields to these classes if isinstance(original_object, Metric) or isinstance( original_object, Runner ): continue encode_func = unbound_encode_func.__get__(self.encoder) sqa_object = encode_func(original_object) if isinstance( original_object, AbandonedArm ): # handle NamedTuple differently object_keys = original_object._asdict().keys() else: object_keys = original_object.__dict__.keys() object_keys = {remove_prefix(key, "_") for key in object_keys} sqa_keys = { remove_prefix(key, "_") for key in sqa_object.attributes if key not in ["id", "_sa_instance_state"] and not is_foreign_key_field(key) } # Account for fields that appear in the Python object but not the SQA # the SQA but not the Python, and for fields that appear in both places # but with different names if class_ in ENCODE_DECODE_FIELD_MAPS: map = ENCODE_DECODE_FIELD_MAPS[class_] for field in map.python_only: sqa_keys.add(field) for field in map.encoded_only: object_keys.add(field) for python, encoded in map.python_to_encoded.items(): sqa_keys.remove(encoded) sqa_keys.add(python) self.assertEqual( object_keys, sqa_keys, msg=f"Mismatch between Python and SQA representation in {class_}.", )
def testEncoders(self): for class_, fake_func in TEST_CASES: original_object = fake_func() # We can skip metrics and runners; the encoders will automatically # handle the addition of new fields to these classes if isinstance(original_object, Metric) or isinstance( original_object, Runner): continue json_object = object_to_json(original_object) object_keys = { remove_prefix(key, "_") for key in original_object.__dict__.keys() } json_keys = {key for key in json_object.keys() if key != "__type"} # Account for fields that appear in the Python object but not the JSON # and for fields that appear in both places but with different names if class_ in ENCODE_DECODE_FIELD_MAPS: map = ENCODE_DECODE_FIELD_MAPS[class_] for field in map.python_only: json_keys.add(field) for field in map.encoded_only: object_keys.add(field) for python, encoded in map.python_to_encoded.items(): json_keys.remove(encoded) json_keys.add(python) # TODO: Remove this check if able. `_slotnames__` is not a class attribute # when testing locally, but it is a class attribute on Travis if class_ == "Type[Model]": object_keys.discard("_slotnames__") if class_ == "Type[MarginalLogLikelihood]": object_keys.discard("add_other_terms") self.assertEqual( object_keys, json_keys, msg= (f"Mismatch between Python and JSON representation in {class_}." f"Python: {object_keys} does not match JSON: {json_keys}."), )