@@ -227,7 +227,93 @@ static void test_mc_TextOpts_to_FLE2TextSearchInsertSpec(_mongocrypt_tester_t *t
227227 }
228228}
229229
230+ static void test_mc_TextOpts_to_FLE2TextSearchInsertSpec_for_query (_mongocrypt_tester_t * tester ) {
231+ typedef struct {
232+ const char * desc ;
233+ const char * in ;
234+ const char * v ;
235+ mongocrypt_query_type_t qt ;
236+ const char * expectError ;
237+ const char * expect ;
238+ } testcase ;
239+
240+ testcase tests [] = {
241+ {.desc = "Works with substring" ,
242+ .in = RAW_STRING ({
243+ "caseSensitive" : true,
244+ "diacriticSensitive" : false,
245+ "substring" : {"strMaxLength" : 10 , "strMinQueryLength" : 3 , "strMaxQueryLength" : 8 }
246+ }),
247+ .v = RAW_STRING ({"v" : "test "}),
248+ .qt = MONGOCRYPT_QUERY_TYPE_SUBSTRINGPREVIEW ,
249+ .expect = RAW_STRING (
250+ {"v" : {"v" : "test ", " casef " : true, " diacf " : false, " substr " : {" mlen " : 10, " ub " : 8, " lb " : 3}}})},
251+ {.desc = "Works with prefix" ,
252+ .in = RAW_STRING ({
253+ "caseSensitive" : true,
254+ "diacriticSensitive" : false,
255+ "prefix" : {"strMinQueryLength" : 3 , "strMaxQueryLength" : 8 }
256+ }),
257+ .v = RAW_STRING ({"v" : "test "}),
258+ .qt = MONGOCRYPT_QUERY_TYPE_PREFIXPREVIEW ,
259+ .expect =
260+ RAW_STRING ({"v" : {"v" : "test ", " casef " : true, " diacf " : false, " prefix " : {" ub " : 8, " lb " : 3}}})},
261+ {.desc = "Works with suffix" ,
262+ .in = RAW_STRING ({
263+ "caseSensitive" : true,
264+ "diacriticSensitive" : false,
265+ "suffix" : {"strMinQueryLength" : 3 , "strMaxQueryLength" : 8 }
266+ }),
267+ .v = RAW_STRING ({"v" : "test "}),
268+ .qt = MONGOCRYPT_QUERY_TYPE_SUFFIXPREVIEW ,
269+ .expect =
270+ RAW_STRING ({"v" : {"v" : "test ", " casef " : true, " diacf " : false, " suffix " : {" ub " : 8, " lb " : 3}}})},
271+ {.desc = "Works with prefix + suffix when querying prefix" ,
272+ .in = RAW_STRING ({
273+ "caseSensitive" : true,
274+ "diacriticSensitive" : false,
275+ "prefix" : {"strMinQueryLength" : 4 , "strMaxQueryLength" : 9 },
276+ "suffix" : {"strMinQueryLength" : 3 , "strMaxQueryLength" : 8 }
277+ }),
278+ .v = RAW_STRING ({"v" : "test "}),
279+ .qt = MONGOCRYPT_QUERY_TYPE_PREFIXPREVIEW ,
280+ .expect =
281+ RAW_STRING ({"v" : {"v" : "test ", " casef " : true, " diacf " : false, " prefix " : {" ub " : 9, " lb " : 4}}})},
282+ {.desc = "Works with prefix + suffix when querying suffix" ,
283+ .in = RAW_STRING ({
284+ "caseSensitive" : true,
285+ "diacriticSensitive" : false,
286+ "prefix" : {"strMinQueryLength" : 4 , "strMaxQueryLength" : 9 },
287+ "suffix" : {"strMinQueryLength" : 3 , "strMaxQueryLength" : 8 }
288+ }),
289+ .v = RAW_STRING ({"v" : "test "}),
290+ .qt = MONGOCRYPT_QUERY_TYPE_SUFFIXPREVIEW ,
291+ .expect =
292+ RAW_STRING ({"v" : {"v" : "test ", " casef " : true, " diacf " : false, " suffix " : {" ub " : 8, " lb " : 3}}})},
293+ };
294+
295+ for (size_t i = 0 ; i < sizeof (tests ) / sizeof (tests [0 ]); i ++ ) {
296+ testcase * test = tests + i ;
297+ mongocrypt_status_t * status = mongocrypt_status_new ();
298+ mc_TextOpts_t txo ;
299+ TEST_PRINTF ("running test_mc_TextOpts_to_FLE2TextSearchInsertSpec subtest: %s\n" , test -> desc );
300+ ASSERT_OK_STATUS (mc_TextOpts_parse (& txo , TMP_BSON_STR (test -> in ), status ), status );
301+ bson_t out = BSON_INITIALIZER ;
302+ bool ret =
303+ mc_TextOpts_to_FLE2TextSearchInsertSpec_for_query (& txo , TMP_BSON_STR (test -> v ), test -> qt , & out , status );
304+ if (!test -> expectError ) {
305+ ASSERT_OK_STATUS (ret , status );
306+ ASSERT_EQUAL_BSON (TMP_BSON_STR (test -> expect ), & out );
307+ } else {
308+ ASSERT_FAILS_STATUS (ret , status , test -> expectError );
309+ }
310+ bson_destroy (& out );
311+ mongocrypt_status_destroy (status );
312+ }
313+ }
314+
230315void _mongocrypt_tester_install_mc_TextOpts (_mongocrypt_tester_t * tester ) {
231316 INSTALL_TEST (test_mc_TextOpts_parse );
232317 INSTALL_TEST (test_mc_TextOpts_to_FLE2TextSearchInsertSpec );
318+ INSTALL_TEST (test_mc_TextOpts_to_FLE2TextSearchInsertSpec_for_query );
233319}
0 commit comments