2019-11-28 12:42:34 +03:00
//! This should test the usage of "external" types. For any unrecognized schema type we expect the
//! type's impl to provide an `pub const API_SCHEMA: &Schema`.
2020-01-15 16:10:36 +03:00
#![ allow(dead_code) ]
2019-11-28 12:42:34 +03:00
use proxmox ::api ::schema ;
use proxmox_api_macro ::api ;
2020-04-17 19:34:10 +03:00
use anyhow ::Error ;
2019-11-28 15:43:25 +03:00
use serde ::Deserialize ;
2019-11-28 13:15:37 +03:00
use serde_json ::Value ;
2019-11-28 12:42:34 +03:00
2019-11-28 13:15:37 +03:00
#[ api(
type : String ,
description : " A string " ,
format : & schema ::ApiStringFormat ::Enum ( & [ " ok " , " not-ok " ] ) ,
) ]
//#[derive(Clone, Debug, Deserialize, Serialize)]
2019-11-28 12:42:34 +03:00
pub struct OkString ( String ) ;
2019-11-28 13:15:37 +03:00
2020-01-08 12:41:29 +03:00
#[ test ]
fn ok_string ( ) {
const TEST_SCHEMA : & 'static ::proxmox ::api ::schema ::Schema =
& ::proxmox ::api ::schema ::StringSchema ::new ( " A string " )
. format ( & schema ::ApiStringFormat ::Enum ( & [ " ok " , " not-ok " ] ) )
. schema ( ) ;
assert_eq! ( TEST_SCHEMA , OkString ::API_SCHEMA ) ;
}
2020-01-08 12:09:41 +03:00
#[ api ]
2020-01-08 12:41:29 +03:00
/// An example of a simple struct type.
pub struct TestStruct {
2020-01-07 15:09:19 +03:00
/// A test string.
2020-01-08 12:41:29 +03:00
test_string : String ,
2020-01-07 15:37:33 +03:00
/// An optional auto-derived value for testing:
another : Option < String > ,
2020-01-07 13:59:24 +03:00
}
2020-01-08 12:41:29 +03:00
#[ test ]
fn test_struct ( ) {
pub const TEST_SCHEMA : & 'static ::proxmox ::api ::schema ::Schema =
& ::proxmox ::api ::schema ::ObjectSchema ::new (
" An example of a simple struct type. " ,
& [
(
" another " ,
true ,
& ::proxmox ::api ::schema ::StringSchema ::new (
" An optional auto-derived value for testing: " ,
)
. schema ( ) ,
) ,
2020-01-23 15:15:59 +03:00
(
" test_string " ,
false ,
& ::proxmox ::api ::schema ::StringSchema ::new ( " A test string. " ) . schema ( ) ,
) ,
2020-01-08 12:41:29 +03:00
] ,
)
2019-11-28 12:42:34 +03:00
. schema ( ) ;
2020-01-08 12:41:29 +03:00
assert_eq! ( TEST_SCHEMA , TestStruct ::API_SCHEMA ) ;
}
#[ api ]
#[ derive(Deserialize) ]
#[ serde(rename_all = " kebab-case " ) ]
/// An example of a struct with renamed fields.
pub struct RenamedStruct {
/// A test string.
test_string : String ,
/// An optional auto-derived value for testing:
#[ serde(rename = " SomeOther " ) ]
another : Option < String > ,
}
#[ test ]
fn renamed_struct ( ) {
const TEST_SCHEMA : & 'static ::proxmox ::api ::schema ::Schema =
& ::proxmox ::api ::schema ::ObjectSchema ::new (
" An example of a struct with renamed fields. " ,
& [
(
" SomeOther " ,
true ,
& ::proxmox ::api ::schema ::StringSchema ::new (
" An optional auto-derived value for testing: " ,
)
. schema ( ) ,
) ,
2020-01-23 15:15:59 +03:00
(
" test-string " ,
false ,
& ::proxmox ::api ::schema ::StringSchema ::new ( " A test string. " ) . schema ( ) ,
) ,
2020-01-08 12:41:29 +03:00
] ,
)
. schema ( ) ;
assert_eq! ( TEST_SCHEMA , RenamedStruct ::API_SCHEMA ) ;
2019-11-28 12:42:34 +03:00
}
2019-12-04 13:52:03 +03:00
#[ api ]
2019-11-28 15:42:46 +03:00
#[ derive(Deserialize) ]
2020-01-07 17:29:56 +03:00
#[ serde(rename_all = " kebab-case " ) ]
/// A selection of either 'onekind', 'another-kind' or 'selection-number-three'.
2019-11-28 14:21:46 +03:00
pub enum Selection {
2020-01-07 17:29:56 +03:00
#[ serde(rename = " onekind " ) ]
OneKind ,
AnotherKind ,
SelectionNumberThree ,
2019-11-28 14:21:46 +03:00
}
2020-01-08 12:41:29 +03:00
#[ test ]
fn selection_test ( ) {
const TEST_SCHEMA : & 'static ::proxmox ::api ::schema ::Schema =
& ::proxmox ::api ::schema ::StringSchema ::new (
" A selection of either \' onekind \' , \' another-kind \' or \' selection-number-three \' . " ,
)
. format ( & ::proxmox ::api ::schema ::ApiStringFormat ::Enum ( & [
" onekind " ,
" another-kind " ,
" selection-number-three " ,
] ) )
. schema ( ) ;
assert_eq! ( TEST_SCHEMA , Selection ::API_SCHEMA ) ;
}
2019-11-28 12:42:34 +03:00
// Initial test:
#[ api(
input : {
properties : {
arg : { type : OkString } ,
2019-11-28 15:49:14 +03:00
selection : { type : Selection } ,
2019-11-28 12:42:34 +03:00
}
} ,
returns : { type : Boolean } ,
) ]
/// Check a string.
///
/// Returns: Whether the string was "ok".
2019-11-28 15:49:14 +03:00
pub fn string_check ( arg : Value , selection : Selection ) -> Result < bool , Error > {
2019-11-28 13:15:37 +03:00
let _ = arg ;
2019-11-28 15:49:14 +03:00
let _ = selection ;
2019-11-28 13:15:37 +03:00
panic! ( " body " )
2019-11-28 12:42:34 +03:00
}
2020-01-08 12:41:29 +03:00
#[ test ]
fn string_check_schema_test ( ) {
const TEST_METHOD : ::proxmox ::api ::ApiMethod = ::proxmox ::api ::ApiMethod ::new (
& ::proxmox ::api ::ApiHandler ::Sync ( & api_function_string_check ) ,
& ::proxmox ::api ::schema ::ObjectSchema ::new (
" Check a string. " ,
& [
( " arg " , false , OkString ::API_SCHEMA ) ,
( " selection " , false , Selection ::API_SCHEMA ) ,
] ,
) ,
)
. returns ( & ::proxmox ::api ::schema ::BooleanSchema ::new ( " Whether the string was \" ok \" . " ) . schema ( ) )
. protected ( false ) ;
assert_eq! ( TEST_METHOD , API_METHOD_STRING_CHECK ) ;
}
2020-01-08 13:23:48 +03:00
#[ api(
properties : {
" a-field " : {
description : " Some description. " ,
} ,
} ,
) ]
#[ derive(Deserialize) ]
#[ serde(rename_all = " kebab-case " ) ]
/// Some Description.
pub struct RenamedAndDescribed {
a_field : String ,
}