tests.helper.assert_called()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2014-2021 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: AGPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU Affero General Public License as
8
# published by the Free Software Foundation, either version 3 of the
9
# License, or (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU Affero General Public License for more details.
15
#
16
# You should have received a copy of the GNU Affero General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19
20
from unittest.mock import Mock
21
22
23
def assert_called_once(mock: Mock):
24
    if hasattr(mock, 'assert_called_once'):
25
        return mock.assert_called_once()
26
27
    if not mock.call_count == 1:
28
        msg = "Expected '%s' to have been called once. Called %s times.%s" % (
29
            mock._mock_name or 'mock',  # pylint: disable=protected-access
30
            mock.call_count,
31
            mock._calls_repr(),  # pylint: disable=protected-access
32
        )
33
        raise AssertionError(msg)
34
35
36
def assert_called(mock: Mock):
37
    """assert that the mock was called at least once"""
38
    if mock.call_count == 0:
39
        msg = "Expected '%s' to have been called." % (
40
            mock._mock_name or 'mock'  # pylint: disable=protected-access
41
        )
42
        raise AssertionError(msg)
43