|
1 | | -import pytest |
2 | | -import tests.common as common |
3 | | -import nomad |
4 | 1 | import os |
| 2 | + |
| 3 | +import mock |
| 4 | +import pytest |
| 5 | +import requests |
5 | 6 | import responses |
6 | 7 |
|
| 8 | +import nomad |
| 9 | +import tests.common as common |
| 10 | + |
7 | 11 |
|
8 | 12 | def test_base_region_qs(): |
9 | 13 | n = nomad.Nomad(host=common.IP, port=common.NOMAD_PORT, verify=False, token=common.NOMAD_TOKEN, region="random") |
@@ -46,6 +50,40 @@ def test_base_delete_connection_error(): |
46 | 50 | j = n.job.deregister_job("example") |
47 | 51 |
|
48 | 52 |
|
| 53 | +@mock.patch("nomad.api.base.requests.Session") |
| 54 | +def test_base_raise_exception_not_requests_response_object(mock_requests): |
| 55 | + mock_requests().delete.side_effect = [requests.RequestException()] |
| 56 | + |
| 57 | + with pytest.raises(nomad.api.exceptions.BaseNomadException) as excinfo: |
| 58 | + n = nomad.Nomad( |
| 59 | + host="162.16.10.102", |
| 60 | + port=common.NOMAD_PORT, |
| 61 | + timeout=0.001, |
| 62 | + verify=False |
| 63 | + ) |
| 64 | + |
| 65 | + _ = n.job.deregister_job("example") |
| 66 | + |
| 67 | + # excinfo is a ExceptionInfo instance, which is a wrapper around the actual exception raised. |
| 68 | + # The main attributes of interest are .type, .value and .traceback. |
| 69 | + # https://docs.pytest.org/en/3.0.1/assert.html#assertions-about-expected-exceptions |
| 70 | + assert hasattr(excinfo.value.nomad_resp, "text") is False |
| 71 | + assert isinstance(excinfo.value.nomad_resp, requests.RequestException) |
| 72 | + assert "raised due" in str(excinfo) |
| 73 | + |
| 74 | + |
| 75 | +def test_base_raise_exception_is_requests_response_object(nomad_setup): |
| 76 | + with pytest.raises(nomad.api.exceptions.BaseNomadException) as excinfo: |
| 77 | + _ = nomad_setup.job.get_job("examplezz") |
| 78 | + |
| 79 | + # excinfo is a ExceptionInfo instance, which is a wrapper around the actual exception raised. |
| 80 | + # The main attributes of interest are .type, .value and .traceback. |
| 81 | + # https://docs.pytest.org/en/3.0.1/assert.html#assertions-about-expected-exceptions |
| 82 | + assert hasattr(excinfo.value.nomad_resp, "text") is True |
| 83 | + assert isinstance(excinfo.value.nomad_resp, requests.Response) |
| 84 | + assert "raised with" in str(excinfo) |
| 85 | + |
| 86 | + |
49 | 87 | @pytest.mark.skipif(tuple(int(i) for i in os.environ.get("NOMAD_VERSION").split(".")) < (0, 7, 0), reason="Nomad dispatch not supported") |
50 | 88 | def test_base_get_connnection_not_authorized(): |
51 | 89 | n = nomad.Nomad( |
|
0 commit comments