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.models.query 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(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)")
46 where.add((Constraint("cn", "cn", CharField()), 'startswith', "test"), AND)
47 self.assertEquals(where.as_sql(), "(cn=test*)")
50 where.add((Constraint("cn", "cn", CharField()), 'endswith', "test"), AND)
51 self.assertEquals(where.as_sql(), "(cn=*test)")
54 where.add((Constraint("cn", "cn", CharField()), 'in', ["foo", "bar"]), AND)
55 self.assertEquals(where.as_sql(), "(|(cn=foo)(cn=bar))")
58 where.add((Constraint("cn", "cn", CharField()), 'contains', "test"), AND)
59 self.assertEquals(where.as_sql(), "(cn=*test*)")
61 def test_integer_field(self):
63 where.add((Constraint("uid", "uid", CharField()), 'exact', 1), AND)
64 self.assertEquals(where.as_sql(), "(uid=1)")
68 where.add((Constraint("cn", "cn", CharField()), 'exact', "foo"), AND)
69 where.add((Constraint("givenName", "givenName", CharField()), 'exact', "bar"), AND)
70 self.assertEquals(where.as_sql(), "(&(cn=foo)(givenName=bar))")
74 where.add((Constraint("cn", "cn", CharField()), 'exact', "foo"), AND)
75 where.add((Constraint("givenName", "givenName", CharField()), 'exact', "bar"), OR)
76 self.assertEquals(where.as_sql(), "(|(cn=foo)(givenName=bar))")