Completed
Push — master ( 3008cd...e7155f )
by Valentin
03:11
created

UtilsTests.testInclusionTriple()   A

Complexity

Conditions 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
1 1
from unittest import TestCase
2 1
import itertools
3
4 1
from ppp_datamodel import Resource, Triple, Missing, Intersection, Union, List, Union, And, Or, Exists, Nth, first, last, Sort
5 1
from ppp_datamodel import utils
6
7 1
T = Triple
8 1
R = Resource
9 1
M = Missing
10
11 1
class UtilsTests(utils.InclusionTestCase):
12 1
    def testContainsMissing(self):
13 1
        f = utils.contains_missing
14 1
        self.assertTrue(f(T(subject=R(value='foo'), object=T(subject=M()))))
15 1
        self.assertFalse(f(T(subject=R(value='foo'), object=T(subject=R('foo')))))
16 1
        self.assertTrue(f(M()))
17 1
    def testInclusionBasic(self):
18 1
        self.assertIncluded(Missing(), Missing())
19 1
        self.assertIncluded(Resource('foo'), Resource('foo'))
20 1
        with self.assertRaises(AssertionError):
21 1
            self.assertIncluded(Resource('foo'), Resource('bar'))
22 1
        self.assertIncluded(List([Resource('foo')]), Resource('foo'))
23 1
        with self.assertRaises(AssertionError):
24 1
            self.assertIncluded(List([Resource('foo')]), Resource('bar'))
25 1
        self.assertIncluded(Resource('foo'), List([Resource('foo')]))
26 1
        with self.assertRaises(AssertionError):
27 1
            self.assertIncluded(Resource('foo'), List([Resource('bar')]))
28 1
        self.assertIncluded(List([Resource('foo')]), List([Resource('foo')]))
29 1
        with self.assertRaises(AssertionError):
30 1
            self.assertIncluded(List([Resource('foo')]),
31
                    List([Resource('bar')]))
32 1
        self.assertIncluded(List([Resource('foo')]),
33
                List([Resource('foo'), Resource('bar')]))
34 1
        with self.assertRaises(AssertionError):
35 1
            self.assertIncluded(List([Resource('foo'), Resource('bar')]),
36
                    List([Resource('foo')]))
37 1
    def testInclusionDifferentType(self):
38 1
        l = [Resource('foo'), Missing(), Triple(Resource('foo'),
39
            Resource('foo'), Resource('foo')),\
40
            Intersection([Resource('foo')]), Union([Resource('foo')]),\
41
            And([Resource('foo')]), Or([Resource('foo')]),
42
            Exists(Resource('foo')), first(Resource('foo')),
43
            last(Resource('foo')), Sort(Resource('foo'), Resource('pred'))]
44 1
        for (t1, t2) in itertools.permutations(l, 2):
45 1
            with self.assertRaises(AssertionError):
46 1
                self.assertFalse(self.assertIncluded(t1, t2))
47 1
    def testInclusionTriple(self):
48 1
        tree1=Triple(Resource('foo'),
49
                List([Resource('a'), Resource('b')]), Missing())
50 1
        tree2=Triple(List([Resource('bar'), Resource('foo')]),
51
                List([Resource('a'), Resource('d'), Resource('b')]), Missing())
52 1
        self.assertIncluded(tree1, tree2)
53 1
        with self.assertRaises(AssertionError):
54 1
            self.assertIncluded(tree2, tree1)
55 1
    def testInclusionTriple2(self):
56 1
        tree1=Triple(Resource('a'),Resource('b'), Resource('c'), Resource('d'))
57 1
        tree2=Triple(Resource('a'),Resource('b'), Resource('c'), List([Resource('d'),Resource('e')]))
58 1
        self.assertIncluded(tree1, tree2)
59 1
        with self.assertRaises(AssertionError):
60 1
            self.assertIncluded(tree2, tree1)
61 1
    def testInclusionFirstLastSort(self):
62 1
        tree1=Triple(Resource('foo'), List([Resource('a'), Resource('b')]),
63
                Missing())
64 1
        tree2=Triple(List([Resource('bar'), Resource('foo')]),
65
                List([Resource('a'), Resource('d'), Resource('b')]), Missing())
66 1
        for op in (last, first):
67 1
            self.assertIncluded(op(tree1), op(tree2))
68 1
            with self.assertRaises(AssertionError):
69 1
                self.assertIncluded(op(tree2), op(tree1))
70 1
        self.assertIncluded(Sort(tree1, Resource('pred')),
71
                Sort(tree2, Resource('pred')))
72 1
        with self.assertRaises(AssertionError):
73 1
            self.assertIncluded(Sort(tree2, Resource('pred')),
74
                    Sort(tree1, Resource('pred')))
75 1
        with self.assertRaises(AssertionError):
76 1
            self.assertIncluded(Sort(tree1, Resource('pred')),
77
                    Sort(tree2, Resource('derp')))
78 1
    def testInclusionIntersectionUnionAndOr(self):
79 1
        tree1=Triple(Resource('foo'), List([Resource('a'), Resource('b')]),
80
                Missing())
81 1
        tree2=Triple(List([Resource('bar'), Resource('foo')]),
82
                List([Resource('a'), Resource('d'), Resource('b')]), Missing())
83 1
        tree3=Missing()
84 1
        for op in [Intersection,Union,And, Or]:
85 1
            self.assertIncluded(op([tree1]), op([tree2]))
86 1
            with self.assertRaises(AssertionError):
87 1
                self.assertIncluded(op([tree2]), op([tree1]))
88 1
            self.assertIncluded(op([tree1, tree3]), op([tree1, tree3]))
89 1
            self.assertIncluded(op([tree1, tree3]), op([tree3, tree1]))
90 1
            with self.assertRaises(AssertionError):
91 1
                self.assertIncluded(op([tree1, tree3]), op([tree1]))
92 1
            with self.assertRaises(AssertionError):
93 1
                self.assertIncluded(op([tree1]), op([tree3]))
94 1
    def testUnknownClass(self):
95 1
        class Foo:
96 1
            pass
97 1
        with self.assertRaises(TypeError):
98
            self.assertIncluded(Foo(),Foo())
99