File: test_predicates.py

package info (click to toggle)
python-django-rules 3.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 320 kB
  • sloc: python: 1,613; makefile: 5; sh: 3
file content (440 lines) | stat: -rw-r--r-- 12,870 bytes parent folder | download | duplicates (2)
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
import functools
from unittest import TestCase

from rules.predicates import (
    NO_VALUE,
    Predicate,
    always_allow,
    always_deny,
    always_false,
    always_true,
    predicate,
)


class PredicateKwonlyTests(TestCase):
    def test_predicate_kwargonly(self):
        def p(foo, *, bar):
            return True

        with self.assertRaises(TypeError):
            predicate(p)

        def p2(foo, *a, bar):
            return True

        with self.assertRaises(TypeError):
            predicate(p2)

        def p3(foo, *, bar="bar"):
            return True

        # Should not fail
        predicate(p3)


class PredicateTests(TestCase):
    def test_always_true(self):
        assert always_true()

    def test_always_false(self):
        assert not always_false()

    def test_always_allow(self):
        assert always_allow()

    def test_always_deny(self):
        assert not always_deny()

    def test_lambda_predicate(self):
        p = Predicate(lambda x: x == "a")
        assert p.name == "<lambda>"
        assert p.num_args == 1
        assert p("a")

    def test_lambda_predicate_custom_name(self):
        p = Predicate(lambda x: x == "a", name="mypred")
        assert p.name == "mypred"
        assert p.num_args == 1
        assert p("a")

    def test_function_predicate(self):
        def mypred(x):
            return x == "a"

        p = Predicate(mypred)
        assert p.name == "mypred"
        assert p.num_args == 1
        assert p("a")

    def test_function_predicate_custom_name(self):
        def mypred(x):
            return x == "a"

        p = Predicate(mypred, name="foo")
        assert p.name == "foo"
        assert p.num_args == 1
        assert p("a")

    def test_partial_function_predicate(self):
        def mypred(one, two, three):
            return one < two < three

        p = Predicate(functools.partial(mypred, 1))
        assert p.name == "mypred"
        assert p.num_args == 2  # 3 - 1 partial
        assert p(2, 3)
        p = Predicate(functools.partial(mypred, 1, 2))
        assert p.name == "mypred"
        assert p.num_args == 1  # 3 - 2 partial
        assert p(3)

    def test_method_predicate(self):
        class SomeClass(object):
            def some_method(self, arg1, arg2):
                return arg1 == arg2

        obj = SomeClass()
        p = Predicate(obj.some_method)
        assert p.name == "some_method"
        assert p.num_args == 2
        assert p(2, 2)

    def test_partial_method_predicate(self):
        class SomeClass(object):
            def some_method(self, arg1, arg2):
                return arg1 == arg2

        obj = SomeClass()
        p = Predicate(functools.partial(obj.some_method, 2))
        assert p.name == "some_method"
        assert p.num_args == 1
        assert p(2)

    def test_class_predicate(self):
        class callableclass(object):
            def __call__(self, arg1, arg2):
                return arg1 == arg2

        fn = callableclass()
        p = Predicate(fn)
        assert p.name == "callableclass"
        assert p.num_args == 2
        assert p("a", "a")

    def test_class_predicate_custom_name(self):
        class callableclass(object):
            def __call__(self, arg):
                return arg == "a"

        fn = callableclass()
        p = Predicate(fn, name="bar")
        assert p.name == "bar"
        assert p.num_args == 1
        assert p("a")

    def test_predicate_predicate(self):
        def mypred(x):
            return x == "a"

        p = Predicate(Predicate(mypred))
        assert p.name == "mypred"
        assert p.num_args == 1
        assert p("a")

    def test_predicate_predicate_custom_name(self):
        def mypred(x):
            return x == "a"

        p = Predicate(Predicate(mypred, name="foo"))
        assert p.name == "foo"
        assert p.num_args == 1
        assert p("a")

    def test_predicate_bind(self):
        @predicate(bind=True)
        def is_bound(self):
            return self is is_bound

        assert is_bound()

        p = None

        def mypred(self):
            return self is p

        p = Predicate(mypred, bind=True)
        assert p()

    def test_decorator(self):
        @predicate
        def mypred(arg1, arg2):
            return True

        assert mypred.name == "mypred"
        assert mypred.num_args == 2

    def test_decorator_noargs(self):
        @predicate()
        def mypred(arg1, arg2):
            return True

        assert mypred.name == "mypred"
        assert mypred.num_args == 2

    def test_decorator_custom_name(self):
        @predicate("foo")
        def mypred():
            return True

        assert mypred.name == "foo"
        assert mypred.num_args == 0

        @predicate(name="bar")
        def myotherpred():
            return False

        assert myotherpred.name == "bar"
        assert myotherpred.num_args == 0

    def test_repr(self):
        @predicate
        def mypred(arg1, arg2):
            return True

        assert repr(mypred).startswith("<Predicate:mypred object at 0x")

    def test_AND(self):
        p_AND1 = always_true & always_false
        assert not p_AND1()
        assert p_AND1.name == "(always_true & always_false)"
        p_AND2 = always_false & always_true
        assert not p_AND2()
        assert p_AND2.name == "(always_false & always_true)"
        p_AND3 = always_true & always_true
        assert p_AND3()
        assert p_AND3.name == "(always_true & always_true)"
        p_AND4 = always_false & always_false
        assert not p_AND4()
        assert p_AND4.name == "(always_false & always_false)"

    def test_OR(self):
        p_OR1 = always_true | always_false
        assert p_OR1()
        assert p_OR1.name == "(always_true | always_false)"
        p_OR2 = always_false | always_true
        assert p_OR2()
        assert p_OR2.name == "(always_false | always_true)"
        p_OR3 = always_true | always_true
        assert p_OR3()
        assert p_OR3.name == "(always_true | always_true)"
        p_OR4 = always_false | always_false
        assert not p_OR4()
        assert p_OR4.name == "(always_false | always_false)"

    def test_XOR(self):
        p_XOR1 = always_true ^ always_false
        assert p_XOR1()
        assert p_XOR1.name == "(always_true ^ always_false)"
        p_XOR2 = always_false ^ always_true
        assert p_XOR2()
        assert p_XOR2.name == "(always_false ^ always_true)"
        p_XOR3 = always_true ^ always_true
        assert not p_XOR3()
        assert p_XOR3.name == "(always_true ^ always_true)"
        p_XOR4 = always_false ^ always_false
        assert not p_XOR4()
        assert p_XOR4.name == "(always_false ^ always_false)"

    def test_INV(self):
        p_INV1 = ~always_true
        assert not p_INV1()
        assert p_INV1.name == "~always_true"
        p_INV2 = ~always_false
        assert p_INV2()
        assert p_INV2.name == "~always_false"
        p_INV3 = ~(~always_true)
        assert p_INV3()
        assert p_INV3.name == "always_true"
        p_INV4 = ~(~always_false)
        assert not p_INV4()
        assert p_INV4.name == "always_false"

    def test_var_args(self):
        @predicate
        def p(*args, **kwargs):
            assert len(args) > 0
            assert len(kwargs) == 0

        assert p.num_args == 0
        p.test("a")
        p.test("a", "b")

    def test_no_args(self):
        @predicate
        def p(*args, **kwargs):
            assert len(args) == 0
            assert len(kwargs) == 0

        assert p.num_args == 0
        p.test()

    def test_one_arg(self):
        @predicate
        def p(a=None, *args, **kwargs):
            assert len(args) == 0
            assert len(kwargs) == 0
            assert a == "a"

        assert p.num_args == 1
        p.test("a")

    def test_two_args(self):
        @predicate
        def p(a=None, b=None, *args, **kwargs):
            assert len(args) == 0
            assert len(kwargs) == 0
            assert a == "a"
            assert b == "b"

        assert p.num_args == 2
        p.test("a", "b")

    def test_no_mask(self):
        @predicate
        def p(a=None, b=None, *args, **kwargs):
            assert len(args) == 0
            assert len(kwargs) == 1
            assert "c" in kwargs
            assert a == "a"
            assert b == "b"

        p("a", b="b", c="c")

    def test_no_value_marker(self):
        @predicate
        def p(a, b=None):
            assert a == "a"
            assert b is None

        assert not NO_VALUE
        p.test("a")
        p.test("a", NO_VALUE)

    def test_short_circuit(self):
        @predicate
        def skipped_predicate(self):
            return None

        @predicate
        def shorted_predicate(self):
            raise Exception("this predicate should not be evaluated")

        assert (always_false & shorted_predicate).test() is False
        assert (always_true | shorted_predicate).test() is True

        def raises(pred):
            try:
                pred.test()
                return False
            except Exception as e:
                return "evaluated" in str(e)

        assert raises(always_true & shorted_predicate)
        assert raises(always_false | shorted_predicate)
        assert raises(skipped_predicate & shorted_predicate)
        assert raises(skipped_predicate | shorted_predicate)

    def test_skip_predicate(self):
        @predicate(bind=True)
        def requires_two_args(self, a, b):
            return a == b if len(self.context.args) > 1 else None

        @predicate
        def passthrough(a):
            return a

        assert (requires_two_args & passthrough).test(True, True) is True
        assert (requires_two_args & passthrough).test(True, False) is False

        # because requires_two_args is called with only one argument
        # its result is not taken into account, only the result of the
        # other predicate matters.
        assert (requires_two_args & passthrough).test(True) is True
        assert (requires_two_args & passthrough).test(False) is False
        assert (requires_two_args | passthrough).test(True) is True
        assert (requires_two_args | passthrough).test(False) is False

        # test that order does not matter
        assert (passthrough & requires_two_args).test(True) is True
        assert (passthrough & requires_two_args).test(False) is False
        assert (passthrough | requires_two_args).test(True) is True
        assert (passthrough | requires_two_args).test(False) is False

        # test that inversion does not modify the result
        assert (~requires_two_args & passthrough).test(True) is True
        assert (~requires_two_args & passthrough).test(False) is False
        assert (~requires_two_args | passthrough).test(True) is True
        assert (~requires_two_args | passthrough).test(False) is False
        assert (passthrough & ~requires_two_args).test(True) is True
        assert (passthrough & ~requires_two_args).test(False) is False
        assert (passthrough | ~requires_two_args).test(True) is True
        assert (passthrough | ~requires_two_args).test(False) is False

        # test that when all predicates are skipped, result is False
        assert requires_two_args.test(True) is False
        assert (requires_two_args | requires_two_args).test(True) is False
        assert (requires_two_args & requires_two_args).test(True) is False

        # test that a skipped predicate doesn't alter the result at all
        assert (requires_two_args | requires_two_args | passthrough).test(True) is True
        assert (requires_two_args & requires_two_args & passthrough).test(True) is True

    def test_invocation_context(self):
        @predicate
        def p1():
            assert id(p1.context) == id(p2.context)
            assert p1.context.args == ("a",)
            return True

        @predicate
        def p2():
            assert id(p1.context) == id(p2.context)
            assert p2.context.args == ("a",)
            return True

        p = p1 & p2
        assert p.test("a")
        assert p.context is None

    def test_invocation_context_nested(self):
        @predicate
        def p1():
            assert p1.context.args == ("b1",)
            return True

        @predicate
        def p2():
            assert p2.context.args == ("b2",)
            return True

        @predicate
        def p():
            assert p1.context.args == ("a",)
            return p1.test("b1") & p2.test("b2")

        assert p.test("a")
        assert p.context is None

    def test_invocation_context_storage(self):
        @predicate
        def p1(a):
            p1.context["p1.a"] = a
            return True

        @predicate
        def p2(a):
            return p2.context["p1.a"] == a

        p = p1 & p2
        assert p.test("a")