Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

#! /usr/bin/python -E 

# 

# Copyright (C) 2007    Red Hat 

# see file 'COPYING' for use and warranty information 

# 

# 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/>. 

# 

 

import sys 

sys.path.insert(0, ".") 

 

import unittest 

import datetime 

 

from ipapython import ipautil 

 

 

class TestCIDict(unittest.TestCase): 

    def setUp(self): 

        self.cidict = ipautil.CIDict() 

        self.cidict["Key1"] = "val1" 

        self.cidict["key2"] = "val2" 

        self.cidict["KEY3"] = "VAL3" 

 

    def tearDown(self): 

        pass 

 

    def testLen(self): 

        self.assertEqual(3, len(self.cidict)) 

 

    def test__GetItem(self): 

        self.assertEqual("val1", self.cidict["Key1"]) 

        self.assertEqual("val1", self.cidict["key1"]) 

        self.assertEqual("val2", self.cidict["KEY2"]) 

        self.assertEqual("VAL3", self.cidict["key3"]) 

        self.assertEqual("VAL3", self.cidict["KEY3"]) 

        try: 

            self.cidict["key4"] 

            fail("should have raised KeyError") 

        except KeyError: 

            pass 

 

    def testGet(self): 

        self.assertEqual("val1", self.cidict.get("Key1")) 

        self.assertEqual("val1", self.cidict.get("key1")) 

        self.assertEqual("val2", self.cidict.get("KEY2")) 

        self.assertEqual("VAL3", self.cidict.get("key3")) 

        self.assertEqual("VAL3", self.cidict.get("KEY3")) 

        self.assertEqual("default", self.cidict.get("key4", "default")) 

 

    def test__SetItem(self): 

        self.cidict["key4"] = "val4" 

        self.assertEqual("val4", self.cidict["key4"]) 

        self.cidict["KEY4"] = "newval4" 

        self.assertEqual("newval4", self.cidict["key4"]) 

 

    def testDel(self): 

        self.assert_(self.cidict.has_key("Key1")) 

        del(self.cidict["Key1"]) 

        self.failIf(self.cidict.has_key("Key1")) 

 

        self.assert_(self.cidict.has_key("key2")) 

        del(self.cidict["KEY2"]) 

        self.failIf(self.cidict.has_key("key2")) 

 

    def testClear(self): 

        self.assertEqual(3, len(self.cidict)) 

        self.cidict.clear() 

        self.assertEqual(0, len(self.cidict)) 

 

    def testCopy(self): 

        """A copy is no longer a CIDict, but should preserve the case of 

           the keys as they were inserted.""" 

        copy = self.cidict.copy() 

        self.assertEqual(3, len(copy)) 

        self.assert_(copy.has_key("Key1")) 

        self.assertEqual("val1", copy["Key1"]) 

        self.failIf(copy.has_key("key1")) 

 

    def testHasKey(self): 

        self.assert_(self.cidict.has_key("KEY1")) 

        self.assert_(self.cidict.has_key("key2")) 

        self.assert_(self.cidict.has_key("key3")) 

 

    def testItems(self): 

        items = self.cidict.items() 

        self.assertEqual(3, len(items)) 

        items_set = set(items) 

        self.assert_(("Key1", "val1") in items_set) 

        self.assert_(("key2", "val2") in items_set) 

        self.assert_(("KEY3", "VAL3") in items_set) 

 

    def testIterItems(self): 

        items = [] 

        for (k,v) in self.cidict.iteritems(): 

            items.append((k,v)) 

        self.assertEqual(3, len(items)) 

        items_set = set(items) 

        self.assert_(("Key1", "val1") in items_set) 

        self.assert_(("key2", "val2") in items_set) 

        self.assert_(("KEY3", "VAL3") in items_set) 

 

    def testIterKeys(self): 

        keys = [] 

        for k in self.cidict.iterkeys(): 

            keys.append(k) 

        self.assertEqual(3, len(keys)) 

        keys_set = set(keys) 

        self.assert_("Key1" in keys_set) 

        self.assert_("key2" in keys_set) 

        self.assert_("KEY3" in keys_set) 

 

    def testIterValues(self): 

        values = [] 

        for k in self.cidict.itervalues(): 

            values.append(k) 

        self.assertEqual(3, len(values)) 

        values_set = set(values) 

        self.assert_("val1" in values_set) 

        self.assert_("val2" in values_set) 

        self.assert_("VAL3" in values_set) 

 

    def testKeys(self): 

        keys = self.cidict.keys() 

        self.assertEqual(3, len(keys)) 

        keys_set = set(keys) 

        self.assert_("Key1" in keys_set) 

        self.assert_("key2" in keys_set) 

        self.assert_("KEY3" in keys_set) 

 

    def testValues(self): 

        values = self.cidict.values() 

        self.assertEqual(3, len(values)) 

        values_set = set(values) 

        self.assert_("val1" in values_set) 

        self.assert_("val2" in values_set) 

        self.assert_("VAL3" in values_set) 

 

    def testUpdate(self): 

        newdict = { "KEY2": "newval2", 

                    "key4": "val4" } 

        self.cidict.update(newdict) 

        self.assertEqual(4, len(self.cidict)) 

 

        items = self.cidict.items() 

        self.assertEqual(4, len(items)) 

        items_set = set(items) 

        self.assert_(("Key1", "val1") in items_set) 

        # note the update "overwrites" the case of the key2 

        self.assert_(("KEY2", "newval2") in items_set) 

        self.assert_(("KEY3", "VAL3") in items_set) 

        self.assert_(("key4", "val4") in items_set) 

 

    def testSetDefault(self): 

        self.assertEqual("val1", self.cidict.setdefault("KEY1", "default")) 

 

        self.failIf(self.cidict.has_key("KEY4")) 

        self.assertEqual("default", self.cidict.setdefault("KEY4", "default")) 

        self.assert_(self.cidict.has_key("KEY4")) 

        self.assertEqual("default", self.cidict["key4"]) 

 

        self.failIf(self.cidict.has_key("KEY5")) 

        self.assertEqual(None, self.cidict.setdefault("KEY5")) 

        self.assert_(self.cidict.has_key("KEY5")) 

        self.assertEqual(None, self.cidict["key5"]) 

 

    def testPop(self): 

        self.assertEqual("val1", self.cidict.pop("KEY1", "default")) 

        self.failIf(self.cidict.has_key("key1")) 

 

        self.assertEqual("val2", self.cidict.pop("KEY2")) 

        self.failIf(self.cidict.has_key("key2")) 

 

        self.assertEqual("default", self.cidict.pop("key4", "default")) 

        try: 

            self.cidict.pop("key4") 

            fail("should have raised KeyError") 

        except KeyError: 

            pass 

 

    def testPopItem(self): 

        items = set(self.cidict.items()) 

        self.assertEqual(3, len(self.cidict)) 

 

        item = self.cidict.popitem() 

        self.assertEqual(2, len(self.cidict)) 

        self.assert_(item in items) 

        items.discard(item) 

 

        item = self.cidict.popitem() 

        self.assertEqual(1, len(self.cidict)) 

        self.assert_(item in items) 

        items.discard(item) 

 

        item = self.cidict.popitem() 

        self.assertEqual(0, len(self.cidict)) 

        self.assert_(item in items) 

        items.discard(item) 

 

class TestTimeParser(unittest.TestCase): 

    def setUp(self): 

        pass 

 

    def tearDown(self): 

        pass 

 

    def testSimple(self): 

        timestr = "20070803" 

 

        time = ipautil.parse_generalized_time(timestr) 

        self.assertEqual(2007, time.year) 

        self.assertEqual(8, time.month) 

        self.assertEqual(3, time.day) 

        self.assertEqual(0, time.hour) 

        self.assertEqual(0, time.minute) 

        self.assertEqual(0, time.second) 

 

    def testHourMinSec(self): 

        timestr = "20051213141205" 

 

        time = ipautil.parse_generalized_time(timestr) 

        self.assertEqual(2005, time.year) 

        self.assertEqual(12, time.month) 

        self.assertEqual(13, time.day) 

        self.assertEqual(14, time.hour) 

        self.assertEqual(12, time.minute) 

        self.assertEqual(5, time.second) 

 

    def testFractions(self): 

        timestr = "2003092208.5" 

 

        time = ipautil.parse_generalized_time(timestr) 

        self.assertEqual(2003, time.year) 

        self.assertEqual(9, time.month) 

        self.assertEqual(22, time.day) 

        self.assertEqual(8, time.hour) 

        self.assertEqual(30, time.minute) 

        self.assertEqual(0, time.second) 

 

        timestr = "199203301544,25" 

 

        time = ipautil.parse_generalized_time(timestr) 

        self.assertEqual(1992, time.year) 

        self.assertEqual(3, time.month) 

        self.assertEqual(30, time.day) 

        self.assertEqual(15, time.hour) 

        self.assertEqual(44, time.minute) 

        self.assertEqual(15, time.second) 

 

        timestr = "20060401185912,8" 

 

        time = ipautil.parse_generalized_time(timestr) 

        self.assertEqual(2006, time.year) 

        self.assertEqual(4, time.month) 

        self.assertEqual(1, time.day) 

        self.assertEqual(18, time.hour) 

        self.assertEqual(59, time.minute) 

        self.assertEqual(12, time.second) 

        self.assertEqual(800000, time.microsecond) 

 

    def testTimeZones(self): 

        timestr = "20051213141205Z" 

 

        time = ipautil.parse_generalized_time(timestr) 

        self.assertEqual(0, time.tzinfo.houroffset) 

        self.assertEqual(0, time.tzinfo.minoffset) 

        offset = time.tzinfo.utcoffset(time.tzinfo.dst()) 

        self.assertEqual(0, offset.seconds) 

 

        timestr = "20051213141205+0500" 

 

        time = ipautil.parse_generalized_time(timestr) 

        self.assertEqual(5, time.tzinfo.houroffset) 

        self.assertEqual(0, time.tzinfo.minoffset) 

        offset = time.tzinfo.utcoffset(time.tzinfo.dst()) 

        self.assertEqual(5 * 60 * 60, offset.seconds) 

 

        timestr = "20051213141205-0500" 

 

        time = ipautil.parse_generalized_time(timestr) 

        self.assertEqual(-5, time.tzinfo.houroffset) 

        self.assertEqual(0, time.tzinfo.minoffset) 

        # NOTE - the offset is always positive - it's minutes 

        #        _east_ of UTC 

        offset = time.tzinfo.utcoffset(time.tzinfo.dst()) 

        self.assertEqual((24 - 5) * 60 * 60, offset.seconds) 

 

        timestr = "20051213141205-0930" 

 

        time = ipautil.parse_generalized_time(timestr) 

        self.assertEqual(-9, time.tzinfo.houroffset) 

        self.assertEqual(-30, time.tzinfo.minoffset) 

        offset = time.tzinfo.utcoffset(time.tzinfo.dst()) 

        self.assertEqual(((24 - 9) * 60 * 60) - (30 * 60), offset.seconds) 

 

 

if __name__ == '__main__': 

    unittest.main()