Test Item Group
This commit is contained in:
parent
7bb9c3f125
commit
7c34f1c472
@ -5,7 +5,7 @@ from __future__ import unicode_literals
|
|||||||
import unittest
|
import unittest
|
||||||
import webnotes
|
import webnotes
|
||||||
from webnotes.utils.nestedset import NestedSetRecursionError, NestedSetMultipleRootsError, \
|
from webnotes.utils.nestedset import NestedSetRecursionError, NestedSetMultipleRootsError, \
|
||||||
rebuild_tree, get_ancestors_of
|
NestedSetChildExistsError, NestedSetInvalidMergeError, rebuild_tree, get_ancestors_of
|
||||||
|
|
||||||
test_records = [
|
test_records = [
|
||||||
[{
|
[{
|
||||||
@ -106,15 +106,35 @@ class TestItem(unittest.TestCase):
|
|||||||
self.assertTrue(lft >= min_lft)
|
self.assertTrue(lft >= min_lft)
|
||||||
self.assertTrue(rgt <= max_rgt)
|
self.assertTrue(rgt <= max_rgt)
|
||||||
|
|
||||||
children = webnotes.conn.sql("""select count(name) from `tabItem Group`
|
no_of_children = self.get_no_of_children(item_group["item_group_name"])
|
||||||
where lft>%s and rgt<%s""", (lft, rgt))[0][0]
|
self.assertTrue(rgt == (lft + 1 + (2 * no_of_children)))
|
||||||
self.assertTrue(rgt == (lft + 1 + (2 * children)))
|
|
||||||
|
no_of_children = self.get_no_of_children(parent_item_group)
|
||||||
|
self.assertTrue(parent_rgt == (parent_lft + 1 + (2 * no_of_children)))
|
||||||
|
|
||||||
|
def get_no_of_children(self, item_group):
|
||||||
|
def get_no_of_children(item_groups, no_of_children):
|
||||||
|
children = []
|
||||||
|
for ig in item_groups:
|
||||||
|
children += webnotes.conn.sql_list("""select name from `tabItem Group`
|
||||||
|
where ifnull(parent_item_group, '')=%s""", ig or '')
|
||||||
|
|
||||||
|
if len(children):
|
||||||
|
return get_no_of_children(children, no_of_children + len(children))
|
||||||
|
else:
|
||||||
|
return no_of_children
|
||||||
|
|
||||||
|
return get_no_of_children([item_group], 0)
|
||||||
|
|
||||||
def test_recursion(self):
|
def test_recursion(self):
|
||||||
group_b = webnotes.bean("Item Group", "_Test Item Group B")
|
group_b = webnotes.bean("Item Group", "_Test Item Group B")
|
||||||
group_b.doc.parent_item_group = "_Test Item Group B - 3"
|
group_b.doc.parent_item_group = "_Test Item Group B - 3"
|
||||||
self.assertRaises(NestedSetRecursionError, group_b.save)
|
self.assertRaises(NestedSetRecursionError, group_b.save)
|
||||||
|
|
||||||
|
# cleanup
|
||||||
|
group_b.doc.parent_item_group = "All Item Groups"
|
||||||
|
group_b.save()
|
||||||
|
|
||||||
def test_rebuild_tree(self):
|
def test_rebuild_tree(self):
|
||||||
rebuild_tree("Item Group", "parent_item_group")
|
rebuild_tree("Item Group", "parent_item_group")
|
||||||
self.test_basic_tree()
|
self.test_basic_tree()
|
||||||
@ -126,35 +146,28 @@ class TestItem(unittest.TestCase):
|
|||||||
self.test_basic_tree()
|
self.test_basic_tree()
|
||||||
|
|
||||||
def test_move_group_into_another(self):
|
def test_move_group_into_another(self):
|
||||||
previous_lft_rgt = self.get_lft_rgt(get_ancestors_of("Item Group", "_Test Item Group B"))
|
# before move
|
||||||
|
old_lft, old_rgt = webnotes.conn.get_value("Item Group", "_Test Item Group C", ["lft", "rgt"])
|
||||||
|
|
||||||
# put B under C
|
# put B under C
|
||||||
group_b = webnotes.bean("Item Group", "_Test Item Group B")
|
group_b = webnotes.bean("Item Group", "_Test Item Group B")
|
||||||
|
lft, rgt = group_b.doc.lft, group_b.doc.rgt
|
||||||
|
|
||||||
group_b.doc.parent_item_group = "_Test Item Group C"
|
group_b.doc.parent_item_group = "_Test Item Group C"
|
||||||
group_b.save()
|
group_b.save()
|
||||||
self.test_basic_tree()
|
self.test_basic_tree()
|
||||||
|
|
||||||
# TODO check rgt of old parent and new parent
|
# after move
|
||||||
# check_ancestors_rgt(previous_lft_rgt, "_Test Item Group C", pass)
|
new_lft, new_rgt = webnotes.conn.get_value("Item Group", "_Test Item Group C", ["lft", "rgt"])
|
||||||
|
|
||||||
|
# lft should reduce
|
||||||
|
self.assertEquals(old_lft - new_lft, rgt - lft + 1)
|
||||||
|
|
||||||
|
# adjacent siblings, hence rgt diff will be 0
|
||||||
|
self.assertEquals(new_rgt - old_rgt, 0)
|
||||||
|
|
||||||
self.move_it_back()
|
self.move_it_back()
|
||||||
|
|
||||||
def check_ancestors_rgt(self, previous_lft_rgt, new_parent, increment):
|
|
||||||
if new_parent in previous_lft_rgt:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def get_lft_rgt(self, item_groups):
|
|
||||||
item_groups = webnotes.conn.sql("""select name, lft, rgt from `tabItem Group`
|
|
||||||
where name in ({})""".format(", ".join(["%s"*len(item_groups)])), tuple(item_groups), as_dict=True)
|
|
||||||
|
|
||||||
out = {}
|
|
||||||
for item_group in item_groups:
|
|
||||||
out[item_group.name] = item_group
|
|
||||||
|
|
||||||
return out
|
|
||||||
|
|
||||||
def test_move_group_into_root(self):
|
def test_move_group_into_root(self):
|
||||||
group_b = webnotes.bean("Item Group", "_Test Item Group B")
|
group_b = webnotes.bean("Item Group", "_Test Item Group B")
|
||||||
group_b.doc.parent_item_group = ""
|
group_b.doc.parent_item_group = ""
|
||||||
@ -163,17 +176,31 @@ class TestItem(unittest.TestCase):
|
|||||||
# trick! works because it hasn't been rolled back :D
|
# trick! works because it hasn't been rolled back :D
|
||||||
self.test_basic_tree()
|
self.test_basic_tree()
|
||||||
|
|
||||||
# TODO check rgt of old parent and new parent
|
|
||||||
|
|
||||||
self.move_it_back()
|
self.move_it_back()
|
||||||
|
|
||||||
|
def print_tree(self):
|
||||||
|
import json
|
||||||
|
print json.dumps(webnotes.conn.sql("select name, lft, rgt from `tabItem Group` order by lft"), indent=1)
|
||||||
|
|
||||||
def test_move_leaf_into_another_group(self):
|
def test_move_leaf_into_another_group(self):
|
||||||
|
# before move
|
||||||
|
old_lft, old_rgt = webnotes.conn.get_value("Item Group", "_Test Item Group C", ["lft", "rgt"])
|
||||||
|
|
||||||
group_b_3 = webnotes.bean("Item Group", "_Test Item Group B - 3")
|
group_b_3 = webnotes.bean("Item Group", "_Test Item Group B - 3")
|
||||||
|
lft, rgt = group_b_3.doc.lft, group_b_3.doc.rgt
|
||||||
|
|
||||||
|
# child of right sibling is moved into it
|
||||||
group_b_3.doc.parent_item_group = "_Test Item Group C"
|
group_b_3.doc.parent_item_group = "_Test Item Group C"
|
||||||
group_b_3.save()
|
group_b_3.save()
|
||||||
self.test_basic_tree()
|
self.test_basic_tree()
|
||||||
|
|
||||||
# TODO check rgt of old parent and new parent
|
new_lft, new_rgt = webnotes.conn.get_value("Item Group", "_Test Item Group C", ["lft", "rgt"])
|
||||||
|
|
||||||
|
# lft should remain the same
|
||||||
|
self.assertEquals(old_lft - new_lft, 0)
|
||||||
|
|
||||||
|
# rgt should increase
|
||||||
|
self.assertEquals(new_rgt - old_rgt, rgt - lft + 1)
|
||||||
|
|
||||||
# move it back
|
# move it back
|
||||||
group_b_3 = webnotes.bean("Item Group", "_Test Item Group B - 3")
|
group_b_3 = webnotes.bean("Item Group", "_Test Item Group B - 3")
|
||||||
@ -186,33 +213,63 @@ class TestItem(unittest.TestCase):
|
|||||||
parent_item_group = webnotes.conn.get_value("Item Group", "_Test Item Group B - 3", "parent_item_group")
|
parent_item_group = webnotes.conn.get_value("Item Group", "_Test Item Group B - 3", "parent_item_group")
|
||||||
rgt = webnotes.conn.get_value("Item Group", parent_item_group, "rgt")
|
rgt = webnotes.conn.get_value("Item Group", parent_item_group, "rgt")
|
||||||
|
|
||||||
|
ancestors = get_ancestors_of("Item Group", "_Test Item Group B - 3")
|
||||||
|
ancestors = webnotes.conn.sql("""select name, rgt from `tabItem Group`
|
||||||
|
where name in ({})""".format(", ".join(["%s"]*len(ancestors))), tuple(ancestors), as_dict=True)
|
||||||
|
|
||||||
webnotes.delete_doc("Item Group", "_Test Item Group B - 3")
|
webnotes.delete_doc("Item Group", "_Test Item Group B - 3")
|
||||||
records_to_test = test_records[2:]
|
records_to_test = test_records[2:]
|
||||||
del records_to_test[4]
|
del records_to_test[4]
|
||||||
self.test_basic_tree(records=records_to_test)
|
self.test_basic_tree(records=records_to_test)
|
||||||
|
|
||||||
# TODO rgt of all ancestors should reduce by 2
|
# rgt of each ancestor would reduce by 2
|
||||||
new_rgt = webnotes.conn.get_value("Item Group", parent_item_group, "rgt")
|
for item_group in ancestors:
|
||||||
self.assertEquals(new_rgt, rgt - 2)
|
new_lft, new_rgt = webnotes.conn.get_value("Item Group", item_group.name, ["lft", "rgt"])
|
||||||
|
self.assertEquals(new_rgt, item_group.rgt - 2)
|
||||||
|
|
||||||
# insert it back
|
# insert it back
|
||||||
webnotes.bean(copy=test_records[6]).insert()
|
webnotes.bean(copy=test_records[6]).insert()
|
||||||
|
|
||||||
self.test_basic_tree()
|
self.test_basic_tree()
|
||||||
|
|
||||||
def test_delete_group(self):
|
def test_delete_group(self):
|
||||||
# TODO cannot delete group with child, but can delete leaf
|
# cannot delete group with child, but can delete leaf
|
||||||
pass
|
self.assertRaises(NestedSetChildExistsError, webnotes.delete_doc, "Item Group", "_Test Item Group B")
|
||||||
|
|
||||||
def test_merge_groups(self):
|
def test_merge_groups(self):
|
||||||
pass
|
webnotes.rename_doc("Item Group", "_Test Item Group B", "_Test Item Group C", merge=True)
|
||||||
|
records_to_test = test_records[2:]
|
||||||
|
del records_to_test[1]
|
||||||
|
self.test_basic_tree(records=records_to_test)
|
||||||
|
|
||||||
|
# insert Group B back
|
||||||
|
webnotes.bean(copy=test_records[3]).insert()
|
||||||
|
self.test_basic_tree()
|
||||||
|
|
||||||
|
# move its children back
|
||||||
|
for name in webnotes.conn.sql_list("""select name from `tabItem Group`
|
||||||
|
where parent_item_group='_Test Item Group C'"""):
|
||||||
|
|
||||||
|
bean = webnotes.bean("Item Group", name)
|
||||||
|
bean.doc.parent_item_group = "_Test Item Group B"
|
||||||
|
bean.save()
|
||||||
|
|
||||||
|
self.test_basic_tree()
|
||||||
|
|
||||||
def test_merge_leaves(self):
|
def test_merge_leaves(self):
|
||||||
pass
|
webnotes.rename_doc("Item Group", "_Test Item Group B - 2", "_Test Item Group B - 1", merge=True)
|
||||||
|
records_to_test = test_records[2:]
|
||||||
|
del records_to_test[3]
|
||||||
|
self.test_basic_tree(records=records_to_test)
|
||||||
|
|
||||||
|
# insert Group B - 2back
|
||||||
|
webnotes.bean(copy=test_records[5]).insert()
|
||||||
|
self.test_basic_tree()
|
||||||
|
|
||||||
def test_merge_leaf_into_group(self):
|
def test_merge_leaf_into_group(self):
|
||||||
# should raise exception
|
self.assertRaises(NestedSetInvalidMergeError, webnotes.rename_doc, "Item Group", "_Test Item Group B - 3",
|
||||||
pass
|
"_Test Item Group B", merge=True)
|
||||||
|
|
||||||
def test_merge_group_into_leaf(self):
|
def test_merge_group_into_leaf(self):
|
||||||
# should raise exception
|
self.assertRaises(NestedSetInvalidMergeError, webnotes.rename_doc, "Item Group", "_Test Item Group B",
|
||||||
pass
|
"_Test Item Group B - 3", merge=True)
|
Loading…
x
Reference in New Issue
Block a user