def test_get_validation_fields(): mocked_required_fields = MagicMock() instance = MagicMock(_allow_empty=False, get_required_fields=mocked_required_fields) ZoopObject.get_validation_fields(instance) mocked_required_fields.assert_called_once()
def test_init(self): validate = MagicMock() instance = MagicMock( get_all_fields=MagicMock(return_value={"id", "name"}), validate_fields=validate, ) setattr(instance, "id", None) setattr(instance, "name", None) # noinspection PyCallByClass ZoopObject.__init__(instance, **self.data) self.assertEqual(instance.id, 1) self.assertIsNone(instance.name, 1) validate.assert_called_once()
def test_get_all_fields(self): instance = MagicMock() expected = {"id", "name", "modificado"} result = ZoopObject.get_all_fields(instance) self.assertEqual(result, expected)
def test_is_value_empty(self): self.assertTrue(ZoopObject.is_value_empty(None)) self.assertTrue(ZoopObject.is_value_empty({})) self.assertTrue(ZoopObject.is_value_empty([{}])) self.assertFalse(ZoopObject.is_value_empty([])) self.assertFalse(ZoopObject.is_value_empty(False)) self.assertFalse(ZoopObject.is_value_empty(0)) self.assertFalse(ZoopObject.is_value_empty(""))
def test_to_dict(self): data = self.data data["foo"] = {} data["bar"] = [{}] data["foo2"] = MagicMock(to_dict=MagicMock( return_value={"foo2": "foo2"})) data["bar2"] = [ MagicMock(to_dict=MagicMock(return_value={"bar2": "bar2"})) ] self.mocked_fields.return_value = { "bar", "bar2", "foo", "foo2", "id", "modificado", "name", } instance = ZoopObject.from_dict(data) self.assertIsInstance(instance, ZoopObject) expected = data """We remove the name, foo and bar because it's values are 'empty'. So they won't return on to_dict method""" expected.pop("name") expected.pop("foo") expected.pop("bar") """We need to transform the data to have equality""" expected["foo2"] = data["foo2"].to_dict() expected["bar2"][0] = data["bar2"][0].to_dict() """Mapeamento custom do original""" original_mapping = instance.get_original_different_fields_mapping() for custom, original in original_mapping.items(): expected[original] = expected.pop(custom) result = instance.to_dict() self.assertEqual(expected, result)
def test_from_dict_allow_empty(self): data = {} instance = ZoopObject.from_dict(data, allow_empty=True) self.assertIsInstance(instance, ZoopObject)
def test_make_data_copy_with_args(self): data = {} new_data = ZoopObject.make_data_copy_with_kwargs(data, foo="bar") self.assertEqual(data, {}) self.assertEqual(new_data.get("foo"), "bar")
def test_get_non_required_fields(self): self.assertEqual({"name", "modificado"}, ZoopObject.get_non_required_fields())
def test_get_required_fields(self): self.assertEqual({"id"}, ZoopObject.get_required_fields())
def test_get_fields(self): self.assertEqual({"id", "name", "modificado"}, ZoopObject.get_fields())
def test_get_all_fields(): mocked_get_fields = MagicMock() instance = MagicMock(_allow_empty=False, get_fields=mocked_get_fields) ZoopObject.get_all_fields(instance) mocked_get_fields.assert_called_once()
def test_from_dict(self): instance = ZoopObject.from_dict(self.data) self.assertIsInstance(instance, ZoopObject) self.assertEqual(instance.id, 1) self.assertIsNone(instance.name)