aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_store_state.py
blob: 8b3a728ece5bd2c73b1414b3e7cf3a34fdb603e7 (plain)
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
from time import sleep

import pytest

from onaptests.steps.base import BaseStep



class TestStep(BaseStep):

    @BaseStep.store_state
    def execute(self):
        return super().execute()

    @property
    def description(self):
        return "Test pass step"

    @property
    def component(self) -> str:
        return "Test"


class TestFailStep(BaseStep):

    @BaseStep.store_state
    def execute(self):
        super().execute()
        raise Exception

    @property
    def description(self):
        return "Test fail step"

    @property
    def component(self) -> str:
        return "Test"


class TestOneSecStep(BaseStep):

    @BaseStep.store_state
    def execute(self):
        super().execute()
        sleep(1)

    @property
    def description(self):
        return "One second test step"

    @property
    def component(self) -> str:
        return "Test"


class TestStepNoSuperExecute(BaseStep):

    @BaseStep.store_state
    def execute(self):
        sleep(1)

    @property
    def description(self):
        return "One second test step - no super execute call"

    @property
    def component(self) -> str:
        return "Test"


def test_store_state():
    ts = TestStep()
    ts.execute()
    assert len(ts.reports_collection.report) == 1
    rep = ts.reports_collection.report[0]
    assert rep.step_description == "[Test] TestStep: Test pass step"
    assert rep.step_execution_status.value == "PASS"
    assert rep.step_execution_duration != 0

    fs = TestFailStep()
    fs.add_step(TestStep())
    with pytest.raises(Exception):
        fs.execute()
    rep_f, rep_s = fs.reports_collection.report
    assert rep_f.step_description == "[Test] TestFailStep: Test fail step"
    assert rep_f.step_execution_status.value == "FAIL"
    assert rep_f.step_execution_duration != 0

    assert rep_s.step_description == "[Test] TestStep: Test pass step"
    assert rep_s.step_execution_status.value == "PASS"
    assert rep_s.step_execution_duration != 0


def test_store_state_time_measurement():

    ts = TestOneSecStep()
    ts.execute()
    assert len(ts.reports_collection.report) == 1
    rep = ts.reports_collection.report[0]
    assert rep.step_execution_duration > 1

    ts = TestOneSecStep()
    ts.add_step(TestOneSecStep())
    ts.execute()
    assert len(ts.reports_collection.report) == 2
    rep_one, rep_two = ts.reports_collection.report
    assert rep_one.step_execution_duration > 1 and rep_one.step_execution_duration < 2
    assert rep_two.step_execution_duration > 1 and rep_two.step_execution_duration < 2

    ts = TestStepNoSuperExecute()
    ts.execute()
    assert len(ts.reports_collection.report) == 1
    rep = ts.reports_collection.report[0]
    assert rep.step_execution_duration < 1