File size: 4,719 Bytes
84d2a97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use std::collections::HashMap;

use api::rest::{
    BaseGroupRequest, Batch, BatchVectorStruct, PointStruct, PointVectors, PointsList,
    SearchGroupsRequestInternal, SearchRequestInternal, Vector, VectorStruct,
};
use sparse::common::sparse_vector::SparseVector;
use validator::Validate;

use crate::operations::types::{
    ContextExamplePair, DiscoverRequestInternal, RecommendExample, RecommendRequestInternal,
};

fn wrong_sparse_vector() -> SparseVector {
    SparseVector {
        indices: vec![1, 2],
        values: vec![0.0, 1.0, 2.0],
    }
}

fn wrong_named_vector_struct() -> api::rest::NamedVectorStruct {
    api::rest::NamedVectorStruct::Sparse(segment::data_types::vectors::NamedSparseVector {
        name: "sparse".to_owned(),
        vector: wrong_sparse_vector(),
    })
}

fn wrong_point_struct() -> PointStruct {
    let vector_data: HashMap<String, _> =
        HashMap::from([("sparse".to_owned(), Vector::Sparse(wrong_sparse_vector()))]);
    PointStruct {
        id: 0.into(),
        vector: VectorStruct::Named(vector_data),
        payload: None,
    }
}

fn wrong_recommend_example() -> RecommendExample {
    RecommendExample::Sparse(wrong_sparse_vector())
}

fn check_validation_error<T: Validate>(v: T) {
    match v.validate() {
        Ok(_) => panic!("Expected validation error"),
        // check if there is an error message about the length of the sparse vector
        Err(e) => assert!(e.to_string().contains("must be the same length as indices")),
    }
}

#[test]
fn validate_error_sparse_vector_point_struct() {
    check_validation_error(wrong_point_struct());
}

#[test]
fn validate_error_sparse_vector_points_batch() {
    let vector_data: HashMap<String, Vec<_>> = HashMap::from([(
        "sparse".to_owned(),
        vec![Vector::Sparse(wrong_sparse_vector())],
    )]);
    check_validation_error(Batch {
        ids: vec![1.into()],
        vectors: BatchVectorStruct::Named(vector_data),
        payloads: None,
    });
}

#[test]
fn validate_error_sparse_vector_points_list() {
    check_validation_error(PointsList {
        points: vec![wrong_point_struct()],
        shard_key: None,
    });
}

#[test]
fn validate_error_sparse_vector_search_request_internal() {
    check_validation_error(SearchRequestInternal {
        vector: wrong_named_vector_struct(),
        filter: None,
        params: None,
        limit: 5,
        offset: None,
        with_payload: None,
        with_vector: None,
        score_threshold: None,
    });
}

#[test]
fn validate_error_sparse_vector_search_groups_request_internal() {
    check_validation_error(SearchGroupsRequestInternal {
        vector: wrong_named_vector_struct(),
        filter: None,
        params: None,
        with_payload: None,
        with_vector: None,
        score_threshold: None,
        group_request: BaseGroupRequest {
            group_by: "sparse".parse().unwrap(),
            group_size: 5,
            limit: 5,
            with_lookup: None,
        },
    });
}

#[test]
fn validate_error_sparse_vector_recommend_example() {
    check_validation_error(RecommendExample::Sparse(wrong_sparse_vector()));
}

#[test]
fn validate_error_sparse_vector_recommend_request_internal() {
    check_validation_error(RecommendRequestInternal {
        positive: vec![wrong_recommend_example()],
        negative: vec![wrong_recommend_example()],
        strategy: None,
        filter: None,
        params: None,
        limit: 5,
        offset: None,
        with_payload: None,
        with_vector: None,
        score_threshold: None,
        using: None,
        lookup_from: None,
    });
}

#[test]
fn validate_error_sparse_vector_context_example_pair() {
    check_validation_error(ContextExamplePair {
        positive: wrong_recommend_example(),
        negative: wrong_recommend_example(),
    });
}

#[test]
fn validate_error_sparse_vector_discover_request_internal() {
    check_validation_error(DiscoverRequestInternal {
        target: Some(wrong_recommend_example()),
        context: Some(vec![ContextExamplePair {
            positive: wrong_recommend_example(),
            negative: wrong_recommend_example(),
        }]),
        filter: None,
        params: None,
        limit: 5,
        offset: None,
        with_payload: None,
        with_vector: None,
        using: None,
        lookup_from: None,
    });
}

#[test]
fn validate_error_sparse_vector_point_vectors() {
    let vector_data: HashMap<String, _> =
        HashMap::from([("sparse".to_owned(), Vector::Sparse(wrong_sparse_vector()))]);

    let vector_struct = VectorStruct::Named(vector_data);

    check_validation_error(PointVectors {
        id: 1.into(),
        vector: vector_struct,
    });
}