def test_parenthesize(self): for value, result in ( (['a'], ['a']), (['a', 'b'], ['a', 'b']), (['(', 'a', ')'], [['a']]), (['a', 'b', '(', 'c', '(', 'd', 'e', ')', 'f', ')', 'g'], ['a', 'b', ['c', ['d', 'e'], 'f'], 'g']), (['a', 'b', '(', 'c'], ['a', 'b', ['c']]), (['a', 'b', '(', 'c', '(', 'd', 'e', ')', 'f'], ['a', 'b', ['c', ['d', 'e'], 'f']]), (['a', 'b', ')'], ['a', 'b']), (['a', 'b', ')', 'c', ')', 'd)'], ['a', 'b']), ): self.assertEqual( rlist(parenthesize(iter(value))), result, msg="parenthesize(%r)" % value)
def test_operatorize(self): "Test operatorize" a = ('a', 'a', 'a') b = ('b', 'b', 'b') c = ('c', 'c', 'c') null_ = ('d', None, 'x') double_null_ = ('e', None, None) for value, result in ( (['a'], ['a']), (['a', 'or', 'b'], [['OR', 'a', 'b']]), (['a', 'or', 'b', 'or', 'c'], [['OR', ['OR', 'a', 'b'], 'c']]), (['a', 'b', 'or', 'c'], ['a', ['OR', 'b', 'c']]), (['a', 'or', 'b', 'c'], [['OR', 'a', 'b'], 'c']), (['a', iter(['b', 'c'])], ['a', ['b', 'c']]), (['a', iter(['b', 'c']), 'd'], ['a', ['b', 'c'], 'd']), (['a', 'or', iter(['b', 'c'])], [['OR', 'a', ['b', 'c']]]), (['a', 'or', iter(['b', 'c']), 'd'], [['OR', 'a', ['b', 'c']], 'd']), (['a', iter(['b', 'c']), 'or', 'd'], ['a', ['OR', ['b', 'c'], 'd']]), (['a', 'or', iter(['b', 'or', 'c'])], [['OR', 'a', [['OR', 'b', 'c']]]]), (['or'], []), (['or', 'a'], ['a']), (['a', iter(['or', 'b'])], ['a', ['b']]), (['a', 'or', 'or', 'b'], [['OR', 'a', 'b']]), (['or', 'or', 'a'], ['a']), (['or', 'or', 'a', 'b'], ['a', 'b']), (['or', 'or', 'a', 'or', 'b'], [['OR', 'a', 'b']]), (['a', iter(['b', 'or', 'c'])], ['a', [['OR', 'b', 'c']]]), ([a, iter([b, ('or',), c])], [a, [['OR', b, c]]]), (['a', iter(['b', 'or'])], ['a', [['OR', 'b']]]), ([null_], [null_]), ([null_, 'or', double_null_], [['OR', null_, double_null_]]), ): self.assertEqual( rlist(operatorize(iter(value))), result, msg="operatorize(%r)" % value)
def test_parse_clause(self): "Test parse clause" dom = DomainParser({ 'name': { 'string': 'Name', 'name': 'name', 'type': 'char', }, 'integer': { 'string': 'Integer', 'name': 'integer', 'type': 'integer', }, 'selection': { 'string': 'Selection', 'name': 'selection', 'type': 'selection', 'selection': [ ('male', 'Male'), ('female', 'Female'), ], }, 'multiselection': { 'string': "MultiSelection", 'name': 'multiselection', 'type': 'multiselection', 'selection': [ ('foo', "Foo"), ('bar', "Bar"), ('baz', "Baz"), ], }, 'reference': { 'string': 'Reference', 'name': 'reference', 'type': 'reference', 'selection': [ ('spam', 'Spam'), ('ham', 'Ham'), ] }, 'many2one': { 'string': 'Many2One', 'name': 'many2one', 'type': 'many2one', }, 'relation': { 'string': "Relation", 'relation': 'relation', 'name': 'relation', 'relation_fields': { 'name': { 'string': "Name", 'name': 'name', 'type': 'char', }, }, }, }) self.assertEqual( rlist(dom.parse_clause([('John',)])), [ ('rec_name', 'ilike', '%John%'), ]) self.assertEqual( rlist(dom.parse_clause([('Name', None, None)])), [ ('name', 'ilike', '%'), ]) self.assertEqual( rlist(dom.parse_clause([('Name', '', None)])), [ ('name', 'ilike', '%'), ]) self.assertEqual( rlist(dom.parse_clause([('Name', '=', None)])), [ ('name', '=', None), ]) self.assertEqual( rlist(dom.parse_clause([('Name', '=', '')])), [ ('name', '=', ''), ]) self.assertEqual( rlist(dom.parse_clause([('Name', None, 'Doe')])), [ ('name', 'ilike', '%Doe%'), ]) self.assertEqual( rlist(dom.parse_clause([('Name', '!', 'Doe')])), [ ('name', 'not ilike', '%Doe%'), ]) self.assertEqual( rlist(dom.parse_clause([('Name', None, ['John', 'Jane'])])), [ ('name', 'in', ['John', 'Jane']), ]) self.assertEqual( rlist(dom.parse_clause([('Name', '!', ['John', 'Jane'])])), [ ('name', 'not in', ['John', 'Jane']), ]) self.assertEqual( rlist(dom.parse_clause([('Selection', None, None)])), [ ('selection', '=', None), ]) self.assertEqual( rlist(dom.parse_clause([('Selection', None, '')])), [ ('selection', '=', ''), ]) self.assertEqual( rlist(dom.parse_clause([('Selection', None, ['Male', 'Female'])])), [('selection', 'in', ['male', 'female'])]) self.assertEqual( rlist(dom.parse_clause([('MultiSelection', None, None)])), [ ('multiselection', '=', None), ]) self.assertEqual( rlist(dom.parse_clause([('MultiSelection', None, '')])), [ ('multiselection', 'in', ['']), ]) self.assertEqual( rlist(dom.parse_clause([('MultiSelection', '=', '')])), [ ('multiselection', '=', ['']), ]) self.assertEqual( rlist(dom.parse_clause([('MultiSelection', '!', '')])), [ ('multiselection', 'not in', ['']), ]) self.assertEqual( rlist(dom.parse_clause([('MultiSelection', '!=', '')])), [ ('multiselection', '!=', ['']), ]) self.assertEqual( rlist(dom.parse_clause( [('MultiSelection', None, ['Foo', 'Bar'])])), [ ('multiselection', 'in', ['foo', 'bar']), ]) self.assertEqual( rlist(dom.parse_clause( [('MultiSelection', '=', ['Foo', 'Bar'])])), [ ('multiselection', '=', ['foo', 'bar']), ]) self.assertEqual( rlist(dom.parse_clause( [('MultiSelection', '!', ['Foo', 'Bar'])])), [ ('multiselection', 'not in', ['foo', 'bar']), ]) self.assertEqual( rlist(dom.parse_clause( [('MultiSelection', '!=', ['Foo', 'Bar'])])), [ ('multiselection', '!=', ['foo', 'bar']), ]) self.assertEqual( rlist(dom.parse_clause([('Integer', None, None)])), [ ('integer', '=', None), ]) self.assertEqual( rlist(dom.parse_clause([('Integer', None, '3..5')])), [[ ('integer', '>=', 3), ('integer', '<=', 5), ]]) self.assertEqual( rlist(dom.parse_clause([('Reference', None, 'foo')])), [ ('reference', 'ilike', '%foo%'), ]) self.assertEqual( rlist(dom.parse_clause([('Reference', None, 'Spam')])), [ ('reference', 'ilike', '%spam%'), ]) self.assertEqual( rlist(dom.parse_clause([('Reference', None, 'Spam,bar')])), [ ('reference.rec_name', 'ilike', '%bar%', 'spam'), ]) self.assertEqual( rlist(dom.parse_clause([('Reference', None, ['foo', 'bar'])])), [ ('reference', 'in', ['foo', 'bar']), ]) self.assertEqual( rlist(dom.parse_clause(['OR', ('Name', None, 'John'), ('Name', None, 'Jane')])), ['OR', ('name', 'ilike', '%John%'), ('name', 'ilike', '%Jane%'), ]) self.assertEqual( rlist(dom.parse_clause([('Many2One', None, 'John')])), [ ('many2one', 'ilike', '%John%'), ]) self.assertEqual( rlist(dom.parse_clause([('Many2One', None, ['John', 'Jane'])])), [ ('many2one.rec_name', 'in', ['John', 'Jane']), ]) self.assertEqual( rlist(dom.parse_clause(iter([iter([['John']])]))), [ [('rec_name', 'ilike', '%John%')]]) self.assertEqual( rlist(dom.parse_clause(iter([['Relation.Name', None, "Test"]]))), [('relation.name', 'ilike', "%Test%")]) self.assertEqual( rlist(dom.parse_clause(iter([['OR']]))), [('rec_name', 'ilike', "%OR%")]) self.assertEqual( rlist(dom.parse_clause(iter([['AND']]))), [('rec_name', 'ilike', "%AND%")])
def test_group(self): "Test group" dom = DomainParser({ 'name': { 'string': 'Name', }, 'firstname': { 'string': 'First Name', }, 'surname': { 'string': '(Sur)Name', }, 'relation': { 'string': "Relation", 'relation': 'relation', 'relation_fields': { 'name': { 'string': "Name", }, }, }, }) self.assertEqual( rlist(dom.group(udlex('Name: Doe'))), [('Name', None, 'Doe')]) self.assertEqual( rlist(dom.group(udlex('"(Sur)Name": Doe'))), [ ('(Sur)Name', None, 'Doe'), ]) self.assertEqual( rlist(dom.group(udlex('Name: Doe Name: John'))), [ ('Name', None, 'Doe'), ('Name', None, 'John'), ]) self.assertEqual( rlist(dom.group(udlex('Name: Name: John'))), [ ('Name', None, None), ('Name', None, 'John')]) self.assertEqual( rlist(dom.group(udlex('First Name: John'))), [ ('First Name', None, 'John'), ]) self.assertEqual( rlist(dom.group(udlex('Name: Doe First Name: John'))), [ ('Name', None, 'Doe'), ('First Name', None, 'John'), ]) self.assertEqual( rlist(dom.group(udlex('First Name: John Name: Doe'))), [ ('First Name', None, 'John'), ('Name', None, 'Doe'), ]) self.assertEqual( rlist(dom.group(udlex('First Name: John First Name: Jane'))), [ ('First Name', None, 'John'), ('First Name', None, 'Jane'), ]) self.assertEqual( rlist(dom.group(udlex('Name: John Doe'))), [ ('Name', None, 'John'), ('Doe',), ]) self.assertEqual( rlist(dom.group(udlex('Name: "John Doe"'))), [ ('Name', None, 'John Doe'), ]) self.assertEqual( rlist(dom.group(udlex('Doe Name: John'))), [ ('Doe',), ('Name', None, 'John'), ]) self.assertEqual( rlist(dom.group(udlex('Name: =Doe'))), [('Name', '=', 'Doe')]) self.assertEqual( rlist(dom.group(udlex('Name: =Doe Name: >John'))), [ ('Name', '=', 'Doe'), ('Name', '>', 'John'), ]) self.assertEqual( rlist(dom.group(udlex('First Name: =John First Name: =Jane'))), [ ('First Name', '=', 'John'), ('First Name', '=', 'Jane'), ]) self.assertEqual( rlist(dom.group(udlex('Name: John;Jane'))), [ ('Name', None, ['John', 'Jane']) ]) self.assertEqual( rlist(dom.group(udlex('Name: John;'))), [ ('Name', None, ['John']) ]) self.assertEqual( rlist(dom.group(udlex('Name: John;Jane Name: Doe'))), [ ('Name', None, ['John', 'Jane']), ('Name', None, 'Doe'), ]) self.assertEqual( rlist(dom.group(udlex('Name: John; Name: Doe'))), [ ('Name', None, ['John']), ('Name', None, 'Doe'), ]) self.assertEqual( rlist(dom.group(udlex('Name:'))), [ ('Name', None, None), ]) self.assertEqual( rlist(dom.group(udlex('Name: ='))), [ ('Name', '=', None), ]) self.assertEqual( rlist(dom.group(udlex('Name: =""'))), [ ('Name', '=', ''), ]) self.assertEqual( rlist(dom.group(udlex('Name: = ""'))), [ ('Name', '=', ''), ]) self.assertEqual( rlist(dom.group(udlex('Name: = Name: Doe'))), [ ('Name', '=', None), ('Name', None, 'Doe'), ]) self.assertEqual( rlist(dom.group(udlex('Name: \\"foo\\"'))), [ ('Name', None, '"foo"'), ]) self.assertEqual( rlist(dom.group(udlex('Name: "" <'))), [ ('Name', '', '<'), ]) self.assertEqual( rlist(dom.group(udlex('Relation.Name: Test'))), [ ('Relation.Name', None, "Test"), ])