|
| 1 | +import io |
| 2 | + |
| 3 | +import pytest |
1 | 4 | import requests |
2 | 5 | import responses |
3 | 6 |
|
| 7 | +from unittest import mock |
| 8 | + |
4 | 9 | from posit.connect import Client |
5 | 10 | from posit.connect.config import Config |
6 | 11 | from posit.connect.bundles import Bundle |
7 | 12 |
|
8 | | -from .api import load_mock # type: ignore |
| 13 | +from .api import load_mock, get_path # type: ignore |
9 | 14 |
|
10 | 15 |
|
11 | 16 | class TestBundleProperties: |
@@ -119,6 +124,216 @@ def test(self): |
119 | 124 | assert mock_bundle_delete.call_count == 1 |
120 | 125 |
|
121 | 126 |
|
| 127 | +class TestBundleDownload: |
| 128 | + @mock.patch("builtins.open", new_callable=mock.mock_open) |
| 129 | + @responses.activate |
| 130 | + def test_output_as_str(self, mock_file: mock.MagicMock): |
| 131 | + content_guid = "f2f37341-e21d-3d80-c698-a935ad614066" |
| 132 | + bundle_id = "101" |
| 133 | + path = get_path( |
| 134 | + f"v1/content/{content_guid}/bundles/{bundle_id}/download/bundle.tar.gz" |
| 135 | + ) |
| 136 | + |
| 137 | + # behavior |
| 138 | + mock_content_get = responses.get( |
| 139 | + f"https://connect.example/__api__/v1/content/{content_guid}", |
| 140 | + json=load_mock(f"v1/content/{content_guid}.json"), |
| 141 | + ) |
| 142 | + |
| 143 | + mock_bundle_get = responses.get( |
| 144 | + f"https://connect.example/__api__/v1/content/{content_guid}/bundles/{bundle_id}", |
| 145 | + json=load_mock( |
| 146 | + f"v1/content/{content_guid}/bundles/{bundle_id}.json" |
| 147 | + ), |
| 148 | + ) |
| 149 | + |
| 150 | + mock_bundle_download = responses.get( |
| 151 | + f"https://connect.example/__api__/v1/content/{content_guid}/bundles/{bundle_id}/download", |
| 152 | + body=path.read_bytes(), |
| 153 | + ) |
| 154 | + |
| 155 | + # setup |
| 156 | + c = Client("12345", "https://connect.example") |
| 157 | + bundle = c.content.get(content_guid).bundles.get(bundle_id) |
| 158 | + |
| 159 | + # invoke |
| 160 | + bundle.download("pathname") |
| 161 | + |
| 162 | + # assert |
| 163 | + assert mock_content_get.call_count == 1 |
| 164 | + assert mock_bundle_get.call_count == 1 |
| 165 | + assert mock_bundle_download.call_count == 1 |
| 166 | + mock_file.assert_called_once_with("pathname", "wb") |
| 167 | + |
| 168 | + @responses.activate |
| 169 | + def test_output_as_io(self): |
| 170 | + content_guid = "f2f37341-e21d-3d80-c698-a935ad614066" |
| 171 | + bundle_id = "101" |
| 172 | + path = get_path( |
| 173 | + f"v1/content/{content_guid}/bundles/{bundle_id}/download/bundle.tar.gz" |
| 174 | + ) |
| 175 | + |
| 176 | + # behavior |
| 177 | + mock_content_get = responses.get( |
| 178 | + f"https://connect.example/__api__/v1/content/{content_guid}", |
| 179 | + json=load_mock(f"v1/content/{content_guid}.json"), |
| 180 | + ) |
| 181 | + |
| 182 | + mock_bundle_get = responses.get( |
| 183 | + f"https://connect.example/__api__/v1/content/{content_guid}/bundles/{bundle_id}", |
| 184 | + json=load_mock( |
| 185 | + f"v1/content/{content_guid}/bundles/{bundle_id}.json" |
| 186 | + ), |
| 187 | + ) |
| 188 | + |
| 189 | + mock_bundle_download = responses.get( |
| 190 | + f"https://connect.example/__api__/v1/content/{content_guid}/bundles/{bundle_id}/download", |
| 191 | + body=path.read_bytes(), |
| 192 | + ) |
| 193 | + |
| 194 | + # setup |
| 195 | + c = Client("12345", "https://connect.example") |
| 196 | + bundle = c.content.get(content_guid).bundles.get(bundle_id) |
| 197 | + |
| 198 | + # invoke |
| 199 | + file = io.BytesIO() |
| 200 | + buffer = io.BufferedWriter(file) |
| 201 | + bundle.download(buffer) |
| 202 | + buffer.seek(0) |
| 203 | + |
| 204 | + # assert |
| 205 | + assert mock_content_get.call_count == 1 |
| 206 | + assert mock_bundle_get.call_count == 1 |
| 207 | + assert mock_bundle_download.call_count == 1 |
| 208 | + assert file.read() == path.read_bytes() |
| 209 | + |
| 210 | + @responses.activate |
| 211 | + def test_invalid_arguments(self): |
| 212 | + content_guid = "f2f37341-e21d-3d80-c698-a935ad614066" |
| 213 | + bundle_id = "101" |
| 214 | + path = get_path( |
| 215 | + f"v1/content/{content_guid}/bundles/{bundle_id}/download/bundle.tar.gz" |
| 216 | + ) |
| 217 | + |
| 218 | + # behavior |
| 219 | + mock_content_get = responses.get( |
| 220 | + f"https://connect.example/__api__/v1/content/{content_guid}", |
| 221 | + json=load_mock(f"v1/content/{content_guid}.json"), |
| 222 | + ) |
| 223 | + |
| 224 | + mock_bundle_get = responses.get( |
| 225 | + f"https://connect.example/__api__/v1/content/{content_guid}/bundles/{bundle_id}", |
| 226 | + json=load_mock( |
| 227 | + f"v1/content/{content_guid}/bundles/{bundle_id}.json" |
| 228 | + ), |
| 229 | + ) |
| 230 | + |
| 231 | + mock_bundle_download = responses.get( |
| 232 | + f"https://connect.example/__api__/v1/content/{content_guid}/bundles/{bundle_id}/download", |
| 233 | + body=path.read_bytes(), |
| 234 | + ) |
| 235 | + |
| 236 | + # setup |
| 237 | + c = Client("12345", "https://connect.example") |
| 238 | + bundle = c.content.get(content_guid).bundles.get(bundle_id) |
| 239 | + |
| 240 | + # invoke |
| 241 | + with pytest.raises(TypeError): |
| 242 | + bundle.download(None) |
| 243 | + |
| 244 | + # assert |
| 245 | + assert mock_content_get.call_count == 1 |
| 246 | + assert mock_bundle_get.call_count == 1 |
| 247 | + |
| 248 | + |
| 249 | +class TestBundlesCreate: |
| 250 | + @responses.activate |
| 251 | + def test(self): |
| 252 | + content_guid = "f2f37341-e21d-3d80-c698-a935ad614066" |
| 253 | + bundle_id = "101" |
| 254 | + pathname = get_path( |
| 255 | + f"v1/content/{content_guid}/bundles/{bundle_id}/download/bundle.tar.gz" |
| 256 | + ) |
| 257 | + |
| 258 | + # behavior |
| 259 | + mock_content_get = responses.get( |
| 260 | + f"https://connect.example/__api__/v1/content/{content_guid}", |
| 261 | + json=load_mock(f"v1/content/{content_guid}.json"), |
| 262 | + ) |
| 263 | + |
| 264 | + mock_bundle_post = responses.post( |
| 265 | + f"https://connect.example/__api__/v1/content/{content_guid}/bundles", |
| 266 | + json=load_mock( |
| 267 | + f"v1/content/{content_guid}/bundles/{bundle_id}.json" |
| 268 | + ), |
| 269 | + ) |
| 270 | + |
| 271 | + # setup |
| 272 | + c = Client("12345", "https://connect.example") |
| 273 | + content = c.content.get(content_guid) |
| 274 | + |
| 275 | + # invoke |
| 276 | + data = pathname.read_bytes() |
| 277 | + bundle = content.bundles.create(data) |
| 278 | + |
| 279 | + # # assert |
| 280 | + assert bundle.id == "101" |
| 281 | + assert mock_content_get.call_count == 1 |
| 282 | + assert mock_bundle_post.call_count == 1 |
| 283 | + |
| 284 | + @responses.activate |
| 285 | + def test_kwargs_pathname(self): |
| 286 | + content_guid = "f2f37341-e21d-3d80-c698-a935ad614066" |
| 287 | + bundle_id = "101" |
| 288 | + pathname = get_path( |
| 289 | + f"v1/content/{content_guid}/bundles/{bundle_id}/download/bundle.tar.gz" |
| 290 | + ) |
| 291 | + |
| 292 | + # behavior |
| 293 | + mock_content_get = responses.get( |
| 294 | + f"https://connect.example/__api__/v1/content/{content_guid}", |
| 295 | + json=load_mock(f"v1/content/{content_guid}.json"), |
| 296 | + ) |
| 297 | + |
| 298 | + mock_bundle_post = responses.post( |
| 299 | + f"https://connect.example/__api__/v1/content/{content_guid}/bundles", |
| 300 | + json=load_mock( |
| 301 | + f"v1/content/{content_guid}/bundles/{bundle_id}.json" |
| 302 | + ), |
| 303 | + ) |
| 304 | + |
| 305 | + # setup |
| 306 | + c = Client("12345", "https://connect.example") |
| 307 | + content = c.content.get(content_guid) |
| 308 | + |
| 309 | + # invoke |
| 310 | + pathname = str(pathname.absolute()) |
| 311 | + bundle = content.bundles.create(pathname) |
| 312 | + |
| 313 | + # # assert |
| 314 | + assert bundle.id == "101" |
| 315 | + assert mock_content_get.call_count == 1 |
| 316 | + assert mock_bundle_post.call_count == 1 |
| 317 | + |
| 318 | + @responses.activate |
| 319 | + def test_invalid_arguments(self): |
| 320 | + content_guid = "f2f37341-e21d-3d80-c698-a935ad614066" |
| 321 | + |
| 322 | + # behavior |
| 323 | + responses.get( |
| 324 | + f"https://connect.example/__api__/v1/content/{content_guid}", |
| 325 | + json=load_mock(f"v1/content/{content_guid}.json"), |
| 326 | + ) |
| 327 | + |
| 328 | + # setup |
| 329 | + c = Client("12345", "https://connect.example") |
| 330 | + content = c.content.get(content_guid) |
| 331 | + |
| 332 | + # invoke |
| 333 | + with pytest.raises(TypeError): |
| 334 | + content.bundles.create(None) |
| 335 | + |
| 336 | + |
122 | 337 | class TestBundlesFind: |
123 | 338 | @responses.activate |
124 | 339 | def test(self): |
|
0 commit comments