StrictDoc Documentation
tests/unit/strictdoc/core/test_graph_database.py
Source file coverage
Path:
tests/unit/strictdoc/core/test_graph_database.py
Lines:
122
Non-empty lines:
104
Non-empty lines covered with requirements:
104 / 104 (100.0%)
Functions:
8
Functions covered by requirements:
8 / 8 (100.0%)
1
"""
2
@relation(SDOC-SRS-28, scope=file)
3
"""
4
 
5
from strictdoc.core.graph.many_to_many_set import ManyToManySet
6
from strictdoc.core.graph.one_to_one_dictionary import OneToOneDictionary
7
from strictdoc.core.graph_database import (
8
    GraphDatabase,
9
)
10
 
11
 
12
def test_01_basic():
13
    class Relation:
14
        INLINE_LINK_TO_ANCHOR = 1
15
 
16
    class Node:
17
        def __init__(self, title):
18
            self.title = title
19
 
20
    anchor = Node("anchor")
21
    inline_link = Node("inline_link")
22
 
23
    graph_database = GraphDatabase(
24
        buckets=[
25
            (Relation.INLINE_LINK_TO_ANCHOR, OneToOneDictionary(Node, Node))
26
        ]
27
    )
28
 
29
    assert (
30
        graph_database.has_link(
31
            link_type=Relation.INLINE_LINK_TO_ANCHOR,
32
            lhs_node=inline_link,
33
            rhs_node=anchor,
34
        )
35
        is False
36
    )
37
 
38
    graph_database.create_link(
39
        link_type=Relation.INLINE_LINK_TO_ANCHOR,
40
        lhs_node=inline_link,
41
        rhs_node=anchor,
42
    )
43
 
44
    assert (
45
        graph_database.get_link_value(
46
            link_type=Relation.INLINE_LINK_TO_ANCHOR,
47
            lhs_node=inline_link,
48
        )
49
        == anchor
50
    )
51
    assert (
52
        graph_database.has_link(
53
            link_type=Relation.INLINE_LINK_TO_ANCHOR,
54
            lhs_node=inline_link,
55
            rhs_node=anchor,
56
        )
57
        is True
58
    )
59
 
60
    graph_database.delete_link(
61
        link_type=Relation.INLINE_LINK_TO_ANCHOR,
62
        lhs_node=inline_link,
63
        rhs_node=anchor,
64
    )
65
 
66
    assert (
67
        graph_database.get_link_value_weak(
68
            link_type=Relation.INLINE_LINK_TO_ANCHOR,
69
            lhs_node=inline_link,
70
        )
71
        is None
72
    )
73
    assert (
74
        graph_database.has_link(
75
            link_type=Relation.INLINE_LINK_TO_ANCHOR,
76
            lhs_node=inline_link,
77
            rhs_node=anchor,
78
        )
79
        is False
80
    )
81
 
82
 
83
def test_02_basic():
84
    class Relation:
85
        INLINE_LINK_TO_ANCHOR = 1
86
 
87
    class Node:
88
        def __init__(self, title):
89
            self.title = title
90
 
91
    anchor = Node("anchor")
92
    inline_link = Node("inline_link")
93
 
94
    graph_database = GraphDatabase(
95
        buckets=[(Relation.INLINE_LINK_TO_ANCHOR, ManyToManySet(Node, Node))]
96
    )
97
 
98
    assert (
99
        graph_database.has_link(
100
            link_type=Relation.INLINE_LINK_TO_ANCHOR,
101
            lhs_node=anchor,
102
            rhs_node=inline_link,
103
        )
104
        is False
105
    )
106
 
107
    graph_database.create_link(
108
        link_type=Relation.INLINE_LINK_TO_ANCHOR,
109
        lhs_node=anchor,
110
        rhs_node=inline_link,
111
    )
112
    assert (
113
        graph_database.has_link(
114
            link_type=Relation.INLINE_LINK_TO_ANCHOR,
115
            lhs_node=anchor,
116
            rhs_node=inline_link,
117
        )
118
        is True
119
    )
120
    assert graph_database.get_link_values_reverse(
121
        link_type=Relation.INLINE_LINK_TO_ANCHOR, rhs_node=inline_link
122
    ) == {anchor}