test_python_api.py 4.32 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# -*- coding: utf-8 -*-
"""Unit tests for Python API"""

from __future__ import unicode_literals
from __future__ import print_function


# Standard imports
import pytest
import tempfile

@pytest.fixture()
def feed_model_with_SCC():
    """Fixture for a Cadbiom model with 4 Strongly Connected Components

    :return: A list of SCC, and the corrected model with start nodes inserted.
    :rtype: <tuple <list>, <str>>
    """

    scc = [['I', 'K', 'J', 'L'], ['Y', 'X', 'Z']]
21
22
    model = """<?xml version = "1.0" encoding="ASCII" standalone="yes" ?>
<model xmlns="http://cadbiom" name="">
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
  <CSimpleNode name="A"/>
  <CSimpleNode name="B"/>
  <CSimpleNode name="E"/>
  <CSimpleNode name="D"/>
  <CSimpleNode name="C"/>
  <CSimpleNode name="Y"/>
  <CSimpleNode name="X"/>
  <CSimpleNode name="Z"/>
  <CSimpleNode name="I"/>
  <CSimpleNode name="K"/>
  <CSimpleNode name="J"/>
  <CSimpleNode name="L"/>
  <CStartNode name="__start__0"/>
  <CStartNode name="__start__1"/>
  <transition ori="I" ext="J" event="" condition=""/>
  <transition ori="L" ext="I" event="" condition=""/>
  <transition ori="K" ext="L" event="" condition=""/>
  <transition ori="X" ext="Z" event="" condition=""/>
  <transition ori="__start__1" ext="X" event="" condition=""/>
  <transition ori="J" ext="K" event="" condition=""/>
  <transition ori="B" ext="D" event="" condition=""/>
  <transition ori="A" ext="B" event="" condition="C"/>
  <transition ori="X" ext="C" event="" condition=""/>
  <transition ori="C" ext="B" event="" condition="A"/>
  <transition ori="I" ext="K" event="" condition=""/>
  <transition ori="D" ext="C" event="" condition=""/>
  <transition ori="J" ext="B" event="" condition=""/>
  <transition ori="__start__0" ext="I" event="" condition=""/>
  <transition ori="Y" ext="X" event="" condition=""/>
  <transition ori="Z" ext="Y" event="" condition=""/>
  <transition ori="D" ext="E" event="" condition=""/>
54
55
56
57
58
59
60
</model>
"""

    return scc, model


def test_SCC_search(feed_model_with_SCC):
61
62
63
64
65
66
67
68
69
70
    """Test the correction of a model by removing Strongly Connected Components

    - We add a start node for every SCC
    - We add a transition between the start node and the smallest node of every
    SCC (sorted in lexicogrpahic order)

    - Thus, we add xml preamble in the model

    Keep in mind that by testing the result of the export of a model, we also
    test its content...
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
    """

    import cadbiom.models.guard_transitions.analyser.model_corrections as mc

    model = feed_model_with_SCC[1]

    # Create the file model in /tmp/
    # Note: prevent the deletion of the file after the close() call
    fd_model = tempfile.NamedTemporaryFile(suffix='.bcx', delete=False)
    fd_model.write(
        """<model xmlns="http://cadbiom" name="">
    <CSimpleNode name="A"/>
    <CSimpleNode name="B"/>
    <CSimpleNode name="E"/>
    <CSimpleNode name="D"/>
    <CSimpleNode name="C"/>
    <CSimpleNode name="Y"/>
    <CSimpleNode name="X"/>
    <CSimpleNode name="Z"/>
    <CSimpleNode name="I"/>
    <CSimpleNode name="K"/>
    <CSimpleNode name="J"/>
    <CSimpleNode name="L"/>
    <transition name="" ori="A" ext="B" event="" condition="C"/>
    <transition name="" ori="B" ext="D" event="" condition=""/>
    <transition name="" ori="C" ext="B" event="" condition="A"/>
    <transition name="" ori="D" ext="C" event="" condition=""/>
    <transition name="" ori="D" ext="E" event="" condition=""/>
    <transition name="" ori="Z" ext="Y" event="" condition=""/>
    <transition name="" ori="Y" ext="X" event="" condition=""/>
    <transition name="" ori="X" ext="Z" event="" condition=""/>
    <transition name="" ori="X" ext="C" event="" condition=""/>
    <transition name="" ori="I" ext="J" event="" condition=""/>
    <transition name="" ori="L" ext="I" event="" condition=""/>
    <transition name="" ori="K" ext="L" event="" condition=""/>
    <transition name="" ori="J" ext="K" event="" condition=""/>
    <transition name="" ori="J" ext="B" event="" condition=""/>
    <transition name="" ori="I" ext="K" event="" condition=""/>
    </model>"""
    )
    fd_model.close()


    # Make a new model file (with "_without_scc" suffix in filename)
    mc.add_start_nodes(fd_model.name) # Filename + path

117
118
    expected_lines = set(model.split("\n"))

119
    with open(fd_model.name[:-4] + "_without_scc.bcx", 'r') as file:
120
121
        found_lines = set(file.read().split("\n"))
        assert found_lines == expected_lines