|
8 | 8 | import mock |
9 | 9 | import os |
10 | 10 | import pytest |
| 11 | +from frozendict import frozendict |
11 | 12 |
|
12 | 13 | mozphab = imp.load_source( |
13 | 14 | "mozphab", os.path.join(os.path.dirname(__file__), os.path.pardir, "moz-phab") |
@@ -115,72 +116,105 @@ def test_check(m_open, m_os, m_ping, m_call): |
115 | 116 | assert not check() |
116 | 117 |
|
117 | 118 |
|
118 | | -@mock.patch("mozphab.ConduitAPI.call") |
119 | | -def test_get_revisions(m_call): |
120 | | - repo = mozphab.Repository("", "", "dummy") |
121 | | - mozphab.conduit.set_repo(repo) |
122 | | - get_revs = mozphab.conduit.get_revisions |
| 119 | +@pytest.fixture |
| 120 | +def get_revs(): |
| 121 | + mozphab.conduit.set_repo(mozphab.Repository("", "", "dummy")) |
| 122 | + return mozphab.conduit.get_revisions |
| 123 | + |
123 | 124 |
|
124 | | - # sanity checks |
| 125 | +@pytest.fixture |
| 126 | +def m_call(request): |
| 127 | + request.addfinalizer(mozphab.cache.reset) |
| 128 | + with mock.patch("mozphab.ConduitAPI.call") as xmock: |
| 129 | + yield xmock |
| 130 | + |
| 131 | + |
| 132 | +def test_get_revisions_both_ids_and_phids_fails(get_revs, m_call): |
125 | 133 | with pytest.raises(ValueError): |
126 | 134 | get_revs(ids=[1], phids=["PHID-1"]) |
| 135 | + |
| 136 | + |
| 137 | +def test_get_revisions_none_ids_fails(get_revs, m_call): |
127 | 138 | with pytest.raises(ValueError): |
128 | 139 | get_revs(ids=None) |
| 140 | + |
| 141 | + |
| 142 | +def test_get_revisions_none_phids_fails(get_revs, m_call): |
129 | 143 | with pytest.raises(ValueError): |
130 | 144 | get_revs(phids=None) |
131 | 145 |
|
132 | | - m_call.return_value = {"data": [dict(id=1, phid="PHID-1")]} |
133 | 146 |
|
134 | | - # differential.revision.search by revision-id |
| 147 | +basic_phab_result = frozendict({"data": [dict(id=1, phid="PHID-1")]}) |
| 148 | + |
| 149 | + |
| 150 | +def test_get_revisions_search_by_revid(get_revs, m_call): |
| 151 | + """differential.revision.search by revision-id""" |
| 152 | + m_call.return_value = basic_phab_result |
| 153 | + |
135 | 154 | assert len(get_revs(ids=[1])) == 1 |
136 | 155 | m_call.assert_called_with( |
137 | 156 | "differential.revision.search", |
138 | 157 | dict(constraints=dict(ids=[1]), attachments=dict(reviewers=True)), |
139 | 158 | ) |
140 | 159 |
|
141 | | - # differential.revision.search by phid |
142 | | - m_call.reset_mock() |
143 | | - mozphab.cache.reset() |
| 160 | + |
| 161 | +def test_get_revisions_search_by_phid(get_revs, m_call): |
| 162 | + """differential.revision.search by phid""" |
| 163 | + m_call.return_value = basic_phab_result |
| 164 | + |
144 | 165 | assert len(get_revs(phids=["PHID-1"])) == 1 |
145 | 166 | m_call.assert_called_with( |
146 | 167 | "differential.revision.search", |
147 | 168 | dict(constraints=dict(phids=["PHID-1"]), attachments=dict(reviewers=True)), |
148 | 169 | ) |
149 | 170 |
|
150 | | - # differential.revision.search by revision-id with duplicates |
151 | | - m_call.reset_mock() |
152 | | - mozphab.cache.reset() |
| 171 | + |
| 172 | +def test_get_revisions_search_by_revid_with_dups(get_revs, m_call): |
| 173 | + """differential.revision.search by revision-id with duplicates""" |
| 174 | + m_call.return_value = basic_phab_result |
| 175 | + |
153 | 176 | assert len(get_revs(ids=[1, 1])) == 2 |
154 | 177 | m_call.assert_called_with( |
155 | 178 | "differential.revision.search", |
156 | 179 | dict(constraints=dict(ids=[1]), attachments=dict(reviewers=True)), |
157 | 180 | ) |
158 | 181 |
|
159 | | - # differential.revision.search by phid with duplicates |
160 | | - m_call.reset_mock() |
161 | | - mozphab.cache.reset() |
| 182 | + |
| 183 | +def test_get_revisions_search_by_phid_with_dups(get_revs, m_call): |
| 184 | + """differential.revision.search by phid with duplicates""" |
| 185 | + m_call.return_value = basic_phab_result |
| 186 | + |
162 | 187 | assert len(get_revs(phids=["PHID-1", "PHID-1"])) == 2 |
163 | 188 | m_call.assert_called_with( |
164 | 189 | "differential.revision.search", |
165 | 190 | dict(constraints=dict(phids=["PHID-1"]), attachments=dict(reviewers=True)), |
166 | 191 | ) |
167 | 192 |
|
168 | | - # ordering of results must match input |
169 | | - m_call.reset_mock() |
170 | | - mozphab.cache.reset() |
171 | | - m_call.return_value = { |
| 193 | + |
| 194 | +multiple_phab_result = frozendict( |
| 195 | + { |
172 | 196 | "data": [ |
173 | 197 | dict(id=1, phid="PHID-1"), |
174 | 198 | dict(id=2, phid="PHID-2"), |
175 | 199 | dict(id=3, phid="PHID-3"), |
176 | 200 | ] |
177 | 201 | } |
| 202 | +) |
| 203 | + |
| 204 | + |
| 205 | +def test_get_revisions_search_by_revids_ordering(get_revs, m_call): |
| 206 | + """ordering of results must match input when querying by revids""" |
| 207 | + m_call.return_value = multiple_phab_result |
178 | 208 | assert get_revs(ids=[2, 1, 3]) == [ |
179 | 209 | dict(id=2, phid="PHID-2"), |
180 | 210 | dict(id=1, phid="PHID-1"), |
181 | 211 | dict(id=3, phid="PHID-3"), |
182 | 212 | ] |
183 | 213 |
|
| 214 | + |
| 215 | +def test_get_revisions_search_by_phids_ordering(get_revs, m_call): |
| 216 | + """ordering of results must match input when querying by phids""" |
| 217 | + m_call.return_value = multiple_phab_result |
184 | 218 | assert get_revs(phids=["PHID-2", "PHID-1", "PHID-3"]) == [ |
185 | 219 | dict(id=2, phid="PHID-2"), |
186 | 220 | dict(id=1, phid="PHID-1"), |
|
0 commit comments