Fix get_max_index to return 0 when nothing found

This commit is contained in:
IRBorisov 2024-03-14 20:22:02 +03:00
parent 4d0ba713f3
commit 7d7016cc67
2 changed files with 9 additions and 1 deletions

View File

@ -77,7 +77,7 @@ class RSForm:
def get_max_index(self, cst_type: CstType) -> int: def get_max_index(self, cst_type: CstType) -> int:
''' Get maximum alias index for specific CstType ''' ''' Get maximum alias index for specific CstType '''
result: int = 1 result: int = 0
items = Constituenta.objects \ items = Constituenta.objects \
.filter(schema=self.item, cst_type=cst_type) \ .filter(schema=self.item, cst_type=cst_type) \
.order_by('-alias') \ .order_by('-alias') \

View File

@ -160,6 +160,14 @@ class TestRSForm(TestCase):
self.assertFalse(schema2.constituents().exists()) self.assertFalse(schema2.constituents().exists())
self.assertEqual(schema1.constituents().count(), 2) self.assertEqual(schema1.constituents().count(), 2)
def test_get_max_index(self):
schema1 = RSForm.create(title='Test1')
Constituenta.objects.create(alias='X1', schema=schema1.item, order=1)
Constituenta.objects.create(alias='D2', cst_type=CstType.TERM, schema=schema1.item, order=2)
self.assertEqual(schema1.get_max_index(CstType.BASE), 1)
self.assertEqual(schema1.get_max_index(CstType.TERM), 2)
self.assertEqual(schema1.get_max_index(CstType.AXIOM), 0)
def test_insert_at(self): def test_insert_at(self):
schema = RSForm.create(title='Test') schema = RSForm.create(title='Test')
cst1 = schema.insert_at(1, 'X1', CstType.BASE) cst1 = schema.insert_at(1, 'X1', CstType.BASE)