[MERGE] lp:~openerp-commiter/openobject-server/trunk-sloppy-rng-jam-validation-aja
bzr revid: jam@tinyerp.com-20120703084620-aioarva0jgombwo4
This commit is contained in:
@@ -6,39 +6,163 @@ from StringIO import StringIO
|
||||
import unittest2
|
||||
|
||||
import openerp
|
||||
from openerp.tools.view_validation import valid_page_in_book, valid_view
|
||||
from openerp.tools.view_validation import *
|
||||
|
||||
invalid_page = etree.parse(StringIO('''\
|
||||
<form>
|
||||
invalid_form = etree.parse(StringIO('''\
|
||||
<form>
|
||||
<label></label>
|
||||
<group>
|
||||
<div>
|
||||
<page></page>
|
||||
<label colspan="True"> </label>
|
||||
<field></field>
|
||||
</div>
|
||||
</group>
|
||||
<notebook>
|
||||
<page>
|
||||
<group col="Two">
|
||||
<div>
|
||||
<label></label>
|
||||
<field colspan="Five"> </field>
|
||||
</div>
|
||||
</group>
|
||||
</page>
|
||||
</notebook>
|
||||
</form>
|
||||
''')).getroot()
|
||||
|
||||
valid_form = etree.parse(StringIO('''\
|
||||
<form string="">
|
||||
<notebook>
|
||||
<label for=""></label>
|
||||
<page>
|
||||
<field name=""></field>
|
||||
<label string=""></label>
|
||||
<field name=""></field>
|
||||
</page>
|
||||
<page>
|
||||
<group colspan="5" col="2">
|
||||
<label for=""></label>
|
||||
<label string="" colspan="5"></label>
|
||||
</group>
|
||||
</page>
|
||||
</notebook>
|
||||
</form>
|
||||
''')).getroot()
|
||||
|
||||
invalid_graph = etree.parse(StringIO('''\
|
||||
<graph>
|
||||
<group>
|
||||
<div>
|
||||
<field></field>
|
||||
<field></field>
|
||||
</div>
|
||||
</group>
|
||||
</graph>
|
||||
''')).getroot()
|
||||
|
||||
valid_graph = etree.parse(StringIO('''\
|
||||
<graph string="">
|
||||
<field></field>
|
||||
<field></field>
|
||||
</graph>
|
||||
''')).getroot()
|
||||
|
||||
invalid_tree = etree.parse(StringIO('''\
|
||||
<tree>
|
||||
<group>
|
||||
<div>
|
||||
<page>
|
||||
</page>
|
||||
<field></field>
|
||||
<field></field>
|
||||
</div>
|
||||
</group>
|
||||
</form>
|
||||
</tree>
|
||||
''')).getroot()
|
||||
|
||||
valid_page = etree.parse(StringIO('''\
|
||||
<form>
|
||||
<notebook>
|
||||
<div>
|
||||
<page>
|
||||
</page>
|
||||
</div>
|
||||
</notebook>
|
||||
</form>
|
||||
valid_tree= etree.parse(StringIO('''\
|
||||
<tree string="">
|
||||
<button></button>
|
||||
<field></field>
|
||||
<field></field>
|
||||
<field></field>
|
||||
<button></button>
|
||||
</tree>
|
||||
''')).getroot()
|
||||
|
||||
|
||||
class test_view_validation(unittest2.TestCase):
|
||||
""" Test the view validation code (but not the views themselves). """
|
||||
|
||||
def test_page_validation(self):
|
||||
assert not valid_page_in_book(invalid_page)
|
||||
assert valid_page_in_book(valid_page)
|
||||
assert not valid_page_in_book(invalid_form)
|
||||
assert valid_page_in_book(valid_form)
|
||||
|
||||
assert not valid_view(invalid_form)
|
||||
assert valid_view(valid_form)
|
||||
|
||||
def test_all_field_validation(self):
|
||||
assert not valid_att_in_field(invalid_form)
|
||||
assert valid_att_in_field(valid_form)
|
||||
|
||||
assert not valid_field_view(invalid_form)
|
||||
assert valid_field_view(valid_form)
|
||||
|
||||
def test_all_label_validation(self):
|
||||
assert not valid_att_in_label(invalid_form)
|
||||
assert valid_att_in_label(valid_form)
|
||||
|
||||
assert not valid_label_view(invalid_form)
|
||||
assert valid_label_view(valid_form)
|
||||
|
||||
def test_form_string_validation(self):
|
||||
assert not valid_att_in_form(invalid_form)
|
||||
assert valid_att_in_form(valid_form)
|
||||
|
||||
assert not valid_form_view(invalid_form)
|
||||
assert valid_form_view(valid_form)
|
||||
|
||||
def test_graph_field_validation(self):
|
||||
assert not valid_field_in_graph(invalid_graph)
|
||||
assert valid_field_in_graph(valid_graph)
|
||||
|
||||
assert not valid_view(invalid_graph)
|
||||
assert valid_view(valid_graph)
|
||||
|
||||
def test_graph_string_validation(self):
|
||||
assert not valid_att_in_graph(invalid_graph)
|
||||
assert valid_att_in_graph(valid_graph)
|
||||
|
||||
assert not valid_graph_view(invalid_graph)
|
||||
assert valid_graph_view(valid_graph)
|
||||
|
||||
def test_tree_field_validation(self):
|
||||
assert not valid_field_in_tree(invalid_tree)
|
||||
assert valid_field_in_tree(valid_tree)
|
||||
|
||||
assert not valid_view(invalid_tree)
|
||||
assert valid_view(valid_tree)
|
||||
|
||||
def test_tree_string_validation(self):
|
||||
assert not valid_att_in_tree(invalid_tree)
|
||||
assert valid_att_in_tree(valid_tree)
|
||||
|
||||
assert not valid_tree_view(invalid_tree)
|
||||
assert valid_tree_view(valid_tree)
|
||||
|
||||
def test_colspan_datatype_validation(self):
|
||||
assert not valid_type_in_colspan(invalid_form)
|
||||
assert valid_type_in_colspan(valid_form)
|
||||
|
||||
assert not valid_colspan_view(invalid_form)
|
||||
assert valid_colspan_view(valid_form)
|
||||
|
||||
def test_col_datatype_validation(self):
|
||||
assert not valid_type_in_col(invalid_form)
|
||||
assert valid_type_in_col(valid_form)
|
||||
|
||||
assert not valid_col_view(invalid_form)
|
||||
assert valid_col_view(valid_form)
|
||||
|
||||
assert not valid_view(invalid_page)
|
||||
assert valid_view(valid_page)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest2.main()
|
||||
|
||||
@@ -4,9 +4,110 @@ def valid_page_in_book(arch):
|
||||
"""A `page` node must be below a `book` node."""
|
||||
return not arch.xpath('//page[not(ancestor::notebook)]')
|
||||
|
||||
def valid_field_in_graph(arch):
|
||||
"""A `field` node must be below a `graph` node."""
|
||||
return not arch.xpath('//graph[not(field)]')
|
||||
|
||||
def valid_field_in_tree(arch):
|
||||
"""A `field` and `button` node must be below a `tree` node."""
|
||||
return not arch.xpath('//tree[not((field) and (button))]')
|
||||
|
||||
def valid_att_in_field(arch):
|
||||
"""A `name` attribute must be in a `field` node."""
|
||||
return not arch.xpath('//field[not (@name)]')
|
||||
|
||||
def valid_att_in_label(arch):
|
||||
"""A `for` and `string` attribute must be in a `lable` node."""
|
||||
return not arch.xpath('//label[not ((@for) or (@string))]')
|
||||
|
||||
def valid_att_in_tree(arch):
|
||||
"""A `string` attribute must be in a `tree` node."""
|
||||
return not arch.xpath('//tree[not (@string)]')
|
||||
|
||||
def valid_att_in_form(arch):
|
||||
"""A `string` attribute must be in a `form` node."""
|
||||
return not arch.xpath('//form[not (@string)]')
|
||||
|
||||
def valid_att_in_graph(arch):
|
||||
"""A `string` attribute must be in a `graph` node."""
|
||||
return not arch.xpath('//graph[not (@string)]')
|
||||
|
||||
def valid_type_in_colspan(arch):
|
||||
"""A `colspan` attribute must be an `integer` type."""
|
||||
for attrib in arch.xpath('//*/@colspan'):
|
||||
try:
|
||||
int(attrib)
|
||||
except:
|
||||
return False
|
||||
return True
|
||||
|
||||
def valid_type_in_col(arch):
|
||||
"""A `col` attribute must be an `integer` type."""
|
||||
for attrib in arch.xpath('//*/@col'):
|
||||
try:
|
||||
int(attrib)
|
||||
except:
|
||||
return False
|
||||
return True
|
||||
|
||||
def valid_colspan_view(arch):
|
||||
for pred in [valid_type_in_colspan]:
|
||||
if not pred(arch):
|
||||
return False
|
||||
return True
|
||||
|
||||
def valid_col_view(arch):
|
||||
for pred in [valid_type_in_col]:
|
||||
if not pred(arch):
|
||||
return False
|
||||
return True
|
||||
|
||||
def valid_field_view(arch):
|
||||
for pred in [valid_att_in_field]:
|
||||
if not pred(arch):
|
||||
return False
|
||||
return True
|
||||
|
||||
def valid_label_view(arch):
|
||||
for pred in [valid_att_in_label]:
|
||||
if not pred(arch):
|
||||
return False
|
||||
return True
|
||||
|
||||
def valid_tree_view(arch):
|
||||
if arch.tag == 'tree':
|
||||
for pred in [valid_att_in_tree]:
|
||||
if not pred(arch):
|
||||
return False
|
||||
return True
|
||||
|
||||
def valid_form_view(arch):
|
||||
if arch.tag == 'form':
|
||||
for pred in [valid_att_in_form]:
|
||||
if not pred(arch):
|
||||
return False
|
||||
return True
|
||||
|
||||
def valid_graph_view(arch):
|
||||
if arch.tag == 'graph':
|
||||
for pred in [valid_att_in_graph]:
|
||||
if not pred(arch):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def valid_view(arch):
|
||||
if arch.tag == 'form':
|
||||
for pred in [valid_page_in_book]:
|
||||
if not pred(arch):
|
||||
return False
|
||||
elif arch.tag == 'graph':
|
||||
for pred in [valid_field_in_graph]:
|
||||
if not pred(arch):
|
||||
return False
|
||||
elif arch.tag == 'tree':
|
||||
for pred in [valid_field_in_tree]:
|
||||
if not pred(arch):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
Reference in New Issue
Block a user