Skip to content

Commit 6fa6b74

Browse files
committed
Add schema testcase
1 parent a3a6381 commit 6fa6b74

File tree

25 files changed

+360
-1824
lines changed

25 files changed

+360
-1824
lines changed

.rustfmt.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
max_width = 100000
2+
tab_spaces = 4
3+
newline_style = "Unix"
4+
fn_call_width = 100000
5+
fn_params_layout = "Compressed"
6+
chain_width = 100000
7+
merge_derives = true
8+
use_small_heuristics = "Default"

crates/vespera_core/src/route.rs

Lines changed: 17 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -319,48 +319,21 @@ mod tests {
319319

320320
#[test]
321321
fn test_path_item_set_operation() {
322-
let mut path_item = PathItem {
323-
get: None,
324-
post: None,
325-
put: None,
326-
patch: None,
327-
delete: None,
328-
head: None,
329-
options: None,
330-
trace: None,
331-
parameters: None,
332-
summary: None,
333-
description: None,
334-
};
335-
336-
let operation = Operation {
337-
operation_id: Some("test_operation".to_string()),
338-
tags: None,
339-
summary: None,
340-
description: None,
341-
parameters: None,
342-
request_body: None,
343-
responses: BTreeMap::new(),
344-
security: None,
345-
};
322+
let mut path_item = PathItem { get: None, post: None, put: None, patch: None, delete: None, head: None, options: None, trace: None, parameters: None, summary: None, description: None };
323+
324+
let operation = Operation { operation_id: Some("test_operation".to_string()), tags: None, summary: None, description: None, parameters: None, request_body: None, responses: BTreeMap::new(), security: None };
346325

347326
// Test setting GET operation
348327
path_item.set_operation(HttpMethod::Get, operation.clone());
349328
assert!(path_item.get.is_some());
350-
assert_eq!(
351-
path_item.get.as_ref().unwrap().operation_id,
352-
Some("test_operation".to_string())
353-
);
329+
assert_eq!(path_item.get.as_ref().unwrap().operation_id, Some("test_operation".to_string()));
354330

355331
// Test setting POST operation
356332
let mut operation_post = operation.clone();
357333
operation_post.operation_id = Some("post_operation".to_string());
358334
path_item.set_operation(HttpMethod::Post, operation_post);
359335
assert!(path_item.post.is_some());
360-
assert_eq!(
361-
path_item.post.as_ref().unwrap().operation_id,
362-
Some("post_operation".to_string())
363-
);
336+
assert_eq!(path_item.post.as_ref().unwrap().operation_id, Some("post_operation".to_string()));
364337

365338
// Test setting PUT operation
366339
let mut operation_put = operation.clone();
@@ -401,30 +374,9 @@ mod tests {
401374

402375
#[test]
403376
fn test_path_item_get_operation() {
404-
let mut path_item = PathItem {
405-
get: None,
406-
post: None,
407-
put: None,
408-
patch: None,
409-
delete: None,
410-
head: None,
411-
options: None,
412-
trace: None,
413-
parameters: None,
414-
summary: None,
415-
description: None,
416-
};
417-
418-
let operation = Operation {
419-
operation_id: Some("test_operation".to_string()),
420-
tags: None,
421-
summary: None,
422-
description: None,
423-
parameters: None,
424-
request_body: None,
425-
responses: BTreeMap::new(),
426-
security: None,
427-
};
377+
let mut path_item = PathItem { get: None, post: None, put: None, patch: None, delete: None, head: None, options: None, trace: None, parameters: None, summary: None, description: None };
378+
379+
let operation = Operation { operation_id: Some("test_operation".to_string()), tags: None, summary: None, description: None, parameters: None, request_body: None, responses: BTreeMap::new(), security: None };
428380

429381
// Initially, all operations should be None
430382
assert!(path_item.get_operation(&HttpMethod::Get).is_none());
@@ -434,21 +386,15 @@ mod tests {
434386
path_item.set_operation(HttpMethod::Get, operation.clone());
435387
let retrieved = path_item.get_operation(&HttpMethod::Get);
436388
assert!(retrieved.is_some());
437-
assert_eq!(
438-
retrieved.unwrap().operation_id,
439-
Some("test_operation".to_string())
440-
);
389+
assert_eq!(retrieved.unwrap().operation_id, Some("test_operation".to_string()));
441390

442391
// Set POST operation
443392
let mut operation_post = operation.clone();
444393
operation_post.operation_id = Some("post_operation".to_string());
445394
path_item.set_operation(HttpMethod::Post, operation_post);
446395
let retrieved = path_item.get_operation(&HttpMethod::Post);
447396
assert!(retrieved.is_some());
448-
assert_eq!(
449-
retrieved.unwrap().operation_id,
450-
Some("post_operation".to_string())
451-
);
397+
assert_eq!(retrieved.unwrap().operation_id, Some("post_operation".to_string()));
452398

453399
// Test all methods
454400
path_item.set_operation(HttpMethod::Put, operation.clone());
@@ -472,55 +418,19 @@ mod tests {
472418

473419
#[test]
474420
fn test_path_item_set_operation_overwrites() {
475-
let mut path_item = PathItem {
476-
get: None,
477-
post: None,
478-
put: None,
479-
patch: None,
480-
delete: None,
481-
head: None,
482-
options: None,
483-
trace: None,
484-
parameters: None,
485-
summary: None,
486-
description: None,
487-
};
488-
489-
let operation1 = Operation {
490-
operation_id: Some("first".to_string()),
491-
tags: None,
492-
summary: None,
493-
description: None,
494-
parameters: None,
495-
request_body: None,
496-
responses: BTreeMap::new(),
497-
security: None,
498-
};
499-
500-
let operation2 = Operation {
501-
operation_id: Some("second".to_string()),
502-
tags: None,
503-
summary: None,
504-
description: None,
505-
parameters: None,
506-
request_body: None,
507-
responses: BTreeMap::new(),
508-
security: None,
509-
};
421+
let mut path_item = PathItem { get: None, post: None, put: None, patch: None, delete: None, head: None, options: None, trace: None, parameters: None, summary: None, description: None };
422+
423+
let operation1 = Operation { operation_id: Some("first".to_string()), tags: None, summary: None, description: None, parameters: None, request_body: None, responses: BTreeMap::new(), security: None };
424+
425+
let operation2 = Operation { operation_id: Some("second".to_string()), tags: None, summary: None, description: None, parameters: None, request_body: None, responses: BTreeMap::new(), security: None };
510426

511427
// Set first operation
512428
path_item.set_operation(HttpMethod::Get, operation1);
513-
assert_eq!(
514-
path_item.get.as_ref().unwrap().operation_id,
515-
Some("first".to_string())
516-
);
429+
assert_eq!(path_item.get.as_ref().unwrap().operation_id, Some("first".to_string()));
517430

518431
// Overwrite with second operation
519432
path_item.set_operation(HttpMethod::Get, operation2);
520-
assert_eq!(
521-
path_item.get.as_ref().unwrap().operation_id,
522-
Some("second".to_string())
523-
);
433+
assert_eq!(path_item.get.as_ref().unwrap().operation_id, Some("second".to_string()));
524434
}
525435

526436
#[test]

crates/vespera_core/src/schema.rs

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -214,46 +214,7 @@ pub struct Schema {
214214
impl Schema {
215215
/// Create a new schema
216216
pub fn new(schema_type: SchemaType) -> Self {
217-
Self {
218-
ref_path: None,
219-
schema_type: Some(schema_type),
220-
format: None,
221-
title: None,
222-
description: None,
223-
default: None,
224-
example: None,
225-
examples: None,
226-
minimum: None,
227-
maximum: None,
228-
exclusive_minimum: None,
229-
exclusive_maximum: None,
230-
multiple_of: None,
231-
min_length: None,
232-
max_length: None,
233-
pattern: None,
234-
items: None,
235-
prefix_items: None,
236-
min_items: None,
237-
max_items: None,
238-
unique_items: None,
239-
properties: None,
240-
required: None,
241-
additional_properties: None,
242-
min_properties: None,
243-
max_properties: None,
244-
r#enum: None,
245-
all_of: None,
246-
any_of: None,
247-
one_of: None,
248-
not: None,
249-
nullable: None,
250-
read_only: None,
251-
write_only: None,
252-
external_docs: None,
253-
defs: None,
254-
dynamic_anchor: None,
255-
dynamic_ref: None,
256-
}
217+
Self { ref_path: None, schema_type: Some(schema_type), format: None, title: None, description: None, default: None, example: None, examples: None, minimum: None, maximum: None, exclusive_minimum: None, exclusive_maximum: None, multiple_of: None, min_length: None, max_length: None, pattern: None, items: None, prefix_items: None, min_items: None, max_items: None, unique_items: None, properties: None, required: None, additional_properties: None, min_properties: None, max_properties: None, r#enum: None, all_of: None, any_of: None, one_of: None, not: None, nullable: None, read_only: None, write_only: None, external_docs: None, defs: None, dynamic_anchor: None, dynamic_ref: None }
257218
}
258219

259220
/// Create a string schema
@@ -278,19 +239,12 @@ impl Schema {
278239

279240
/// Create an array schema
280241
pub fn array(items: SchemaRef) -> Self {
281-
Self {
282-
items: Some(Box::new(items)),
283-
..Self::new(SchemaType::Array)
284-
}
242+
Self { items: Some(Box::new(items)), ..Self::new(SchemaType::Array) }
285243
}
286244

287245
/// Create an object schema
288246
pub fn object() -> Self {
289-
Self {
290-
properties: Some(BTreeMap::new()),
291-
required: Some(Vec::new()),
292-
..Self::new(SchemaType::Object)
293-
}
247+
Self { properties: Some(BTreeMap::new()), required: Some(Vec::new()), ..Self::new(SchemaType::Object) }
294248
}
295249
}
296250

@@ -371,3 +325,44 @@ pub trait SchemaBuilder: Sized {
371325
// This trait is used as a marker for derive macro
372326
// The actual schema conversion will be implemented separately
373327
}
328+
329+
#[cfg(test)]
330+
mod tests {
331+
use super::*;
332+
use rstest::rstest;
333+
334+
#[rstest]
335+
#[case(Schema::string(), SchemaType::String)]
336+
#[case(Schema::integer(), SchemaType::Integer)]
337+
#[case(Schema::number(), SchemaType::Number)]
338+
#[case(Schema::boolean(), SchemaType::Boolean)]
339+
fn primitive_helpers_set_schema_type(#[case] schema: Schema, #[case] expected: SchemaType) {
340+
assert_eq!(schema.schema_type, Some(expected));
341+
}
342+
343+
#[test]
344+
fn array_helper_sets_type_and_items() {
345+
let item_schema = Schema::boolean();
346+
let schema = Schema::array(SchemaRef::Inline(Box::new(item_schema.clone())));
347+
348+
assert_eq!(schema.schema_type, Some(SchemaType::Array));
349+
let items = schema.items.expect("items should be set");
350+
match *items {
351+
SchemaRef::Inline(inner) => {
352+
assert_eq!(inner.schema_type, Some(SchemaType::Boolean));
353+
}
354+
SchemaRef::Ref(_) => panic!("array helper should set inline items"),
355+
}
356+
}
357+
358+
#[test]
359+
fn object_helper_initializes_collections() {
360+
let schema = Schema::object();
361+
362+
assert_eq!(schema.schema_type, Some(SchemaType::Object));
363+
let props = schema.properties.expect("properties should be initialized");
364+
assert!(props.is_empty());
365+
let required = schema.required.expect("required should be initialized");
366+
assert!(required.is_empty());
367+
}
368+
}

0 commit comments

Comments
 (0)