|
| 1 | +import json |
1 | 2 | import logging |
2 | 3 |
|
| 4 | +import pytest |
3 | 5 | import requests |
4 | 6 |
|
5 | 7 | from src.config import Config |
|
11 | 13 | configure_logging(level=logging.DEBUG) |
12 | 14 |
|
13 | 15 |
|
14 | | -def test_opensearch_search_engine(monkeypatch): |
15 | | - config = Config.load("tests/unit/resources/good_config_opensearch.yaml") |
16 | | - search_engine = OpenSearchEngine("http://testurl/testcore") |
| 16 | +@pytest.fixture |
| 17 | +def opensearch_config(): |
| 18 | + """Fixture that loads a valid OpenSearch config for unit tests.""" |
| 19 | + return Config.load("tests/unit/resources/good_config_opensearch.yaml") |
17 | 20 |
|
18 | | - doc = { |
19 | | - "id": "1", |
20 | | - "title": "test title", |
21 | | - "description": "test description" |
22 | | - } |
23 | 21 |
|
24 | | - hit = { |
| 22 | +@pytest.fixture |
| 23 | +def opensearch_hit(): |
| 24 | + return { |
25 | 25 | "_index": "testcore", |
26 | 26 | "_id": "1", |
27 | 27 | "_score": 1.0, |
28 | | - "_source": doc |
| 28 | + "_source": { |
| 29 | + "id": "1", |
| 30 | + "title": "test title", |
| 31 | + "description": "test description" |
| 32 | + } |
29 | 33 | } |
30 | 34 |
|
| 35 | + |
| 36 | +def test_fetch_for_query_generation(monkeypatch, opensearch_config, opensearch_hit): |
| 37 | + opensearch = OpenSearchEngine("http://testurl/testcore") |
| 38 | + |
31 | 39 | expected_doc = Document( |
32 | | - id=doc["id"], |
33 | | - fields={k: search_engine._normalize(v) for k, v in doc.items() if k != "id"} |
| 40 | + id="1", |
| 41 | + fields={ |
| 42 | + "title": opensearch._normalize("test title"), |
| 43 | + "description": opensearch._normalize("test description") |
| 44 | + } |
| 45 | + ) |
| 46 | + |
| 47 | + def mock_post(*args, **kwargs): |
| 48 | + return MockResponseOpenSearchEngine([opensearch_hit], status_code=200) |
| 49 | + |
| 50 | + monkeypatch.setattr(requests, "post", mock_post) |
| 51 | + |
| 52 | + result = opensearch.fetch_for_query_generation( |
| 53 | + documents_filter=opensearch_config.documents_filter, |
| 54 | + doc_number=opensearch_config.doc_number, |
| 55 | + doc_fields=opensearch_config.doc_fields |
34 | 56 | ) |
35 | 57 |
|
36 | | - def post(*args, **kwargs): |
37 | | - return MockResponseOpenSearchEngine([hit], status_code=200) |
| 58 | + assert len(result) == 1, "Expected one document" |
| 59 | + assert result[0] == expected_doc, "Mismatch in query generated doc" |
38 | 60 |
|
39 | | - monkeypatch.setattr(requests, "post", post) |
40 | 61 |
|
41 | | - result = search_engine.fetch_for_query_generation( |
42 | | - documents_filter=config.documents_filter, |
43 | | - doc_number=config.doc_number, |
44 | | - doc_fields=config.doc_fields |
| 62 | +def test_fetch_for_evaluation(monkeypatch, opensearch_config, opensearch_hit): |
| 63 | + opensearch = OpenSearchEngine("http://testurl/testcore") |
| 64 | + |
| 65 | + expected_doc = Document( |
| 66 | + id="1", |
| 67 | + fields={ |
| 68 | + "title": opensearch._normalize("test title"), |
| 69 | + "description": opensearch._normalize("test description") |
| 70 | + } |
45 | 71 | ) |
46 | 72 |
|
47 | | - assert len(result) == 1 |
48 | | - assert result[0] == expected_doc |
| 73 | + def mock_post(*args, **kwargs): |
| 74 | + return MockResponseOpenSearchEngine([opensearch_hit], status_code=200) |
49 | 75 |
|
50 | | - result = search_engine.fetch_for_evaluation( |
| 76 | + monkeypatch.setattr(requests, "post", mock_post) |
| 77 | + |
| 78 | + result = opensearch.fetch_for_evaluation( |
| 79 | + query_template=opensearch_config.query_template, |
51 | 80 | keyword="car", |
52 | | - query_template=config.query_template, |
53 | | - doc_fields=config.doc_fields |
| 81 | + doc_fields=opensearch_config.doc_fields |
54 | 82 | ) |
55 | 83 |
|
56 | | - assert len(result) == 1 |
57 | | - assert result[0] == expected_doc |
| 84 | + assert len(result) == 1, "Expected one document" |
| 85 | + assert result[0] == expected_doc, "Mismatch in doc evaluation" |
| 86 | + |
| 87 | + |
| 88 | +def test_normalize(): |
| 89 | + engine = OpenSearchEngine("http://dummy") |
| 90 | + |
| 91 | + assert engine._normalize(" hello ") == ["hello"], "Failed to normalize string" |
| 92 | + assert engine._normalize(["a", " b "]) == ["a", "b"], "Failed to normalize list of strings" |
| 93 | + assert engine._normalize(123) == ["123"], "Failed to normalize integer" |
| 94 | + assert engine._normalize(None) == [], "Failed to normalize None" |
| 95 | + assert engine._normalize({"key": " value "}) == [json.dumps({"key": "value"})], "Failed to normalize dict" |
| 96 | + |
0 commit comments