# -*- coding: utf-8 -*-
# 
# django-ldapdb
# Copyright (C) 2009 Bolloré telecom
# See AUTHORS file for a full list of contributors.
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from django.test import TestCase
from django.db.models.sql.where import Constraint, AND, OR

from ldapdb.models.fields import CharField
from ldapdb.models.query import WhereNode, escape_ldap_filter

class WhereTestCase(TestCase):
    def test_escape(self):
        self.assertEquals(escape_ldap_filter('foo*bar'), 'foo\\2abar')
        self.assertEquals(escape_ldap_filter('foo(bar'), 'foo\\28bar')
        self.assertEquals(escape_ldap_filter('foo)bar'), 'foo\\29bar')
        self.assertEquals(escape_ldap_filter('foo\\bar'), 'foo\\5cbar')
        self.assertEquals(escape_ldap_filter('foo\\bar*wiz'), 'foo\\5cbar\\2awiz')

    def test_single(self):
        where = WhereNode()
        where.add((Constraint("cn", "cn", None), 'exact', "test"), AND)
        self.assertEquals(where.as_sql(), "(cn=test)")

        where = WhereNode()
        where.add((Constraint("cn", "cn", None), 'startswith', "test"), AND)
        self.assertEquals(where.as_sql(), "(cn=test*)")

        where = WhereNode()
        where.add((Constraint("cn", "cn", None), 'endswith', "test"), AND)
        self.assertEquals(where.as_sql(), "(cn=*test)")

        where = WhereNode()
        where.add((Constraint("cn", "cn", None), 'in', ["foo", "bar"]), AND)
        self.assertEquals(where.as_sql(), "(|(cn=foo)(cn=bar))")

        where = WhereNode()
        where.add((Constraint("cn", "cn", None), 'contains', "test"), AND)
        self.assertEquals(where.as_sql(), "(cn=*test*)")

    def test_escaped(self):
        where = WhereNode()
        where.add((Constraint("cn", "cn", None), 'exact', "(test)"), AND)
        self.assertEquals(where.as_sql(), "(cn=\\28test\\29)")

    def test_and(self):
        where = WhereNode()
        where.add((Constraint("cn", "cn", None), 'exact', "foo"), AND)
        where.add((Constraint("givenName", "givenName", None), 'exact', "bar"), AND)
        self.assertEquals(where.as_sql(), "(&(cn=foo)(givenName=bar))")

    def test_or(self):
        where = WhereNode()
        where.add((Constraint("cn", "cn", None), 'exact', "foo"), AND)
        where.add((Constraint("givenName", "givenName", None), 'exact', "bar"), OR)
        self.assertEquals(where.as_sql(), "(|(cn=foo)(givenName=bar))")


@@ -902,16 +902,30 @@
 harField
+, IntegerField
 %0Afrom ld
@@ -1424,14 +1424,18 @@
 est_
-single
+char_field
 (sel
@@ -1500,36 +1500,43 @@
 int(%22cn%22, %22cn%22, 
-None
+CharField()
 ), 'exact', %22tes
@@ -1667,28 +1667,35 @@
 %22cn%22, %22cn%22, 
-None
+CharField()
 ), 'startswi
@@ -1836,28 +1836,35 @@
 %22cn%22, %22cn%22, 
-None
+CharField()
 ), 'endswith
@@ -2003,28 +2003,35 @@
 %22cn%22, %22cn%22, 
-None
+CharField()
 ), 'in', %5B%22f
@@ -2181,28 +2181,35 @@
 %22cn%22, %22cn%22, 
-None
+CharField()
 ), 'contains
@@ -2279,24 +2279,216 @@
 =*test*)%22)%0A%0A
+    def test_integer_field(self):%0A        where = WhereNode()%0A        where.add((Constraint(%22uid%22, %22uid%22, CharField()), 'exact', 1), AND)%0A        self.assertEquals(where.as_sql(), %22(uid=1)%22)%0A%0A
     def test
@@ -2565,36 +2565,43 @@
 int(%22cn%22, %22cn%22, 
-None
+CharField()
 ), 'exact', %22(te
@@ -2762,36 +2762,43 @@
 int(%22cn%22, %22cn%22, 
-None
+CharField()
 ), 'exact', %22foo
@@ -2854,36 +2854,43 @@
 %22, %22givenName%22, 
-None
+CharField()
 ), 'exact', %22bar
@@ -3064,20 +3064,27 @@
 , %22cn%22, 
-None
+CharField()
 ), 'exac
@@ -3160,12 +3160,19 @@
 e%22, 
-None
+CharField()
 ), '

improve tests : Constraint should operate on a field