def test_move_down(self): menu = Menu(name='menu_move_down') menu.save() menu_item1 = MenuItem.objects.create(caption='menu_item1', parent=menu.root_item) menu_item2 = MenuItem.objects.create(caption='menu_item2', parent=menu.root_item) menu_item3 = MenuItem.objects.create(caption='menu_item3', parent=menu.root_item) menu_item4 = MenuItem.objects.create(caption='menu_item4', parent=menu.root_item) self.assertEquals(menu_item1.rank, 0) self.assertEquals(menu_item2.rank, 1) self.assertEquals(menu_item3.rank, 2) self.assertEquals(menu_item4.rank, 3) response = self.client.post('/test_treemenus_admin/treemenus/menu/%s/items/%s/move_down/' % (menu.pk, menu_item3.pk)) self.assertRedirects(response, '/test_treemenus_admin/treemenus/menu/%s/' % menu.pk) # Retrieve objects from db menu_item1 = MenuItem.objects.get(caption='menu_item1', parent=menu.root_item) menu_item2 = MenuItem.objects.get(caption='menu_item2', parent=menu.root_item) menu_item3 = MenuItem.objects.get(caption='menu_item3', parent=menu.root_item) menu_item4 = MenuItem.objects.get(caption='menu_item4', parent=menu.root_item) self.assertEquals(menu_item1.rank, 0) self.assertEquals(menu_item2.rank, 1) self.assertEquals(menu_item3.rank, 3) self.assertEquals(menu_item4.rank, 2) # Test forbidden move up self.assertRaises(MenuItem.DoesNotExist, lambda: move_item(menu_item3, 1))
def test_move_item_or_clean_ranks(self): menu = Menu(name='menu_move_item_or_clean_ranks') menu.save() menu_item1 = MenuItem.objects.create(caption='menu_item1', parent=menu.root_item) menu_item2 = MenuItem.objects.create(caption='menu_item2', parent=menu.root_item) menu_item3 = MenuItem.objects.create(caption='menu_item3', parent=menu.root_item) menu_item4 = MenuItem.objects.create(caption='menu_item4', parent=menu.root_item) self.assertEquals(menu_item1.rank, 0) self.assertEquals(menu_item2.rank, 1) self.assertEquals(menu_item3.rank, 2) self.assertEquals(menu_item4.rank, 3) # Corrupt ranks menu_item1.rank = 0 menu_item1.save() menu_item2.rank = 0 menu_item2.save() menu_item3.rank = 0 menu_item3.save() menu_item4.rank = 0 menu_item4.save() move_item_or_clean_ranks(menu_item3, -1) # Move up # Retrieve objects from db menu_item1 = MenuItem.objects.get(caption='menu_item1', parent=menu.root_item) menu_item2 = MenuItem.objects.get(caption='menu_item2', parent=menu.root_item) menu_item3 = MenuItem.objects.get(caption='menu_item3', parent=menu.root_item) menu_item4 = MenuItem.objects.get(caption='menu_item4', parent=menu.root_item) self.assertEquals(menu_item1.rank, 0) self.assertEquals(menu_item2.rank, 2) self.assertEquals(menu_item3.rank, 1) self.assertEquals(menu_item4.rank, 3) # Corrupt ranks menu_item1.rank = 18 menu_item1.save() menu_item2.rank = -1 menu_item2.save() menu_item3.rank = 6 menu_item3.save() menu_item4.rank = 99 menu_item4.save() move_item_or_clean_ranks(menu_item1, 1) # Try to move down # Retrieve objects from db menu_item1 = MenuItem.objects.get(caption='menu_item1', parent=menu.root_item) menu_item2 = MenuItem.objects.get(caption='menu_item2', parent=menu.root_item) menu_item3 = MenuItem.objects.get(caption='menu_item3', parent=menu.root_item) menu_item4 = MenuItem.objects.get(caption='menu_item4', parent=menu.root_item) self.assertEquals(menu_item1.rank, 3) self.assertEquals(menu_item2.rank, 0) self.assertEquals(menu_item3.rank, 1) self.assertEquals(menu_item4.rank, 2)
def test_clean_children_ranks(self): menu = Menu(name='menu_clean_children_ranks') menu.save() menu_item1 = MenuItem.objects.create(caption='menu_item1', parent=menu.root_item) menu_item2 = MenuItem.objects.create(caption='menu_item2', parent=menu.root_item) menu_item3 = MenuItem.objects.create(caption='menu_item3', parent=menu.root_item) menu_item4 = MenuItem.objects.create(caption='menu_item4', parent=menu.root_item) # Initial check self.assertEquals(menu_item1.rank, 0) self.assertEquals(menu_item2.rank, 1) self.assertEquals(menu_item3.rank, 2) self.assertEquals(menu_item4.rank, 3) # Mess up ranks menu_item1.rank = 99 menu_item1.save() menu_item2.rank = -150 menu_item2.save() menu_item3.rank = 3 menu_item3.save() menu_item4.rank = 67 menu_item4.save() clean_ranks(menu.root_item.children()) # Retrieve objects from db menu_item1 = MenuItem.objects.get(caption='menu_item1', parent=menu.root_item) menu_item2 = MenuItem.objects.get(caption='menu_item2', parent=menu.root_item) menu_item3 = MenuItem.objects.get(caption='menu_item3', parent=menu.root_item) menu_item4 = MenuItem.objects.get(caption='menu_item4', parent=menu.root_item) self.assertEquals(menu_item1.rank, 3) self.assertEquals(menu_item2.rank, 0) self.assertEquals(menu_item3.rank, 1) self.assertEquals(menu_item4.rank, 2)
def run(): from treemenus.models import Menu treemenus_menu_1 = Menu() treemenus_menu_1.name = u'root' treemenus_menu_1.save() treemenus_menu_2 = Menu() treemenus_menu_2.name = u'persona' treemenus_menu_2.save() treemenus_menu_3 = Menu() treemenus_menu_3.name = u'enfermeria' treemenus_menu_3.save() from treemenus.models import MenuItem treemenus_menuitem_1 = MenuItem() treemenus_menuitem_1.parent = None treemenus_menuitem_1.caption = u'root' treemenus_menuitem_1.url = u'' treemenus_menuitem_1.named_url = u'' treemenus_menuitem_1.level = 0 treemenus_menuitem_1.rank = 0 treemenus_menuitem_1.menu = treemenus_menu_1 treemenus_menuitem_1.save() treemenus_menuitem_2 = MenuItem() treemenus_menuitem_2.parent = None treemenus_menuitem_2.caption = u'root' treemenus_menuitem_2.url = u'' treemenus_menuitem_2.named_url = u'' treemenus_menuitem_2.level = 0 treemenus_menuitem_2.rank = 0 treemenus_menuitem_2.menu = treemenus_menu_2 treemenus_menuitem_2.save() treemenus_menuitem_3 = MenuItem() treemenus_menuitem_3.parent = treemenus_menuitem_1 treemenus_menuitem_3.caption = u'Enfermer\xeda' treemenus_menuitem_3.url = u'' treemenus_menuitem_3.named_url = u'nightingale-index' treemenus_menuitem_3.level = 1 treemenus_menuitem_3.rank = 4 treemenus_menuitem_3.menu = treemenus_menu_1 treemenus_menuitem_3.save() treemenus_menuitem_4 = MenuItem() treemenus_menuitem_4.parent = treemenus_menuitem_1 treemenus_menuitem_4.caption = u'Admisiones' treemenus_menuitem_4.url = u'' treemenus_menuitem_4.named_url = u'admision-index ' treemenus_menuitem_4.level = 1 treemenus_menuitem_4.rank = 3 treemenus_menuitem_4.menu = treemenus_menu_1 treemenus_menuitem_4.save() treemenus_menuitem_5 = MenuItem() treemenus_menuitem_5.parent = treemenus_menuitem_1 treemenus_menuitem_5.caption = u'Examenes' treemenus_menuitem_5.url = u'' treemenus_menuitem_5.named_url = u'examen-index ' treemenus_menuitem_5.level = 1 treemenus_menuitem_5.rank = 2 treemenus_menuitem_5.menu = treemenus_menu_1 treemenus_menuitem_5.save() treemenus_menuitem_6 = MenuItem() treemenus_menuitem_6.parent = treemenus_menuitem_1 treemenus_menuitem_6.caption = u'Inicio' treemenus_menuitem_6.url = u'' treemenus_menuitem_6.named_url = u'home' treemenus_menuitem_6.level = 1 treemenus_menuitem_6.rank = 0 treemenus_menuitem_6.menu = treemenus_menu_1 treemenus_menuitem_6.save() treemenus_menuitem_7 = MenuItem() treemenus_menuitem_7.parent = treemenus_menuitem_1 treemenus_menuitem_7.caption = u'Pacientes' treemenus_menuitem_7.url = u'' treemenus_menuitem_7.named_url = u'persona-index' treemenus_menuitem_7.level = 1 treemenus_menuitem_7.rank = 1 treemenus_menuitem_7.menu = treemenus_menu_1 treemenus_menuitem_7.save() treemenus_menuitem_8 = MenuItem() treemenus_menuitem_8.parent = treemenus_menuitem_1 treemenus_menuitem_8.caption = u'B\xfasqueda' treemenus_menuitem_8.url = u'' treemenus_menuitem_8.named_url = u'haystack_search' treemenus_menuitem_8.level = 1 treemenus_menuitem_8.rank = 5 treemenus_menuitem_8.menu = treemenus_menu_1 treemenus_menuitem_8.save() treemenus_menuitem_9 = MenuItem() treemenus_menuitem_9.parent = treemenus_menuitem_2 treemenus_menuitem_9.caption = u'Agregar Examen' treemenus_menuitem_9.url = u'' treemenus_menuitem_9.named_url = u'examen-agregar persona.id' treemenus_menuitem_9.level = 1 treemenus_menuitem_9.rank = 0 treemenus_menuitem_9.menu = treemenus_menu_2 treemenus_menuitem_9.save() treemenus_menuitem_10 = MenuItem() treemenus_menuitem_10.parent = treemenus_menuitem_2 treemenus_menuitem_10.caption = u'Ver Datos Personales' treemenus_menuitem_10.url = u'' treemenus_menuitem_10.named_url = u'persona-view-id persona.id' treemenus_menuitem_10.level = 1 treemenus_menuitem_10.rank = 1 treemenus_menuitem_10.menu = treemenus_menu_2 treemenus_menuitem_10.save() treemenus_menuitem_11 = MenuItem() treemenus_menuitem_11.parent = treemenus_menuitem_2 treemenus_menuitem_11.caption = u'Examenes' treemenus_menuitem_11.url = u'' treemenus_menuitem_11.named_url = u'examen-persona-lista persona.id' treemenus_menuitem_11.level = 1 treemenus_menuitem_11.rank = 2 treemenus_menuitem_11.menu = treemenus_menu_2 treemenus_menuitem_11.save() treemenus_menuitem_12 = MenuItem() treemenus_menuitem_12.parent = treemenus_menuitem_2 treemenus_menuitem_12.caption = u'Admitir Paciente' treemenus_menuitem_12.url = u'' treemenus_menuitem_12.named_url = u'admision-persona-agregar persona.id' treemenus_menuitem_12.level = 1 treemenus_menuitem_12.rank = 3 treemenus_menuitem_12.menu = treemenus_menu_2 treemenus_menuitem_12.save() treemenus_menu_1.root_item = treemenus_menuitem_1 treemenus_menu_1.save() treemenus_menu_2.root_item = treemenus_menuitem_2 treemenus_menu_2.save()
def test_change_parents(self): menu = Menu(name='menu_change_parents') menu.save() menu_item1 = MenuItem.objects.create(caption='menu_item1', parent=menu.root_item) menu_item2 = MenuItem.objects.create(caption='menu_item2', parent=menu.root_item) menu_item3 = MenuItem.objects.create(caption='menu_item3', parent=menu_item1) menu_item4 = MenuItem.objects.create(caption='menu_item4', parent=menu_item1) menu_item5 = MenuItem.objects.create(caption='menu_item5', parent=menu_item1) # menu # ri # mi1 # mi3 # mi4 # mi5 # mi2 # Check initial ranks self.assertEquals(menu_item1.rank, 0) self.assertEquals(menu_item2.rank, 1) self.assertEquals(menu_item3.rank, 0) self.assertEquals(menu_item4.rank, 1) self.assertEquals(menu_item5.rank, 2) # Check initial levels self.assertEquals(menu_item1.level, 1) self.assertEquals(menu_item2.level, 1) self.assertEquals(menu_item3.level, 2) self.assertEquals(menu_item4.level, 2) self.assertEquals(menu_item5.level, 2) # Change parent for some items menu_item4.parent = menu.root_item menu_item4.save() menu_item5.parent = menu_item2 menu_item5.save() # menu # ri # mi1 # mi3 # mi2 # mi5 # mi4 # Refetch items from db menu_item1 = MenuItem.objects.get(pk=menu_item1.pk) menu_item2 = MenuItem.objects.get(pk=menu_item2.pk) menu_item3 = MenuItem.objects.get(pk=menu_item3.pk) menu_item4 = MenuItem.objects.get(pk=menu_item4.pk) menu_item5 = MenuItem.objects.get(pk=menu_item5.pk) # Check ranks self.assertEquals(menu_item1.rank, 0) self.assertEquals(menu_item2.rank, 1) self.assertEquals(menu_item3.rank, 0) self.assertEquals(menu_item4.rank, 2) self.assertEquals(menu_item5.rank, 0) # Check levels self.assertEquals(menu_item1.level, 1) self.assertEquals(menu_item2.level, 1) self.assertEquals(menu_item3.level, 2) self.assertEquals(menu_item4.level, 1) self.assertEquals(menu_item5.level, 2) # Change parent for some items menu_item2.parent = menu_item1 menu_item2.save() menu_item5.parent = menu_item1 menu_item5.save() menu_item3.parent = menu.root_item menu_item3.save() menu_item1.parent = menu_item4 menu_item1.save() # menu # ri # mi4 # mi1 # mi2 # mi5 # mi3 # Refetch items from db menu_item1 = MenuItem.objects.get(pk=menu_item1.pk) menu_item2 = MenuItem.objects.get(pk=menu_item2.pk) menu_item3 = MenuItem.objects.get(pk=menu_item3.pk) menu_item4 = MenuItem.objects.get(pk=menu_item4.pk) menu_item5 = MenuItem.objects.get(pk=menu_item5.pk) # Check ranks self.assertEquals(menu_item1.rank, 0) self.assertEquals(menu_item2.rank, 0) self.assertEquals(menu_item3.rank, 1) self.assertEquals(menu_item4.rank, 0) self.assertEquals(menu_item5.rank, 1) # Check levels self.assertEquals(menu_item1.level, 2) self.assertEquals(menu_item2.level, 3) self.assertEquals(menu_item3.level, 1) self.assertEquals(menu_item4.level, 1) self.assertEquals(menu_item5.level, 3) # Change parent for some items menu_item2.parent = menu_item4 menu_item2.save() menu_item4.parent = menu_item3 menu_item4.save() menu_item1.parent = menu.root_item menu_item1.save() menu_item5.parent = menu_item4 menu_item5.save() # menu # ri # mi3 # mi4 # mi2 # mi5 # mi1 # Refetch items from db menu_item1 = MenuItem.objects.get(pk=menu_item1.pk) menu_item2 = MenuItem.objects.get(pk=menu_item2.pk) menu_item3 = MenuItem.objects.get(pk=menu_item3.pk) menu_item4 = MenuItem.objects.get(pk=menu_item4.pk) menu_item5 = MenuItem.objects.get(pk=menu_item5.pk) # Check ranks self.assertEquals(menu_item1.rank, 1) self.assertEquals(menu_item2.rank, 0) self.assertEquals(menu_item3.rank, 0) self.assertEquals(menu_item4.rank, 0) self.assertEquals(menu_item5.rank, 1) # Check levels self.assertEquals(menu_item1.level, 1) self.assertEquals(menu_item2.level, 3) self.assertEquals(menu_item3.level, 1) self.assertEquals(menu_item4.level, 2) self.assertEquals(menu_item5.level, 3)
def test_delete(self): menu = Menu(name='menu_delete') menu.save() menu_item1 = MenuItem.objects.create(caption='menu_item1', parent=menu.root_item) menu_item2 = MenuItem.objects.create(caption='menu_item2', parent=menu.root_item) menu_item3 = MenuItem.objects.create(caption='menu_item3', parent=menu_item1) menu_item4 = MenuItem.objects.create(caption='menu_item4', parent=menu_item1) menu_item5 = MenuItem.objects.create(caption='menu_item5', parent=menu_item1) menu_item6 = MenuItem.objects.create(caption='menu_item6', parent=menu_item2) menu_item7 = MenuItem.objects.create(caption='menu_item7', parent=menu_item4) menu_item8 = MenuItem.objects.create(caption='menu_item8', parent=menu_item4) menu_item9 = MenuItem.objects.create(caption='menu_item9', parent=menu_item1) menu_item10 = MenuItem.objects.create(caption='menu_item10', parent=menu_item4) # menu # ri # mi1 # mi3 # mi4 # mi7 # mi8 # mi10 # mi5 # mi9 # mi2 # mi6 # Check initial ranks self.assertEquals(menu_item1.rank, 0) self.assertEquals(menu_item2.rank, 1) self.assertEquals(menu_item3.rank, 0) self.assertEquals(menu_item4.rank, 1) self.assertEquals(menu_item5.rank, 2) self.assertEquals(menu_item6.rank, 0) self.assertEquals(menu_item7.rank, 0) self.assertEquals(menu_item8.rank, 1) self.assertEquals(menu_item9.rank, 3) self.assertEquals(menu_item10.rank, 2) # Check initial levels self.assertEquals(menu_item1.level, 1) self.assertEquals(menu_item2.level, 1) self.assertEquals(menu_item3.level, 2) self.assertEquals(menu_item4.level, 2) self.assertEquals(menu_item5.level, 2) self.assertEquals(menu_item6.level, 2) self.assertEquals(menu_item7.level, 3) self.assertEquals(menu_item8.level, 3) self.assertEquals(menu_item9.level, 2) self.assertEquals(menu_item10.level, 3) # Delete some items menu_item8.delete() menu_item3.delete() # menu # ri # mi1 # mi4 # mi7 # mi10 # mi5 # mi9 # mi2 # mi6 # Refetch items from db menu_item1 = MenuItem.objects.get(pk=menu_item1.pk) menu_item2 = MenuItem.objects.get(pk=menu_item2.pk) menu_item4 = MenuItem.objects.get(pk=menu_item4.pk) menu_item5 = MenuItem.objects.get(pk=menu_item5.pk) menu_item6 = MenuItem.objects.get(pk=menu_item6.pk) menu_item7 = MenuItem.objects.get(pk=menu_item7.pk) menu_item9 = MenuItem.objects.get(pk=menu_item9.pk) menu_item10 = MenuItem.objects.get(pk=menu_item10.pk) # Check ranks self.assertEquals(menu_item1.rank, 0) self.assertEquals(menu_item2.rank, 1) self.assertEquals(menu_item4.rank, 0) self.assertEquals(menu_item5.rank, 1) self.assertEquals(menu_item6.rank, 0) self.assertEquals(menu_item7.rank, 0) self.assertEquals(menu_item9.rank, 2) self.assertEquals(menu_item10.rank, 1) # Check levels self.assertEquals(menu_item1.level, 1) self.assertEquals(menu_item2.level, 1) self.assertEquals(menu_item4.level, 2) self.assertEquals(menu_item5.level, 2) self.assertEquals(menu_item6.level, 2) self.assertEquals(menu_item7.level, 3) self.assertEquals(menu_item9.level, 2) self.assertEquals(menu_item10.level, 3) # Delete some items menu_item4.delete() menu_item5.delete() # menu # ri # mi1 # mi9 # mi2 # mi6 # Refetch items from db menu_item1 = MenuItem.objects.get(pk=menu_item1.pk) menu_item2 = MenuItem.objects.get(pk=menu_item2.pk) menu_item6 = MenuItem.objects.get(pk=menu_item6.pk) menu_item9 = MenuItem.objects.get(pk=menu_item9.pk) # Check ranks self.assertEquals(menu_item1.rank, 0) self.assertEquals(menu_item2.rank, 1) self.assertEquals(menu_item6.rank, 0) self.assertEquals(menu_item9.rank, 0) # Check levels self.assertEquals(menu_item1.level, 1) self.assertEquals(menu_item2.level, 1) self.assertEquals(menu_item6.level, 2) self.assertEquals(menu_item9.level, 2) # Check that deleted items are in fact, gone. self.assertRaises(MenuItem.DoesNotExist, lambda: MenuItem.objects.get(pk=menu_item3.pk)) self.assertRaises(MenuItem.DoesNotExist, lambda: MenuItem.objects.get(pk=menu_item4.pk)) self.assertRaises(MenuItem.DoesNotExist, lambda: MenuItem.objects.get(pk=menu_item5.pk)) self.assertRaises(MenuItem.DoesNotExist, lambda: MenuItem.objects.get(pk=menu_item7.pk)) self.assertRaises(MenuItem.DoesNotExist, lambda: MenuItem.objects.get(pk=menu_item8.pk)) self.assertRaises(MenuItem.DoesNotExist, lambda: MenuItem.objects.get(pk=menu_item10.pk))