prevent lookups on ImageField and ListField for now
[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.models.query 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(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         where = WhereNode()
46         where.add((Constraint("cn", "cn", CharField()), 'startswith', "test"), AND)
47         self.assertEquals(where.as_sql(), "(cn=test*)")
48
49         where = WhereNode()
50         where.add((Constraint("cn", "cn", CharField()), 'endswith', "test"), AND)
51         self.assertEquals(where.as_sql(), "(cn=*test)")
52
53         where = WhereNode()
54         where.add((Constraint("cn", "cn", CharField()), 'in', ["foo", "bar"]), AND)
55         self.assertEquals(where.as_sql(), "(|(cn=foo)(cn=bar))")
56
57         where = WhereNode()
58         where.add((Constraint("cn", "cn", CharField()), 'contains', "test"), AND)
59         self.assertEquals(where.as_sql(), "(cn=*test*)")
60
61     def test_integer_field(self):
62         where = WhereNode()
63         where.add((Constraint("uid", "uid", CharField()), 'exact', 1), AND)
64         self.assertEquals(where.as_sql(), "(uid=1)")
65
66     def test_and(self):
67         where = WhereNode()
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))")
71
72     def test_or(self):
73         where = WhereNode()
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))")
77