-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathtests.py
More file actions
1903 lines (1693 loc) · 72.4 KB
/
tests.py
File metadata and controls
1903 lines (1693 loc) · 72.4 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Python API for the Nitrate test case management system.
# Copyright (c) 2012 Red Hat, Inc. All rights reserved.
# Author: Petr Splichal <psplicha@redhat.com>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# This library 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 2.1 of the License, or (at your option) any later version.
#
# This library 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.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""
Test Suite
For running the unit test suite additional sections are required in the
configuration file. These contain the url of the test server and the
data of existing objects to be tested, for example:
[test]
url = https://test.server/xmlrpc/
[user]
id = 1234
login = username
email = username@example.com
[product]
id = 60
name = Red Hat Enterprise Linux 6
[version]
id = 1234
name = 6.0
[build]
id = 12345
name = RHEL6.6-20140404
[component]
id = 123
name = wget
product = Red Hat Enterprise Linux 6
[category]
id = 123
name = Integration
[plantype]
id = 1
name = General
[tag]
id = 1234
name = TestTag
[testplan]
id = 1234
name = Test plan
type = Function
product = Red Hat Enterprise Linux 6
version = 6.1
status = ENABLED
owner = login
[testrun]
id = 6757
summary = Test Run Summary
started = 2012-12-12 12:12:12
finished = None
[testcase]
id = 1234
summary = Test case summary
product = Red Hat Enterprise Linux 6
category = Sanity
created = 2012-12-12 12:12:12
[caserun]
id = 123456
status = PASSED
To exercise the whole test suite run "python -m nitrate.tests". To test
only subset of tests pick the desired classes on the command line:
python -m nitrate.tests TestCase
Performance tests
~~~~~~~~~~~~~~~~~~
For running the performance test suite an additional section containing
information about the test bed is required:
[performance]
testplan = 1234
testrun = 12345
Use the test-bed-prepare.py script attached in the test directory to
prepare the structure of test plans, test runs and test cases. To run
the performance test suite use --performance command line option.
"""
from __future__ import print_function
import six
import sys
import random
import optparse
import tempfile
import unittest
import datetime
from nitrate.utils import *
from nitrate.cache import *
from nitrate.config import *
from nitrate.base import *
from nitrate.immutable import *
from nitrate.mutable import *
from nitrate.containers import *
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Constants
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Since python 2.7 the test suite results are too verbose
VERBOSE_UNITTEST = sys.version_info >= (2, 7)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Internal Utilities
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def _print_time(elapsed_time):
""" Human readable time format for performance tests """
converted_time = str(datetime.timedelta(seconds=elapsed_time)).split('.')
sys.stderr.write("{0} ... ".format(converted_time[0]))
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Utils
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class UtilsTests(unittest.TestCase):
""" Tests for utility functions """
def test_listed(self):
""" Function listed() sanity """
self.assertEqual(listed(range(1)), "0")
self.assertEqual(listed(range(2)), "0 and 1")
self.assertEqual(listed(range(3), quote='"'), '"0", "1" and "2"')
self.assertEqual(listed(range(4), max=3), "0, 1, 2 and 1 more")
self.assertEqual(listed(range(5), 'number', max=3),
"0, 1, 2 and 2 more numbers")
self.assertEqual(listed(range(6), 'category'), "6 categories")
self.assertEqual(listed(7, "leaf", "leaves"), "7 leaves")
def test_unlisted(self):
""" Function unlisted() sanity """
self.assertEqual(unlisted("1, 2 and 3"), ["1", "2", "3"])
self.assertEqual(unlisted("1, 2, 3"), ["1", "2", "3"])
self.assertEqual(unlisted("1 2 3"), ["1", "2", "3"])
def test_sliced(self):
""" Function sliced() sanity """
loaf = list(range(9))
self.assertEqual(list(sliced(loaf, 9)), [loaf])
self.assertEqual(
list(sliced(loaf, 5)), [[0, 1, 2, 3, 4], [5, 6, 7, 8]])
self.assertEqual(
list(sliced(loaf, 3)), [[0, 1, 2], [3, 4, 5], [6, 7, 8]])
def test_get_set_log_level(self):
""" Get & set the logging level """
original = Logging.get()
for level in [log.DEBUG, log.WARN, log.ERROR]:
set_log_level(level)
self.assertEqual(Logging.get(), level)
self.assertEqual(get_log_level(), level)
Logging.set(original)
def test_get_set_cache_level(self):
""" Get & set the caching level """
original = Caching().get()
for level in [CACHE_NONE, CACHE_CHANGES, CACHE_OBJECTS]:
set_cache_level(level)
self.assertEqual(Caching().get(), level)
self.assertEqual(get_cache_level(), level)
Caching().set(original)
def test_get_set_color_mode(self):
""" Get & set the color mode """
original = Coloring().get()
for mode in [COLOR_ON, COLOR_OFF, COLOR_AUTO]:
set_color_mode(mode)
self.assertEqual(Coloring().get(), mode)
self.assertEqual(get_color_mode(), mode)
Coloring().set(original)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Build
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class BuildTests(unittest.TestCase):
def setUp(self):
""" Clear cache, save cache level and initialize test data """
self.product = config.product
self.build = config.build
self.requests = Nitrate._requests
self.cache_level = get_cache_level()
cache.clear()
def tearDown(self):
""" Restore cache level """
set_cache_level(self.cache_level)
def test_fetch_by_id(self):
""" Fetch by id """
build = Build(self.build.id)
self.assertEqual(build.name, self.build.name)
def test_fetch_by_name_and_product(self):
""" Fetch by name and product """
# Named arguments
build = Build(name=self.build.name, product=self.product.name)
self.assertEqual(build.id, self.build.id)
# Backward compatibility
build = Build(build=self.build.name, product=self.product.name)
self.assertEqual(build.id, self.build.id)
def test_invalid_build_id(self):
""" Invalid build id should raise exception """
fun = lambda: Build(-1).name
self.assertRaises(NitrateError, fun)
def test_invalid_build_name(self):
""" Invalid build name should raise exception """
fun = lambda: Build(name="bbbad-bbbuild", product=self.product.name).id
self.assertRaises(NitrateError, fun)
def test_cache_none(self):
""" Cache none """
set_cache_level(CACHE_NONE)
build1 = Build(self.build.id)
self.assertEqual(build1.name, self.build.name)
build2 = Build(self.build.id)
self.assertEqual(build2.name, self.build.name)
self.assertEqual(build1, build2)
self.assertEqual(Nitrate._requests, self.requests + 2)
def test_cache_objects(self):
""" Cache objects """
set_cache_level(CACHE_OBJECTS)
build1 = Build(self.build.id)
self.assertEqual(build1.name, self.build.name)
build2 = Build(self.build.id)
self.assertEqual(build2.name, self.build.name)
self.assertEqual(build1, build2)
self.assertEqual(id(build1), id(build2))
self.assertEqual(Nitrate._requests, self.requests + 1)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Category
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class CategoryTests(unittest.TestCase):
def setUp(self):
""" Clear cache, save cache level and initialize test data """
cache.clear()
self.cache_level = get_cache_level()
self.product = config.product
self.category = config.category
self.requests = Nitrate._requests
def tierDown(self):
""" Restore cache level """
set_cache_level(self.cache_level)
def test_fetch_by_id(self):
""" Fetch by id """
category = Category(self.category.id)
self.assertEqual(category.name, self.category.name)
def test_fetch_by_name_and_product(self):
""" Fetch by name and product """
# Named arguments
category = Category(
name=self.category.name, product=self.product.name)
self.assertEqual(category.id, self.category.id)
# Backward compatibility
category = Category(
category=self.category.name, product=self.product.name)
self.assertEqual(category.id, self.category.id)
def test_cache_objects(self):
""" Cache objects """
set_cache_level(CACHE_OBJECTS)
# The first round (fetch category data from server)
category = Category(self.category.id)
self.assertEqual(category.name, self.category.name)
self.assertEqual(Nitrate._requests, self.requests + 1)
# The second round (there should be no more requests)
category = Category(self.category.id)
self.assertEqual(category.name, self.category.name)
self.assertEqual(Nitrate._requests, self.requests + 1)
def test_cache_none(self):
""" Cache none """
set_cache_level(CACHE_NONE)
# The first round (fetch category data from server)
category = Category(self.category.id)
self.assertEqual(category.name, self.category.name)
self.assertEqual(Nitrate._requests, self.requests + 1)
# The second round (there should be another request)
category = Category(self.category.id)
self.assertEqual(category.name, self.category.name)
self.assertEqual(Nitrate._requests, self.requests + 2)
def test_invalid_category(self):
""" Invalid category should raise exception """
def fun():
Category(category="Bad", product=self.product.name).id
original_log_level = get_log_level()
set_log_level(log.CRITICAL)
self.assertRaises(NitrateError, fun)
set_log_level(original_log_level)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# PlanType
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class PlanTypeTests(unittest.TestCase):
def setUp(self):
""" Clear cache, save cache level and initialize test data """
cache.clear()
self.original_cache_level = get_cache_level()
self.plantype = config.plantype
self.requests = Nitrate._requests
def tierDown(self):
""" Restore cache level """
set_cache_level(self.original_cache_level)
def test_invalid_type(self):
""" Invalid plan type should raise exception """
def fun():
PlanType(name="Bad Plan Type").id
original_log_level = get_log_level()
set_log_level(log.CRITICAL)
self.assertRaises(NitrateError, fun)
set_log_level(original_log_level)
def test_valid_type(self):
""" Valid plan type initialization """
# Initialize by id
plantype = PlanType(self.plantype.id)
self.assertEqual(plantype.name, self.plantype.name)
# Initialize by name (explicit)
plantype = PlanType(name=self.plantype.name)
self.assertEqual(plantype.id, self.plantype.id)
# Initialize by name (autodetection)
plantype = PlanType(self.plantype.name)
self.assertEqual(plantype.id, self.plantype.id)
def test_cache_objects(self):
""" Cache objects """
set_cache_level(CACHE_OBJECTS)
# The first round (fetch plantype data from server)
plantype1 = PlanType(self.plantype.id)
self.assertTrue(isinstance(plantype1.name, six.string_types))
self.assertEqual(Nitrate._requests, self.requests + 1)
# The second round (there should be no more requests)
plantype2 = PlanType(self.plantype.id)
self.assertTrue(isinstance(plantype2.name, six.string_types))
self.assertEqual(Nitrate._requests, self.requests + 1)
# The third round (fetching by plan type name)
plantype3 = PlanType(self.plantype.name)
self.assertTrue(isinstance(plantype3.id, int))
self.assertEqual(Nitrate._requests, self.requests + 1)
# All plan types should point to the same object
self.assertEqual(id(plantype1), id(plantype2))
self.assertEqual(id(plantype1), id(plantype3))
def test_cache_none(self):
""" Cache none """
set_cache_level(CACHE_NONE)
# The first round (fetch plantype data from server)
plantype1 = PlanType(self.plantype.id)
self.assertTrue(isinstance(plantype1.name, six.string_types))
self.assertEqual(Nitrate._requests, self.requests + 1)
# The second round (there should be another request)
plantype2 = PlanType(self.plantype.id)
self.assertTrue(isinstance(plantype2.name, six.string_types))
self.assertEqual(Nitrate._requests, self.requests + 2)
# The third round (fetching by plan type name)
plantype3 = PlanType(self.plantype.name)
self.assertTrue(isinstance(plantype3.id, int))
self.assertEqual(Nitrate._requests, self.requests + 3)
# Plan types should be different objects in memory
self.assertNotEqual(id(plantype1), id(plantype2))
self.assertNotEqual(id(plantype1), id(plantype3))
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Product
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class ProductTests(unittest.TestCase):
def setUp(self):
""" Set up test product from the config """
self.product = config.product
def testGetById(self):
""" Get product by id """
product = Product(self.product.id)
self.assertTrue(isinstance(product, Product), "Check the instance")
self.assertEqual(product.name, self.product.name)
def testGetByName(self):
""" Get product by name """
product = Product(name=self.product.name)
self.assertTrue(isinstance(product, Product), "Check the instance")
self.assertEqual(product.id, self.product.id)
def testSearch(self):
""" Product search """
products = Product.search(name=self.product.name)
self.assertEqual(len(products), 1, "Single product returned")
self.assertEqual(products[0].id, self.product.id)
def testProductCaching(self):
""" Test caching in Product class """
requests = Nitrate._requests
# Turn off caching
set_cache_level(CACHE_NONE)
product = Product(self.product.id)
log.info(product.name)
product = Product(self.product.id)
log.info(product.name)
self.assertEqual(Nitrate._requests, requests + 2)
# Turn on caching
Product._cache = {}
set_cache_level(CACHE_OBJECTS)
product = Product(self.product.id)
log.info(product.name)
product = Product(self.product.id)
log.info(product.name)
self.assertEqual(Nitrate._requests, requests + 3)
def testProductAdvancedCachingID(self):
""" Advanced caching (init by ID) """
requests = Nitrate._requests
Product._cache = {}
# Turn off caching
set_cache_level(CACHE_NONE)
product = Product(self.product.id)
log.info(product.name)
product2 = Product(self.product.name)
log.info(product2.id)
self.assertEqual(Nitrate._requests, requests + 2)
self.assertNotEqual(id(product), id(product2))
# Turn on caching
Product._cache = {}
set_cache_level(CACHE_OBJECTS)
product = Product(self.product.id)
log.info(product.name)
product2 = Product(self.product.name)
log.info(product2.id)
self.assertEqual(Nitrate._requests, requests + 3)
self.assertEqual(id(product), id(product2))
def testProductAdvancedCachingName(self):
""" Advanced caching (init by name) """
requests = Nitrate._requests
Product._cache = {}
# Turn off caching
set_cache_level(CACHE_NONE)
product = Product(self.product.name)
log.info(product.id)
product2 = Product(self.product.id)
log.info(product2.name)
self.assertEqual(Nitrate._requests, requests + 2)
self.assertNotEqual(id(product), id(product2))
# Turn on caching
Product._cache = {}
set_cache_level(CACHE_OBJECTS)
product = Product(self.product.name)
log.info(product.id)
product2 = Product(self.product.id)
log.info(product2.name)
self.assertEqual(Nitrate._requests, requests + 3)
self.assertEqual(id(product), id(product2))
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# User
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class UserTests(unittest.TestCase):
def setUp(self):
""" Clear cache, save cache level and initialize test data """
cache.clear()
self.original_cache_level = get_cache_level()
self.user = config.user
self.requests = Nitrate._requests
def tierDown(self):
""" Restore cache level """
set_cache_level(self.original_cache_level)
def test_no_name(self):
""" User with no name set in preferences """
user = User()
user._name = None
self.assertEqual(str(user), u"No Name")
def test_current_user(self):
""" Current user available & sane """
user = User()
for data in [user.login, user.email, user.name]:
self.assertTrue(isinstance(data, six.string_types))
self.assertTrue(isinstance(user.id, int))
def test_cache_none(self):
""" Cache none """
set_cache_level(CACHE_NONE)
# Initialize the same user by id, login and email
user1 = User(self.user.id)
log.info(user1.name)
user2 = User(self.user.login)
log.info(user2.name)
user3 = User(self.user.email)
log.info(user3.name)
# Three requests to the server should be performed
self.assertEqual(Nitrate._requests, self.requests + 3)
# User data should be the same
for user in [user1, user2, user3]:
self.assertEqual(user.id, self.user.id)
self.assertEqual(user.login, self.user.login)
self.assertEqual(user.email, self.user.email)
# Users should be different objects in memory
self.assertNotEqual(id(user1), id(user2))
self.assertNotEqual(id(user1), id(user3))
def test_cache_objects(self):
""" Cache objects """
set_cache_level(CACHE_OBJECTS)
# Initialize the same user by id, login and email
user1 = User(self.user.id)
log.info(user1.name)
user2 = User(self.user.login)
log.info(user2.name)
user3 = User(self.user.email)
log.info(user3.name)
# Single request to the server should be performed
self.assertEqual(Nitrate._requests, self.requests + 1)
# All users objects should be identical
self.assertEqual(id(user1), id(user2))
self.assertEqual(id(user1), id(user3))
def test_initialization_by_id(self):
""" Initializate by id """
user = User(self.user.id)
self.assertEqual(user.id, self.user.id)
self.assertEqual(user.login, self.user.login)
self.assertEqual(user.email, self.user.email)
def test_initialization_by_login(self):
""" Initializate by login """
# Check both explicit parameter and autodetection
user1 = User(login=self.user.login)
user2 = User(self.user.login)
for user in [user1, user2]:
self.assertEqual(user.id, self.user.id)
self.assertEqual(user.login, self.user.login)
self.assertEqual(user.email, self.user.email)
def test_initialization_by_email(self):
""" Initializate by email """
# Check both explicit parameter and autodetection
user1 = User(email=self.user.email)
user2 = User(self.user.email)
for user in [user1, user2]:
self.assertEqual(user.id, self.user.id)
self.assertEqual(user.login, self.user.login)
self.assertEqual(user.email, self.user.email)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Version
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class VersionTests(unittest.TestCase):
def setUp(self):
""" Set up version from the config """
cache.clear()
self.cache_level = get_cache_level()
self.version = config.version
self.product = config.product
self.requests = Nitrate._requests
def tierDown(self):
""" Restore cache level """
set_cache_level(self.cache_level)
def test_fetch_by_id(self):
""" Fetch by id """
version = Version(self.version.id)
self.assertEqual(version.name, self.version.name)
def test_fetch_by_name_and_product(self):
""" Fetch by name and product """
# Named arguments
version = Version(
name=self.version.name, product=self.product.name)
self.assertEqual(version.id, self.version.id)
# Backward compatibility
version = Version(
version=self.version.name, product=self.product.name)
self.assertEqual(version.id, self.version.id)
def test_cache_none(self):
""" Cache none """
set_cache_level(CACHE_NONE)
version = Version(self.version.id)
self.assertEqual(version.name, self.version.name)
version = Version(self.version.id)
self.assertEqual(version.name, self.version.name)
# Fetches the version twice ---> 2 requests
self.assertEqual(Nitrate._requests, self.requests + 2)
def test_cache_objects(self):
""" Cache objects """
set_cache_level(CACHE_OBJECTS)
version = Version(self.version.id)
self.assertEqual(version.name, self.version.name)
version = Version(self.version.id)
self.assertEqual(version.name, self.version.name)
# Should fetch version just once ---> 1 request
self.assertEqual(Nitrate._requests, self.requests + 1)
# def test_cache_persistent(self):
# """ Cache persistent """
# set_cache_level(CACHE_PERSISTENT)
# # Fetch the version (populate the cache)
# version = Version(self.version.id)
# self.assertEqual(version.name, self.version.name)
# # Save, clear & load cache
# cache.save()
# cache.clear()
# cache.load()
# requests = Nitrate._requests
# # Fetch once again ---> no additional request
# version = Version(self.version.id)
# self.assertEqual(version.name, self.version.name)
# self.assertEqual(Nitrate._requests, requests)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Component
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class ComponentTests(unittest.TestCase):
def setUp(self):
""" Set up component from the config """
self.component = config.component
def testFetchById(self):
""" Fetch component by id """
component = Component(self.component.id)
self.assertTrue(isinstance(component, Component))
self.assertEqual(component.name, self.component.name)
self.assertEqual(component.product.name, self.component.product)
def testFetchByName(self):
""" Fetch component by name and product """
component = Component(
name=self.component.name, product=self.component.product)
self.assertTrue(isinstance(component, Component))
self.assertEqual(component.id, self.component.id)
def testSearchByName(self):
""" Search for component by name """
components = Component.search(name=self.component.name)
self.assertTrue(components[0].name == self.component.name)
def testCachingOn(self):
""" Component caching on """
# Make sure the cache is empty
Component._cache = {}
# Enable cache, remember current number of requests
original = get_cache_level()
set_cache_level(CACHE_OBJECTS)
requests = Nitrate._requests
# The first round (fetch component data from server)
component = Component(self.component.id)
self.assertTrue(isinstance(component.name, six.string_types))
self.assertEqual(Nitrate._requests, requests + 1)
# The second round (there should be no more requests)
component = Component(self.component.id)
self.assertTrue(isinstance(component.name, six.string_types))
self.assertEqual(Nitrate._requests, requests + 1)
# Restore cache level
set_cache_level(original)
def testCachingOff(self):
""" Component caching off """
# Enable cache, remember current number of requests
original = get_cache_level()
set_cache_level(CACHE_NONE)
requests = Nitrate._requests
# The first round (fetch component data from server)
component = Component(self.component.id)
self.assertTrue(isinstance(component.name, six.string_types))
self.assertEqual(Nitrate._requests, requests + 1)
del component
# The second round (there should be another request)
component = Component(self.component.id)
self.assertTrue(isinstance(component.name, six.string_types))
self.assertEqual(Nitrate._requests, requests + 2)
# Restore cache level
set_cache_level(original)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Tag
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class TagTests(unittest.TestCase):
def setUp(self):
""" Set up component from the config """
self.tag = config.tag
def test_fetch_by_id(self):
""" Fetch component by id """
tag = Tag(self.tag.id)
self.assertTrue(isinstance(tag, Tag))
self.assertEqual(tag.name, self.tag.name)
def test_fetch_by_name(self):
""" Fetch tag by name """
for tag in [Tag(name=self.tag.name), Tag(self.tag.name)]:
self.assertTrue(isinstance(tag, Tag))
self.assertEqual(tag.id, self.tag.id)
def test_caching(self):
""" Tag caching """
# First fetch
cache.clear()
tag = Tag(self.tag.id)
self.assertEqual(tag.name, self.tag.name)
# Object caching
if get_cache_level() < CACHE_OBJECTS:
return
requests = Nitrate._requests
tag = Tag(self.tag.id)
self.assertEqual(tag.name, self.tag.name)
self.assertEqual(requests, Nitrate._requests)
# Persistent caching
if get_cache_level() < CACHE_PERSISTENT:
return
cache.save()
cache.clear()
cache.load()
tag = Tag(self.tag.id)
self.assertEqual(tag.name, self.tag.name)
self.assertEqual(requests, Nitrate._requests)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# TestPlan
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class TestPlanTests(unittest.TestCase):
def setUp(self):
""" Set up test plan from the config """
self.testplan = config.testplan
self.requests = Nitrate._requests
self.cache_level = get_cache_level()
cache.clear()
def tierDown(self):
""" Restore cache level """
set_cache_level(self.cache_level)
def test_create_invalid(self):
""" Create a new test plan (missing required parameters) """
self.assertRaises(NitrateError, TestPlan, name="Test plan")
def test_create_valid(self):
""" Create a new test plan (valid) """
testplan = TestPlan(
name="Test plan",
type=self.testplan.type,
product=self.testplan.product,
version=self.testplan.version)
self.assertTrue(isinstance(testplan, TestPlan))
self.assertEqual(testplan.name, "Test plan")
def test_fetch_nonexistent(self):
""" Fetch non-existent test plan """
with self.assertRaises(NitrateError):
TestPlan(-1).name
def test_get_by_id(self):
""" Fetch an existing test plan by id """
testplan = TestPlan(self.testplan.id)
self.assertTrue(isinstance(testplan, TestPlan))
self.assertEqual(testplan.name, self.testplan.name)
self.assertEqual(testplan.type.name, self.testplan.type)
self.assertEqual(testplan.product.name, self.testplan.product)
self.assertEqual(testplan.version.name, self.testplan.version)
self.assertEqual(testplan.owner.login, self.testplan.owner)
def test_plan_status(self):
""" Test read/write access to the test plan status """
# Prepare original and negated status
original = PlanStatus(self.testplan.status)
negated = PlanStatus(not original.id)
# Test original value
testplan = TestPlan(self.testplan.id)
self.assertEqual(testplan.status, original)
testplan.status = negated
testplan.update()
del testplan
# Test negated value
testplan = TestPlan(self.testplan.id)
self.assertEqual(testplan.status, negated)
testplan.status = original
testplan.update()
del testplan
# Back to the original value
testplan = TestPlan(self.testplan.id)
self.assertEqual(testplan.status, original)
def test_cache_none(self):
""" Cache none """
# Fetch test plan twice ---> two requests
set_cache_level(CACHE_NONE)
testplan = TestPlan(self.testplan.id)
self.assertEqual(testplan.name, self.testplan.name)
testplan = TestPlan(self.testplan.id)
self.assertEqual(testplan.name, self.testplan.name)
self.assertEqual(Nitrate._requests, self.requests + 2)
def test_cache_objects(self):
""" Cache objects """
# Fetch test plan twice ---> just one request
set_cache_level(CACHE_OBJECTS)
testplan = TestPlan(self.testplan.id)
self.assertEqual(testplan.name, self.testplan.name)
testplan = TestPlan(self.testplan.id)
self.assertEqual(testplan.name, self.testplan.name)
self.assertEqual(Nitrate._requests, self.requests + 1)
# def test_cache_persistent(self):
# """ Cache persistent """
# set_cache_level(CACHE_PERSISTENT)
# # Fetch the test plan (populate the cache)
# testplan = TestPlan(self.testplan.id)
# log.debug(testplan.name)
# # Save, clear & load cache
# cache.save()
# cache.clear()
# cache.load()
# requests = Nitrate._requests
# # Fetch once again ---> no additional request
# testplan = TestPlan(self.testplan.id)
# self.assertEqual(testplan.name, self.testplan.name)
# self.assertEqual(Nitrate._requests, requests)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# TestRun
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class TestRunTests(unittest.TestCase):
def setUp(self):
""" Set up test plan from the config """
self.testplan = config.testplan
self.testcase = config.testcase
self.testrun = config.testrun
def testCreateInvalid(self):
""" Create a new test run (missing required parameters) """
self.assertRaises(NitrateError, TestRun, summary="Test run")
def testCreateValid(self):
""" Create a new test run (valid) """
testrun = TestRun(summary="Test run", testplan=self.testplan.id)
self.assertTrue(isinstance(testrun, TestRun))
self.assertEqual(testrun.summary, "Test run")
def testCreateOptionalFields(self):
""" Create a new test run, including optional fields """
testrun = TestRun(
summary="Test run", testplan=self.testplan.id, errata=1234)
self.assertTrue(isinstance(testrun, TestRun))
self.assertEqual(testrun.summary, "Test run")
self.assertEqual(testrun.errata, 1234)
def test_fetch_nonexistent(self):
""" Fetch non-existent test run """
with self.assertRaises(NitrateError):
TestRun(-1).notes
def testGetById(self):
""" Fetch an existing test run by id """
testrun = TestRun(self.testrun.id)
self.assertTrue(isinstance(testrun, TestRun))
self.assertEqual(testrun.summary, self.testrun.summary)
self.assertEqual(str(testrun.started), self.testrun.started)
self.assertEqual(str(testrun.finished), self.testrun.finished)
def testErrata(self):
""" Set, get and change errata """
for errata in [111, 222, 333]:
# Update the errata field, push to the server
testrun = TestRun(self.testrun.id)
testrun.errata = errata
testrun.update()
# Fetch the test run again, check for correct errata
testrun = TestRun(self.testrun.id)
self.assertEqual(testrun.errata, errata)
def testDisabledCasesOmitted(self):
""" Disabled test cases should be omitted """
# Prepare disabled test case
testcase = TestCase(self.testcase.id)
original = testcase.status
testcase.status = CaseStatus("DISABLED")
testcase.update()
# Create the test run, make sure the test case is not there
testrun = TestRun(testplan=self.testplan.id)
self.assertTrue(testcase.id not in
[caserun.testcase.id for caserun in testrun])
# Restore the original status
testcase.status = original
testcase.update()
def test_include_only_selected_cases(self):
""" Include only selected test cases in the new run """
testcase = TestCase(self.testcase.id)
testplan = TestPlan(self.testplan.id)
# No test case should be linked
testrun = TestRun(testplan=testplan, testcases=[])
self.assertTrue(testcase.id not in
[caserun.testcase.id for caserun in testrun])
# Select test case by test case object
testrun = TestRun(testplan=testplan, testcases=[testcase])
self.assertTrue(testcase.id in
[caserun.testcase.id for caserun in testrun])
# Select test case by id
testrun = TestRun(testplan=testplan, testcases=[testcase.id])
self.assertTrue(testcase.id in
[caserun.testcase.id for caserun in testrun])
def testTestRunCaching(self):
""" Test caching in TestRun class """
TestRun._cache = {}
requests = Nitrate._requests
# Turn off caching
set_cache_level(CACHE_NONE)
testrun = TestRun(self.testrun.id)
log.info(testrun.summary)
testrun = TestRun(self.testrun.id)
log.info(testrun.summary)
self.assertEqual(Nitrate._requests, requests + 2)
# Turn on caching
TestRun._cache = {}
set_cache_level(CACHE_OBJECTS)
testrun = TestRun(self.testrun.id)
log.info(testrun.summary)
testrun = TestRun(self.testrun.id)
log.info(testrun.summary)
self.assertEqual(Nitrate._requests, requests + 3)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# TestCase
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class TestCaseTests(unittest.TestCase):
def setUp(self):
""" Set up test case from the config """
self.testcase = config.testcase
self.performance = config.performance
def testCreateInvalid(self):
""" Create a new test case (missing required parameters) """
self.assertRaises(
NitrateError, TestCase, summary="Test case summary")
def testCreateValid(self):
""" Create a new test case (valid) """
case = TestCase(summary="Test case summary",
product="Red Hat Enterprise Linux 6", category="Sanity")
self.assertTrue(
isinstance(case, TestCase), "Check created instance")
self.assertEqual(case.summary, "Test case summary")
self.assertEqual(case.priority, Priority("P3"))
self.assertEqual(str(case.category), "Sanity")