2023-01-17 13:51:07 +01:00
use meili_snap ::{ json_string , snapshot } ;
2022-10-20 18:00:07 +02:00
use serde_json ::{ json , Value } ;
2021-02-18 19:52:00 +01:00
use crate ::common ::Server ;
#[ actix_rt::test ]
async fn create_and_get_index ( ) {
let server = Server ::new ( ) . await ;
let index = server . index ( " test " ) ;
let ( _ , code ) = index . create ( None ) . await ;
2021-12-02 16:03:26 +01:00
assert_eq! ( code , 202 ) ;
index . wait_task ( 0 ) . await ;
2021-02-18 19:52:00 +01:00
let ( response , code ) = index . get ( ) . await ;
assert_eq! ( code , 200 ) ;
assert_eq! ( response [ " uid " ] , " test " ) ;
assert! ( response . get ( " createdAt " ) . is_some ( ) ) ;
assert! ( response . get ( " updatedAt " ) . is_some ( ) ) ;
assert_eq! ( response [ " createdAt " ] , response [ " updatedAt " ] ) ;
assert_eq! ( response [ " primaryKey " ] , Value ::Null ) ;
2022-05-24 11:29:03 +02:00
assert_eq! ( response . as_object ( ) . unwrap ( ) . len ( ) , 4 ) ;
2021-02-18 19:52:00 +01:00
}
#[ actix_rt::test ]
2021-10-21 14:42:01 +02:00
async fn error_get_unexisting_index ( ) {
2021-02-18 19:52:00 +01:00
let server = Server ::new ( ) . await ;
let index = server . index ( " test " ) ;
2021-10-21 14:42:01 +02:00
let ( response , code ) = index . get ( ) . await ;
let expected_response = json! ( {
2021-10-26 19:36:48 +02:00
" message " : " Index `test` not found. " ,
2021-10-21 14:42:01 +02:00
" code " : " index_not_found " ,
" type " : " invalid_request " ,
2023-01-19 16:10:05 +01:00
" link " : " https://docs.meilisearch.com/errors#index_not_found "
2021-10-21 14:42:01 +02:00
} ) ;
2021-02-18 19:52:00 +01:00
2021-10-21 14:42:01 +02:00
assert_eq! ( response , expected_response ) ;
2021-06-15 22:21:52 +02:00
assert_eq! ( code , 404 ) ;
2021-02-18 19:52:00 +01:00
}
2021-02-19 19:14:25 +01:00
#[ actix_rt::test ]
async fn no_index_return_empty_list ( ) {
let server = Server ::new ( ) . await ;
2022-05-24 11:29:03 +02:00
let ( response , code ) = server . list_indexes ( None , None ) . await ;
2021-02-19 19:14:25 +01:00
assert_eq! ( code , 200 ) ;
2022-05-24 11:29:03 +02:00
assert! ( response [ " results " ] . is_array ( ) ) ;
assert! ( response [ " results " ] . as_array ( ) . unwrap ( ) . is_empty ( ) ) ;
2021-02-19 19:14:25 +01:00
}
2021-02-19 19:23:58 +01:00
#[ actix_rt::test ]
async fn list_multiple_indexes ( ) {
let server = Server ::new ( ) . await ;
server . index ( " test " ) . create ( None ) . await ;
server . index ( " test1 " ) . create ( Some ( " key " ) ) . await ;
2021-12-02 16:03:26 +01:00
server . index ( " test " ) . wait_task ( 1 ) . await ;
2022-05-24 11:29:03 +02:00
let ( response , code ) = server . list_indexes ( None , None ) . await ;
2021-02-19 19:23:58 +01:00
assert_eq! ( code , 200 ) ;
2022-05-24 11:29:03 +02:00
assert! ( response [ " results " ] . is_array ( ) ) ;
let arr = response [ " results " ] . as_array ( ) . unwrap ( ) ;
2021-02-19 19:23:58 +01:00
assert_eq! ( arr . len ( ) , 2 ) ;
2022-10-20 18:00:07 +02:00
assert! ( arr . iter ( ) . any ( | entry | entry [ " uid " ] = = " test " & & entry [ " primaryKey " ] = = Value ::Null ) ) ;
assert! ( arr . iter ( ) . any ( | entry | entry [ " uid " ] = = " test1 " & & entry [ " primaryKey " ] = = " key " ) ) ;
2021-02-19 19:23:58 +01:00
}
2021-12-02 16:03:26 +01:00
2022-05-24 11:29:03 +02:00
#[ actix_rt::test ]
async fn get_and_paginate_indexes ( ) {
let server = Server ::new ( ) . await ;
const NB_INDEXES : usize = 50 ;
for i in 0 .. NB_INDEXES {
server . index ( & format! ( " test_ {i:02} " ) ) . create ( None ) . await ;
2022-10-20 18:00:07 +02:00
server . index ( & format! ( " test_ {i:02} " ) ) . wait_task ( i as u64 ) . await ;
2022-05-24 11:29:03 +02:00
}
// basic
let ( response , code ) = server . list_indexes ( None , None ) . await ;
assert_eq! ( code , 200 ) ;
assert_eq! ( response [ " limit " ] , json! ( 20 ) ) ;
assert_eq! ( response [ " offset " ] , json! ( 0 ) ) ;
assert_eq! ( response [ " total " ] , json! ( NB_INDEXES ) ) ;
assert! ( response [ " results " ] . is_array ( ) ) ;
let arr = response [ " results " ] . as_array ( ) . unwrap ( ) ;
assert_eq! ( arr . len ( ) , 20 ) ;
// ensuring we get all the indexes in the alphabetical order
assert! ( ( 0 .. 20 )
. map ( | idx | format! ( " test_ {idx:02} " ) )
. zip ( arr )
. all ( | ( expected , entry ) | entry [ " uid " ] = = expected ) ) ;
// with an offset
let ( response , code ) = server . list_indexes ( Some ( 15 ) , None ) . await ;
assert_eq! ( code , 200 ) ;
assert_eq! ( response [ " limit " ] , json! ( 20 ) ) ;
assert_eq! ( response [ " offset " ] , json! ( 15 ) ) ;
assert_eq! ( response [ " total " ] , json! ( NB_INDEXES ) ) ;
assert! ( response [ " results " ] . is_array ( ) ) ;
let arr = response [ " results " ] . as_array ( ) . unwrap ( ) ;
assert_eq! ( arr . len ( ) , 20 ) ;
assert! ( ( 15 .. 35 )
. map ( | idx | format! ( " test_ {idx:02} " ) )
. zip ( arr )
. all ( | ( expected , entry ) | entry [ " uid " ] = = expected ) ) ;
// with an offset and not enough elements
let ( response , code ) = server . list_indexes ( Some ( 45 ) , None ) . await ;
assert_eq! ( code , 200 ) ;
assert_eq! ( response [ " limit " ] , json! ( 20 ) ) ;
assert_eq! ( response [ " offset " ] , json! ( 45 ) ) ;
assert_eq! ( response [ " total " ] , json! ( NB_INDEXES ) ) ;
assert! ( response [ " results " ] . is_array ( ) ) ;
let arr = response [ " results " ] . as_array ( ) . unwrap ( ) ;
assert_eq! ( arr . len ( ) , 5 ) ;
assert! ( ( 45 .. 50 )
. map ( | idx | format! ( " test_ {idx:02} " ) )
. zip ( arr )
. all ( | ( expected , entry ) | entry [ " uid " ] = = expected ) ) ;
// with a limit lower than the default
let ( response , code ) = server . list_indexes ( None , Some ( 5 ) ) . await ;
assert_eq! ( code , 200 ) ;
assert_eq! ( response [ " limit " ] , json! ( 5 ) ) ;
assert_eq! ( response [ " offset " ] , json! ( 0 ) ) ;
assert_eq! ( response [ " total " ] , json! ( NB_INDEXES ) ) ;
assert! ( response [ " results " ] . is_array ( ) ) ;
let arr = response [ " results " ] . as_array ( ) . unwrap ( ) ;
assert_eq! ( arr . len ( ) , 5 ) ;
assert! ( ( 0 .. 5 )
. map ( | idx | format! ( " test_ {idx:02} " ) )
. zip ( arr )
. all ( | ( expected , entry ) | entry [ " uid " ] = = expected ) ) ;
// with a limit higher than the default
let ( response , code ) = server . list_indexes ( None , Some ( 40 ) ) . await ;
assert_eq! ( code , 200 ) ;
assert_eq! ( response [ " limit " ] , json! ( 40 ) ) ;
assert_eq! ( response [ " offset " ] , json! ( 0 ) ) ;
assert_eq! ( response [ " total " ] , json! ( NB_INDEXES ) ) ;
assert! ( response [ " results " ] . is_array ( ) ) ;
let arr = response [ " results " ] . as_array ( ) . unwrap ( ) ;
assert_eq! ( arr . len ( ) , 40 ) ;
assert! ( ( 0 .. 40 )
. map ( | idx | format! ( " test_ {idx:02} " ) )
. zip ( arr )
. all ( | ( expected , entry ) | entry [ " uid " ] = = expected ) ) ;
// with a limit higher than the default
let ( response , code ) = server . list_indexes ( None , Some ( 80 ) ) . await ;
assert_eq! ( code , 200 ) ;
assert_eq! ( response [ " limit " ] , json! ( 80 ) ) ;
assert_eq! ( response [ " offset " ] , json! ( 0 ) ) ;
assert_eq! ( response [ " total " ] , json! ( NB_INDEXES ) ) ;
assert! ( response [ " results " ] . is_array ( ) ) ;
let arr = response [ " results " ] . as_array ( ) . unwrap ( ) ;
assert_eq! ( arr . len ( ) , 50 ) ;
assert! ( ( 0 .. 50 )
. map ( | idx | format! ( " test_ {idx:02} " ) )
. zip ( arr )
. all ( | ( expected , entry ) | entry [ " uid " ] = = expected ) ) ;
// with a limit and an offset
let ( response , code ) = server . list_indexes ( Some ( 20 ) , Some ( 10 ) ) . await ;
assert_eq! ( code , 200 ) ;
assert_eq! ( response [ " limit " ] , json! ( 10 ) ) ;
assert_eq! ( response [ " offset " ] , json! ( 20 ) ) ;
assert_eq! ( response [ " total " ] , json! ( NB_INDEXES ) ) ;
assert! ( response [ " results " ] . is_array ( ) ) ;
let arr = response [ " results " ] . as_array ( ) . unwrap ( ) ;
assert_eq! ( arr . len ( ) , 10 ) ;
assert! ( ( 20 .. 30 )
. map ( | idx | format! ( " test_ {idx:02} " ) )
. zip ( arr )
. all ( | ( expected , entry ) | entry [ " uid " ] = = expected ) ) ;
}
2021-12-02 16:03:26 +01:00
#[ actix_rt::test ]
async fn get_invalid_index_uid ( ) {
let server = Server ::new ( ) . await ;
let index = server . index ( " this is not a valid index name " ) ;
let ( response , code ) = index . get ( ) . await ;
2023-01-17 13:51:07 +01:00
snapshot! ( code , @ " 400 Bad Request " ) ;
snapshot! ( json_string! ( response ) , @ r ###"
{
2024-09-16 21:22:24 +02:00
" message " : " `this is not a valid index name` is not a valid index uid. Index uid can be an integer or a string containing only alphanumeric characters, hyphens (-) and underscores (_), and can not be more than 512 bytes. " ,
2023-01-17 13:51:07 +01:00
" code " : " invalid_index_uid " ,
" type " : " invalid_request " ,
2023-01-19 15:48:20 +01:00
" link " : " https://docs.meilisearch.com/errors#invalid_index_uid "
2023-01-17 13:51:07 +01:00
}
" ###);
2021-12-02 16:03:26 +01:00
}