|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from qwen3_embed.rerank.cross_encoder.text_cross_encoder_base import TextCrossEncoderBase |
| 6 | + |
| 7 | + |
| 8 | +class StubCrossEncoder(TextCrossEncoderBase): |
| 9 | + """Stub class for testing TextCrossEncoderBase.""" |
| 10 | + |
| 11 | + @classmethod |
| 12 | + def _list_supported_models(cls) -> list[Any]: |
| 13 | + return [] |
| 14 | + |
| 15 | + |
| 16 | +def test_text_cross_encoder_base_init(): |
| 17 | + """Verify that TextCrossEncoderBase.__init__ correctly sets attributes.""" |
| 18 | + model_name = "test-model" |
| 19 | + cache_dir = "/tmp/cache" |
| 20 | + threads = 4 |
| 21 | + kwargs = {"local_files_only": True, "extra_param": "value"} |
| 22 | + |
| 23 | + encoder = StubCrossEncoder( |
| 24 | + model_name=model_name, cache_dir=cache_dir, threads=threads, **kwargs |
| 25 | + ) |
| 26 | + |
| 27 | + assert encoder.model_name == model_name |
| 28 | + assert encoder.cache_dir == cache_dir |
| 29 | + assert encoder.threads == threads |
| 30 | + assert encoder._local_files_only is True |
| 31 | + |
| 32 | + |
| 33 | +def test_text_cross_encoder_base_not_implemented_methods(): |
| 34 | + """Verify that abstract methods in TextCrossEncoderBase raise NotImplementedError.""" |
| 35 | + encoder = StubCrossEncoder(model_name="test-model") |
| 36 | + |
| 37 | + with pytest.raises(NotImplementedError, match="should be overridden by subclasses"): |
| 38 | + encoder.rerank(query="query", documents=["doc1"]) |
| 39 | + |
| 40 | + with pytest.raises(NotImplementedError, match="should be overridden by subclasses"): |
| 41 | + encoder.rerank_pairs(pairs=[("q", "d")]) |
| 42 | + |
| 43 | + with pytest.raises(NotImplementedError, match="should be overridden by subclasses"): |
| 44 | + encoder.token_count(pairs=[("q", "d")]) |
0 commit comments