From 17ac7aba978119da08dcf9f21beed4bd9144dcc1 Mon Sep 17 00:00:00 2001 From: Hiroyasu OHYAMA Date: Fri, 8 Oct 2021 01:24:55 +0900 Subject: [PATCH 1/3] Fixed bug of cloning Entry that has array_xxx typed Attribute --- entry/models.py | 2 +- entry/tests/test_model.py | 57 ++++++++++++++++++++++++++------------- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/entry/models.py b/entry/models.py index 757389c94..7ef5a6695 100644 --- a/entry/models.py +++ b/entry/models.py @@ -533,7 +533,7 @@ def clone(self, user, **extra_params): # When the Attribute is array, this method also clone co-AttributeValues if self.schema.type & AttrTypeValue['array']: for co_attrv in attrv.data_array.all(): - new_attrv.data_array.add(co_attrv.clone(user)) + new_attrv.data_array.add(co_attrv.clone(user, parent_attr=cloned_attr)) cloned_attr.values.add(new_attrv) diff --git a/entry/tests/test_model.py b/entry/tests/test_model.py index 304223b45..b7bc44e3c 100644 --- a/entry/tests/test_model.py +++ b/entry/tests/test_model.py @@ -828,36 +828,55 @@ def test_clone_attribute_typed_array_string(self): self.assertEqual(v1.value, v2.value) def test_clone_entry(self): - self._entity.attrs.add(EntityAttr.objects.create(**{ - 'name': 'attr', + test_entity = Entity.objects.create(name='E0', created_user=self._user) + test_entity.attrs.add(EntityAttr.objects.create(**{ + 'name': 'string', 'type': AttrTypeValue['string'], 'created_user': self._user, - 'parent_entity': self._entity, + 'parent_entity': test_entity, })) - entry = Entry.objects.create(name='entry', schema=self._entity, created_user=self._user) + test_entity.attrs.add(EntityAttr.objects.create(**{ + 'name': 'arrobj', + 'type': AttrTypeValue['array_object'], + 'created_user': self._user, + 'parent_entity': test_entity, + })) + + entry = Entry.objects.create(name='entry', schema=test_entity, created_user=self._user) entry.complement_attrs(self._user) - entry_attr = entry.attrs.last() - for i in range(10): - entry_attr.add_value(self._user, str(i)) + # register initial AttributeValue for each Attributes + attr_string = entry.attrs.get(schema__name='string', is_active=True) + for i in range(3): + attr_string.add_value(self._user, str(i)) - clone = entry.clone(self._user) + attr_arrobj = entry.attrs.get(schema__name='arrobj', is_active=True) + attr_arrobj.add_value(self._user, [entry]) - self.assertIsNotNone(clone) - self.assertNotEqual(clone.id, entry.id) - self.assertEqual(clone.name, entry.name) - self.assertEqual(clone.attrs.count(), entry.attrs.count()) - self.assertNotEqual(clone.attrs.last(), entry_attr) + cloned_entry = entry.clone(self._user) + + self.assertIsNotNone(cloned_entry) + self.assertNotEqual(cloned_entry.id, entry.id) + self.assertEqual(cloned_entry.name, entry.name) + self.assertEqual(cloned_entry.attrs.count(), entry.attrs.count()) + self.assertNotEqual(cloned_entry.attrs.last(), attr_string) # checks parent_entry in the cloned Attribute object is updated - clone_attr = clone.attrs.last() - self.assertEqual(entry_attr.parent_entry, entry) - self.assertEqual(clone_attr.parent_entry, clone) + for (original_attr, cloned_attr) in [ + (attr_string, cloned_entry.attrs.get(schema__name='string', is_active=True)), + (attr_arrobj, cloned_entry.attrs.get(schema__name='arrobj', is_active=True))]: + + self.assertEqual(original_attr.parent_entry, entry) + self.assertEqual(cloned_attr.parent_entry, cloned_entry) + + # checks parent_entry in the cloned AttributeValue object is updated + self.assertEqual(original_attr.values.last().parent_attr, original_attr) + self.assertEqual(cloned_attr.values.last().parent_attr, cloned_attr) - # checks parent_entry in the cloned AttributeValue object is updated - self.assertEqual(entry_attr.values.last().parent_attr, entry_attr) - self.assertEqual(clone_attr.values.last().parent_attr, clone_attr) + # checks AttributeValue.parent_attr for each child AttributeValue(s) + for co_attrv in cloned_attr.values.last().data_array.all(): + self.assertEqual(co_attrv.parent_attr, cloned_attr) def test_clone_entry_with_non_permitted_attributes(self): # set EntityAttr attr3 is not public From 64c3920daa14b387fdedeabd3e7b40e23f8ab957 Mon Sep 17 00:00:00 2001 From: Hiroyasu OHYAMA Date: Fri, 8 Oct 2021 01:45:00 +0900 Subject: [PATCH 2/3] Simplify Entry.ModelTest.test_get_default_value --- entry/tests/test_model.py | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/entry/tests/test_model.py b/entry/tests/test_model.py index b7bc44e3c..98b67eca4 100644 --- a/entry/tests/test_model.py +++ b/entry/tests/test_model.py @@ -3351,24 +3351,23 @@ def test_get_default_value(self): entry = Entry.objects.create(name='entry', schema=entity, created_user=user) entry.complement_attrs(user) - default_values = [ - {'name': 'str', 'value': ''}, - {'name': 'text', 'value': ''}, - {'name': 'obj', 'value': None}, - {'name': 'name', 'value': {'name': '', 'id': None}}, - {'name': 'bool', 'value': False}, - {'name': 'group', 'value': None}, - {'name': 'date', 'value': None}, - {'name': 'arr_str', 'value': []}, - {'name': 'arr_obj', 'value': []}, - {'name': 'arr_name', 'value': dict().values()}, - {'name': 'arr_group', 'value': []}, - ] - for (i, attr) in enumerate(entry.attrs.all()): - self.assertEqual(default_values[i]['name'], attr.name) + default_values = { + 'str': '', + 'text': '', + 'obj': None, + 'name': {'name': '', 'id': None}, + 'bool': False, + 'group': None, + 'date': None, + 'arr_str': [], + 'arr_obj': [], + 'arr_name': dict().values(), + 'arr_group': [], + } + for attr in entry.attrs.all(): if attr.name == 'arr_name': - self.assertEqual(list(default_values[i]['value']), + self.assertEqual(list(default_values[attr.name]), list(AttributeValue.get_default_value(attr))) else: - self.assertEqual(default_values[i]['value'], + self.assertEqual(default_values[attr.name], AttributeValue.get_default_value(attr)) From 822714b16c75b87bb6c460c3f1c639d1d9047624 Mon Sep 17 00:00:00 2001 From: Hiroyasu OHYAMA Date: Fri, 8 Oct 2021 01:46:52 +0900 Subject: [PATCH 3/3] Updated CHANGELOG for previous commit --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b4543887d..07dfd5d87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ ### Fixed +## v3.3.1 + +### Fixed +* Fixed bug AttributeValue.parent_attr is different with child's one (#272) + ## v3.3.0 ### Added