1# encoding=utf-8
2# Copyright © 2014, 2016, 2019 Intel Corporation
3
4# Permission is hereby granted, free of charge, to any person obtaining a copy
5# of this software and associated documentation files (the "Software"), to deal
6# in the Software without restriction, including without limitation the rights
7# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8# copies of the Software, and to permit persons to whom the Software is
9# furnished to do so, subject to the following conditions:
10
11# The above copyright notice and this permission notice shall be included in
12# all copies or substantial portions of the Software.
13
14# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20# SOFTWARE.
21
22"""Tests for framework.status.
23
24This module does not have the comprehensive tests for all of the various
25combinations of comparisons between the various kinds of functions. Instead, it
26just asserts that the regressions, fixes, etc lists are as expected.
27
28"""
29
30import itertools
31
32import pytest
33
34from framework import status
35
36
37# Statuses from worst to last. NotRun is intentionally not in this list and
38# tested separately because of upcoming features for it
39STATUSES = [
40    status.PASS,
41    status.WARN,
42    status.DMESG_WARN,
43    status.FAIL,
44    status.DMESG_FAIL,
45    status.TIMEOUT,
46    status.CRASH,
47    status.INCOMPLETE,
48]
49
50# all statuses except pass are problems
51PROBLEMS = STATUSES[1:]
52
53# Create lists of fixes and regressions programmatically based on the STATUSES
54# list. This means less code, and easier expansion changes.
55REGRESSIONS = list(itertools.combinations(STATUSES, 2)) + \
56              list(itertools.combinations([status.SKIP] + PROBLEMS, 2))
57FIXES = list(itertools.combinations(reversed(STATUSES), 2)) + \
58        list(itertools.combinations(list(reversed(PROBLEMS)) + [status.SKIP], 2))
59
60# The statuses that don't cause changes when transitioning from one another
61NO_OPS = [status.SKIP, status.NOTRUN]
62
63
64@pytest.mark.raises(exception=status.StatusException)
65def test_bad_lookup():
66    """status.status_lookup: An unexepcted value raises a StatusException"""
67    status.status_lookup('foobar')
68
69
70@pytest.mark.raises(exception=TypeError)
71def test_status_eq_raises():
72    """status.Status: eq comparison to uncomparable object results in TypeError"""
73    status.PASS == dict()
74
75
76@pytest.mark.raises(exception=TypeError)
77def test_nochangestatus_eq_raises():
78    """status.NoChangeStatus: eq comparison to uncomparable type results in TypeError"""
79    status.NOTRUN == dict()
80
81
82@pytest.mark.raises(exception=TypeError)
83def test_nochangestatus_ne_raises():
84    """status.NoChangeStatus: ne comparison to uncomparable type results in TypeError"""
85    status.NOTRUN != dict()
86
87
88def test_status_in():
89    """status.Status: A status can be looked up with 'x in y' synatx"""
90    stat = status.PASS
91    slist = ['pass']
92
93    assert stat in slist
94
95
96@pytest.mark.parametrize(
97    'stat', itertools.chain(STATUSES, [status.SKIP, status.NOTRUN]))
98def test_lookup(stat):
99    status.status_lookup(stat)
100
101
102@pytest.mark.parametrize('new,old', REGRESSIONS)
103def test_regression(new, old):
104    assert status.status_lookup(new) < status.status_lookup(old)
105
106
107@pytest.mark.parametrize('new,old', FIXES)
108def test_fixes(new, old):
109    assert status.status_lookup(new) > status.status_lookup(old)
110
111
112@pytest.mark.parametrize('new,old', itertools.permutations(STATUSES, 2))
113def test_changes(new, old):
114    assert status.status_lookup(new) != status.status_lookup(old)
115
116
117@pytest.mark.parametrize('new,old', itertools.permutations(NO_OPS, 2))
118def test_no_change(new, old):
119    new = status.status_lookup(new)
120    old = status.status_lookup(old)
121    assert not new < old
122    assert not new > old
123
124
125@pytest.mark.parametrize("stat,op,expected", [
126    (status.Status('Test', 0, (0, 0)), str, 'Test'),
127    (status.Status('Test', 0, (0, 0)), bytes, b'Test'),
128    (status.Status('Test', 0, (0, 0)), int, 0),
129    (status.Status('Test', 0, (0, 0)), repr, 'Status("Test", 0, (0, 0))'),
130    (status.Status('Test', 0, (0, 0)), hash, hash('Test')),
131    (status.NoChangeStatus('No'), hash, hash('No')),
132])
133def test_status_comparisons(stat, op, expected):
134    """Test status.Status equality protocol."""
135    assert op(stat) == expected
136