1 # -*- coding: utf-8 -*-
4 # Copyright (c) 2009-2010, Bolloré telecom
7 # See AUTHORS file for a full list of contributors.
9 # Redistribution and use in source and binary forms, with or without modification,
10 # are permitted provided that the following conditions are met:
12 # 1. Redistributions of source code must retain the above copyright notice,
13 # this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in the
17 # documentation and/or other materials provided with the distribution.
19 # 3. Neither the name of Bolloré telecom nor the names of its contributors
20 # may be used to endorse or promote products derived from this software
21 # without specific prior written permission.
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
27 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 from django.test import TestCase
36 from django.db.models.sql.where import Constraint, AND, OR
38 from ldapdb import escape_ldap_filter
39 from ldapdb.models.fields import CharField, IntegerField, ListField
40 from ldapdb.models.query import WhereNode
42 class WhereTestCase(TestCase):
43 def test_escape(self):
44 self.assertEquals(escape_ldap_filter(u'fôöbàr'), u'fôöbàr')
45 self.assertEquals(escape_ldap_filter('foo*bar'), 'foo\\2abar')
46 self.assertEquals(escape_ldap_filter('foo(bar'), 'foo\\28bar')
47 self.assertEquals(escape_ldap_filter('foo)bar'), 'foo\\29bar')
48 self.assertEquals(escape_ldap_filter('foo\\bar'), 'foo\\5cbar')
49 self.assertEquals(escape_ldap_filter('foo\\bar*wiz'), 'foo\\5cbar\\2awiz')
51 def test_char_field_exact(self):
53 where.add((Constraint("cn", "cn", CharField()), 'exact', "test"), AND)
54 self.assertEquals(where.as_sql(), ("(cn=test)", []))
57 where.add((Constraint("cn", "cn", CharField()), 'exact', "(test)"), AND)
58 self.assertEquals(where.as_sql(), ("(cn=\\28test\\29)", []))
60 def test_char_field_in(self):
62 where.add((Constraint("cn", "cn", CharField()), 'in', ["foo", "bar"]), AND)
63 self.assertEquals(where.as_sql(), ("(|(cn=foo)(cn=bar))", []))
66 where.add((Constraint("cn", "cn", CharField()), 'in', ["(foo)", "(bar)"]), AND)
67 self.assertEquals(where.as_sql(), ("(|(cn=\\28foo\\29)(cn=\\28bar\\29))", []))
69 def test_char_field_startswith(self):
71 where.add((Constraint("cn", "cn", CharField()), 'startswith', "test"), AND)
72 self.assertEquals(where.as_sql(), ("(cn=test*)", []))
75 where.add((Constraint("cn", "cn", CharField()), 'startswith', "te*st"), AND)
76 self.assertEquals(where.as_sql(), ("(cn=te\\2ast*)", []))
78 def test_char_field_endswith(self):
80 where.add((Constraint("cn", "cn", CharField()), 'endswith', "test"), AND)
81 self.assertEquals(where.as_sql(), ("(cn=*test)", []))
84 where.add((Constraint("cn", "cn", CharField()), 'endswith', "te*st"), AND)
85 self.assertEquals(where.as_sql(), ("(cn=*te\\2ast)", []))
87 def test_char_field_contains(self):
89 where.add((Constraint("cn", "cn", CharField()), 'contains', "test"), AND)
90 self.assertEquals(where.as_sql(), ("(cn=*test*)", []))
93 where.add((Constraint("cn", "cn", CharField()), 'contains', "te*st"), AND)
94 self.assertEquals(where.as_sql(), ("(cn=*te\\2ast*)", []))
96 def test_integer_field(self):
98 where.add((Constraint("uid", "uid", IntegerField()), 'exact', 1), AND)
99 self.assertEquals(where.as_sql(), ("(uid=1)", []))
102 where.add((Constraint("uid", "uid", IntegerField()), 'gte', 1), AND)
103 self.assertEquals(where.as_sql(), ("(uid>=1)", []))
106 where.add((Constraint("uid", "uid", IntegerField()), 'lte', 1), AND)
107 self.assertEquals(where.as_sql(), ("(uid<=1)", []))
109 def test_list_field_contains(self):
111 where.add((Constraint("memberUid", "memberUid", ListField()), 'contains', 'foouser'), AND)
112 self.assertEquals(where.as_sql(), ("(memberUid=foouser)", []))
115 where.add((Constraint("memberUid", "memberUid", ListField()), 'contains', '(foouser)'), AND)
116 self.assertEquals(where.as_sql(), ("(memberUid=\\28foouser\\29)", []))
120 where.add((Constraint("cn", "cn", CharField()), 'exact', "foo"), AND)
121 where.add((Constraint("givenName", "givenName", CharField()), 'exact', "bar"), AND)
122 self.assertEquals(where.as_sql(), ("(&(cn=foo)(givenName=bar))", []))
126 where.add((Constraint("cn", "cn", CharField()), 'exact', "foo"), AND)
127 where.add((Constraint("givenName", "givenName", CharField()), 'exact', "bar"), OR)
128 self.assertEquals(where.as_sql(), ("(|(cn=foo)(givenName=bar))", []))