Skip to content

Commit

Permalink
Merge pull request #273 from userlocalhost/bugfix/clnoe_entry
Browse files Browse the repository at this point in the history
Fixed bug of cloning Entry that has array_xxx typed Attribute
  • Loading branch information
hinashi authored Oct 8, 2021
2 parents 4a8199a + 822714b commit 86cc2a4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 37 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion entry/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
90 changes: 54 additions & 36 deletions entry/tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(entry_attr.values.last().parent_attr, entry_attr)
self.assertEqual(clone_attr.values.last().parent_attr, clone_attr)
# 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 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
Expand Down Expand Up @@ -3332,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))

0 comments on commit 86cc2a4

Please sign in to comment.