fix lookups with django 1.2
[matthijs/upstream/django-ldapdb.git] / ldapdb / tests.py
1 # -*- coding: utf-8 -*-
2
3 # django-ldapdb
4 # Copyright (C) 2009-2010 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 import escape_ldap_filter
25 from ldapdb.models.fields import CharField, IntegerField, ListField
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_exact(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()), 'exact', "(test)"), AND)
43         self.assertEquals(where.as_sql(), "(cn=\\28test\\29)")
44
45     def test_char_field_in(self):
46         where = WhereNode()
47         where.add((Constraint("cn", "cn", CharField()), 'in', ["foo", "bar"]), AND)
48         self.assertEquals(where.as_sql(), "(|(cn=foo)(cn=bar))")
49
50         where = WhereNode()
51         where.add((Constraint("cn", "cn", CharField()), 'in', ["(foo)", "(bar)"]), AND)
52         self.assertEquals(where.as_sql(), "(|(cn=\\28foo\\29)(cn=\\28bar\\29))")
53
54     def test_char_field_startswith(self):
55         where = WhereNode()
56         where.add((Constraint("cn", "cn", CharField()), 'startswith', "test"), AND)
57         self.assertEquals(where.as_sql(), "(cn=test*)")
58
59         where = WhereNode()
60         where.add((Constraint("cn", "cn", CharField()), 'startswith', "te*st"), AND)
61         self.assertEquals(where.as_sql(), "(cn=te\\2ast*)")
62
63     def test_char_field_endswith(self):
64         where = WhereNode()
65         where.add((Constraint("cn", "cn", CharField()), 'endswith', "test"), AND)
66         self.assertEquals(where.as_sql(), "(cn=*test)")
67
68         where = WhereNode()
69         where.add((Constraint("cn", "cn", CharField()), 'endswith', "te*st"), AND)
70         self.assertEquals(where.as_sql(), "(cn=*te\\2ast)")
71
72     def test_char_field_contains(self):
73         where = WhereNode()
74         where.add((Constraint("cn", "cn", CharField()), 'contains', "test"), AND)
75         self.assertEquals(where.as_sql(), "(cn=*test*)")
76
77         where = WhereNode()
78         where.add((Constraint("cn", "cn", CharField()), 'contains', "te*st"), AND)
79         self.assertEquals(where.as_sql(), "(cn=*te\\2ast*)")
80
81     def test_integer_field(self):
82         where = WhereNode()
83         where.add((Constraint("uid", "uid", IntegerField()), 'exact', 1), AND)
84         self.assertEquals(where.as_sql(), "(uid=1)")
85
86         where = WhereNode()
87         where.add((Constraint("uid", "uid", IntegerField()), 'gte', 1), AND)
88         self.assertEquals(where.as_sql(), "(uid>=1)")
89
90         where = WhereNode()
91         where.add((Constraint("uid", "uid", IntegerField()), 'lte', 1), AND)
92         self.assertEquals(where.as_sql(), "(uid<=1)")
93
94     def test_and(self):
95         where = WhereNode()
96         where.add((Constraint("cn", "cn", CharField()), 'exact', "foo"), AND)
97         where.add((Constraint("givenName", "givenName", CharField()), 'exact', "bar"), AND)
98         self.assertEquals(where.as_sql(), "(&(cn=foo)(givenName=bar))")
99
100     def test_or(self):
101         where = WhereNode()
102         where.add((Constraint("cn", "cn", CharField()), 'exact', "foo"), AND)
103         where.add((Constraint("givenName", "givenName", CharField()), 'exact', "bar"), OR)
104         self.assertEquals(where.as_sql(), "(|(cn=foo)(givenName=bar))")
105