|
| 1 | +from collections.abc import Iterable |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import pytest |
| 6 | + |
| 7 | +from qwen3_embed.common.model_description import DenseModelDescription |
| 8 | +from qwen3_embed.common.types import NumpyArray |
| 9 | +from qwen3_embed.text.text_embedding_base import TextEmbeddingBase |
| 10 | + |
| 11 | + |
| 12 | +class MockEmbedding(TextEmbeddingBase): |
| 13 | + """A concrete implementation of TextEmbeddingBase for testing purposes.""" |
| 14 | + |
| 15 | + @classmethod |
| 16 | + def _list_supported_models(cls) -> list[DenseModelDescription]: |
| 17 | + return [] |
| 18 | + |
| 19 | + def embed( |
| 20 | + self, |
| 21 | + documents: str | Iterable[str], |
| 22 | + batch_size: int = 256, |
| 23 | + parallel: int | None = None, |
| 24 | + **kwargs: Any, |
| 25 | + ) -> Iterable[NumpyArray]: |
| 26 | + # Simple mock implementation that yields dummy arrays |
| 27 | + if isinstance(documents, str): |
| 28 | + documents = [documents] |
| 29 | + for _ in documents: |
| 30 | + yield np.zeros(10) |
| 31 | + |
| 32 | + @classmethod |
| 33 | + def get_embedding_size(cls, model_name: str) -> int: |
| 34 | + return 10 |
| 35 | + |
| 36 | + @property |
| 37 | + def embedding_size(self) -> int: |
| 38 | + return 10 |
| 39 | + |
| 40 | + def token_count(self, texts: str | Iterable[str], **kwargs: Any) -> int: |
| 41 | + return 5 |
| 42 | + |
| 43 | + |
| 44 | +def test_text_embedding_base_init(): |
| 45 | + """Test that TextEmbeddingBase initializes its attributes correctly.""" |
| 46 | + model_name = "test-model" |
| 47 | + cache_dir = "/tmp/cache" |
| 48 | + threads = 4 |
| 49 | + local_files_only = True |
| 50 | + |
| 51 | + # We instantiate the mock because the base class is abstract (uses NotImplementedError) |
| 52 | + model = MockEmbedding( |
| 53 | + model_name=model_name, |
| 54 | + cache_dir=cache_dir, |
| 55 | + threads=threads, |
| 56 | + local_files_only=local_files_only, |
| 57 | + ) |
| 58 | + |
| 59 | + assert model.model_name == model_name |
| 60 | + assert model.cache_dir == cache_dir |
| 61 | + assert model.threads == threads |
| 62 | + assert model._local_files_only is True |
| 63 | + assert model._embedding_size is None |
| 64 | + |
| 65 | + |
| 66 | +def test_passage_embed_delegation(): |
| 67 | + """Test that passage_embed correctly delegates to the embed method.""" |
| 68 | + model = MockEmbedding(model_name="test-model") |
| 69 | + texts = ["text1", "text2"] |
| 70 | + |
| 71 | + embeddings = list(model.passage_embed(texts)) |
| 72 | + |
| 73 | + assert len(embeddings) == 2 |
| 74 | + for emb in embeddings: |
| 75 | + assert isinstance(emb, np.ndarray) |
| 76 | + assert emb.shape == (10,) |
| 77 | + |
| 78 | + |
| 79 | +def test_query_embed_single_string(): |
| 80 | + """Test that query_embed handles a single string correctly.""" |
| 81 | + model = MockEmbedding(model_name="test-model") |
| 82 | + query = "test query" |
| 83 | + |
| 84 | + embeddings = list(model.query_embed(query)) |
| 85 | + |
| 86 | + assert len(embeddings) == 1 |
| 87 | + assert isinstance(embeddings[0], np.ndarray) |
| 88 | + assert embeddings[0].shape == (10,) |
| 89 | + |
| 90 | + |
| 91 | +def test_query_embed_iterable(): |
| 92 | + """Test that query_embed handles an iterable of strings correctly.""" |
| 93 | + model = MockEmbedding(model_name="test-model") |
| 94 | + queries = ["query1", "query2"] |
| 95 | + |
| 96 | + embeddings = list(model.query_embed(queries)) |
| 97 | + |
| 98 | + assert len(embeddings) == 2 |
| 99 | + for emb in embeddings: |
| 100 | + assert isinstance(emb, np.ndarray) |
| 101 | + assert emb.shape == (10,) |
| 102 | + |
| 103 | + |
| 104 | +def test_base_class_raises_not_implemented(): |
| 105 | + """Test that the base class methods raise NotImplementedError when called directly or via incomplete subclass.""" |
| 106 | + |
| 107 | + class IncompleteEmbedding(TextEmbeddingBase): |
| 108 | + @classmethod |
| 109 | + def _list_supported_models(cls) -> list[DenseModelDescription]: |
| 110 | + return [] |
| 111 | + |
| 112 | + model = IncompleteEmbedding(model_name="test-model") |
| 113 | + |
| 114 | + with pytest.raises(NotImplementedError): |
| 115 | + list(model.embed("test")) |
| 116 | + |
| 117 | + with pytest.raises(NotImplementedError, match="Subclasses must implement this method"): |
| 118 | + IncompleteEmbedding.get_embedding_size("test-model") |
| 119 | + |
| 120 | + with pytest.raises(NotImplementedError, match="Subclasses must implement this method"): |
| 121 | + _ = model.embedding_size |
| 122 | + |
| 123 | + with pytest.raises(NotImplementedError, match="Subclasses must implement this method"): |
| 124 | + model.token_count("test") |
0 commit comments