|
| 1 | +from unittest.mock import MagicMock |
| 2 | +from uuid import UUID |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from pyoaev import OpenAEV |
| 7 | +from pyoaev.apis.inputs.search import Filter, FilterGroup, SearchPaginationInput |
| 8 | + |
| 9 | + |
| 10 | +class MockResponse: |
| 11 | + def __init__(self, json_data=None, status_code=200): |
| 12 | + self._json_data = json_data |
| 13 | + self.status_code = status_code |
| 14 | + self.history = None |
| 15 | + self.content = None |
| 16 | + self.headers = {"Content-Type": "application/json"} |
| 17 | + |
| 18 | + def json(self): |
| 19 | + return self._json_data or {} |
| 20 | + |
| 21 | + |
| 22 | +def build_search_input(): |
| 23 | + return SearchPaginationInput( |
| 24 | + 0, |
| 25 | + 20, |
| 26 | + FilterGroup( |
| 27 | + "or", |
| 28 | + [ |
| 29 | + Filter( |
| 30 | + "targets", |
| 31 | + "and", |
| 32 | + "eq", |
| 33 | + ["target_1", "target_2", "target_3"], |
| 34 | + ) |
| 35 | + ], |
| 36 | + ), |
| 37 | + None, |
| 38 | + None, |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.parametrize( |
| 43 | + "tenant_id, expected_url", |
| 44 | + [ |
| 45 | + ( |
| 46 | + None, |
| 47 | + "url/api/endpoints/targets", |
| 48 | + ), |
| 49 | + ( |
| 50 | + UUID("2cffad3a-0001-4078-b0e2-ef74274022c3"), |
| 51 | + "url/api/tenants/2cffad3a-0001-4078-b0e2-ef74274022c3/endpoints/targets", |
| 52 | + ), |
| 53 | + ], |
| 54 | + ids=[ |
| 55 | + "legacy_routing_no_tenant", |
| 56 | + "tenant_routing_enabled", |
| 57 | + ], |
| 58 | +) |
| 59 | +def test_search_input_correctly_serialised(monkeypatch, tenant_id, expected_url): |
| 60 | + mock_request = MagicMock(return_value=MockResponse()) |
| 61 | + monkeypatch.setattr("requests.Session.request", mock_request) |
| 62 | + |
| 63 | + api_client = OpenAEV("url", "token", tenant_id=tenant_id) |
| 64 | + search_input = build_search_input() |
| 65 | + expected_json = search_input.to_dict() |
| 66 | + api_client.endpoint.searchTargets(search_input) |
| 67 | + |
| 68 | + assert mock_request.call_count == 1 |
| 69 | + _, kwargs = mock_request.call_args |
| 70 | + |
| 71 | + assert kwargs["method"] == "post" |
| 72 | + assert kwargs["json"] == expected_json |
| 73 | + assert kwargs["url"] == expected_url |
0 commit comments