Skip to content

Commit f32ceb2

Browse files
committed
add error tests for strategies
1 parent 881d56c commit f32ceb2

File tree

6 files changed

+62
-0
lines changed

6 files changed

+62
-0
lines changed

test_elasticsearch/test_strategies.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
20+
from elasticsearch.helpers.vectorstore import (
21+
DenseVectorScriptScoreStrategy,
22+
DenseVectorStrategy,
23+
SparseVectorStrategy,
24+
)
25+
26+
27+
def test_sparse_vector_strategy_raises_errors():
28+
strategy = SparseVectorStrategy("my_model_id")
29+
30+
with pytest.raises(ValueError):
31+
# missing query
32+
strategy.es_query(None, None, "text_field", "vector_field", 10, 20, [])
33+
34+
with pytest.raises(ValueError):
35+
# query vector not allowed
36+
strategy.es_query("hi", [1, 2, 3], "text_field", "vector_field", 10, 20, [])
37+
38+
39+
def test_dense_vector_strategy_raises_error():
40+
with pytest.raises(ValueError):
41+
# unknown distance
42+
DenseVectorStrategy(hybrid=True, text_field=None)
43+
44+
with pytest.raises(ValueError):
45+
# unknown distance
46+
DenseVectorStrategy(distance="unknown distance").es_mappings_settings(
47+
"text_field", "vector_field", 10
48+
)
49+
50+
51+
def test_dense_vector_script_score_strategy_raises_error():
52+
with pytest.raises(ValueError):
53+
# missing query vector
54+
DenseVectorScriptScoreStrategy().es_query(
55+
None, None, "text_field", "vector_field", 10, 20, []
56+
)
57+
58+
with pytest.raises(ValueError):
59+
# unknown distance
60+
DenseVectorScriptScoreStrategy(distance="unknown distance").es_query(
61+
None, [1, 2, 3], "text_field", "vector_field", 10, 20, []
62+
)

0 commit comments

Comments
 (0)