2021-02-18 19:50:52 +01:00
use crate ::common ::Server ;
2021-09-30 11:20:16 +02:00
use serde_json ::{ json , Value } ;
2021-02-18 19:50:52 +01: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-07-05 11:21:02 +02:00
assert_eq! ( code , 201 ) ;
2021-02-18 19:50:52 +01:00
assert_eq! ( response [ " uid " ] , " test " ) ;
2021-03-15 18:38:13 +01:00
assert_eq! ( response [ " name " ] , " test " ) ;
2021-02-18 19:50:52 +01:00
assert! ( response . get ( " createdAt " ) . is_some ( ) ) ;
assert! ( response . get ( " updatedAt " ) . is_some ( ) ) ;
assert_eq! ( response [ " createdAt " ] , response [ " updatedAt " ] ) ;
assert_eq! ( response [ " primaryKey " ] , Value ::Null ) ;
2021-03-15 18:38:13 +01:00
assert_eq! ( response . as_object ( ) . unwrap ( ) . len ( ) , 5 ) ;
2021-02-18 19:50:52 +01: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-07-05 11:21:02 +02:00
assert_eq! ( code , 201 ) ;
2021-02-18 19:50:52 +01:00
assert_eq! ( response [ " uid " ] , " test " ) ;
2021-03-15 18:38:13 +01:00
assert_eq! ( response [ " name " ] , " test " ) ;
2021-02-18 19:50:52 +01:00
assert! ( response . get ( " createdAt " ) . is_some ( ) ) ;
assert! ( response . get ( " updatedAt " ) . is_some ( ) ) ;
2021-03-12 14:48:43 +01:00
//assert_eq!(response["createdAt"], response["updatedAt"]);
2021-02-18 19:50:52 +01:00
assert_eq! ( response [ " primaryKey " ] , " primary " ) ;
2021-03-15 18:38:13 +01:00
assert_eq! ( response . as_object ( ) . unwrap ( ) . len ( ) , 5 ) ;
2021-02-18 19:50:52 +01:00
}
2021-09-30 11:20:16 +02: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 ) ;
index . wait_update_id ( 0 ) . await ;
let ( response , code ) = index . get ( ) . await ;
assert_eq! ( code , 200 ) ;
assert_eq! ( response [ " primaryKey " ] , Value ::Null ) ;
}
2021-02-18 20:32:34 +01: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 ;
assert_eq! ( index1 . get ( ) . await . 1 , 200 ) ;
assert_eq! ( index2 . get ( ) . await . 1 , 200 ) ;
assert_eq! ( index3 . get ( ) . await . 1 , 200 ) ;
2021-06-15 22:21:52 +02:00
assert_eq! ( index4 . get ( ) . await . 1 , 404 ) ;
2021-02-18 20:32:34 +01:00
}
2021-10-21 14:42:01 +02: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 ;
assert_eq! ( code , 201 ) ;
let ( response , code ) = index . create ( Some ( " primary " ) ) . await ;
let expected_response = json! ( {
2021-10-26 19:36:48 +02:00
" message " : " Index `test` already exists. " ,
2021-10-21 14:42:01 +02:00
" code " : " index_already_exists " ,
" type " : " invalid_request " ,
" link " :" https://docs.meilisearch.com/errors#index_already_exists "
} ) ;
assert_eq! ( response , expected_response ) ;
assert_eq! ( code , 409 ) ;
}
#[ 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! ( {
2021-10-26 19:36:48 +02:00
" message " : " `test test#!` is not a valid index uid. Index uid can be an integer or a string containing only alphanumeric characters, hyphens (-) and underscores (_). " ,
2021-10-21 14:42:01 +02: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 ) ;
}