2022-10-13 06:56:57 +08:00
use actix_web ::http ::header ::ContentType ;
use actix_web ::test ;
use http ::header ::ACCEPT_ENCODING ;
2021-09-30 17:20:16 +08:00
use serde_json ::{ json , Value } ;
2021-02-19 02:50:52 +08:00
2022-10-21 00:00:07 +08:00
use crate ::common ::encoder ::Encoder ;
use crate ::common ::Server ;
2021-02-19 02:50:52 +08:00
#[ actix_rt::test ]
async fn create_index_no_primary_key ( ) {
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 ) ;
assert_eq! ( response [ " status " ] , " enqueued " ) ;
2022-10-10 01:43:51 +08:00
let response = index . wait_task ( 0 ) . await ;
assert_eq! ( response [ " status " ] , " succeeded " ) ;
assert_eq! ( response [ " type " ] , " indexCreation " ) ;
assert_eq! ( response [ " details " ] [ " primaryKey " ] , Value ::Null ) ;
}
#[ actix_rt::test ]
async fn create_index_with_gzip_encoded_request ( ) {
let server = Server ::new ( ) . await ;
let index = server . index_with_encoder ( " test " , Encoder ::Gzip ) ;
let ( response , code ) = index . create ( None ) . await ;
assert_eq! ( code , 202 ) ;
assert_eq! ( response [ " status " ] , " enqueued " ) ;
let response = index . wait_task ( 0 ) . await ;
assert_eq! ( response [ " status " ] , " succeeded " ) ;
assert_eq! ( response [ " type " ] , " indexCreation " ) ;
assert_eq! ( response [ " details " ] [ " primaryKey " ] , Value ::Null ) ;
}
2022-10-13 06:56:57 +08:00
#[ actix_rt::test ]
async fn create_index_with_gzip_encoded_request_and_receiving_brotli_encoded_response ( ) {
let server = Server ::new ( ) . await ;
2022-10-20 23:08:52 +08:00
let app = server . init_web_app ( ) . await ;
2022-10-13 06:56:57 +08:00
let body = serde_json ::to_string ( & json! ( {
" uid " : " test " ,
" primaryKey " : None ::< & str > ,
} ) )
. unwrap ( ) ;
let req = test ::TestRequest ::post ( )
. uri ( " /indexes " )
. insert_header ( Encoder ::Gzip . header ( ) . unwrap ( ) )
. insert_header ( ( ACCEPT_ENCODING , " br " ) )
. insert_header ( ContentType ::json ( ) )
. set_payload ( Encoder ::Gzip . encode ( body ) )
. to_request ( ) ;
let res = test ::call_service ( & app , req ) . await ;
assert_eq! ( res . status ( ) , 202 ) ;
let bytes = test ::read_body ( res ) . await ;
let decoded = Encoder ::Brotli . decode ( bytes ) ;
let parsed_response =
2022-10-13 21:35:31 +08:00
serde_json ::from_slice ::< Value > ( decoded . into ( ) . as_ref ( ) ) . expect ( " Expecting valid json " ) ;
2022-10-13 06:56:57 +08:00
assert_eq! ( parsed_response [ " taskUid " ] , 0 ) ;
assert_eq! ( parsed_response [ " indexUid " ] , " test " ) ;
}
2022-10-10 01:43:51 +08:00
#[ actix_rt::test ]
async fn create_index_with_zlib_encoded_request ( ) {
let server = Server ::new ( ) . await ;
let index = server . index_with_encoder ( " test " , Encoder ::Deflate ) ;
let ( response , code ) = index . create ( None ) . await ;
assert_eq! ( code , 202 ) ;
assert_eq! ( response [ " status " ] , " enqueued " ) ;
let response = index . wait_task ( 0 ) . await ;
assert_eq! ( response [ " status " ] , " succeeded " ) ;
assert_eq! ( response [ " type " ] , " indexCreation " ) ;
assert_eq! ( response [ " details " ] [ " primaryKey " ] , Value ::Null ) ;
}
#[ actix_rt::test ]
async fn create_index_with_brotli_encoded_request ( ) {
let server = Server ::new ( ) . await ;
let index = server . index_with_encoder ( " test " , Encoder ::Brotli ) ;
let ( response , code ) = index . create ( None ) . await ;
assert_eq! ( code , 202 ) ;
assert_eq! ( response [ " status " ] , " enqueued " ) ;
2021-12-02 23:03:26 +08:00
let response = index . wait_task ( 0 ) . await ;
assert_eq! ( response [ " status " ] , " succeeded " ) ;
assert_eq! ( response [ " type " ] , " indexCreation " ) ;
assert_eq! ( response [ " details " ] [ " primaryKey " ] , Value ::Null ) ;
2021-02-19 02:50:52 +08:00
}
#[ actix_rt::test ]
async fn create_index_with_primary_key ( ) {
let server = Server ::new ( ) . await ;
let index = server . index ( " test " ) ;
let ( response , code ) = index . create ( Some ( " primary " ) ) . await ;
2021-12-02 23:03:26 +08:00
assert_eq! ( code , 202 ) ;
assert_eq! ( response [ " status " ] , " enqueued " ) ;
let response = index . wait_task ( 0 ) . await ;
assert_eq! ( response [ " status " ] , " succeeded " ) ;
assert_eq! ( response [ " type " ] , " indexCreation " ) ;
assert_eq! ( response [ " details " ] [ " primaryKey " ] , " primary " ) ;
2021-02-19 02:50:52 +08:00
}
2021-09-30 17:20:16 +08:00
#[ actix_rt::test ]
async fn create_index_with_invalid_primary_key ( ) {
let document = json! ( [ { " id " : 2 , " title " : " Pride and Prejudice " } ] ) ;
let server = Server ::new ( ) . await ;
let index = server . index ( " movies " ) ;
let ( _response , code ) = index . add_documents ( document , Some ( " title " ) ) . await ;
assert_eq! ( code , 202 ) ;
2021-12-02 23:03:26 +08:00
index . wait_task ( 0 ) . await ;
2021-09-30 17:20:16 +08:00
let ( response , code ) = index . get ( ) . await ;
assert_eq! ( code , 200 ) ;
assert_eq! ( response [ " primaryKey " ] , Value ::Null ) ;
}
2021-02-19 03:32:34 +08:00
#[ actix_rt::test ]
async fn test_create_multiple_indexes ( ) {
let server = Server ::new ( ) . await ;
let index1 = server . index ( " test1 " ) ;
let index2 = server . index ( " test2 " ) ;
let index3 = server . index ( " test3 " ) ;
let index4 = server . index ( " test4 " ) ;
index1 . create ( None ) . await ;
index2 . create ( None ) . await ;
index3 . create ( None ) . await ;
2021-12-02 23:03:26 +08:00
index1 . wait_task ( 0 ) . await ;
index1 . wait_task ( 1 ) . await ;
index1 . wait_task ( 2 ) . await ;
2021-02-19 03:32:34 +08:00
assert_eq! ( index1 . get ( ) . await . 1 , 200 ) ;
assert_eq! ( index2 . get ( ) . await . 1 , 200 ) ;
assert_eq! ( index3 . get ( ) . await . 1 , 200 ) ;
2021-06-16 04:21:52 +08:00
assert_eq! ( index4 . get ( ) . await . 1 , 404 ) ;
2021-02-19 03:32:34 +08:00
}
2021-10-21 20:42:01 +08:00
#[ actix_rt::test ]
async fn error_create_existing_index ( ) {
let server = Server ::new ( ) . await ;
let index = server . index ( " test " ) ;
let ( _ , code ) = index . create ( Some ( " primary " ) ) . await ;
2021-12-02 23:03:26 +08:00
assert_eq! ( code , 202 ) ;
2021-10-21 20:42:01 +08:00
2021-12-02 23:03:26 +08:00
index . create ( Some ( " primary " ) ) . await ;
let response = index . wait_task ( 1 ) . await ;
2021-10-21 20:42:01 +08:00
let expected_response = json! ( {
2021-10-27 01:36:48 +08:00
" message " : " Index `test` already exists. " ,
2021-10-21 20:42:01 +08:00
" code " : " index_already_exists " ,
" type " : " invalid_request " ,
" link " :" https://docs.meilisearch.com/errors#index_already_exists "
} ) ;
2021-12-02 23:03:26 +08:00
assert_eq! ( response [ " error " ] , expected_response ) ;
2021-10-21 20:42:01 +08:00
}
#[ actix_rt::test ]
async fn error_create_with_invalid_index_uid ( ) {
let server = Server ::new ( ) . await ;
let index = server . index ( " test test#! " ) ;
let ( response , code ) = index . create ( None ) . await ;
let expected_response = json! ( {
2022-06-06 18:38:46 +08:00
" message " : " invalid index uid `test test#!`, the uid must be an integer or a string containing only alphanumeric characters a-z A-Z 0-9, hyphens - and underscores _. " ,
2021-10-21 20:42:01 +08:00
" code " : " invalid_index_uid " ,
" type " : " invalid_request " ,
" link " : " https://docs.meilisearch.com/errors#invalid_index_uid "
} ) ;
assert_eq! ( response , expected_response ) ;
assert_eq! ( code , 400 ) ;
}