Debian Codegraph @

tests/test_postgresql.py

raw
1 # vim: set ft=python ts=4 sw=4 expandtab:
2 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
3 #
4 # C E D A R
5 # S O L U T I O N S "Software done right."
6 # S O F T W A R E
7 #
8 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
9 #
10 # Copyright (c) 2006,2010,2015 Kenneth J. Pronovici.
11 # All rights reserved.
12 #
13 # This program is free software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License,
15 # Version 2, as published by the Free Software Foundation.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 #
21 # Copies of the GNU General Public License are available from
22 # the Free Software Foundation website, http://www.gnu.org/.
23 #
24 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
25 #
26 # Author : Kenneth J. Pronovici <[email protected]>
27 # Language : Python 3
28 # Project : Cedar Backup, release 3
29 # Purpose : Tests PostgreSQL extension functionality.
30 #
31 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
32
33 ########################################################################
34 # Module documentation
35 ########################################################################
36
37 """
38 Unit tests for CedarBackup3/extend/postgresql.py.
39
40 Code Coverage
41 =============
42
43 This module contains individual tests for the many of the public functions
44 and classes implemented in extend/postgresql.py. There are also tests for
45 several of the private methods.
46
47 Unfortunately, it's rather difficult to test this code in an automated
48 fashion, even if you have access to PostgreSQL, since the actual dump would
49 need to have access to a real database. Because of this, there aren't any
50 tests below that actually talk to a database.
51
52 As a compromise, I test some of the private methods in the implementation.
53 Normally, I don't like to test private methods, but in this case, testing
54 the private methods will help give us some reasonable confidence in the code
55 even if we can't talk to a database.. This isn't perfect, but it's better
56 than nothing.
57
58 Naming Conventions
59 ==================
60
61 I prefer to avoid large unit tests which validate more than one piece of
62 functionality, and I prefer to avoid using overly descriptive (read: long)
63 test names, as well. Instead, I use lots of very small tests that each
64 validate one specific thing. These small tests are then named with an index
65 number, yielding something like ``testAddDir_001`` or ``testValidate_010``.
66 Each method has a docstring describing what it's supposed to accomplish. I
67 feel that this makes it easier to judge how important a given failure is,
68 and also makes it somewhat easier to diagnose and fix individual problems.
69
70 Testing XML Extraction
71 ======================
72
73 It's difficult to validated that generated XML is exactly "right",
74 especially when dealing with pretty-printed XML. We can't just provide a
75 constant string and say "the result must match this". Instead, what we do
76 is extract a node, build some XML from it, and then feed that XML back into
77 another object's constructor. If that parse process succeeds and the old
78 object is equal to the new object, we assume that the extract was
79 successful.
80
81 It would arguably be better if we could do a completely independent check -
82 but implementing that check would be equivalent to re-implementing all of
83 the existing functionality that we're validating here! After all, the most
84 important thing is that data can move seamlessly from object to XML document
85 and back to object.
86
87 Full vs. Reduced Tests
88 ======================
89
90 All of the tests in this module are considered safe to be run in an average
91 build environment. There is a no need to use a POSTGRESQLTESTS_FULL
92 environment variable to provide a "reduced feature set" test suite as for
93 some of the other test modules.
94
95 @author Kenneth J. Pronovici <[email protected]>
96 """
97
98
99 ########################################################################
100 # Import modules and do runtime validations
101 ########################################################################
102
103 import unittest
104
105 from CedarBackup3.extend.postgresql import LocalConfig, PostgresqlConfig
106 from CedarBackup3.testutil import configureLogging, failUnlessAssignRaises, findResources
107 from CedarBackup3.xmlutil import createOutputDom, serializeDom
108
109 #######################################################################
110 # Module-wide configuration and constants
111 #######################################################################
112
113 DATA_DIRS = [
114 "./data",
115 "./tests/data",
116 ]
117 RESOURCES = [
118 "postgresql.conf.1",
119 "postgresql.conf.2",
120 "postgresql.conf.3",
121 "postgresql.conf.4",
122 "postgresql.conf.5",
123 ]
124
125
126 #######################################################################
127 # Test Case Classes
128 #######################################################################
129
130 #############################
131 # TestPostgresqlConfig class
132 #############################
133
134
135 class TestPostgresqlConfig(unittest.TestCase):
136 """Tests for the PostgresqlConfig class."""
137
138 ################
139 # Setup methods
140 ################
141
142 @classmethod
143 def setUpClass(cls):
144 configureLogging()
145
146 ##################
147 # Utility methods
148 ##################
149
150 def failUnlessAssignRaises(self, exception, obj, prop, value):
151 """Equivalent of :any:`failUnlessRaises`, but used for property assignments instead."""
152 failUnlessAssignRaises(self, exception, obj, prop, value)
153
154 ############################
155 # Test __repr__ and __str__
156 ############################
157
158 def testStringFuncs_001(self):
159 """
160 Just make sure that the string functions don't have errors (i.e. bad variable names).
161 """
162 obj = PostgresqlConfig()
163 obj.__repr__()
164 obj.__str__()
165
166 ##################################
167 # Test constructor and attributes
168 ##################################
169
170 def testConstructor_001(self):
171 """
172 Test constructor with no values filled in.
173 """
174 postgresql = PostgresqlConfig()
175 self.assertEqual(None, postgresql.user)
176 self.assertEqual(None, postgresql.compressMode)
177 self.assertEqual(False, postgresql.all)
178 self.assertEqual(None, postgresql.databases)
179
180 def testConstructor_002(self):
181 """
182 Test constructor with all values filled in, with valid values, databases=None.
183 """
184 postgresql = PostgresqlConfig("user", "none", False, None)
185 self.assertEqual("user", postgresql.user)
186 self.assertEqual("none", postgresql.compressMode)
187 self.assertEqual(False, postgresql.all)
188 self.assertEqual(None, postgresql.databases)
189
190 def testConstructor_003(self):
191 """
192 Test constructor with all values filled in, with valid values, no databases.
193 """
194 postgresql = PostgresqlConfig("user", "none", True, [])
195 self.assertEqual("user", postgresql.user)
196 self.assertEqual("none", postgresql.compressMode)
197 self.assertEqual(True, postgresql.all)
198 self.assertEqual([], postgresql.databases)
199
200 def testConstructor_004(self):
201 """
202 Test constructor with all values filled in, with valid values, with one database.
203 """
204 postgresql = PostgresqlConfig("user", "gzip", True, ["one"])
205 self.assertEqual("user", postgresql.user)
206 self.assertEqual("gzip", postgresql.compressMode)
207 self.assertEqual(True, postgresql.all)
208 self.assertEqual(["one"], postgresql.databases)
209
210 def testConstructor_005(self):
211 """
212 Test constructor with all values filled in, with valid values, with multiple databases.
213 """
214 postgresql = PostgresqlConfig("user", "bzip2", True, ["one", "two"])
215 self.assertEqual("user", postgresql.user)
216 self.assertEqual("bzip2", postgresql.compressMode)
217 self.assertEqual(True, postgresql.all)
218 self.assertEqual(["one", "two"], postgresql.databases)
219
220 def testConstructor_006(self):
221 """
222 Test assignment of user attribute, None value.
223 """
224 postgresql = PostgresqlConfig(user="user")
225 self.assertEqual("user", postgresql.user)
226 postgresql.user = None
227 self.assertEqual(None, postgresql.user)
228
229 def testConstructor_007(self):
230 """
231 Test assignment of user attribute, valid value.
232 """
233 postgresql = PostgresqlConfig()
234 self.assertEqual(None, postgresql.user)
235 postgresql.user = "user"
236 self.assertEqual("user", postgresql.user)
237
238 def testConstructor_008(self):
239 """
240 Test assignment of user attribute, invalid value (empty).
241 """
242 postgresql = PostgresqlConfig()
243 self.assertEqual(None, postgresql.user)
244 self.failUnlessAssignRaises(ValueError, postgresql, "user", "")
245 self.assertEqual(None, postgresql.user)
246
247 def testConstructor_009(self):
248 """
249 Test assignment of compressMode attribute, None value.
250 """
251 postgresql = PostgresqlConfig(compressMode="none")
252 self.assertEqual("none", postgresql.compressMode)
253 postgresql.compressMode = None
254 self.assertEqual(None, postgresql.compressMode)
255
256 def testConstructor_010(self):
257 """
258 Test assignment of compressMode attribute, valid value.
259 """
260 postgresql = PostgresqlConfig()
261 self.assertEqual(None, postgresql.compressMode)
262 postgresql.compressMode = "none"
263 self.assertEqual("none", postgresql.compressMode)
264 postgresql.compressMode = "gzip"
265 self.assertEqual("gzip", postgresql.compressMode)
266 postgresql.compressMode = "bzip2"
267 self.assertEqual("bzip2", postgresql.compressMode)
268
269 def testConstructor_011(self):
270 """
271 Test assignment of compressMode attribute, invalid value (empty).
272 """
273 postgresql = PostgresqlConfig()
274 self.assertEqual(None, postgresql.compressMode)
275 self.failUnlessAssignRaises(ValueError, postgresql, "compressMode", "")
276 self.assertEqual(None, postgresql.compressMode)
277
278 def testConstructor_012(self):
279 """
280 Test assignment of compressMode attribute, invalid value (not in list).
281 """
282 postgresql = PostgresqlConfig()
283 self.assertEqual(None, postgresql.compressMode)
284 self.failUnlessAssignRaises(ValueError, postgresql, "compressMode", "bogus")
285 self.assertEqual(None, postgresql.compressMode)
286
287 def testConstructor_013(self):
288 """
289 Test assignment of all attribute, None value.
290 """
291 postgresql = PostgresqlConfig(all=True)
292 self.assertEqual(True, postgresql.all)
293 postgresql.all = None
294 self.assertEqual(False, postgresql.all)
295
296 def testConstructor_014(self):
297 """
298 Test assignment of all attribute, valid value (real boolean).
299 """
300 postgresql = PostgresqlConfig()
301 self.assertEqual(False, postgresql.all)
302 postgresql.all = True
303 self.assertEqual(True, postgresql.all)
304 postgresql.all = False
305 self.assertEqual(False, postgresql.all)
306
307 def testConstructor_015(self):
308 """
309 Test assignment of all attribute, valid value (expression).
310 """
311 postgresql = PostgresqlConfig()
312 self.assertEqual(False, postgresql.all)
313 postgresql.all = 0
314 self.assertEqual(False, postgresql.all)
315 postgresql.all = []
316 self.assertEqual(False, postgresql.all)
317 postgresql.all = None
318 self.assertEqual(False, postgresql.all)
319 postgresql.all = ["a"]
320 self.assertEqual(True, postgresql.all)
321 postgresql.all = 3
322 self.assertEqual(True, postgresql.all)
323
324 def testConstructor_016(self):
325 """
326 Test assignment of databases attribute, None value.
327 """
328 postgresql = PostgresqlConfig(databases=[])
329 self.assertEqual([], postgresql.databases)
330 postgresql.databases = None
331 self.assertEqual(None, postgresql.databases)
332
333 def testConstructor_017(self):
334 """
335 Test assignment of databases attribute, [] value.
336 """
337 postgresql = PostgresqlConfig()
338 self.assertEqual(None, postgresql.databases)
339 postgresql.databases = []
340 self.assertEqual([], postgresql.databases)
341
342 def testConstructor_018(self):
343 """
344 Test assignment of databases attribute, single valid entry.
345 """
346 postgresql = PostgresqlConfig()
347 self.assertEqual(None, postgresql.databases)
348 postgresql.databases = [
349 "/whatever",
350 ]
351 self.assertEqual(["/whatever"], postgresql.databases)
352 postgresql.databases.append("/stuff")
353 self.assertEqual(["/whatever", "/stuff"], postgresql.databases)
354
355 def testConstructor_019(self):
356 """
357 Test assignment of databases attribute, multiple valid entries.
358 """
359 postgresql = PostgresqlConfig()
360 self.assertEqual(None, postgresql.databases)
361 postgresql.databases = [
362 "/whatever",
363 "/stuff",
364 ]
365 self.assertEqual(["/whatever", "/stuff"], postgresql.databases)
366 postgresql.databases.append("/etc/X11")
367 self.assertEqual(["/whatever", "/stuff", "/etc/X11"], postgresql.databases)
368
369 def testConstructor_020(self):
370 """
371 Test assignment of databases attribute, single invalid entry (empty).
372 """
373 postgresql = PostgresqlConfig()
374 self.assertEqual(None, postgresql.databases)
375 self.failUnlessAssignRaises(ValueError, postgresql, "databases", [""])
376 self.assertEqual(None, postgresql.databases)
377
378 def testConstructor_021(self):
379 """
380 Test assignment of databases attribute, mixed valid and invalid entries.
381 """
382 postgresql = PostgresqlConfig()
383 self.assertEqual(None, postgresql.databases)
384 self.failUnlessAssignRaises(ValueError, postgresql, "databases", ["good", "", "alsogood"])
385 self.assertEqual(None, postgresql.databases)
386
387 ############################
388 # Test comparison operators
389 ############################
390
391 def testComparison_001(self):
392 """
393 Test comparison of two identical objects, all attributes None.
394 """
395 postgresql1 = PostgresqlConfig()
396 postgresql2 = PostgresqlConfig()
397 self.assertEqual(postgresql1, postgresql2)
398 self.assertTrue(postgresql1 == postgresql2)
399 self.assertTrue(not postgresql1 < postgresql2)
400 self.assertTrue(postgresql1 <= postgresql2)
401 self.assertTrue(not postgresql1 > postgresql2)
402 self.assertTrue(postgresql1 >= postgresql2)
403 self.assertTrue(not postgresql1 != postgresql2)
404
405 def testComparison_002(self):
406 """
407 Test comparison of two identical objects, all attributes non-None, list None.
408 """
409 postgresql1 = PostgresqlConfig("user", "gzip", True, None)
410 postgresql2 = PostgresqlConfig("user", "gzip", True, None)
411 self.assertEqual(postgresql1, postgresql2)
412 self.assertTrue(postgresql1 == postgresql2)
413 self.assertTrue(not postgresql1 < postgresql2)
414 self.assertTrue(postgresql1 <= postgresql2)
415 self.assertTrue(not postgresql1 > postgresql2)
416 self.assertTrue(postgresql1 >= postgresql2)
417 self.assertTrue(not postgresql1 != postgresql2)
418
419 def testComparison_003(self):
420 """
421 Test comparison of two identical objects, all attributes non-None, list empty.
422 """
423 postgresql1 = PostgresqlConfig("user", "bzip2", True, [])
424 postgresql2 = PostgresqlConfig("user", "bzip2", True, [])
425 self.assertEqual(postgresql1, postgresql2)
426 self.assertTrue(postgresql1 == postgresql2)
427 self.assertTrue(not postgresql1 < postgresql2)
428 self.assertTrue(postgresql1 <= postgresql2)
429 self.assertTrue(not postgresql1 > postgresql2)
430 self.assertTrue(postgresql1 >= postgresql2)
431 self.assertTrue(not postgresql1 != postgresql2)
432
433 def testComparison_004(self):
434 """
435 Test comparison of two identical objects, all attributes non-None, list non-empty.
436 """
437 postgresql1 = PostgresqlConfig("user", "none", True, ["whatever"])
438 postgresql2 = PostgresqlConfig("user", "none", True, ["whatever"])
439 self.assertEqual(postgresql1, postgresql2)
440 self.assertTrue(postgresql1 == postgresql2)
441 self.assertTrue(not postgresql1 < postgresql2)
442 self.assertTrue(postgresql1 <= postgresql2)
443 self.assertTrue(not postgresql1 > postgresql2)
444 self.assertTrue(postgresql1 >= postgresql2)
445 self.assertTrue(not postgresql1 != postgresql2)
446
447 def testComparison_005(self):
448 """
449 Test comparison of two differing objects, user differs (one None).
450 """
451 postgresql1 = PostgresqlConfig()
452 postgresql2 = PostgresqlConfig(user="user")
453 self.assertNotEqual(postgresql1, postgresql2)
454 self.assertTrue(not postgresql1 == postgresql2)
455 self.assertTrue(postgresql1 < postgresql2)
456 self.assertTrue(postgresql1 <= postgresql2)
457 self.assertTrue(not postgresql1 > postgresql2)
458 self.assertTrue(not postgresql1 >= postgresql2)
459 self.assertTrue(postgresql1 != postgresql2)
460
461 def testComparison_006(self):
462 """
463 Test comparison of two differing objects, user differs.
464 """
465 postgresql1 = PostgresqlConfig("user1", "gzip", True, ["whatever"])
466 postgresql2 = PostgresqlConfig("user2", "gzip", True, ["whatever"])
467 self.assertNotEqual(postgresql1, postgresql2)
468 self.assertTrue(not postgresql1 == postgresql2)
469 self.assertTrue(postgresql1 < postgresql2)
470 self.assertTrue(postgresql1 <= postgresql2)
471 self.assertTrue(not postgresql1 > postgresql2)
472 self.assertTrue(not postgresql1 >= postgresql2)
473 self.assertTrue(postgresql1 != postgresql2)
474
475 def testComparison_007(self):
476 """
477 Test comparison of two differing objects, compressMode differs (one None).
478 """
479 postgresql1 = PostgresqlConfig()
480 postgresql2 = PostgresqlConfig(compressMode="gzip")
481 self.assertNotEqual(postgresql1, postgresql2)
482 self.assertTrue(not postgresql1 == postgresql2)
483 self.assertTrue(postgresql1 < postgresql2)
484 self.assertTrue(postgresql1 <= postgresql2)
485 self.assertTrue(not postgresql1 > postgresql2)
486 self.assertTrue(not postgresql1 >= postgresql2)
487 self.assertTrue(postgresql1 != postgresql2)
488
489 def testComparison_008(self):
490 """
491 Test comparison of two differing objects, compressMode differs.
492 """
493 postgresql1 = PostgresqlConfig("user", "bzip2", True, ["whatever"])
494 postgresql2 = PostgresqlConfig("user", "gzip", True, ["whatever"])
495 self.assertNotEqual(postgresql1, postgresql2)
496 self.assertTrue(not postgresql1 == postgresql2)
497 self.assertTrue(postgresql1 < postgresql2)
498 self.assertTrue(postgresql1 <= postgresql2)
499 self.assertTrue(not postgresql1 > postgresql2)
500 self.assertTrue(not postgresql1 >= postgresql2)
501 self.assertTrue(postgresql1 != postgresql2)
502
503 def testComparison_009(self):
504 """
505 Test comparison of two differing objects, all differs (one None).
506 """
507 postgresql1 = PostgresqlConfig()
508 postgresql2 = PostgresqlConfig(all=True)
509 self.assertNotEqual(postgresql1, postgresql2)
510 self.assertTrue(not postgresql1 == postgresql2)
511 self.assertTrue(postgresql1 < postgresql2)
512 self.assertTrue(postgresql1 <= postgresql2)
513 self.assertTrue(not postgresql1 > postgresql2)
514 self.assertTrue(not postgresql1 >= postgresql2)
515 self.assertTrue(postgresql1 != postgresql2)
516
517 def testComparison_010(self):
518 """
519 Test comparison of two differing objects, all differs.
520 """
521 postgresql1 = PostgresqlConfig("user", "gzip", False, ["whatever"])
522 postgresql2 = PostgresqlConfig("user", "gzip", True, ["whatever"])
523 self.assertNotEqual(postgresql1, postgresql2)
524 self.assertTrue(not postgresql1 == postgresql2)
525 self.assertTrue(postgresql1 < postgresql2)
526 self.assertTrue(postgresql1 <= postgresql2)
527 self.assertTrue(not postgresql1 > postgresql2)
528 self.assertTrue(not postgresql1 >= postgresql2)
529 self.assertTrue(postgresql1 != postgresql2)
530
531 def testComparison_011(self):
532 """
533 Test comparison of two differing objects, databases differs (one None, one empty).
534 """
535 postgresql1 = PostgresqlConfig()
536 postgresql2 = PostgresqlConfig(databases=[])
537 self.assertNotEqual(postgresql1, postgresql2)
538 self.assertTrue(not postgresql1 == postgresql2)
539 self.assertTrue(postgresql1 < postgresql2)
540 self.assertTrue(postgresql1 <= postgresql2)
541 self.assertTrue(not postgresql1 > postgresql2)
542 self.assertTrue(not postgresql1 >= postgresql2)
543 self.assertTrue(postgresql1 != postgresql2)
544
545 def testComparison_012(self):
546 """
547 Test comparison of two differing objects, databases differs (one None, one not empty).
548 """
549 postgresql1 = PostgresqlConfig()
550 postgresql2 = PostgresqlConfig(databases=["whatever"])
551 self.assertNotEqual(postgresql1, postgresql2)
552 self.assertTrue(not postgresql1 == postgresql2)
553 self.assertTrue(postgresql1 < postgresql2)
554 self.assertTrue(postgresql1 <= postgresql2)
555 self.assertTrue(not postgresql1 > postgresql2)
556 self.assertTrue(not postgresql1 >= postgresql2)
557 self.assertTrue(postgresql1 != postgresql2)
558
559 def testComparison_013(self):
560 """
561 Test comparison of two differing objects, databases differs (one empty, one not empty).
562 """
563 postgresql1 = PostgresqlConfig("user", "gzip", True, [])
564 postgresql2 = PostgresqlConfig("user", "gzip", True, ["whatever"])
565 self.assertNotEqual(postgresql1, postgresql2)
566 self.assertTrue(not postgresql1 == postgresql2)
567 self.assertTrue(postgresql1 < postgresql2)
568 self.assertTrue(postgresql1 <= postgresql2)
569 self.assertTrue(not postgresql1 > postgresql2)
570 self.assertTrue(not postgresql1 >= postgresql2)
571 self.assertTrue(postgresql1 != postgresql2)
572
573 def testComparison_014(self):
574 """
575 Test comparison of two differing objects, databases differs (both not empty).
576 """
577 postgresql1 = PostgresqlConfig("user", "gzip", True, ["whatever"])
578 postgresql2 = PostgresqlConfig("user", "gzip", True, ["whatever", "bogus"])
579 self.assertNotEqual(postgresql1, postgresql2)
580 self.assertTrue(not postgresql1 == postgresql2)
581 self.assertTrue(not postgresql1 < postgresql2) # note: different than standard due to unsorted list
582 self.assertTrue(not postgresql1 <= postgresql2) # note: different than standard due to unsorted list
583 self.assertTrue(postgresql1 > postgresql2) # note: different than standard due to unsorted list
584 self.assertTrue(postgresql1 >= postgresql2) # note: different than standard due to unsorted list
585 self.assertTrue(postgresql1 != postgresql2)
586
587
588 ########################
589 # TestLocalConfig class
590 ########################
591
592
593 class TestLocalConfig(unittest.TestCase):
594 """Tests for the LocalConfig class."""
595
596 ################
597 # Setup methods
598 ################
599
600 @classmethod
601 def setUpClass(cls):
602 configureLogging()
603
604 def setUp(self):
605 try:
606 self.resources = findResources(RESOURCES, DATA_DIRS)
607 except Exception as e:
608 self.fail(e)
609
610 def tearDown(self):
611 pass
612
613 ##################
614 # Utility methods
615 ##################
616
617 def failUnlessAssignRaises(self, exception, obj, prop, value):
618 """Equivalent of :any:`failUnlessRaises`, but used for property assignments instead."""
619 failUnlessAssignRaises(self, exception, obj, prop, value)
620
621 def validateAddConfig(self, origConfig):
622 """
623 Validates that document dumped from ``LocalConfig.addConfig`` results in
624 identical object.
625
626 We dump a document containing just the postgresql configuration, and then make
627 sure that if we push that document back into the ``LocalConfig`` object,
628 that the resulting object matches the original.
629
630 The ``self.failUnlessEqual`` method is used for the validation, so if the
631 method call returns normally, everything is OK.
632
633 Args:
634 origConfig: Original configuration
635 """
636 (xmlDom, parentNode) = createOutputDom()
637 origConfig.addConfig(xmlDom, parentNode)
638 xmlData = serializeDom(xmlDom)
639 newConfig = LocalConfig(xmlData=xmlData, validate=False)
640 self.assertEqual(origConfig, newConfig)
641
642 ############################
643 # Test __repr__ and __str__
644 ############################
645
646 def testStringFuncs_001(self):
647 """
648 Just make sure that the string functions don't have errors (i.e. bad variable names).
649 """
650 obj = LocalConfig()
651 obj.__repr__()
652 obj.__str__()
653
654 #####################################################
655 # Test basic constructor and attribute functionality
656 #####################################################
657
658 def testConstructor_001(self):
659 """
660 Test empty constructor, validate=False.
661 """
662 config = LocalConfig(validate=False)
663 self.assertEqual(None, config.postgresql)
664
665 def testConstructor_002(self):
666 """
667 Test empty constructor, validate=True.
668 """
669 config = LocalConfig(validate=True)
670 self.assertEqual(None, config.postgresql)
671
672 def testConstructor_003(self):
673 """
674 Test with empty config document as both data and file, validate=False.
675 """
676 path = self.resources["postgresql.conf.1"]
677 with open(path) as f:
678 contents = f.read()
679 self.assertRaises(ValueError, LocalConfig, xmlData=contents, xmlPath=path, validate=False)
680
681 def testConstructor_004(self):
682 """
683 Test assignment of postgresql attribute, None value.
684 """
685 config = LocalConfig()
686 config.postgresql = None
687 self.assertEqual(None, config.postgresql)
688
689 def testConstructor_005(self):
690 """
691 Test assignment of postgresql attribute, valid value.
692 """
693 config = LocalConfig()
694 config.postgresql = PostgresqlConfig()
695 self.assertEqual(PostgresqlConfig(), config.postgresql)
696
697 def testConstructor_006(self):
698 """
699 Test assignment of postgresql attribute, invalid value (not PostgresqlConfig).
700 """
701 config = LocalConfig()
702 self.failUnlessAssignRaises(ValueError, config, "postgresql", "STRING!")
703
704 ############################
705 # Test comparison operators
706 ############################
707
708 def testComparison_001(self):
709 """
710 Test comparison of two identical objects, all attributes None.
711 """
712 config1 = LocalConfig()
713 config2 = LocalConfig()
714 self.assertEqual(config1, config2)
715 self.assertTrue(config1 == config2)
716 self.assertTrue(not config1 < config2)
717 self.assertTrue(config1 <= config2)
718 self.assertTrue(not config1 > config2)
719 self.assertTrue(config1 >= config2)
720 self.assertTrue(not config1 != config2)
721
722 def testComparison_002(self):
723 """
724 Test comparison of two identical objects, all attributes non-None.
725 """
726 config1 = LocalConfig()
727 config1.postgresql = PostgresqlConfig()
728
729 config2 = LocalConfig()
730 config2.postgresql = PostgresqlConfig()
731
732 self.assertEqual(config1, config2)
733 self.assertTrue(config1 == config2)
734 self.assertTrue(not config1 < config2)
735 self.assertTrue(config1 <= config2)
736 self.assertTrue(not config1 > config2)
737 self.assertTrue(config1 >= config2)
738 self.assertTrue(not config1 != config2)
739
740 def testComparison_003(self):
741 """
742 Test comparison of two differing objects, postgresql differs (one None).
743 """
744 config1 = LocalConfig()
745 config2 = LocalConfig()
746 config2.postgresql = PostgresqlConfig()
747 self.assertNotEqual(config1, config2)
748 self.assertTrue(not config1 == config2)
749 self.assertTrue(config1 < config2)
750 self.assertTrue(config1 <= config2)
751 self.assertTrue(not config1 > config2)
752 self.assertTrue(not config1 >= config2)
753 self.assertTrue(config1 != config2)
754
755 def testComparison_004(self):
756 """
757 Test comparison of two differing objects, postgresql differs.
758 """
759 config1 = LocalConfig()
760 config1.postgresql = PostgresqlConfig(user="one")
761
762 config2 = LocalConfig()
763 config2.postgresql = PostgresqlConfig(user="two")
764
765 self.assertNotEqual(config1, config2)
766 self.assertTrue(not config1 == config2)
767 self.assertTrue(config1 < config2)
768 self.assertTrue(config1 <= config2)
769 self.assertTrue(not config1 > config2)
770 self.assertTrue(not config1 >= config2)
771 self.assertTrue(config1 != config2)
772
773 ######################
774 # Test validate logic
775 ######################
776
777 def testValidate_001(self):
778 """
779 Test validate on a None postgresql section.
780 """
781 config = LocalConfig()
782 config.postgresql = None
783 self.assertRaises(ValueError, config.validate)
784
785 def testValidate_002(self):
786 """
787 Test validate on an empty postgresql section.
788 """
789 config = LocalConfig()
790 config.postgresql = PostgresqlConfig()
791 self.assertRaises(ValueError, config.validate)
792
793 def testValidate_003(self):
794 """
795 Test validate on a non-empty postgresql section, all=True, databases=None.
796 """
797 config = LocalConfig()
798 config.postgresql = PostgresqlConfig("user", "gzip", True, None)
799 config.validate()
800
801 def testValidate_004(self):
802 """
803 Test validate on a non-empty postgresql section, all=True, empty databases.
804 """
805 config = LocalConfig()
806 config.postgresql = PostgresqlConfig("user", "none", True, [])
807 config.validate()
808
809 def testValidate_005(self):
810 """
811 Test validate on a non-empty postgresql section, all=True, non-empty databases.
812 """
813 config = LocalConfig()
814 config.postgresql = PostgresqlConfig("user", "bzip2", True, ["whatever"])
815 self.assertRaises(ValueError, config.validate)
816
817 def testValidate_006(self):
818 """
819 Test validate on a non-empty postgresql section, all=False, databases=None.
820 """
821 config = LocalConfig()
822 config.postgresql = PostgresqlConfig("user", "gzip", False, None)
823 self.assertRaises(ValueError, config.validate)
824
825 def testValidate_007(self):
826 """
827 Test validate on a non-empty postgresql section, all=False, empty databases.
828 """
829 config = LocalConfig()
830 config.postgresql = PostgresqlConfig("user", "bzip2", False, [])
831 self.assertRaises(ValueError, config.validate)
832
833 def testValidate_008(self):
834 """
835 Test validate on a non-empty postgresql section, all=False, non-empty databases.
836 """
837 config = LocalConfig()
838 config.postgresql = PostgresqlConfig("user", "gzip", False, ["whatever"])
839 config.validate()
840
841 def testValidate_009(self):
842 """
843 Test validate on a non-empty postgresql section, with user=None.
844 """
845 config = LocalConfig()
846 config.postgresql = PostgresqlConfig(None, "gzip", True, None)
847 config.validate()
848
849 ############################
850 # Test parsing of documents
851 ############################
852
853 def testParse_001(self):
854 """
855 Parse empty config document.
856 """
857 path = self.resources["postgresql.conf.1"]
858 with open(path) as f:
859 contents = f.read()
860 self.assertRaises(ValueError, LocalConfig, xmlPath=path, validate=True)
861 self.assertRaises(ValueError, LocalConfig, xmlData=contents, validate=True)
862 config = LocalConfig(xmlPath=path, validate=False)
863 self.assertEqual(None, config.postgresql)
864 config = LocalConfig(xmlData=contents, validate=False)
865 self.assertEqual(None, config.postgresql)
866
867 def testParse_003(self):
868 """
869 Parse config document containing only a postgresql section, no databases, all=True.
870 """
871 path = self.resources["postgresql.conf.2"]
872 with open(path) as f:
873 contents = f.read()
874 config = LocalConfig(xmlPath=path, validate=False)
875 self.assertNotEqual(None, config.postgresql)
876 self.assertEqual("user", config.postgresql.user)
877 self.assertEqual("none", config.postgresql.compressMode)
878 self.assertEqual(True, config.postgresql.all)
879 self.assertEqual(None, config.postgresql.databases)
880 config = LocalConfig(xmlData=contents, validate=False)
881 self.assertEqual("user", config.postgresql.user)
882 self.assertEqual("none", config.postgresql.compressMode)
883 self.assertEqual(True, config.postgresql.all)
884 self.assertEqual(None, config.postgresql.databases)
885
886 def testParse_004(self):
887 """
888 Parse config document containing only a postgresql section, single database, all=False.
889 """
890 path = self.resources["postgresql.conf.3"]
891 with open(path) as f:
892 contents = f.read()
893 config = LocalConfig(xmlPath=path, validate=False)
894 self.assertNotEqual(None, config.postgresql)
895 self.assertEqual("user", config.postgresql.user)
896 self.assertEqual("gzip", config.postgresql.compressMode)
897 self.assertEqual(False, config.postgresql.all)
898 self.assertEqual(["database"], config.postgresql.databases)
899 config = LocalConfig(xmlData=contents, validate=False)
900 self.assertNotEqual(None, config.postgresql)
901 self.assertEqual("user", config.postgresql.user)
902 self.assertEqual("gzip", config.postgresql.compressMode)
903 self.assertEqual(False, config.postgresql.all)
904 self.assertEqual(["database"], config.postgresql.databases)
905
906 def testParse_005(self):
907 """
908 Parse config document containing only a postgresql section, multiple databases, all=False.
909 """
910 path = self.resources["postgresql.conf.4"]
911 with open(path) as f:
912 contents = f.read()
913 config = LocalConfig(xmlPath=path, validate=False)
914 self.assertNotEqual(None, config.postgresql)
915 self.assertEqual("user", config.postgresql.user)
916 self.assertEqual("bzip2", config.postgresql.compressMode)
917 self.assertEqual(False, config.postgresql.all)
918 self.assertEqual(["database1", "database2"], config.postgresql.databases)
919 config = LocalConfig(xmlData=contents, validate=False)
920 self.assertNotEqual(None, config.postgresql)
921 self.assertEqual("user", config.postgresql.user)
922 self.assertEqual("bzip2", config.postgresql.compressMode)
923 self.assertEqual(False, config.postgresql.all)
924 self.assertEqual(["database1", "database2"], config.postgresql.databases)
925
926 def testParse_006(self):
927 """
928 Parse config document containing only a postgresql section, no user, multiple databases, all=False.
929 """
930 path = self.resources["postgresql.conf.5"]
931 with open(path) as f:
932 contents = f.read()
933 config = LocalConfig(xmlPath=path, validate=False)
934 self.assertNotEqual(None, config.postgresql)
935 self.assertEqual(None, config.postgresql.user)
936 self.assertEqual("bzip2", config.postgresql.compressMode)
937 self.assertEqual(False, config.postgresql.all)
938 self.assertEqual(["database1", "database2"], config.postgresql.databases)
939 config = LocalConfig(xmlData=contents, validate=False)
940 self.assertNotEqual(None, config.postgresql)
941 self.assertEqual(None, config.postgresql.user)
942 self.assertEqual("bzip2", config.postgresql.compressMode)
943 self.assertEqual(False, config.postgresql.all)
944 self.assertEqual(["database1", "database2"], config.postgresql.databases)
945
946 ###################
947 # Test addConfig()
948 ###################
949
950 def testAddConfig_001(self):
951 """
952 Test with empty config document
953 """
954 config = LocalConfig()
955 self.validateAddConfig(config)
956
957 def testAddConfig_003(self):
958 """
959 Test with no databases, all other values filled in, all=True.
960 """
961 config = LocalConfig()
962 config.postgresql = PostgresqlConfig("user", "none", True, None)
963 self.validateAddConfig(config)
964
965 def testAddConfig_004(self):
966 """
967 Test with no databases, all other values filled in, all=False.
968 """
969 config = LocalConfig()
970 config.postgresql = PostgresqlConfig("user", "gzip", False, None)
971 self.validateAddConfig(config)
972
973 def testAddConfig_005(self):
974 """
975 Test with single database, all other values filled in, all=True.
976 """
977 config = LocalConfig()
978 config.postgresql = PostgresqlConfig("user", "bzip2", True, ["database"])
979 self.validateAddConfig(config)
980
981 def testAddConfig_006(self):
982 """
983 Test with single database, all other values filled in, all=False.
984 """
985 config = LocalConfig()
986 config.postgresql = PostgresqlConfig("user", "none", False, ["database"])
987 self.validateAddConfig(config)
988
989 def testAddConfig_007(self):
990 """
991 Test with multiple databases, all other values filled in, all=True.
992 """
993 config = LocalConfig()
994 config.postgresql = PostgresqlConfig("user", "bzip2", True, ["database1", "database2"])
995 self.validateAddConfig(config)
996
997 def testAddConfig_008(self):
998 """
999 Test with multiple databases, all other values filled in, all=False.
1000 """
1001 config = LocalConfig()
1002 config.postgresql = PostgresqlConfig("user", "gzip", True, ["database1", "database2"])
1003 self.validateAddConfig(config)
1004
1005 def testAddConfig_009(self):
1006 """
1007 Test with multiple databases, user=None but all other values filled in, all=False.
1008 """
1009 config = LocalConfig()
1010 config.postgresql = PostgresqlConfig(None, "gzip", True, ["database1", "database2"])
1011 self.validateAddConfig(config)