-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_models.py
107 lines (77 loc) · 2.45 KB
/
test_models.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
from attacktree.models import (
Action,
Block,
Detect,
Discovery,
Edge,
Root,
Goal,
) # classes
from attacktree.models import mitreAttack, rules # dicts
from attacktree.renderer import Renderer
from attacktree.brain import Brain
import pytest
import inspect
import math # TODO: Refactor out the floats for probability and replace with 0-100int
def test_insertBetween_50perecent(render=False):
# Prep
root = Root("Start")
goal = Goal("Finish")
a = root.action("A")
b = a.action("B")
c = b.action("C")
c.connectTo(goal)
# Verify prep
assert len(a.edges) == 1
assert a.edges[0].childNode == b
# Create a block
block = Block(label="Test Block", implemented=True, cost=5000, pDefend=50)
# The tested function
block.insertBetween(a, b)
# After
assert len(a.edges) == 1
assert len(block.edges) == 1
assert a.edges[0].childNode.edges[0].childNode == b
if render:
Renderer().render(root=root, fname=inspect.currentframe().f_code.co_name)
def test_insertBlockBetween_100perecent(render=False):
# Prep
root = Root("Start")
goal = Goal("Finish")
a = root.action("A")
b = a.action("B")
c = b.action("C")
c.connectTo(goal)
# Before
assert len(b.edges) == 1
# Create a second block with a pSuccess of 100
block = Block(label="Test Block", implemented=True, cost=5000, pDefend=100)
# The tested function
block.insertBetween(b, c)
# After
assert (
len(b.edges) == 1
) # As the block has 100% chance of working, c should be unreachable
assert b.edges[0].childNode == block
if render:
Renderer().render(root=root, fname=inspect.currentframe().f_code.co_name)
def test_instertBlockBetweenPatiallySuccessfulActions(render=True):
# Prep
root = Root("Start")
goal = Goal("Finish")
a = root.add(Action(label="A", pSuccess=80))
b = a.action("B")
c = b.action("C")
c.connectTo(goal)
# a has an 80percent chance of working.
# lets add a blocker that's 20% effective
block = Block(label="Test Block", implemented=True, cost=5000, pDefend=50)
# The tested function
block.insertBetween(a, b)
assert len(a.edges) == 1
assert a.edges[0].childNode == block
assert len(block.edges) == 1
assert block.edges[0].childNode == b
# The tested function:
if render:
Renderer().render(root=root, fname=inspect.currentframe().f_code.co_name)