This repository has been archived by the owner on Feb 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_jeni.py
1188 lines (915 loc) · 37 KB
/
test_jeni.py
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
from collections import OrderedDict as odict
from decimal import Decimal
from fractions import Fraction
import sys
import unittest
import jeni
class BasicInjector(jeni.Injector):
pass
@BasicInjector.provider('hello')
class HelloProvider(jeni.Provider):
def get(self, name=None):
if name is None:
name = 'world'
return 'Hello, {}!'.format(name)
@BasicInjector.factory('eggs')
def eggs():
return 'eggs!'
@BasicInjector.factory('echo')
def echo(name=None):
return name
@BasicInjector.factory('space')
def space():
return {}
BasicInjector.value('zero', 0)
@BasicInjector.provider('answer')
def answer():
yield 42
@BasicInjector.provider('spam', name=True)
def spam():
count_str = yield 'spam'
while True:
count_str = yield 'spam' * int(count_str)
class WithOrderedSpace(jeni.Injector):
pass
@WithOrderedSpace.factory('space')
def ospace():
return odict()
class SubInjector(BasicInjector):
pass
@SubInjector.provider('answer')
def sub_answer():
yield 'No one knows.'
class BoringInjector(jeni.Injector):
pass
@BoringInjector.factory('hello')
@BoringInjector.factory('eggs')
@BoringInjector.factory('answer')
@BoringInjector.factory('spam')
def be_boring(name=None):
if name is None:
name = 'this injector'
return '{} is boring'.format(name)
class BasicInjectorTestCase(unittest.TestCase):
def setUp(self):
self.injector = BasicInjector()
def test_provider_class(self):
self.assertEqual('Hello, world!', self.injector.get('hello'))
self.assertEqual('Hello, world!', self.injector.get('hello'))
self.assertEqual('Hello, thing!', self.injector.get('hello:thing'))
self.assertEqual('Hello, thing!', self.injector.get('hello:thing'))
def test_factory(self):
self.assertEqual('eggs!', self.injector.get('eggs'))
self.assertEqual('eggs!', self.injector.get('eggs'))
# This factory does not accept name.
self.assertRaises(TypeError, self.injector.get, 'eggs:thing')
def test_factory_identity(self):
space1 = self.injector.get('space')
space2 = self.injector.get('space')
self.assertIs(space1, space2)
def test_factory_with_name(self):
self.assertEqual(None, self.injector.get('echo'))
self.assertEqual('foo', self.injector.get('echo:foo'))
def test_value(self):
self.assertEqual(0, self.injector.get('zero'))
self.assertEqual(0, self.injector.get('zero'))
# This does not accept name.
self.assertRaises(TypeError, self.injector.get, 'zero:thing')
def test_generator(self):
self.assertEqual(42, self.injector.get('answer'))
self.assertEqual(42, self.injector.get('answer'))
# This generator does not accept name.
self.assertRaises(TypeError, self.injector.get, 'answer:thing')
def test_generator_with_name(self):
self.assertEqual('spam', self.injector.get('spam'))
self.assertEqual('spam', self.injector.get('spam'))
self.assertEqual('spamspam', self.injector.get('spam:2'))
self.assertEqual('spamspamspam', self.injector.get('spam:3'))
self.assertEqual('spamspamspamspam', self.injector.get('spam:4'))
self.assertEqual('spam', self.injector.get('spam'))
self.assertRaises(ValueError, self.injector.get, 'spam:forty')
self.assertEqual('spam', self.injector.get('spam'))
def test_not_registered(self):
self.assertRaises(LookupError, self.injector.get, 'nothing')
def test_provider_class_registration(self):
class SubInjector(BasicInjector):
pass
SubInjector.provider('hello2', HelloProvider)
injector = SubInjector()
self.assertEqual('Hello, world!', injector.get('hello2'))
self.assertEqual('Hello, world!', injector.get('hello'))
def test_factory_registration(self):
class SubInjector(BasicInjector):
pass
SubInjector.factory('eggs2', eggs)
injector = SubInjector()
self.assertEqual('eggs!', injector.get('eggs2'))
self.assertEqual('eggs!', injector.get('eggs'))
class InjectSelfTestCase(unittest.TestCase):
def test_provide_self_default(self):
self.injector = jeni.Injector()
self.assertEqual(self.injector, self.injector.get('injector'))
def test_provide_self_true(self):
self.injector = jeni.Injector(provide_self=True)
self.assertEqual(self.injector, self.injector.get('injector'))
def test_provide_self_false(self):
self.injector = jeni.Injector(provide_self=False)
self.assertRaises(LookupError, self.injector.get, 'injector')
def test_provide_self_is_self(self):
injector1 = jeni.Injector(provide_self=True)
injector2 = jeni.Injector(provide_self=True)
self.assertEqual(injector1, injector1.get('injector'))
self.assertEqual(injector2, injector2.get('injector'))
class SubInjectorTestCase(BasicInjectorTestCase):
def setUp(self):
self.injector = SubInjector()
def test_generator(self):
self.assertEqual('No one knows.', self.injector.get('answer'))
self.assertEqual('No one knows.', self.injector.get('answer'))
# This generator does not accept name.
self.assertRaises(TypeError, self.injector.get, 'answer:thing')
class InjectorSubTestCase(BasicInjectorTestCase):
def setUp(self):
self.injector = BasicInjector()
def test_subinjector(self):
subinjector = self.injector.sub()
self.assertIsInstance(subinjector, jeni.Injector)
def test_subinjector_inheritance(self):
injector_class = type(self.injector)
subinjector_class = type(self.injector.sub())
self.assertTrue(issubclass(subinjector_class, injector_class))
def test_subinjector_isolation(self):
space = self.injector.get('space')
subinjector = self.injector.sub()
sub_space = subinjector.get('space')
self.assertIsNot(space, sub_space)
def test_subinjector_local_values(self):
subinjector = self.injector.sub(zero=0.0)
self.assertIsInstance(subinjector.get('zero'), float)
local_values = dict(zero=0.0)
subinjector = self.injector.sub(local_values)
self.assertIsInstance(subinjector.get('zero'), float)
more_local_values = dict(zero=Fraction(0, 1))
subinjector = self.injector.sub(local_values, more_local_values)
self.assertIsInstance(subinjector.get('zero'), Fraction)
subinjector = self.injector.sub(
local_values, more_local_values, zero=Decimal('0'))
self.assertIsInstance(subinjector.get('zero'), Decimal)
def test_subinjector_mixins(self):
subinjector = self.injector.sub(WithOrderedSpace)
injector_class = type(self.injector)
subinjector_class = type(self.injector.sub())
self.assertTrue(issubclass(subinjector_class, injector_class))
self.assertIsInstance(subinjector, WithOrderedSpace)
self.assertIsInstance(subinjector.get('space'), odict)
def test_subinjector_provides_injector(self):
subinjector = self.injector.sub()
self.assertEqual(subinjector, subinjector.get('injector'))
class BasicInjectorAnnotationTestCase(unittest.TestCase):
def setUp(self):
self.injector = BasicInjector()
@jeni.annotate('hello', 'hello:x', 'eggs', 'answer', 'spam', 'spam:2')
def fn(hello, hello_x, eggs, answer, spam, spam2):
self.assertEqual('Hello, world!', hello)
self.assertEqual('Hello, x!', hello_x)
self.assertEqual('eggs!', eggs)
self.assertEqual(42, answer)
self.assertEqual('spam', spam)
self.assertEqual('spamspam', spam2)
return 0
self.fn = fn
def test_apply(self):
self.assertEqual(0, self.injector.apply(self.fn))
def test_partial(self):
fn = self.injector.partial(self.fn)
self.assertEqual(0, fn())
self.assertEqual(0, fn())
def test_eager_partial(self):
fn = self.injector.eager_partial(self.fn)
self.assertEqual(0, fn())
self.assertEqual(0, fn())
def test_not_registered(self):
@jeni.annotate('hello', 'nothing')
def fn(hello, nothing):
raise RuntimeError('UnsetError should raise before this on apply.')
self.assertRaises(RuntimeError, fn, 'hello', None)
self.assertRaises(LookupError, self.injector.apply, fn)
def test_no_annotation(self):
def fn():
"unused"
self.assertRaises(AttributeError, self.injector.apply, fn)
def test_annotate_without_annotations(self):
# This instrumentation works in both Python 2 & Python 3.
def fn(hello):
"unused"
fn.__annotations__ = {'hello': 'hello:world'}
jeni.annotate(fn)
self.assertTrue(jeni.annotate.has_annotations(fn))
def test_annotate_fn_name_keyword(self):
# Test that annotation of 'fn' is not accidentally reserved by jeni.
def foo(fn=None):
"unused"
decorator = jeni.annotate(fn='function')
try:
decorator(foo)
raise TypeError('code coverage support')
except TypeError:
_, err, _ = sys.exc_info()
msg = "Annotation cannot support argument named 'fn'."
if 'coverage' not in str(err): self.fail(msg)
class SubInjectorAnnotationTestCase(BasicInjectorAnnotationTestCase):
def setUp(self):
self.injector = SubInjector()
@jeni.annotate('hello', 'hello:x', 'eggs', 'answer', 'spam', 'spam:2')
def fn(hello, hello_x, eggs, answer, spam, spam2):
self.assertEqual('Hello, world!', hello)
self.assertEqual('Hello, x!', hello_x)
self.assertEqual('eggs!', eggs)
self.assertEqual('No one knows.', answer)
self.assertEqual('spam', spam)
self.assertEqual('spamspam', spam2)
return 0
self.fn = fn
class BoringAnnotationTestCase(BasicInjectorAnnotationTestCase):
def setUp(self):
self.injector = BoringInjector()
@jeni.annotate('hello', 'hello:x', 'eggs', 'answer', 'spam', 'spam:2')
def fn(hello, hello_x, eggs, answer, spam, spam2):
self.assertEqual('this injector is boring', hello)
self.assertEqual('x is boring', hello_x)
self.assertEqual('this injector is boring', eggs)
self.assertEqual('this injector is boring', answer)
self.assertEqual('this injector is boring', spam)
self.assertEqual('2 is boring', spam2)
return 0
self.fn = fn
class ApplyScenariosTestCase(unittest.TestCase):
def setUp(self):
self.injector = BasicInjector()
@jeni.annotate('hello')
def fn_annotated(hello, *a, **kw):
return hello, a, kw
self.fn_annotated = fn_annotated
def fn_not_annotated(*a, **kw):
return a, kw
self.fn_not_annotated = fn_not_annotated
@jeni.annotate(a='doesnotexist1', b='doesnotexist2', c='hello')
def fn_with_lookup_error(a, b, c=None):
return a, b, c
self.fn_with_lookup_error = fn_with_lookup_error
def test_apply(self):
self.assertEqual(
('Hello, world!', (), {}),
self.injector.apply(self.fn_annotated))
self.assertRaises(
AttributeError,
self.injector.apply, self.fn_not_annotated)
def test_additional_arguments_apply(self):
self.assertEqual(
('Hello, world!', ('a', 'b'), {'letter': 'c'}),
self.injector.apply(self.fn_annotated, 'a', 'b', letter='c'))
self.assertRaises(
AttributeError,
self.injector.apply, self.fn_not_annotated, 'a', 'b', letter='c')
def test_apply_regardless(self):
self.assertEqual(
('Hello, world!', (), {}),
self.injector.apply_regardless(self.fn_annotated))
self.assertEqual(
((), {}),
self.injector.apply_regardless(self.fn_not_annotated))
def test_additional_arguments_apply_regardless(self):
self.assertEqual(
('Hello, world!', ('a', 'b'), {'letter': 'c'}),
self.injector.apply_regardless(
self.fn_annotated, 'a', 'b', letter='c'))
self.assertEqual(
(('a', 'b'), {'letter': 'c'}),
self.injector.apply_regardless(
self.fn_not_annotated, 'a', 'b', letter='c'))
def test_partial(self):
self.assertEqual(
('Hello, world!', (), {}),
self.injector.partial(self.fn_annotated)())
self.assertRaises(
AttributeError,
self.injector.partial, self.fn_not_annotated)
def test_eager_partial(self):
self.assertEqual(
('Hello, world!', (), {}),
self.injector.eager_partial(self.fn_annotated)())
self.assertRaises(
AttributeError,
self.injector.eager_partial, self.fn_not_annotated)
def test_additional_arguments_partial(self):
self.assertEqual(
('Hello, world!', ('a', 'b'), {'letter': 'c'}),
self.injector.partial(self.fn_annotated, 'a', 'b', letter='c')())
self.assertRaises(
AttributeError,
self.injector.partial, self.fn_not_annotated, 'a', 'b', letter='c')
def test_partial_as_maybe(self):
self.assertEqual(
('a', 'b', 'Hello, world!'),
self.injector.partial(self.fn_with_lookup_error, 'a', 'b')())
self.assertEqual(
('a', 'b', 'Hello, world!'),
self.injector.partial(self.fn_with_lookup_error)('a', 'b'))
def test_eager_partial_as_maybe(self):
self.assertEqual(
('a', 'b', 'Hello, world!'),
self.injector.eager_partial(self.fn_with_lookup_error, 'a', 'b')())
self.assertEqual(
('a', 'b', 'Hello, world!'),
self.injector.eager_partial(self.fn_with_lookup_error)('a', 'b'))
def test_partial_regardless(self):
self.assertEqual(
('Hello, world!', (), {}),
self.injector.partial_regardless(self.fn_annotated)())
self.assertEqual(
((), {}),
self.injector.partial_regardless(self.fn_not_annotated)())
def test_eager_partial_regardless(self):
self.assertEqual(
('Hello, world!', (), {}),
self.injector.eager_partial_regardless(self.fn_annotated)())
self.assertEqual(
((), {}),
self.injector.eager_partial_regardless(self.fn_not_annotated)())
def test_additional_arguments_partial_regardless(self):
self.assertEqual(
('Hello, world!', ('a', 'b'), {'letter': 'c'}),
self.injector.partial_regardless(
self.fn_annotated, 'a', 'b', letter='c')())
self.assertEqual(
(('a', 'b'), {'letter': 'c'}),
self.injector.partial_regardless(
self.fn_not_annotated, 'a', 'b', letter='c')())
@jeni.annotate('spam', 'eggs')
def spam_eggs(spam, eggs, name=None):
if name is not None:
return ' '.join((spam, eggs, name))
return ' '.join((spam, eggs))
class AnnotatedInitProvider(jeni.Provider):
@jeni.annotate('spam:4')
def __init__(self, spam):
self.foo = None
self.spam = spam
def get(self):
return self
def close(self):
self.foo = self.spam
class AnnotatedProviderTestCase(unittest.TestCase):
def test_spam_eggs(self):
class Injector(BasicInjector):
pass
Injector.factory('dish', spam_eggs)
injector = Injector()
self.assertEqual(('spam eggs!'), injector.get('dish'))
self.assertEqual(('spam eggs! eel'), injector.get('dish:eel'))
def test_annotated_generator(self):
class Injector(BasicInjector):
pass
@Injector.provider('spammish')
@jeni.annotate('spam')
def generator(spam):
yield spam
injector = Injector()
self.assertEqual('spam', injector.get('spammish'))
def test_annotated_init(self):
class Injector(BasicInjector):
pass
Injector.provider('annotated_init', AnnotatedInitProvider)
injector = Injector()
provider = injector.get('annotated_init')
self.assertEqual(None, provider.foo)
injector.close()
self.assertEqual('spamspamspamspam', provider.foo)
class TestCycles(unittest.TestCase):
def setUp(self):
class Injector(jeni.Injector): pass
@Injector.factory('one')
@jeni.annotate('three')
def three_minus_2(three):
return not three-2
@Injector.factory('two')
@jeni.annotate('one')
def one_times_2(one):
return not one*2
@Injector.factory('three')
@jeni.annotate('two')
def two_plus_1(two):
return not two+1
self.injector = Injector()
# Call functions to simplify coverage.
three_minus_2(3)
one_times_2(1)
two_plus_1(2)
def test_cycles(self):
with self.assertRaises(jeni.DependencyCycleError) as raises:
self.injector.get('one')
self.assertEqual(raises.exception.notes, (
('one', None),
('three', None),
('two', None),
('one', None)))
class WrapsTestCase(unittest.TestCase):
def setUp(self):
@jeni.annotate('spam')
def foo(spam):
"Foo the spam."
return spam
self.foo = foo
@jeni.wraps(foo)
def wrapper(spam):
return ('spam', spam)
self.wrapper = wrapper
self.injector = BasicInjector()
def test_has_annotations(self):
self.assertTrue(jeni.annotate.has_annotations(self.wrapper))
def test_name_and_doc(self):
self.assertEqual('foo', self.wrapper.__name__)
self.assertEqual('Foo the spam.', self.wrapper.__doc__)
def test_injection(self):
self.assertEqual('spam', self.injector.apply(self.foo))
self.assertEqual(('spam', 'spam'), self.injector.apply(self.wrapper))
class MaybeTestCase(unittest.TestCase):
def setUp(self):
self.note = jeni.maybe('the_real_note')
def test_maybe(self):
self.assertEqual((jeni.MAYBE, 'the_real_note'), self.note)
class PartialNoteTestCase(unittest.TestCase):
def setUp(self):
self.fn = lambda a, b, c: None
self.note = jeni.partial(self.fn)
self.note_with_args = jeni.partial(self.fn, 'a', 'bee', c='cee')
def test_partial(self):
# (PARTIAL, (function, args, kwargs_items_tuple))
self.assertEqual((jeni.PARTIAL, (self.fn, (), ())), self.note)
def test_partial_with_args(self):
self.assertEqual(
(jeni.PARTIAL, (self.fn, ('a', 'bee'), (('c', 'cee'),))),
self.note_with_args)
class EagerPartialNoteTestCase(unittest.TestCase):
def setUp(self):
self.fn = lambda a, b, c: None
self.note = jeni.eager_partial(self.fn)
self.note_with_args = jeni.eager_partial(self.fn, 'a', 'bee', c='cee')
def test_partial(self):
# (EAGER_PARTIAL, (function, args, kwargs_items_tuple))
self.assertEqual((jeni.EAGER_PARTIAL, (self.fn, (), ())), self.note)
def test_partial_with_args(self):
self.assertEqual(
(jeni.EAGER_PARTIAL, (self.fn, ('a', 'bee'), (('c', 'cee'),))),
self.note_with_args)
@BasicInjector.factory('error')
def error():
raise jeni.UnsetError()
@jeni.annotate('error')
def unset_arg(unused):
raise RuntimeError('UnsetError should raise before this on apply.')
@jeni.annotate('hello', unused='error')
def unset_kwarg(hello, unused=None):
raise RuntimeError('UnsetError should raise before this on apply.')
@jeni.annotate('hello', unused=jeni.annotate.maybe('error'))
def unset_maybe_kwarg(hello, unused=None):
return unused
@jeni.annotate('hello', unused=jeni.annotate.maybe('notprovided'))
def notprovided_maybe_kwarg(hello, unused=None):
return unused
class UnsetArgumentTestCase(unittest.TestCase):
def setUp(self):
self.injector = BasicInjector()
def test_unset_arg(self):
self.assertRaises(RuntimeError, unset_arg, None)
self.assertRaises(jeni.UnsetError, self.injector.apply, unset_arg)
def test_unset_kwarg(self):
self.assertRaises(RuntimeError, unset_kwarg, None)
self.assertRaises(jeni.UnsetError, self.injector.apply, unset_kwarg)
def test_unset_maybe_kwarg(self):
self.assertIs(None, self.injector.apply(unset_maybe_kwarg))
def test_notprovided_maybe_kwarg(self):
self.assertIs(None, self.injector.apply(notprovided_maybe_kwarg))
class UnsetInjector(jeni.Injector):
pass
@UnsetInjector.factory('unset_msg')
def unset_msg_factory():
raise jeni.UnsetError('exception message')
@UnsetInjector.factory('unset_no_msg')
def unset_no_msg_factory():
raise jeni.UnsetError()
class UnsetErrorMessageTestCase(unittest.TestCase):
def setUp(self):
self.injector = UnsetInjector()
def get_error(self, note):
err = None
try:
self.injector.get(note)
except jeni.UnsetError:
_, err, _ = sys.exc_info()
self.assertIsNotNone(err)
return err
def test_unset_msg(self):
err = self.get_error('unset_msg')
self.assertEqual(str(err), "exception message: 'unset_msg'")
self.assertEqual(err.note, 'unset_msg')
def test_unset_no_msg(self):
err = self.get_error('unset_no_msg')
self.assertEqual(str(err), "'unset_no_msg'")
self.assertEqual(err.note, 'unset_no_msg')
def hello_simple():
return 'wat'
@jeni.annotate('hello:partial')
def hello_partial(hello):
return hello
@jeni.annotate('hello:again', jeni.annotate.partial(hello_partial))
def hello_again_partial(hello, fn, **keywords):
return hello, fn(), keywords
@jeni.annotate('hello:again', jeni.annotate.partial_regardless(hello_partial))
def hello_again_partial_regardless_annotated(hello, fn, **keywords):
return hello, fn(), keywords
@jeni.annotate('hello:again', jeni.annotate.partial_regardless(hello_simple))
def hello_again_partial_regardless_unannotated(hello, fn, **keywords):
return hello, fn(), keywords
@jeni.annotate('hello:again', jeni.annotate.eager_partial(hello_partial))
def hello_again_eager_partial(hello, fn, **keywords):
return hello, fn(), keywords
@jeni.annotate('hello:again', jeni.annotate.eager_partial_regardless(hello_partial))
def hello_again_eager_partial_regardless_annotated(hello, fn, **keywords):
return hello, fn(), keywords
@jeni.annotate('hello:again', jeni.annotate.eager_partial_regardless(hello_simple))
def hello_again_eager_partial_regardless_unannotated(hello, fn, **keywords):
return hello, fn(), keywords
class InjectPartialTestCase(unittest.TestCase):
def setUp(self):
self.injector = BasicInjector()
def test_partial_injection(self):
self.assertEqual(
('Hello, again!', 'Hello, partial!', {'c': 'cee'}),
self.injector.apply(hello_again_partial, c='cee'))
def test_partial_regardless_injection(self):
self.assertEqual(
('Hello, again!', 'Hello, partial!', {'c': 'cee'}),
self.injector.apply(
hello_again_partial_regardless_annotated, c='cee'))
self.assertEqual(
('Hello, again!', 'wat', {'c': 'cee'}),
self.injector.apply(
hello_again_partial_regardless_unannotated, c='cee'))
def test_eager_partial_injection(self):
self.assertEqual(
('Hello, again!', 'Hello, partial!', {'c': 'cee'}),
self.injector.apply(hello_again_eager_partial, c='cee'))
def test_eager_partial_regardless_injection(self):
self.assertEqual(
('Hello, again!', 'Hello, partial!', {'c': 'cee'}),
self.injector.apply(
hello_again_eager_partial_regardless_annotated, c='cee'))
self.assertEqual(
('Hello, again!', 'wat', {'c': 'cee'}),
self.injector.apply(
hello_again_eager_partial_regardless_unannotated, c='cee'))
class LazyPartialTestCase(unittest.TestCase):
def setUp(self):
self.injector = BasicInjector()
def test_partial(self):
note = 'hello:partial'
self.assertEqual(0, self.injector.stats[note])
fn = self.injector.partial(hello_partial)
self.assertEqual(0, self.injector.stats[note])
fn()
self.assertEqual(1, self.injector.stats[note])
fn()
self.assertEqual(1, self.injector.stats[note])
def test_eager_partial(self):
note = 'hello:partial'
self.assertEqual(0, self.injector.stats[note])
fn = self.injector.eager_partial(hello_partial)
self.assertEqual(1, self.injector.stats[note])
fn()
self.assertEqual(1, self.injector.stats[note])
fn()
self.assertEqual(1, self.injector.stats[note])
def test_partial_injection(self):
note = 'hello:partial'
self.assertEqual(0, self.injector.stats[note])
fn = self.injector.partial(hello_again_partial)
self.assertEqual(0, self.injector.stats[note])
fn()
self.assertEqual(1, self.injector.stats[note])
fn()
self.assertEqual(1, self.injector.stats[note])
def test_eager_partial_injection(self):
note = 'hello:partial'
self.assertEqual(0, self.injector.stats[note])
fn = self.injector.eager_partial(hello_again_eager_partial)
self.assertEqual(1, self.injector.stats[note])
fn()
self.assertEqual(1, self.injector.stats[note])
fn()
self.assertEqual(1, self.injector.stats[note])
class CloseMe(object):
# List of all closed instances for use in test inspection.
closed_items = []
def __init__(self, note):
self.closed = None
self.note = note
def open(self):
self.closed = False
def close(self):
assert not self.closed, '{!r} already closed'.format(self)
self.closed_items.append(self)
self.closed = True
class CloseTestInjector(jeni.Injector):
pass
@CloseTestInjector.provider('via_class')
class ClosingProvider(jeni.Provider):
def __init__(self):
self.thing = CloseMe('via_class')
def get(self, name=None):
if self.thing.closed is None:
self.thing.open()
return self.thing
def close(self):
self.thing.close()
@CloseTestInjector.provider('via_generator')
def closing_generator():
thing = CloseMe('via_generator')
thing.open()
yield thing
thing.close()
@CloseTestInjector.provider('via_generator_with_name', name=True)
def closing_generator_with_name():
# Name is not actually used, but generator accepts name.
try:
thing = CloseMe('via_generator_with_name')
thing.open()
yield thing
while True:
yield thing
finally:
thing.close()
@CloseTestInjector.factory('unset_thing')
def unset_thing_factory():
return CloseMe('unset')
@CloseTestInjector.provider('unset')
class UnsetProvider(jeni.Provider):
@jeni.annotate('unset_thing')
def __init__(self, thing):
self.closed = False
self.thing = thing
def get(self):
raise jeni.UnsetError()
def close(self):
assert not self.closed, '{!r} already closed'.format(self)
self.closed = True
self.thing.close()
@CloseTestInjector.provider('annotated_close')
class AnnotatedCloseProvider(jeni.Provider):
def get(self):
return self
@jeni.annotate('echo:bar')
def close(self, echo):
"This should raise TypeError."
CloseTestInjector.factory('echo', echo)
class ClosingTestCase(unittest.TestCase):
def setUp(self):
self.injector = CloseTestInjector()
def test_multiple_close(self):
self.injector.close()
self.assertRaises(RuntimeError, self.injector.close)
self.assertRaises(RuntimeError, self.injector.close)
def assert_with_note(self, note):
thing = self.injector.get(note)
self.assertIs(thing, self.injector.get(note))
self.assertEqual(False, thing.closed)
self.injector.close()
self.assertEqual(True, thing.closed)
def test_close_via_class(self):
self.assert_with_note('via_class')
def test_close_via_generator(self):
self.assert_with_note('via_generator')
def test_close_via_generator_with_name(self):
self.assert_with_note('via_generator_with_name')
def test_close_via_generator_with_name_name(self):
self.assert_with_note('via_generator_with_name:name')
def _test_close_order(self):
injector = CloseTestInjector()
num_prev_closed_items = len(CloseMe.closed_items)
notes = ['via_generator', 'via_class', 'via_generator_with_name']
close_order = list(reversed(notes))
injector.get('echo') # Add a non-closing provider for good measure.
for note in notes:
injector.get(note)
injector.get('via_class') # Get again; should not impact order.
injector.close()
self.assertEqual(
close_order,
[x.note for x in CloseMe.closed_items[num_prev_closed_items:]])
def test_close_order(self):
# Test multiple times given failed test is random order.
for x in range(100):
self._test_close_order()
def test_unset_closed(self):
thing = self.injector.get('unset_thing')
self.assertRaises(jeni.UnsetError, self.injector.get, 'unset')
self.injector.close()
self.assertTrue(thing.closed)
def test_cannot_get_after_close(self):
self.assertEqual('thing', self.injector.get('echo:thing'))
self.injector.close()
self.assertRaises(RuntimeError, self.injector.get, 'echo:thing')
def test_annotated_close(self):
self.injector.get('annotated_close')
# Injector.close does not apply annotations.
self.assertRaises(TypeError, self.injector.close)
def test_multiple_get_then_close(self):
self.injector.get('via_class:foo')
self.injector.get('via_class:bar')
self.injector.close() # AssertionError likely if this fails.
class InjectorStatsTestCase(unittest.TestCase):
def setUp(self):
self.injector = BasicInjector()
def test_empty_stats(self):
self.assertEqual({}, self.injector.stats)
def test_a_few_calls(self):
stats = {
'hello': 1,
'hello:thing': 1,
'eggs': 2,
(jeni.PARTIAL_REGARDLESS, (eggs, (), ())): 1,
}
self.injector.get('eggs')
self.injector.get('hello')
self.injector.get('hello:thing')
self.injector.get('eggs')
self.assertEqual(stats, self.injector.stats)
def test_many_calls(self):
stats = {
'hello': 10,
'hello:thing': 15,
'eggs': 21,
(jeni.PARTIAL_REGARDLESS, (eggs, (), ())): 1,
}
for _ in range(10):
self.injector.get('hello')
for _ in range(21):
self.injector.get('eggs')
for _ in range(15):
self.injector.get('hello:thing')
self.assertEqual(stats, self.injector.stats)
class ContextManagerTestCase(unittest.TestCase):
def test_with_block(self):
with CloseTestInjector() as injector:
thing = injector.get('via_class')
self.assertEqual(False, thing.closed)
self.assertEqual(True, thing.closed)