django 1.2 compatibility fixes
[matthijs/upstream/django-ldapdb.git] / ldapdb / tests.py
1 # -*- coding: utf-8 -*-
2
3 # django-ldapdb
4 # Copyright (C) 2009 BollorĂ© telecom
5 # See AUTHORS file for a full list of contributors.
6
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.
11
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.
16
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/>.
19 #
20
21 from django.test import TestCase
22 from django.db.models.sql.where import Constraint, AND, OR
23
24 from ldapdb.models.query import escape_ldap_filter
25 from ldapdb.models.fields import CharField, IntegerField
26 from ldapdb.models.query import WhereNode
27
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')
35
36     def test_char_field(self):
37         where = WhereNode()
38         where.add((Constraint("cn", "cn", CharField()), 'exact', "test"), AND)
39         self.assertEquals(where.as_sql(), "(cn=test)")
40
41         where = WhereNode()
42         where.add((Constraint("cn", "cn", CharField()), 'startswith', "test"), AND)
43         self.assertEquals(where.as_sql(), "(cn=test*)")
44
45         where = WhereNode()
46         where.add((Constraint("cn", "cn", CharField()), 'endswith', "test"), AND)
47         self.assertEquals(where.as_sql(), "(cn=*test)")
48
49         where = WhereNode()
50         where.add((Constraint("cn", "cn", CharField()), 'in', ["foo", "bar"]), AND)
51         self.assertEquals(where.as_sql(), "(|(cn=foo)(cn=bar))")
52
53         where = WhereNode()
54         where.add((Constraint("cn", "cn", CharField()), 'contains', "test"), AND)
55         self.assertEquals(where.as_sql(), "(cn=*test*)")
56
57     def test_integer_field(self):
58         where = WhereNode()
59         where.add((Constraint("uid", "uid", CharField()), 'exact', 1), AND)
60         self.assertEquals(where.as_sql(), "(uid=1)")
61
62     def test_escaped(self):
63         where = WhereNode()
64         where.add((Constraint("cn", "cn", CharField()), 'exact', "(test)"), AND)
65         self.assertEquals(where.as_sql(), "(cn=\\28test\\29)")
66
67     def test_and(self):
68         where = WhereNode()
69         where.add((Constraint("cn", "cn", CharField()), 'exact', "foo"), AND)
70         where.add((Constraint("givenName", "givenName", CharField()), 'exact', "bar"), AND)
71         self.assertEquals(where.as_sql(), "(&(cn=foo)(givenName=bar))")
72
73     def test_or(self):
74         where = WhereNode()
75         where.add((Constraint("cn", "cn", CharField()), 'exact', "foo"), AND)
76         where.add((Constraint("givenName", "givenName", CharField()), 'exact', "bar"), OR)
77         self.assertEquals(where.as_sql(), "(|(cn=foo)(givenName=bar))")
78