1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
# Created on 2014.01.12
#
# @author: Giovanni Cannata
#
# Copyright 2015 Giovanni Cannata
#
# This file is part of ldap3.
#
# ldap3 is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ldap3 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with ldap3 in the COPYING and COPYING.LESSER files.
# If not, see <http://www.gnu.org/licenses/>.
import unittest
from ldap3.utils.ciDict import CaseInsensitiveDict
class Test(unittest.TestCase):
def test_create_empty_case_insensitive_dict(self):
cid = CaseInsensitiveDict()
self.assertTrue(isinstance(cid, CaseInsensitiveDict))
def test_create_case_insensitive_dict_from_dict(self):
dic = dict()
dic['ONE'] = 1
dic['TWO'] = 2
dic[3] = 3
cid = CaseInsensitiveDict(dic)
self.assertEqual(cid['ONE'], 1)
self.assertEqual(cid['one'], 1)
self.assertEqual(cid['TWO'], 2)
self.assertEqual(cid['two'], 2)
self.assertEqual(cid[3], 3)
def test_create_case_insensitive_dict_from_parameters(self):
cid = CaseInsensitiveDict(one=1, two=2)
self.assertEqual(cid['ONE'], 1)
self.assertEqual(cid['one'], 1)
self.assertEqual(cid['TWO'], 2)
self.assertEqual(cid['two'], 2)
def test_add_values_to_case_insentitive_dict(self):
cid = CaseInsensitiveDict()
cid['oNe'] = 1
cid['tWo'] = 2
cid[3] = 3
self.assertEqual(cid['ONE'], 1)
self.assertEqual(cid['one'], 1)
self.assertEqual(cid['TWO'], 2)
self.assertEqual(cid['two'], 2)
self.assertEqual(cid[3], 3)
def test_modify_value_in_case_insentitive_dict_invariant_key(self):
cid = CaseInsensitiveDict()
cid['oNe'] = 1
cid['tWo'] = 2
cid[3] = 3
self.assertEqual(cid[3], 3)
cid[3] = 'Three'
self.assertEqual(cid['ONE'], 1)
self.assertEqual(cid['oNe'], 1)
self.assertEqual(cid['TWO'], 2)
self.assertEqual(cid['tWo'], 2)
self.assertEqual(cid[3], 'Three')
def test_modify_value_in_case_insentitive_dict_same_case_key(self):
cid = CaseInsensitiveDict()
cid['oNe'] = 1
cid['tWo'] = 2
cid[3] = 3
self.assertEqual(cid['ONE'], 1)
self.assertEqual(cid['oNe'], 1)
cid['oNe'] = 'ONE'
self.assertEqual(cid['ONE'], 'ONE')
self.assertEqual(cid['oNe'], 'ONE')
self.assertEqual(cid['TWO'], 2)
self.assertEqual(cid['two'], 2)
self.assertEqual(cid[3], 3)
def test_modify_value_in_case_insentitive_dict_different_case_key(self):
cid = CaseInsensitiveDict()
cid['oNe'] = 1
cid['tWo'] = 2
cid[3] = 3
self.assertEqual(cid['ONE'], 1)
self.assertEqual(cid['oNe'], 1)
cid['one'] = 'ONE'
self.assertEqual(cid['ONE'], 'ONE')
self.assertEqual(cid['oNe'], 'ONE')
self.assertEqual(cid['TWO'], 2)
self.assertEqual(cid['two'], 2)
self.assertEqual(cid[3], 3)
def test_delete_item_in_case_insentitive_dict_invariant_key(self):
cid = CaseInsensitiveDict()
cid['oNe'] = 1
cid['tWo'] = 2
cid[3] = 3
self.assertEqual(cid[3], 3)
del cid[3]
self.assertEqual(cid['ONE'], 1)
self.assertEqual(cid['oNe'], 1)
self.assertEqual(cid['TWO'], 2)
self.assertEqual(cid['tWo'], 2)
try:
cid[3]
except KeyError:
self.assertTrue(True)
except Exception:
self.assertTrue(False)
def test_delete_item_in_case_insentitive_dict_same_case_key(self):
cid = CaseInsensitiveDict()
cid['oNe'] = 1
cid['tWo'] = 2
cid[3] = 3
self.assertEqual(cid['ONE'], 1)
self.assertEqual(cid['oNe'], 1)
del cid['oNe']
self.assertEqual(cid['TWO'], 2)
self.assertEqual(cid['two'], 2)
self.assertEqual(cid[3], 3)
try:
cid['oNe']
except KeyError:
self.assertTrue(True)
except Exception:
self.assertTrue(False)
try:
cid['ONE']
except KeyError:
self.assertTrue(True)
except Exception:
self.assertTrue(False)
def test_delete_item_in_case_insentitive_dict_different_case_key(self):
cid = CaseInsensitiveDict()
cid['oNe'] = 1
cid['tWo'] = 2
cid[3] = 3
self.assertEqual(cid['ONE'], 1)
self.assertEqual(cid['oNe'], 1)
del cid['one']
self.assertEqual(cid['TWO'], 2)
self.assertEqual(cid['two'], 2)
self.assertEqual(cid[3], 3)
try:
cid['oNe']
except KeyError:
self.assertTrue(True)
except Exception:
self.assertTrue(False)
try:
cid['ONE']
except KeyError:
self.assertTrue(True)
except Exception:
self.assertTrue(False)
def test_len_empty_case_insensitive_dict(self):
cid = CaseInsensitiveDict()
self.assertEqual(len(cid), 0)
def test_len_case_insentitive_dict(self):
cid = CaseInsensitiveDict()
cid['oNe'] = 1
cid['tWo'] = 2
cid[3] = 3
self.assertEqual(len(cid), 3)
cid['ONE'] = 'ONE'
self.assertEqual(len(cid), 3)
def test_case_insensitive_dict_contains_invariant_key(self):
cid = CaseInsensitiveDict()
cid['oNe'] = 1
cid['tWo'] = 2
cid[3] = 3
self.assertTrue(3 in cid)
self.assertFalse('THREE' in cid)
self.assertFalse(4 in cid)
def test_case_insensitive_dict_contains_same_case_key(self):
cid = CaseInsensitiveDict()
cid['oNe'] = 1
cid['tWo'] = 2
cid[3] = 3
self.assertTrue('oNe' in cid)
self.assertFalse('THREE' in cid)
self.assertFalse(4 in cid)
def test_case_insensitive_dict_contains_different_case_key(self):
cid = CaseInsensitiveDict()
cid['oNe'] = 1
cid['tWo'] = 2
cid[3] = 3
self.assertTrue('ONE' in cid)
self.assertFalse('THREE' in cid)
self.assertFalse(4 in cid)
def test_copy_case_insensitive_dict(self):
cid = CaseInsensitiveDict()
cid['oNe'] = 1
cid['tWo'] = 2
cid[3] = 3
cid2 = cid.copy()
self.assertEqual(cid2['ONE'], 1)
self.assertEqual(cid2['one'], 1)
self.assertEqual(cid2['TWO'], 2)
self.assertEqual(cid2['two'], 2)
self.assertEqual(cid2[3], 3)
def test_equality_case_insensitive_dict_with_same_case(self):
cid = CaseInsensitiveDict()
cid['one'] = 1
cid['two'] = 2
cid[3] = 3
cid2 = CaseInsensitiveDict()
cid2['one'] = 1
cid2['two'] = 2
cid2[3] = 3
self.assertEqual(cid, cid2)
def test_equality_case_insensitive_dict_with_different_case(self):
cid = CaseInsensitiveDict()
cid['one'] = 1
cid['two'] = 2
cid[3] = 3
cid2 = CaseInsensitiveDict()
cid2['ONE'] = 1
cid2['TWO'] = 2
cid2[3] = 3
self.assertEqual(cid, cid2)
def test_equality_case_insensitive_dict_with_same_case_dict(self):
cid = CaseInsensitiveDict()
cid['one'] = 1
cid['two'] = 2
cid[3] = 3
dic = dict()
dic['one'] = 1
dic['two'] = 2
dic[3] = 3
self.assertEqual(cid, dic)
def test_equality_case_insensitive_dict_with_different_case_dict(self):
cid = CaseInsensitiveDict()
cid['one'] = 1
cid['two'] = 2
cid[3] = 3
dic = dict()
dic['ONE'] = 1
dic['TWO'] = 2
dic[3] = 3
self.assertEqual(cid, dic)
def test_preserve_key_case_case_insensitive_dict(self):
cid = CaseInsensitiveDict()
cid['One'] = 1
cid['Two'] = 2
cid[3] = 3
key_list = list(cid.keys())
self.assertTrue('One' in key_list)
self.assertTrue('Two' in key_list)
self.assertTrue(3 in key_list)
self.assertFalse('ONE' in key_list)
self.assertFalse('one' in key_list)
self.assertFalse('TWO' in key_list)
self.assertFalse('TWO' in key_list)
self.assertFalse(4 in key_list)
|