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) ]
2024-09-26 13:41:08 +03:00
use std ::collections ::HashMap ;
2019-11-28 12:42:34 +03:00
use proxmox_api_macro ::api ;
2021-10-07 10:36:06 +03:00
use proxmox_schema as schema ;
use proxmox_schema ::{ ApiType , EnumEntry } ;
2019-11-28 12:42:34 +03:00
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
2024-09-26 13:41:08 +03:00
pub const TEXT_SCHEMA : schema ::Schema = schema ::StringSchema ::new ( " Text. " ) . schema ( ) ;
2019-11-28 13:15:37 +03:00
#[ api(
type : String ,
description : " A string " ,
2020-04-29 11:42:36 +03:00
format : & schema ::ApiStringFormat ::Enum ( & [
EnumEntry ::new ( " ok " , " Ok " ) ,
EnumEntry ::new ( " not-ok " , " Not OK " ) ,
] ) ,
2019-11-28 13:15:37 +03:00
) ]
//#[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 ( ) {
2021-10-07 10:36:06 +03:00
const TEST_SCHEMA : ::proxmox_schema ::Schema = ::proxmox_schema ::StringSchema ::new ( " A string " )
. format ( & schema ::ApiStringFormat ::Enum ( & [
EnumEntry ::new ( " ok " , " Ok " ) ,
EnumEntry ::new ( " not-ok " , " Not OK " ) ,
] ) )
. schema ( ) ;
2020-01-08 12:41:29 +03:00
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 ( ) {
2021-10-07 10:36:06 +03:00
pub const TEST_SCHEMA : ::proxmox_schema ::Schema = ::proxmox_schema ::ObjectSchema ::new (
" An example of a simple struct type. " ,
& [
(
" another " ,
true ,
& ::proxmox_schema ::StringSchema ::new ( " An optional auto-derived value for testing: " )
2020-01-08 12:41:29 +03:00
. schema ( ) ,
2021-10-07 10:36:06 +03:00
) ,
(
" test_string " ,
false ,
& ::proxmox_schema ::StringSchema ::new ( " A test string. " ) . schema ( ) ,
) ,
] ,
)
. 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 ( ) {
2021-10-07 10:36:06 +03:00
const TEST_SCHEMA : ::proxmox_schema ::Schema = ::proxmox_schema ::ObjectSchema ::new (
2020-07-16 15:13:13 +03:00
" An example of a struct with renamed fields. " ,
& [
(
" SomeOther " ,
true ,
2021-10-07 10:36:06 +03:00
& ::proxmox_schema ::StringSchema ::new ( " An optional auto-derived value for testing: " )
. schema ( ) ,
2020-07-16 15:13:13 +03:00
) ,
(
" test-string " ,
false ,
2021-10-07 10:36:06 +03:00
& ::proxmox_schema ::StringSchema ::new ( " A test string. " ) . schema ( ) ,
2020-07-16 15:13:13 +03:00
) ,
] ,
)
. schema ( ) ;
2020-01-08 12:41:29 +03:00
assert_eq! ( TEST_SCHEMA , RenamedStruct ::API_SCHEMA ) ;
2019-11-28 12:42:34 +03:00
}
2019-12-04 13:52:03 +03:00
#[ api ]
2022-12-12 13:30:46 +03:00
#[ derive(Default, 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-04-29 11:42:36 +03:00
/// The first kind.
2020-01-07 17:29:56 +03:00
#[ serde(rename = " onekind " ) ]
OneKind ,
2020-04-29 11:42:36 +03:00
/// Some other kind.
2022-12-12 13:30:46 +03:00
#[ default ]
2020-01-07 17:29:56 +03:00
AnotherKind ,
2020-04-29 11:42:36 +03:00
/// And yet another.
2020-01-07 17:29:56 +03:00
SelectionNumberThree ,
2019-11-28 14:21:46 +03:00
}
2020-01-08 12:41:29 +03:00
#[ test ]
fn selection_test ( ) {
2021-10-07 10:36:06 +03:00
const TEST_SCHEMA : ::proxmox_schema ::Schema = ::proxmox_schema ::StringSchema ::new (
2020-07-16 15:13:13 +03:00
" A selection of either \' onekind \' , \' another-kind \' or \' selection-number-three \' . " ,
)
2021-10-07 10:36:06 +03:00
. format ( & ::proxmox_schema ::ApiStringFormat ::Enum ( & [
2020-07-16 15:13:13 +03:00
EnumEntry ::new ( " onekind " , " The first kind. " ) ,
EnumEntry ::new ( " another-kind " , " Some other kind. " ) ,
EnumEntry ::new ( " selection-number-three " , " And yet another. " ) ,
] ) )
2022-12-12 13:30:46 +03:00
. default ( " another-kind " )
2020-07-16 15:13:13 +03:00
. schema ( ) ;
2020-01-08 12:41:29 +03:00
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
}
} ,
2020-12-18 14:25:51 +03:00
returns : { optional : true , type : Boolean } ,
2019-11-28 12:42:34 +03:00
) ]
/// 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 ( ) {
2021-10-07 10:36:06 +03:00
const TEST_METHOD : ::proxmox_router ::ApiMethod = ::proxmox_router ::ApiMethod ::new (
& ::proxmox_router ::ApiHandler ::Sync ( & api_function_string_check ) ,
& ::proxmox_schema ::ObjectSchema ::new (
2020-01-08 12:41:29 +03:00
" Check a string. " ,
& [
2020-07-10 11:39:49 +03:00
( " arg " , false , & OkString ::API_SCHEMA ) ,
( " selection " , false , & Selection ::API_SCHEMA ) ,
2020-01-08 12:41:29 +03:00
] ,
) ,
)
2021-10-07 10:36:06 +03:00
. returns ( ::proxmox_schema ::ReturnType ::new (
2020-12-18 14:25:51 +03:00
true ,
2021-10-07 10:36:06 +03:00
& ::proxmox_schema ::BooleanSchema ::new ( " Whether the string was \" ok \" . " ) . schema ( ) ,
2020-12-18 14:25:51 +03:00
) )
2020-01-08 12:41:29 +03:00
. 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 ,
}
2024-09-26 13:41:08 +03:00
#[ api(
properties : { } ,
additional_properties : " rest " ,
) ]
#[ derive(Deserialize) ]
/// Some Description.
pub struct UnspecifiedData {
/// Text.
field : String ,
/// Remaining data.
rest : HashMap < String , Value > ,
}
#[ test ]
fn additional_properties_test ( ) {
const TEST_UNSPECIFIED : ::proxmox_schema ::Schema =
::proxmox_schema ::ObjectSchema ::new ( " Some Description. " , & [ ( " field " , false , & TEXT_SCHEMA ) ] )
. additional_properties ( true )
. schema ( ) ;
assert_eq! ( TEST_UNSPECIFIED , UnspecifiedData ::API_SCHEMA ) ;
}