-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtest_get_board.py
154 lines (110 loc) · 5.47 KB
/
test_get_board.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#
# Copyright (c) 2020-2021 Arm Limited and Contributors. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
"""Tests for `mbed_tools.targets.get_board`."""
import pytest
from unittest import mock
from mbed_tools.targets._internal.exceptions import BoardAPIError
# Import from top level as this is the expected interface for users
from mbed_tools.targets import get_board_by_online_id, get_board_by_product_code, get_board_by_jlink_slug
from mbed_tools.targets.get_board import (
_DatabaseMode,
_get_database_mode,
get_board,
)
from mbed_tools.targets.env import env
from mbed_tools.targets.exceptions import UnknownBoard, UnsupportedMode
from tests.targets.factories import make_board
@pytest.fixture
def mock_get_board():
with mock.patch("mbed_tools.targets.get_board.get_board", autospec=True) as gbp:
yield gbp
@pytest.fixture
def mock_env():
with mock.patch("mbed_tools.targets.get_board.env", spec_set=env) as gbp:
yield gbp
@pytest.fixture
def mocked_boards():
with mock.patch("mbed_tools.targets.get_board.Boards", autospec=True) as gbp:
yield gbp
class TestGetBoard:
def test_online_mode(self, mock_env, mocked_boards):
mock_env.MBED_DATABASE_MODE = "ONLINE"
fn = mock.Mock()
subject = get_board(fn)
assert subject == mocked_boards.from_online_database().get_board.return_value
mocked_boards.from_online_database().get_board.assert_called_once_with(fn)
def test_offline_mode(self, mock_env, mocked_boards):
mock_env.MBED_DATABASE_MODE = "OFFLINE"
fn = mock.Mock()
subject = get_board(fn)
assert subject == mocked_boards.from_offline_database().get_board.return_value
mocked_boards.from_offline_database().get_board.assert_called_once_with(fn)
def test_auto_mode_calls_offline_boards_first(self, mock_env, mocked_boards):
mock_env.MBED_DATABASE_MODE = "AUTO"
fn = mock.Mock()
subject = get_board(fn)
assert subject == mocked_boards.from_offline_database().get_board.return_value
mocked_boards.from_online_database().get_board.assert_not_called()
mocked_boards.from_offline_database().get_board.assert_called_once_with(fn)
def test_auto_mode_falls_back_to_online_database_when_board_not_found(self, mock_env, mocked_boards):
mock_env.MBED_DATABASE_MODE = "AUTO"
mocked_boards.from_offline_database().get_board.side_effect = UnknownBoard
fn = mock.Mock()
subject = get_board(fn)
assert subject == mocked_boards.from_online_database().get_board.return_value
mocked_boards.from_offline_database().get_board.assert_called_once_with(fn)
mocked_boards.from_online_database().get_board.assert_called_once_with(fn)
def test_auto_mode_raises_when_board_not_found_offline_with_no_network(self, mock_env, mocked_boards):
mock_env.MBED_DATABASE_MODE = "AUTO"
mocked_boards.from_offline_database().get_board.side_effect = UnknownBoard
mocked_boards.from_online_database().get_board.side_effect = BoardAPIError
fn = mock.Mock()
with pytest.raises(UnknownBoard):
get_board(fn)
mocked_boards.from_offline_database().get_board.assert_called_once_with(fn)
mocked_boards.from_online_database().get_board.assert_called_once_with(fn)
class TestGetBoardByProductCode:
def test_matches_boards_by_product_code(self, mock_get_board):
product_code = "swag"
assert get_board_by_product_code(product_code) == mock_get_board.return_value
# Test callable matches correct boards
fn = mock_get_board.call_args[0][0]
matching_board = make_board(product_code=product_code)
not_matching_board = make_board(product_code="whatever")
assert fn(matching_board)
assert not fn(not_matching_board)
class TestGetBoardByOnlineId:
def test_matches_boards_by_online_id(self, mock_get_board):
target_type = "platform"
assert get_board_by_online_id(slug="slug", target_type=target_type) == mock_get_board.return_value
# Test callable matches correct boards
fn = mock_get_board.call_args[0][0]
matching_board_1 = make_board(target_type=target_type, slug="slug")
matching_board_2 = make_board(target_type=target_type, slug="SlUg")
not_matching_board = make_board(target_type=target_type, slug="whatever")
assert fn(matching_board_1)
assert fn(matching_board_2)
assert not fn(not_matching_board)
class TestGetBoardByJlinkSlug:
def test_matches_boards_by_online_id(self, mock_get_board):
assert get_board_by_jlink_slug(slug="slug") == mock_get_board.return_value
# Test callable matches correct boards
fn = mock_get_board.call_args[0][0]
matching_board_1 = make_board(slug="slug")
matching_board_2 = make_board(board_type="slug")
matching_board_3 = make_board(board_name="slug")
not_matching_board = make_board()
assert fn(matching_board_1)
assert fn(matching_board_2)
assert fn(matching_board_3)
assert not fn(not_matching_board)
class TestGetDatabaseMode:
def test_returns_configured_database_mode(self, mock_env):
mock_env.MBED_DATABASE_MODE = "OFFLINE"
assert _get_database_mode() == _DatabaseMode.OFFLINE
def test_raises_when_configuration_is_not_supported(self, mock_env):
mock_env.MBED_DATABASE_MODE = "NOT_VALID"
with pytest.raises(UnsupportedMode):
_get_database_mode()