B: Fix incorrect operation deletion consequences

This commit is contained in:
Ivan 2024-11-15 20:37:00 +03:00
parent 592914b6c0
commit bc956e0af2
2 changed files with 45 additions and 3 deletions

View File

@ -97,11 +97,29 @@ class OperationSchema:
def delete_operation(self, target: int, keep_constituents: bool = False): def delete_operation(self, target: int, keep_constituents: bool = False):
''' Delete operation. ''' ''' Delete operation. '''
self.cache.ensure_loaded()
operation = self.cache.operation_by_id[target] operation = self.cache.operation_by_id[target]
if not keep_constituents:
schema = self.cache.get_schema(operation) schema = self.cache.get_schema(operation)
if schema is not None: children = self.cache.graph.outputs[target]
if schema is not None and len(children) > 0:
if not keep_constituents:
self.before_delete_cst(schema, schema.cache.constituents) self.before_delete_cst(schema, schema.cache.constituents)
else:
items = schema.cache.constituents
ids = [cst.pk for cst in items]
inheritance_to_delete: list[Inheritance] = []
for child_id in children:
child_operation = self.cache.operation_by_id[child_id]
child_schema = self.cache.get_schema(child_operation)
if child_schema is None:
continue
self._undo_substitutions_cst(items, child_operation, child_schema)
for item in self.cache.inheritance[child_id]:
if item.parent_id in ids:
inheritance_to_delete.append(item)
for item in inheritance_to_delete:
self.cache.remove_inheritance(item)
Inheritance.objects.filter(pk__in=[item.pk for item in inheritance_to_delete]).delete()
self.cache.remove_operation(target) self.cache.remove_operation(target)
operation.delete() operation.delete()
self.save(update_fields=['time_update']) self.save(update_fields=['time_update'])

View File

@ -250,6 +250,30 @@ class TestChangeOperations(EndpointTester):
self.assertEqual(self.ks4D2.definition_formal, r'X1 X2 X3 S1 D1') self.assertEqual(self.ks4D2.definition_formal, r'X1 X2 X3 S1 D1')
self.assertEqual(self.ks5D4.definition_formal, r'X1 X2 X3 S1 D1 D2 D3') self.assertEqual(self.ks5D4.definition_formal, r'X1 X2 X3 S1 D1 D2 D3')
@decl_endpoint('/api/oss/{item}/delete-operation', method='patch')
def test_delete_operation_keep_schema(self):
data = {
'positions': [],
'target': self.operation1.pk,
'keep_constituents': True,
'delete_schema': False
}
self.executeOK(data=data, item=self.owned_id)
self.ks1.refresh_from_db()
self.ks4D2.refresh_from_db()
self.ks5D4.refresh_from_db()
subs1_2 = self.operation4.getQ_substitutions()
self.assertEqual(subs1_2.count(), 0)
subs3_4 = self.operation5.getQ_substitutions()
self.assertEqual(subs3_4.count(), 1)
self.assertEqual(self.ks1.constituents().count(), 3)
self.assertEqual(self.ks4.constituents().count(), 6)
self.assertEqual(self.ks5.constituents().count(), 8)
self.assertEqual(self.ks4D2.definition_formal, r'X1 X2 X3 S1 D1')
self.assertEqual(self.ks5D4.definition_formal, r'X1 X2 X3 S1 D1 D2 D3')
self.assertFalse(Constituenta.objects.filter(as_child__parent_id=self.ks1D1.pk).exists())
@decl_endpoint('/api/oss/{item}/update-operation', method='patch') @decl_endpoint('/api/oss/{item}/update-operation', method='patch')
def test_change_substitutions(self): def test_change_substitutions(self):
data = { data = {