Skip to content

Commit d4fcc89

Browse files
fix: correct mock response formats for tasks and databases (#45)
- mock_tasks_list now returns a direct array (matches get_all_tasks() return type) - mock_databases_list now returns the correct nested subscription structure (matches AccountSubscriptionDatabases expected by get_subscription_databases()) - Add integration tests that verify mock helpers work with actual handlers
1 parent 5bc3bc3 commit d4fcc89

File tree

1 file changed

+97
-6
lines changed

1 file changed

+97
-6
lines changed

src/testing/server.rs

Lines changed: 97 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,30 @@ impl MockCloudServer {
180180
// =========================================================================
181181

182182
/// Mock the databases list endpoint (GET /subscriptions/{id}/databases)
183+
///
184+
/// Returns the correct nested structure expected by `get_subscription_databases()`:
185+
/// ```json
186+
/// {
187+
/// "accountId": 12345,
188+
/// "subscription": [{
189+
/// "subscriptionId": 123,
190+
/// "numberOfDatabases": 2,
191+
/// "databases": [...]
192+
/// }]
193+
/// }
194+
/// ```
183195
pub async fn mock_databases_list(&self, subscription_id: i32, databases: Vec<Value>) {
184196
Mock::given(method("GET"))
185197
.and(path(format!("/subscriptions/{subscription_id}/databases")))
186198
.and(header("x-api-key", "test-key"))
187199
.and(header("x-api-secret-key", "test-secret"))
188200
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
189-
"subscriptionId": subscription_id,
190-
"databases": databases
201+
"accountId": 12345,
202+
"subscription": [{
203+
"subscriptionId": subscription_id,
204+
"numberOfDatabases": databases.len(),
205+
"databases": databases
206+
}]
191207
})))
192208
.mount(&self.server)
193209
.await;
@@ -256,14 +272,14 @@ impl MockCloudServer {
256272
// =========================================================================
257273

258274
/// Mock the tasks list endpoint (GET /tasks)
275+
///
276+
/// Returns a direct array since `get_all_tasks()` returns `Result<Vec<TaskStateUpdate>>`.
259277
pub async fn mock_tasks_list(&self, tasks: Vec<Value>) {
260278
Mock::given(method("GET"))
261279
.and(path("/tasks"))
262280
.and(header("x-api-key", "test-key"))
263281
.and(header("x-api-secret-key", "test-secret"))
264-
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
265-
"tasks": tasks
266-
})))
282+
.respond_with(ResponseTemplate::new(200).set_body_json(tasks))
267283
.mount(&self.server)
268284
.await;
269285
}
@@ -373,7 +389,8 @@ impl MockCloudServer {
373389
#[cfg(test)]
374390
mod tests {
375391
use super::*;
376-
use crate::AccountHandler;
392+
use crate::testing::fixtures::{DatabaseFixture, SubscriptionFixture, TaskFixture};
393+
use crate::{AccountHandler, DatabaseHandler, SubscriptionHandler, TaskHandler};
377394

378395
#[tokio::test]
379396
async fn test_mock_server_start() {
@@ -420,4 +437,78 @@ mod tests {
420437

421438
assert!(result.is_err());
422439
}
440+
441+
#[tokio::test]
442+
async fn test_mock_subscriptions_list_with_handler() {
443+
let server = MockCloudServer::start().await;
444+
445+
server
446+
.mock_subscriptions_list(vec![
447+
SubscriptionFixture::new(123, "Production").build(),
448+
SubscriptionFixture::new(456, "Staging").build(),
449+
])
450+
.await;
451+
452+
let client = server.client();
453+
let handler = SubscriptionHandler::new(client);
454+
let result = handler.get_all_subscriptions().await.unwrap();
455+
456+
assert!(result.subscriptions.is_some());
457+
let subs = result.subscriptions.unwrap();
458+
assert_eq!(subs.len(), 2);
459+
assert_eq!(subs[0].id, Some(123));
460+
assert_eq!(subs[1].id, Some(456));
461+
}
462+
463+
#[tokio::test]
464+
async fn test_mock_databases_list_with_handler() {
465+
let server = MockCloudServer::start().await;
466+
467+
server
468+
.mock_databases_list(
469+
123,
470+
vec![
471+
DatabaseFixture::new(1, "cache-db").build(),
472+
DatabaseFixture::new(2, "session-db").build(),
473+
],
474+
)
475+
.await;
476+
477+
let client = server.client();
478+
let handler = DatabaseHandler::new(client);
479+
let result = handler
480+
.get_subscription_databases(123, None, None)
481+
.await
482+
.unwrap();
483+
484+
assert!(!result.subscription.is_empty());
485+
let sub_info = &result.subscription[0];
486+
assert_eq!(sub_info.subscription_id, 123);
487+
assert_eq!(sub_info.databases.len(), 2);
488+
assert_eq!(sub_info.databases[0].name, Some("cache-db".to_string()));
489+
}
490+
491+
#[tokio::test]
492+
async fn test_mock_tasks_list_with_handler() {
493+
let server = MockCloudServer::start().await;
494+
495+
server
496+
.mock_tasks_list(vec![
497+
TaskFixture::new("task-1")
498+
.status("processing-completed")
499+
.build(),
500+
TaskFixture::new("task-2")
501+
.status("processing-in-progress")
502+
.build(),
503+
])
504+
.await;
505+
506+
let client = server.client();
507+
let handler = TaskHandler::new(client);
508+
let result = handler.get_all_tasks().await.unwrap();
509+
510+
assert_eq!(result.len(), 2);
511+
assert_eq!(result[0].task_id, Some("task-1".to_string()));
512+
assert_eq!(result[1].task_id, Some("task-2".to_string()));
513+
}
423514
}

0 commit comments

Comments
 (0)