1 # -*- coding: utf-8 -*-
4 # Copyright (C) 2009-2010 Bolloré telecom
5 # See AUTHORS file for a full list of contributors.
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 from django.test import TestCase
22 from django.db.models.sql.where import Constraint, AND, OR
24 from ldapdb import escape_ldap_filter
25 from ldapdb.models.fields import CharField, IntegerField, ListField
26 from ldapdb.models.query import WhereNode
28 class WhereTestCase(TestCase):
29 def test_escape(self):
30 self.assertEquals(escape_ldap_filter('foo*bar'), 'foo\\2abar')
31 self.assertEquals(escape_ldap_filter('foo(bar'), 'foo\\28bar')
32 self.assertEquals(escape_ldap_filter('foo)bar'), 'foo\\29bar')
33 self.assertEquals(escape_ldap_filter('foo\\bar'), 'foo\\5cbar')
34 self.assertEquals(escape_ldap_filter('foo\\bar*wiz'), 'foo\\5cbar\\2awiz')
36 def test_char_field_exact(self):
38 where.add((Constraint("cn", "cn", CharField()), 'exact', "test"), AND)
39 self.assertEquals(where.as_sql(), ("(cn=test)", []))
42 where.add((Constraint("cn", "cn", CharField()), 'exact', "(test)"), AND)
43 self.assertEquals(where.as_sql(), ("(cn=\\28test\\29)", []))
45 def test_char_field_in(self):
47 where.add((Constraint("cn", "cn", CharField()), 'in', ["foo", "bar"]), AND)
48 self.assertEquals(where.as_sql(), ("(|(cn=foo)(cn=bar))", []))
51 where.add((Constraint("cn", "cn", CharField()), 'in', ["(foo)", "(bar)"]), AND)
52 self.assertEquals(where.as_sql(), ("(|(cn=\\28foo\\29)(cn=\\28bar\\29))", []))
54 def test_char_field_startswith(self):
56 where.add((Constraint("cn", "cn", CharField()), 'startswith', "test"), AND)
57 self.assertEquals(where.as_sql(), ("(cn=test*)", []))
60 where.add((Constraint("cn", "cn", CharField()), 'startswith', "te*st"), AND)
61 self.assertEquals(where.as_sql(), ("(cn=te\\2ast*)", []))
63 def test_char_field_endswith(self):
65 where.add((Constraint("cn", "cn", CharField()), 'endswith', "test"), AND)
66 self.assertEquals(where.as_sql(), ("(cn=*test)", []))
69 where.add((Constraint("cn", "cn", CharField()), 'endswith', "te*st"), AND)
70 self.assertEquals(where.as_sql(), ("(cn=*te\\2ast)", []))
72 def test_char_field_contains(self):
74 where.add((Constraint("cn", "cn", CharField()), 'contains', "test"), AND)
75 self.assertEquals(where.as_sql(), ("(cn=*test*)", []))
78 where.add((Constraint("cn", "cn", CharField()), 'contains', "te*st"), AND)
79 self.assertEquals(where.as_sql(), ("(cn=*te\\2ast*)", []))
81 def test_integer_field(self):
83 where.add((Constraint("uid", "uid", IntegerField()), 'exact', 1), AND)
84 self.assertEquals(where.as_sql(), ("(uid=1)", []))
87 where.add((Constraint("uid", "uid", IntegerField()), 'gte', 1), AND)
88 self.assertEquals(where.as_sql(), ("(uid>=1)", []))
91 where.add((Constraint("uid", "uid", IntegerField()), 'lte', 1), AND)
92 self.assertEquals(where.as_sql(), ("(uid<=1)", []))
94 def test_list_field_contains(self):
96 where.add((Constraint("memberUid", "memberUid", ListField()), 'contains', 'foouser'), AND)
97 self.assertEquals(where.as_sql(), ("(memberUid=foouser)", []))
100 where.add((Constraint("memberUid", "memberUid", ListField()), 'contains', '(foouser)'), AND)
101 self.assertEquals(where.as_sql(), ("(memberUid=\\28foouser\\29)", []))
105 where.add((Constraint("cn", "cn", CharField()), 'exact', "foo"), AND)
106 where.add((Constraint("givenName", "givenName", CharField()), 'exact', "bar"), AND)
107 self.assertEquals(where.as_sql(), ("(&(cn=foo)(givenName=bar))", []))
111 where.add((Constraint("cn", "cn", CharField()), 'exact', "foo"), AND)
112 where.add((Constraint("givenName", "givenName", CharField()), 'exact', "bar"), OR)
113 self.assertEquals(where.as_sql(), ("(|(cn=foo)(givenName=bar))", []))