1from .. import stability
2from collections import OrderedDict, defaultdict
3
4def test_is_inconsistent():
5    assert stability.is_inconsistent({"PASS": 10}, 10) is False
6    assert stability.is_inconsistent({"PASS": 9}, 10) is True
7    assert stability.is_inconsistent({"PASS": 9, "FAIL": 1}, 10) is True
8    assert stability.is_inconsistent({"PASS": 8, "FAIL": 1}, 10) is True
9
10
11def test_find_slow_status():
12    assert stability.find_slow_status({
13        "longest_duration": {"TIMEOUT": 10},
14        "timeout": 10}) is None
15    assert stability.find_slow_status({
16        "longest_duration": {"CRASH": 10},
17        "timeout": 10}) is None
18    assert stability.find_slow_status({
19        "longest_duration": {"ERROR": 10},
20        "timeout": 10}) is None
21    assert stability.find_slow_status({
22        "longest_duration": {"PASS": 1},
23        "timeout": 10}) is None
24    assert stability.find_slow_status({
25        "longest_duration": {"PASS": 81},
26        "timeout": 100}) == "PASS"
27    assert stability.find_slow_status({
28        "longest_duration": {"TIMEOUT": 10, "FAIL": 81},
29        "timeout": 100}) == "FAIL"
30    assert stability.find_slow_status({
31        "longest_duration": {"SKIP": 0}}) is None
32
33
34def test_get_steps():
35    logger = None
36
37    steps = stability.get_steps(logger, 0, 0, [])
38    assert len(steps) == 0
39
40    steps = stability.get_steps(logger, 0, 0, [{}])
41    assert len(steps) == 0
42
43    repeat_loop = 1
44    flag_name = 'flag'
45    flag_value = 'y'
46    steps = stability.get_steps(logger, repeat_loop, 0, [
47                                {flag_name: flag_value}])
48    assert len(steps) == 1
49    assert steps[0][0] == "Running tests in a loop %d times with flags %s=%s" % (
50        repeat_loop, flag_name, flag_value)
51
52    repeat_loop = 0
53    repeat_restart = 1
54    flag_name = 'flag'
55    flag_value = 'n'
56    steps = stability.get_steps(logger, repeat_loop, repeat_restart, [
57                                {flag_name: flag_value}])
58    assert len(steps) == 1
59    assert steps[0][0] == "Running tests in a loop with restarts %d times with flags %s=%s" % (
60        repeat_restart, flag_name, flag_value)
61
62    repeat_loop = 10
63    repeat_restart = 5
64    steps = stability.get_steps(logger, repeat_loop, repeat_restart, [{}])
65    assert len(steps) == 2
66    assert steps[0][0] == "Running tests in a loop %d times" % repeat_loop
67    assert steps[1][0] == (
68        "Running tests in a loop with restarts %d times" % repeat_restart)
69
70
71def test_log_handler():
72    handler = stability.LogHandler()
73    data = OrderedDict()
74    data["test"] = "test_name"
75    test = handler.find_or_create_test(data)
76    assert test["subtests"] == OrderedDict()
77    assert test["status"] == defaultdict(int)
78    assert test["longest_duration"] == defaultdict(float)
79    assert test == handler.find_or_create_test(data)
80
81    start_time = 100
82    data["time"] = start_time
83    handler.test_start(data)
84    assert test["start_time"] == start_time
85
86    data["subtest"] = "subtest_name"
87    subtest = handler.find_or_create_subtest(data)
88    assert subtest["status"] == defaultdict(int)
89    assert subtest["messages"] == set()
90    assert subtest == handler.find_or_create_subtest(data)
91
92    data["status"] = 0
93    assert subtest["status"][data["status"]] == 0
94    handler.test_status(data)
95    assert subtest["status"][data["status"]] == 1
96    handler.test_status(data)
97    assert subtest["status"][data["status"]] == 2
98    data["status"] = 1
99    assert subtest["status"][data["status"]] == 0
100    message = "test message"
101    data["message"] = message
102    handler.test_status(data)
103    assert subtest["status"][data["status"]] == 1
104    assert len(subtest["messages"]) == 1
105    assert message in subtest["messages"]
106
107    test_duration = 10
108    data["time"] = data["time"] + test_duration
109    handler.test_end(data)
110    assert test["longest_duration"][data["status"]] == test_duration
111    assert "timeout" not in test
112
113    data["test2"] = "test_name_2"
114    timeout = 5
115    data["extra"] = {}
116    data["extra"]["test_timeout"] = timeout
117    handler.test_start(data)
118    handler.test_end(data)
119    assert test["timeout"] == timeout * 1000
120
121
122def test_err_string():
123    assert stability.err_string(
124        {u'OK': 1, u'FAIL': 1}, 1) == u"**Duplicate subtest name**"
125    assert stability.err_string(
126        {u'OK': 2, u'FAIL': 1}, 2) == u"**Duplicate subtest name**"
127    assert stability.err_string({u'SKIP': 1}, 0) == u"Duplicate subtest name"
128    assert stability.err_string(
129        {u'SKIP': 1, u'OK': 1}, 1) == u"Duplicate subtest name"
130
131    assert stability.err_string(
132        {u'FAIL': 1}, 2) == u"**FAIL: 1/2, MISSING: 1/2**"
133    assert stability.err_string(
134        {u'FAIL': 1, u'OK': 1}, 3) == u"**FAIL: 1/3, OK: 1/3, MISSING: 1/3**"
135
136    assert stability.err_string(
137        {u'OK': 1, u'FAIL': 1}, 2) == u"**FAIL: 1/2, OK: 1/2**"
138
139    assert stability.err_string(
140        {u'OK': 2, u'FAIL': 1, u'SKIP': 1}, 4) == u"FAIL: 1/4, OK: 2/4, SKIP: 1/4"
141    assert stability.err_string(
142        {u'FAIL': 1, u'SKIP': 1, u'OK': 2}, 4) == u"FAIL: 1/4, OK: 2/4, SKIP: 1/4"
143