2021-06-23 22:21:32 +08:00
use std ::collections ::HashMap ;
use once_cell ::sync ::Lazy ;
2021-06-24 22:25:52 +08:00
use serde_json ::{ json , Value } ;
2021-06-23 22:21:32 +08:00
2021-02-24 16:30:51 +08:00
use crate ::common ::Server ;
2021-06-23 22:21:32 +08:00
static DEFAULT_SETTINGS_VALUES : Lazy < HashMap < & 'static str , Value > > = Lazy ::new ( | | {
let mut map = HashMap ::new ( ) ;
map . insert ( " displayed_attributes " , json! ( [ " * " ] ) ) ;
map . insert ( " searchable_attributes " , json! ( [ " * " ] ) ) ;
map . insert ( " filterable_attributes " , json! ( [ ] ) ) ;
map . insert ( " distinct_attribute " , json! ( Value ::Null ) ) ;
2021-06-24 22:25:52 +08:00
map . insert (
" ranking_rules " ,
2021-08-25 00:52:28 +08:00
json! ( [
" words " ,
" typo " ,
" proximity " ,
" attribute " ,
2021-09-08 22:54:39 +08:00
" sort " ,
2021-08-25 00:52:28 +08:00
" exactness "
] ) ,
2021-06-24 22:25:52 +08:00
) ;
2021-06-23 22:21:32 +08:00
map . insert ( " stop_words " , json! ( [ ] ) ) ;
map . insert ( " synonyms " , json! ( { } ) ) ;
2022-06-09 00:03:56 +08:00
map . insert (
" faceting " ,
json! ( {
" maxValuesByFacet " : json ! ( 100 ) ,
} ) ,
) ;
2021-06-23 22:21:32 +08:00
map
} ) ;
2021-02-24 16:30:51 +08:00
#[ actix_rt::test ]
async fn get_settings_unexisting_index ( ) {
let server = Server ::new ( ) . await ;
2021-09-29 00:10:09 +08:00
let ( response , code ) = server . index ( " test " ) . settings ( ) . await ;
assert_eq! ( code , 404 , " {} " , response )
2021-02-24 16:30:51 +08:00
}
#[ actix_rt::test ]
async fn get_settings ( ) {
let server = Server ::new ( ) . await ;
let index = server . index ( " test " ) ;
index . create ( None ) . await ;
2021-12-02 23:03:26 +08:00
index . wait_task ( 0 ) . await ;
2021-02-24 16:30:51 +08:00
let ( response , code ) = index . settings ( ) . await ;
assert_eq! ( code , 200 ) ;
let settings = response . as_object ( ) . unwrap ( ) ;
2022-06-09 16:17:55 +08:00
assert_eq! ( settings . keys ( ) . len ( ) , 11 ) ;
2021-02-24 16:30:51 +08:00
assert_eq! ( settings [ " displayedAttributes " ] , json! ( [ " * " ] ) ) ;
assert_eq! ( settings [ " searchableAttributes " ] , json! ( [ " * " ] ) ) ;
2021-06-04 05:47:16 +08:00
assert_eq! ( settings [ " filterableAttributes " ] , json! ( [ ] ) ) ;
2021-08-24 21:46:14 +08:00
assert_eq! ( settings [ " sortableAttributes " ] , json! ( [ ] ) ) ;
2021-04-20 18:07:22 +08:00
assert_eq! ( settings [ " distinctAttribute " ] , json! ( null ) ) ;
2021-03-16 01:11:10 +08:00
assert_eq! (
settings [ " rankingRules " ] ,
2021-08-25 00:52:28 +08:00
json! ( [
" words " ,
" typo " ,
" proximity " ,
" attribute " ,
2021-09-08 22:54:39 +08:00
" sort " ,
2021-08-25 00:52:28 +08:00
" exactness "
] )
2021-03-16 01:11:10 +08:00
) ;
2021-04-06 21:43:27 +08:00
assert_eq! ( settings [ " stopWords " ] , json! ( [ ] ) ) ;
2022-06-09 00:03:56 +08:00
assert_eq! (
settings [ " faceting " ] ,
json! ( {
2022-06-09 16:17:55 +08:00
" maxValuesPerFacet " : 100 ,
} )
) ;
assert_eq! (
settings [ " pagination " ] ,
json! ( {
" limitedTo " : 1000 ,
2022-06-09 00:03:56 +08:00
} )
) ;
2021-02-24 16:30:51 +08:00
}
2021-02-24 16:42:36 +08:00
#[ actix_rt::test ]
2021-11-04 20:38:44 +08:00
async fn error_update_settings_unknown_field ( ) {
2021-02-24 16:42:36 +08:00
let server = Server ::new ( ) . await ;
let index = server . index ( " test " ) ;
let ( _response , code ) = index . update_settings ( json! ( { " foo " : 12 } ) ) . await ;
2021-06-22 17:10:57 +08:00
assert_eq! ( code , 400 ) ;
2021-02-24 16:42:36 +08:00
}
#[ actix_rt::test ]
async fn test_partial_update ( ) {
let server = Server ::new ( ) . await ;
let index = server . index ( " test " ) ;
2021-03-16 01:11:10 +08:00
let ( _response , _code ) = index
. update_settings ( json! ( { " displayedAttributes " : [ " foo " ] } ) )
. await ;
2021-12-02 23:03:26 +08:00
index . wait_task ( 0 ) . await ;
2021-02-24 16:42:36 +08:00
let ( response , code ) = index . settings ( ) . await ;
assert_eq! ( code , 200 ) ;
2021-03-16 01:11:10 +08:00
assert_eq! ( response [ " displayedAttributes " ] , json! ( [ " foo " ] ) ) ;
assert_eq! ( response [ " searchableAttributes " ] , json! ( [ " * " ] ) ) ;
2021-02-24 16:42:36 +08:00
2021-03-16 01:11:10 +08:00
let ( _response , _ ) = index
. update_settings ( json! ( { " searchableAttributes " : [ " bar " ] } ) )
. await ;
2021-12-02 23:03:26 +08:00
index . wait_task ( 1 ) . await ;
2021-02-24 16:42:36 +08:00
let ( response , code ) = index . settings ( ) . await ;
assert_eq! ( code , 200 ) ;
2021-03-16 01:11:10 +08:00
assert_eq! ( response [ " displayedAttributes " ] , json! ( [ " foo " ] ) ) ;
assert_eq! ( response [ " searchableAttributes " ] , json! ( [ " bar " ] ) ) ;
2021-02-24 16:42:36 +08:00
}
2021-02-24 17:14:36 +08:00
#[ actix_rt::test ]
2021-11-04 20:38:44 +08:00
async fn error_delete_settings_unexisting_index ( ) {
2021-02-24 17:14:36 +08:00
let server = Server ::new ( ) . await ;
let index = server . index ( " test " ) ;
2021-12-02 23:03:26 +08:00
let ( _response , code ) = index . delete_settings ( ) . await ;
assert_eq! ( code , 202 ) ;
2021-11-04 20:38:44 +08:00
2021-12-02 23:03:26 +08:00
let response = index . wait_task ( 0 ) . await ;
2021-11-04 20:38:44 +08:00
2021-12-02 23:03:26 +08:00
assert_eq! ( response [ " status " ] , " failed " ) ;
2021-02-24 17:14:36 +08:00
}
2021-02-24 17:19:22 +08:00
#[ actix_rt::test ]
async fn reset_all_settings ( ) {
let server = Server ::new ( ) . await ;
let index = server . index ( " test " ) ;
2021-06-04 02:08:31 +08:00
let documents = json! ( [
{
" id " : 1 ,
" name " : " curqui " ,
" age " : 99
}
] ) ;
let ( response , code ) = index . add_documents ( documents , None ) . await ;
assert_eq! ( code , 202 ) ;
2022-05-17 17:17:32 +08:00
assert_eq! ( response [ " taskUid " ] , 0 ) ;
2021-12-02 23:03:26 +08:00
index . wait_task ( 0 ) . await ;
2021-06-04 02:08:31 +08:00
2021-03-16 01:11:10 +08:00
index
2021-06-03 20:19:56 +08:00
. update_settings ( json! ( { " displayedAttributes " : [ " name " , " age " ] , " searchableAttributes " : [ " name " ] , " stopWords " : [ " the " ] , " filterableAttributes " : [ " age " ] , " synonyms " : { " puppy " : [ " dog " , " doggo " , " potat " ] } } ) )
2021-03-16 01:11:10 +08:00
. await ;
2021-12-02 23:03:26 +08:00
index . wait_task ( 1 ) . await ;
2021-02-24 17:19:22 +08:00
let ( response , code ) = index . settings ( ) . await ;
assert_eq! ( code , 200 ) ;
2021-06-04 02:08:31 +08:00
assert_eq! ( response [ " displayedAttributes " ] , json! ( [ " name " , " age " ] ) ) ;
assert_eq! ( response [ " searchableAttributes " ] , json! ( [ " name " ] ) ) ;
2021-04-07 00:29:38 +08:00
assert_eq! ( response [ " stopWords " ] , json! ( [ " the " ] ) ) ;
2021-06-03 20:19:56 +08:00
assert_eq! (
response [ " synonyms " ] ,
json! ( { " puppy " : [ " dog " , " doggo " , " potat " ] } )
) ;
2021-06-04 05:47:16 +08:00
assert_eq! ( response [ " filterableAttributes " ] , json! ( [ " age " ] ) ) ;
2021-02-24 17:19:22 +08:00
index . delete_settings ( ) . await ;
2021-12-02 23:03:26 +08:00
index . wait_task ( 2 ) . await ;
2021-02-24 17:19:22 +08:00
let ( response , code ) = index . settings ( ) . await ;
assert_eq! ( code , 200 ) ;
2021-03-16 01:11:10 +08:00
assert_eq! ( response [ " displayedAttributes " ] , json! ( [ " * " ] ) ) ;
assert_eq! ( response [ " searchableAttributes " ] , json! ( [ " * " ] ) ) ;
2021-04-07 00:29:38 +08:00
assert_eq! ( response [ " stopWords " ] , json! ( [ ] ) ) ;
2021-06-04 05:47:16 +08:00
assert_eq! ( response [ " filterableAttributes " ] , json! ( [ ] ) ) ;
2021-06-14 20:35:40 +08:00
assert_eq! ( response [ " synonyms " ] , json! ( { } ) ) ;
2021-06-04 02:08:31 +08:00
let ( response , code ) = index . get_document ( 1 , None ) . await ;
assert_eq! ( code , 200 ) ;
assert! ( response . as_object ( ) . unwrap ( ) . get ( " age " ) . is_some ( ) ) ;
2021-02-24 17:19:22 +08:00
}
2021-02-24 16:30:51 +08:00
#[ actix_rt::test ]
async fn update_setting_unexisting_index ( ) {
let server = Server ::new ( ) . await ;
let index = server . index ( " test " ) ;
let ( _response , code ) = index . update_settings ( json! ( { } ) ) . await ;
2021-03-17 21:44:32 +08:00
assert_eq! ( code , 202 ) ;
2021-12-02 23:03:26 +08:00
let response = index . wait_task ( 0 ) . await ;
assert_eq! ( response [ " status " ] , " succeeded " ) ;
2021-02-24 16:30:51 +08:00
let ( _response , code ) = index . get ( ) . await ;
assert_eq! ( code , 200 ) ;
2021-12-02 23:03:26 +08:00
index . delete_settings ( ) . await ;
let response = index . wait_task ( 1 ) . await ;
assert_eq! ( response [ " status " ] , " succeeded " ) ;
2021-02-24 16:30:51 +08:00
}
2021-03-10 21:43:10 +08:00
#[ actix_rt::test ]
2021-11-04 20:38:44 +08:00
async fn error_update_setting_unexisting_index_invalid_uid ( ) {
2021-03-10 21:43:10 +08:00
let server = Server ::new ( ) . await ;
let index = server . index ( " test##! " ) ;
2021-09-29 00:10:09 +08:00
let ( response , code ) = index . update_settings ( json! ( { } ) ) . await ;
2021-12-02 23:03:26 +08:00
assert_eq! ( code , 400 ) ;
2021-11-04 20:38:44 +08:00
2021-12-02 23:03:26 +08:00
let expected = json! ( {
2022-06-06 18:38:46 +08:00
" message " : " invalid index uid `test##! `, the uid must be an integer or a string containing only alphanumeric characters a-z A-Z 0-9, hyphens - and underscores _. " ,
2021-11-04 20:38:44 +08:00
" code " : " invalid_index_uid " ,
" type " : " invalid_request " ,
2021-12-02 23:03:26 +08:00
" link " : " https://docs.meilisearch.com/errors#invalid_index_uid " } ) ;
2021-11-04 20:38:44 +08:00
2021-12-02 23:03:26 +08:00
assert_eq! ( response , expected ) ;
2021-03-10 21:43:10 +08:00
}
2021-02-24 16:30:51 +08:00
macro_rules ! test_setting_routes {
( $( $setting :ident ) , * ) = > {
$(
mod $setting {
use crate ::common ::Server ;
2021-06-23 22:21:32 +08:00
use super ::DEFAULT_SETTINGS_VALUES ;
2021-02-24 16:30:51 +08:00
#[ actix_rt::test ]
async fn get_unexisting_index ( ) {
let server = Server ::new ( ) . await ;
let url = format! ( " /indexes/test/settings/ {} " ,
stringify! ( $setting )
. chars ( )
. map ( | c | if c = = '_' { '-' } else { c } )
. collect ::< String > ( ) ) ;
let ( _response , code ) = server . service . get ( url ) . await ;
2021-06-16 04:21:52 +08:00
assert_eq! ( code , 404 ) ;
2021-02-24 16:30:51 +08:00
}
#[ actix_rt::test ]
async fn update_unexisting_index ( ) {
let server = Server ::new ( ) . await ;
let url = format! ( " /indexes/test/settings/ {} " ,
stringify! ( $setting )
. chars ( )
. map ( | c | if c = = '_' { '-' } else { c } )
. collect ::< String > ( ) ) ;
2022-06-02 18:24:27 +08:00
let ( response , code ) = server . service . put ( url , serde_json ::Value ::Null ) . await ;
2021-04-01 21:50:45 +08:00
assert_eq! ( code , 202 , " {} " , response ) ;
2021-12-02 23:03:26 +08:00
server . index ( " " ) . wait_task ( 0 ) . await ;
2021-03-12 07:37:43 +08:00
let ( response , code ) = server . index ( " test " ) . get ( ) . await ;
assert_eq! ( code , 200 , " {} " , response ) ;
2021-02-24 16:30:51 +08:00
}
#[ actix_rt::test ]
async fn delete_unexisting_index ( ) {
let server = Server ::new ( ) . await ;
let url = format! ( " /indexes/test/settings/ {} " ,
stringify! ( $setting )
. chars ( )
. map ( | c | if c = = '_' { '-' } else { c } )
. collect ::< String > ( ) ) ;
2021-12-02 23:03:26 +08:00
let ( _ , code ) = server . service . delete ( url ) . await ;
assert_eq! ( code , 202 ) ;
let response = server . index ( " " ) . wait_task ( 0 ) . await ;
assert_eq! ( response [ " status " ] , " failed " ) ;
2021-06-23 22:21:32 +08:00
}
#[ actix_rt::test ]
async fn get_default ( ) {
let server = Server ::new ( ) . await ;
let index = server . index ( " test " ) ;
let ( response , code ) = index . create ( None ) . await ;
2021-12-02 23:03:26 +08:00
assert_eq! ( code , 202 , " {} " , response ) ;
index . wait_task ( 0 ) . await ;
2021-06-23 22:21:32 +08:00
let url = format! ( " /indexes/test/settings/ {} " ,
stringify! ( $setting )
. chars ( )
. map ( | c | if c = = '_' { '-' } else { c } )
. collect ::< String > ( ) ) ;
let ( response , code ) = server . service . get ( url ) . await ;
assert_eq! ( code , 200 , " {} " , response ) ;
let expected = DEFAULT_SETTINGS_VALUES . get ( stringify! ( $setting ) ) . unwrap ( ) ;
assert_eq! ( expected , & response ) ;
2021-02-24 16:30:51 +08:00
}
}
) *
} ;
}
test_setting_routes! (
2021-06-04 05:47:16 +08:00
filterable_attributes ,
2021-02-24 16:30:51 +08:00
displayed_attributes ,
2021-04-07 00:29:38 +08:00
searchable_attributes ,
2021-06-23 22:21:32 +08:00
distinct_attribute ,
2021-06-03 20:19:56 +08:00
stop_words ,
2021-06-23 22:21:32 +08:00
ranking_rules ,
2021-06-03 20:19:56 +08:00
synonyms
2021-03-16 01:11:10 +08:00
) ;
2021-11-04 20:38:44 +08:00
#[ actix_rt::test ]
async fn error_set_invalid_ranking_rules ( ) {
let server = Server ::new ( ) . await ;
let index = server . index ( " test " ) ;
index . create ( None ) . await ;
let ( _response , _code ) = index
. update_settings ( json! ( { " rankingRules " : [ " manyTheFish " ] } ) )
. await ;
2021-12-02 23:03:26 +08:00
index . wait_task ( 1 ) . await ;
let ( response , code ) = index . get_task ( 1 ) . await ;
2021-11-04 20:38:44 +08:00
assert_eq! ( code , 200 ) ;
assert_eq! ( response [ " status " ] , " failed " ) ;
2021-11-05 00:03:13 +08:00
let expected_error = json! ( {
2022-06-07 00:33:07 +08:00
" message " : r #" `manyTheFish` ranking rule is invalid. Valid ranking rules are words, typo, sort, proximity, attribute, exactness and custom ranking rules. " #,
2021-11-05 00:03:13 +08:00
" code " : " invalid_ranking_rule " ,
" type " : " invalid_request " ,
" link " : " https://docs.meilisearch.com/errors#invalid_ranking_rule "
} ) ;
assert_eq! ( response [ " error " ] , expected_error ) ;
2021-11-04 20:38:44 +08:00
}
#[ actix_rt::test ]
async fn set_and_reset_distinct_attribute_with_dedicated_route ( ) {
let server = Server ::new ( ) . await ;
let index = server . index ( " test " ) ;
let ( _response , _code ) = index . update_distinct_attribute ( json! ( " test " ) ) . await ;
2021-12-02 23:03:26 +08:00
index . wait_task ( 0 ) . await ;
2021-11-04 20:38:44 +08:00
let ( response , _ ) = index . get_distinct_attribute ( ) . await ;
assert_eq! ( response , " test " ) ;
index . update_distinct_attribute ( json! ( null ) ) . await ;
2021-12-02 23:03:26 +08:00
index . wait_task ( 1 ) . await ;
2021-11-04 20:38:44 +08:00
let ( response , _ ) = index . get_distinct_attribute ( ) . await ;
assert_eq! ( response , json! ( null ) ) ;
}