|
| 1 | +//! Scenario: database_contexts_crud. |
| 2 | +//! |
| 3 | +//! Defined in www.hotdata.dev/api/test-scenarios.yaml — create a scratch |
| 4 | +//! database, upsert a named context document, read it back, confirm it appears |
| 5 | +//! in list_database_contexts, delete the context, then delete the database. |
| 6 | +//! Upserting the same name twice verifies replace-on-write. |
| 7 | +
|
| 8 | +mod common; |
| 9 | + |
| 10 | +use hotdata::apis::{database_context_api, databases_api}; |
| 11 | +use hotdata::models; |
| 12 | + |
| 13 | +#[tokio::test] |
| 14 | +async fn database_contexts_crud() { |
| 15 | + let client = skip_if_no_creds!(); |
| 16 | + let config = client.configuration(); |
| 17 | + |
| 18 | + let database_id = common::create_scratch_database(&client, "dbctx-crud").await; |
| 19 | + let context_name = "sdkci_context"; |
| 20 | + |
| 21 | + // Initial upsert. |
| 22 | + let initial = database_context_api::upsert_database_context( |
| 23 | + config, |
| 24 | + &database_id, |
| 25 | + models::UpsertDatabaseContextRequest::new( |
| 26 | + "first revision".to_string(), |
| 27 | + context_name.to_string(), |
| 28 | + ), |
| 29 | + ) |
| 30 | + .await |
| 31 | + .expect("upsert_database_context (create) should succeed"); |
| 32 | + assert_eq!(initial.context.name, context_name); |
| 33 | + assert_eq!(initial.context.content, "first revision"); |
| 34 | + |
| 35 | + // Upsert the same name again — replace-on-write, not a duplicate. |
| 36 | + database_context_api::upsert_database_context( |
| 37 | + config, |
| 38 | + &database_id, |
| 39 | + models::UpsertDatabaseContextRequest::new( |
| 40 | + "second revision".to_string(), |
| 41 | + context_name.to_string(), |
| 42 | + ), |
| 43 | + ) |
| 44 | + .await |
| 45 | + .expect("upsert_database_context (replace) should succeed"); |
| 46 | + |
| 47 | + let fetched = database_context_api::get_database_context(config, &database_id, context_name) |
| 48 | + .await |
| 49 | + .expect("get_database_context should succeed"); |
| 50 | + assert_eq!(fetched.context.name, context_name); |
| 51 | + assert_eq!( |
| 52 | + fetched.context.content, "second revision", |
| 53 | + "upsert with an existing name should replace the content" |
| 54 | + ); |
| 55 | + |
| 56 | + let listing = database_context_api::list_database_contexts(config, &database_id) |
| 57 | + .await |
| 58 | + .expect("list_database_contexts should succeed"); |
| 59 | + let matches: Vec<_> = listing |
| 60 | + .contexts |
| 61 | + .iter() |
| 62 | + .filter(|c| c.name == context_name) |
| 63 | + .collect(); |
| 64 | + assert_eq!( |
| 65 | + matches.len(), |
| 66 | + 1, |
| 67 | + "expected exactly one context named {context_name} (replace, not append), got {}", |
| 68 | + matches.len() |
| 69 | + ); |
| 70 | + |
| 71 | + database_context_api::delete_database_context(config, &database_id, context_name) |
| 72 | + .await |
| 73 | + .expect("delete_database_context should succeed"); |
| 74 | + |
| 75 | + let after_delete = |
| 76 | + database_context_api::get_database_context(config, &database_id, context_name).await; |
| 77 | + match after_delete { |
| 78 | + Err(err) => assert_eq!( |
| 79 | + common::status_of(&err), |
| 80 | + Some(404), |
| 81 | + "expected 404 after context delete, got {err:?}" |
| 82 | + ), |
| 83 | + Ok(_) => panic!("get_database_context should fail with 404 after delete"), |
| 84 | + } |
| 85 | + |
| 86 | + // Tear down the scratch database. |
| 87 | + databases_api::delete_database(config, &database_id) |
| 88 | + .await |
| 89 | + .expect("delete_database should succeed"); |
| 90 | +} |
0 commit comments