|
| 1 | +from unittest import mock |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from search.solrapi import SolrAPIError, SolrManagementAPI |
| 6 | + |
| 7 | + |
| 8 | +def _mock_response(json_payload): |
| 9 | + resp = mock.Mock() |
| 10 | + resp.json.return_value = json_payload |
| 11 | + resp.raise_for_status = mock.Mock() |
| 12 | + return resp |
| 13 | + |
| 14 | + |
| 15 | +@mock.patch("search.solrapi.requests") |
| 16 | +def test_upsert_request_handler_adds_when_absent(mock_requests): |
| 17 | + # Successful add → 200 with no errorMessages. |
| 18 | + mock_requests.post.return_value = _mock_response({}) |
| 19 | + |
| 20 | + api = SolrManagementAPI("http://search:8983", "freesound1234") |
| 21 | + api.upsert_request_handler("/select_similarity", "solr.SearchHandler") |
| 22 | + |
| 23 | + assert mock_requests.post.call_count == 1 |
| 24 | + _, kwargs = mock_requests.post.call_args |
| 25 | + assert kwargs["json"] == {"add-requesthandler": {"name": "/select_similarity", "class": "solr.SearchHandler"}} |
| 26 | + |
| 27 | + |
| 28 | +@mock.patch("search.solrapi.requests") |
| 29 | +def test_upsert_request_handler_falls_back_to_update_on_duplicate(mock_requests): |
| 30 | + # Solr returns 200 with errorMessages when the handler already exists; we |
| 31 | + # then retry with update, which succeeds. |
| 32 | + mock_requests.post.side_effect = [ |
| 33 | + _mock_response({"errorMessages": [{"add-requesthandler": "'/select_similarity' already exists"}]}), |
| 34 | + _mock_response({}), |
| 35 | + ] |
| 36 | + |
| 37 | + api = SolrManagementAPI("http://search:8983", "freesound1234") |
| 38 | + api.upsert_request_handler("/select_similarity", "solr.SearchHandler") |
| 39 | + |
| 40 | + assert mock_requests.post.call_count == 2 |
| 41 | + second_call_json = mock_requests.post.call_args_list[1].kwargs["json"] |
| 42 | + assert second_call_json == {"update-requesthandler": {"name": "/select_similarity", "class": "solr.SearchHandler"}} |
| 43 | + |
| 44 | + |
| 45 | +@mock.patch("search.solrapi.requests") |
| 46 | +def test_upsert_request_handler_raises_when_update_also_fails(mock_requests): |
| 47 | + # Both add and update return errorMessages → surface as SolrAPIError. |
| 48 | + mock_requests.post.side_effect = [ |
| 49 | + _mock_response({"errorMessages": [{"add-requesthandler": "boom"}]}), |
| 50 | + _mock_response({"errorMessages": [{"update-requesthandler": "boom too"}]}), |
| 51 | + ] |
| 52 | + |
| 53 | + api = SolrManagementAPI("http://search:8983", "freesound1234") |
| 54 | + with pytest.raises(SolrAPIError): |
| 55 | + api.upsert_request_handler("/select_similarity", "solr.SearchHandler") |
| 56 | + |
| 57 | + |
| 58 | +@mock.patch("search.solrapi.requests") |
| 59 | +def test_upsert_request_handler_includes_defaults(mock_requests): |
| 60 | + mock_requests.post.return_value = _mock_response({}) |
| 61 | + |
| 62 | + api = SolrManagementAPI("http://search:8983", "freesound1234") |
| 63 | + api.upsert_request_handler("/select_similarity", "solr.SearchHandler", defaults={"defType": "dismax"}) |
| 64 | + |
| 65 | + _, kwargs = mock_requests.post.call_args |
| 66 | + assert kwargs["json"]["add-requesthandler"]["defaults"] == {"defType": "dismax"} |
0 commit comments