File: test_api.py

package info (click to toggle)
git-pw 2.0.0-2
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 480 kB
  • sloc: python: 1,972; makefile: 4
file content (135 lines) | stat: -rw-r--r-- 3,488 bytes parent folder | download
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
"""Unit tests for ``git_pw/api.py``."""

import mock
import pytest

from git_pw import api


@mock.patch.object(api, 'LOG')
@mock.patch.object(api, 'CONF')
def test_get_server_undefined(mock_conf, mock_log):
    mock_conf.server = None

    with pytest.raises(SystemExit):
        api._get_server()

    assert mock_log.error.called


@mock.patch.object(api, 'LOG')
@mock.patch.object(api, 'CONF')
def test_get_server_missing_version(mock_conf, mock_log):
    mock_conf.server = 'https://example.com/api'

    server = api._get_server()

    assert mock_log.warning.called
    assert server == 'https://example.com/api'


@mock.patch.object(api, 'LOG')
@mock.patch.object(api, 'CONF')
def test_get_server_missing_version_and_path(mock_conf, mock_log):
    mock_conf.server = 'https://example.com/'

    server = api._get_server()

    assert mock_log.warning.called
    assert server == 'https://example.com/api'


@mock.patch.object(api, 'LOG')
@mock.patch.object(api, 'CONF')
def test_get_project_undefined(mock_conf, mock_log):
    mock_conf.project = None

    with pytest.raises(SystemExit):
        api._get_project()

    assert mock_log.error.called


@mock.patch.object(api, 'CONF')
def test_get_project_wildcard(mock_conf):
    mock_conf.project = '*'

    project = api._get_project()

    assert project == ''


@mock.patch.object(api, '_get_server')
def test_version_missing(mock_server):
    mock_server.return_value = 'https://example.com/api'

    assert api.version() == (1, 0)


@mock.patch.object(api, '_get_server')
def test_version(mock_server):
    mock_server.return_value = 'https://example.com/api/1.1'

    assert api.version() == (1, 1)


@mock.patch.object(api, 'index')
def test_retrieve_filter_ids_too_short(mock_index):
    with pytest.raises(SystemExit):
        api.retrieve_filter_ids('users', 'owner', 'f')

    assert not mock_index.called


@mock.patch.object(api, 'LOG')
@mock.patch.object(api, 'index')
def test_retrieve_filter_ids_no_matches(mock_index, mock_log):
    mock_index.return_value = []

    ids = api.retrieve_filter_ids('users', 'owner', 'foo')

    assert mock_log.warning.called
    assert ids == []


@mock.patch.object(api, 'LOG')
@mock.patch.object(api, 'version')
@mock.patch.object(api, 'index')
def test_retrieve_filter_ids_multiple_matches_1_0(mock_index, mock_version,
                                                  mock_log):
    mock_index.return_value = [
        {'id': 1}, {'id': 2},  # incomplete but good enough
    ]
    mock_version.return_value = (1, 0)

    ids = api.retrieve_filter_ids('users', 'owner', 'foo')

    assert mock_log.warning.called
    assert ids == [('owner', 1), ('owner', 2)]


@mock.patch.object(api, 'LOG')
@mock.patch.object(api, 'version')
@mock.patch.object(api, 'index')
def test_retrieve_filter_ids_multiple_matches_1_1(mock_index, mock_version,
                                                  mock_log):
    mock_index.return_value = [
        {'id': 1}, {'id': 2},  # incomplete but good enough
    ]
    mock_version.return_value = (1, 1)

    ids = api.retrieve_filter_ids('users', 'owner', 'foo')

    assert not mock_log.warning.called
    assert ids == [('owner', 1), ('owner', 2)]


@mock.patch.object(api, 'LOG')
@mock.patch.object(api, 'index')
def test_retrieve_filter_ids(mock_index, mock_log):
    mock_index.return_value = [{'id': 1}]

    ids = api.retrieve_filter_ids('users', 'owner', 'foo')

    assert not mock_log.warning.called
    assert ids == [('owner', 1)]