|
| 1 | +"""Fixtures for the synonym set tests.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +import requests |
| 5 | + |
| 6 | +from typesense.api_call import ApiCall |
| 7 | +from typesense.synonym_set import SynonymSet |
| 8 | +from typesense.synonym_sets import SynonymSets |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture(scope="function", name="create_synonym_set") |
| 12 | +def create_synonym_set_fixture() -> None: |
| 13 | + """Create a synonym set in the Typesense server.""" |
| 14 | + url = "http://localhost:8108/synonym_sets/test-set" |
| 15 | + headers = {"X-TYPESENSE-API-KEY": "xyz"} |
| 16 | + data = { |
| 17 | + "items": [ |
| 18 | + { |
| 19 | + "id": "company_synonym", |
| 20 | + "synonyms": ["companies", "corporations", "firms"], |
| 21 | + } |
| 22 | + ] |
| 23 | + } |
| 24 | + |
| 25 | + resp = requests.put(url, headers=headers, json=data, timeout=3) |
| 26 | + resp.raise_for_status() |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture(scope="function", name="delete_all_synonym_sets") |
| 30 | +def clear_typesense_synonym_sets() -> None: |
| 31 | + """Remove all synonym sets from the Typesense server.""" |
| 32 | + url = "http://localhost:8108/synonym_sets" |
| 33 | + headers = {"X-TYPESENSE-API-KEY": "xyz"} |
| 34 | + |
| 35 | + # Get the list of synonym sets |
| 36 | + response = requests.get(url, headers=headers, timeout=3) |
| 37 | + response.raise_for_status() |
| 38 | + data = response.json() |
| 39 | + |
| 40 | + # Delete each synonym set |
| 41 | + for synset in data: |
| 42 | + name = synset.get("name") |
| 43 | + if not name: |
| 44 | + continue |
| 45 | + delete_url = f"{url}/{name}" |
| 46 | + delete_response = requests.delete(delete_url, headers=headers, timeout=3) |
| 47 | + delete_response.raise_for_status() |
| 48 | + |
| 49 | + |
| 50 | +@pytest.fixture(scope="function", name="actual_synonym_sets") |
| 51 | +def actual_synonym_sets_fixture(actual_api_call: ApiCall) -> SynonymSets: |
| 52 | + """Return a SynonymSets object using a real API.""" |
| 53 | + return SynonymSets(actual_api_call) |
| 54 | + |
| 55 | + |
| 56 | +@pytest.fixture(scope="function", name="actual_synonym_set") |
| 57 | +def actual_synonym_set_fixture(actual_api_call: ApiCall) -> SynonymSet: |
| 58 | + """Return a SynonymSet object using a real API.""" |
| 59 | + return SynonymSet(actual_api_call, "test-set") |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture(scope="function", name="fake_synonym_sets") |
| 63 | +def fake_synonym_sets_fixture(fake_api_call: ApiCall) -> SynonymSets: |
| 64 | + """Return a SynonymSets object with test values.""" |
| 65 | + return SynonymSets(fake_api_call) |
| 66 | + |
| 67 | + |
| 68 | +@pytest.fixture(scope="function", name="fake_synonym_set") |
| 69 | +def fake_synonym_set_fixture(fake_api_call: ApiCall) -> SynonymSet: |
| 70 | + """Return a SynonymSet object with test values.""" |
| 71 | + return SynonymSet(fake_api_call, "test-set") |
| 72 | + |
| 73 | + |
0 commit comments