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('foo*bar'), 'foo\\2abar')
45 self.assertEquals(escape_ldap_filter('foo(bar'), 'foo\\28bar')
46 self.assertEquals(escape_ldap_filter('foo)bar'), 'foo\\29bar')
47 self.assertEquals(escape_ldap_filter('foo\\bar'), 'foo\\5cbar')
48 self.assertEquals(escape_ldap_filter('foo\\bar*wiz'), 'foo\\5cbar\\2awiz')
50 def test_char_field_exact(self):
52 where.add((Constraint("cn", "cn", CharField()), 'exact', "test"), AND)
53 self.assertEquals(where.as_sql(), ("(cn=test)", []))
56 where.add((Constraint("cn", "cn", CharField()), 'exact', "(test)"), AND)
57 self.assertEquals(where.as_sql(), ("(cn=\\28test\\29)", []))
59 def test_char_field_in(self):
61 where.add((Constraint("cn", "cn", CharField()), 'in', ["foo", "bar"]), AND)
62 self.assertEquals(where.as_sql(), ("(|(cn=foo)(cn=bar))", []))
65 where.add((Constraint("cn", "cn", CharField()), 'in', ["(foo)", "(bar)"]), AND)
66 self.assertEquals(where.as_sql(), ("(|(cn=\\28foo\\29)(cn=\\28bar\\29))", []))
68 def test_char_field_startswith(self):
70 where.add((Constraint("cn", "cn", CharField()), 'startswith', "test"), AND)
71 self.assertEquals(where.as_sql(), ("(cn=test*)", []))
74 where.add((Constraint("cn", "cn", CharField()), 'startswith', "te*st"), AND)
75 self.assertEquals(where.as_sql(), ("(cn=te\\2ast*)", []))
77 def test_char_field_endswith(self):
79 where.add((Constraint("cn", "cn", CharField()), 'endswith', "test"), AND)
80 self.assertEquals(where.as_sql(), ("(cn=*test)", []))
83 where.add((Constraint("cn", "cn", CharField()), 'endswith', "te*st"), AND)
84 self.assertEquals(where.as_sql(), ("(cn=*te\\2ast)", []))
86 def test_char_field_contains(self):
88 where.add((Constraint("cn", "cn", CharField()), 'contains', "test"), AND)
89 self.assertEquals(where.as_sql(), ("(cn=*test*)", []))
92 where.add((Constraint("cn", "cn", CharField()), 'contains', "te*st"), AND)
93 self.assertEquals(where.as_sql(), ("(cn=*te\\2ast*)", []))
95 def test_integer_field(self):
97 where.add((Constraint("uid", "uid", IntegerField()), 'exact', 1), AND)
98 self.assertEquals(where.as_sql(), ("(uid=1)", []))
101 where.add((Constraint("uid", "uid", IntegerField()), 'gte', 1), AND)
102 self.assertEquals(where.as_sql(), ("(uid>=1)", []))
105 where.add((Constraint("uid", "uid", IntegerField()), 'lte', 1), AND)
106 self.assertEquals(where.as_sql(), ("(uid<=1)", []))
108 def test_list_field_contains(self):
110 where.add((Constraint("memberUid", "memberUid", ListField()), 'contains', 'foouser'), AND)
111 self.assertEquals(where.as_sql(), ("(memberUid=foouser)", []))
114 where.add((Constraint("memberUid", "memberUid", ListField()), 'contains', '(foouser)'), AND)
115 self.assertEquals(where.as_sql(), ("(memberUid=\\28foouser\\29)", []))
119 where.add((Constraint("cn", "cn", CharField()), 'exact', "foo"), AND)
120 where.add((Constraint("givenName", "givenName", CharField()), 'exact', "bar"), AND)
121 self.assertEquals(where.as_sql(), ("(&(cn=foo)(givenName=bar))", []))
125 where.add((Constraint("cn", "cn", CharField()), 'exact', "foo"), AND)
126 where.add((Constraint("givenName", "givenName", CharField()), 'exact', "bar"), OR)
127 self.assertEquals(where.as_sql(), ("(|(cn=foo)(givenName=bar))", []))