|
| 1 | +# Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +# license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright |
| 4 | +# ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +# the Apache License, Version 2.0 (the "License"); you may |
| 6 | +# not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import pytest |
| 19 | +from elasticsearch.helpers.vectorstore import ( |
| 20 | + SparseVectorStrategy, |
| 21 | + DenseVectorStrategy, |
| 22 | + DenseVectorScriptScoreStrategy, |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +def test_sparse_vector_strategy_raises_errors(): |
| 27 | + strategy = SparseVectorStrategy("my_model_id") |
| 28 | + |
| 29 | + with pytest.raises(ValueError): |
| 30 | + # missing query |
| 31 | + strategy.es_query(None, None, "text_field", "vector_field", 10, 20, []) |
| 32 | + |
| 33 | + with pytest.raises(ValueError): |
| 34 | + # query vector not allowed |
| 35 | + strategy.es_query("hi", [1, 2, 3], "text_field", "vector_field", 10, 20, []) |
| 36 | + |
| 37 | + |
| 38 | +def test_dense_vector_strategy_raises_error(): |
| 39 | + with pytest.raises(ValueError): |
| 40 | + # unknown distance |
| 41 | + DenseVectorStrategy(hybrid=True, text_field=None) |
| 42 | + |
| 43 | + with pytest.raises(ValueError): |
| 44 | + # unknown distance |
| 45 | + DenseVectorStrategy(distance="unknown distance").es_mappings_settings( |
| 46 | + "text_field", "vector_field", 10 |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +def test_dense_vector_script_score_strategy_raises_error(): |
| 51 | + with pytest.raises(ValueError): |
| 52 | + # missing query vector |
| 53 | + DenseVectorScriptScoreStrategy().es_query( |
| 54 | + None, None, "text_field", "vector_field", 10, 20, [] |
| 55 | + ) |
| 56 | + |
| 57 | + with pytest.raises(ValueError): |
| 58 | + # unknown distance |
| 59 | + DenseVectorScriptScoreStrategy(distance="unknown distance").es_query( |
| 60 | + None, [1, 2, 3], "text_field", "vector_field", 10, 20, [] |
| 61 | + ) |
0 commit comments