added tests, use specific CI
This commit is contained in:
@@ -234,3 +234,138 @@ pub async fn sync_clear_user(
|
||||
db_service.execute(query).await.map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_sync_queue_item_serialization() {
|
||||
let item = SyncQueueItem {
|
||||
id: 1,
|
||||
user_id: "user-123".to_string(),
|
||||
operation: "favorite".to_string(),
|
||||
item_id: Some("item-456".to_string()),
|
||||
payload: Some(r#"{"isFavorite": true}"#.to_string()),
|
||||
status: "pending".to_string(),
|
||||
retry_count: 0,
|
||||
created_at: Some("2024-02-14T08:00:00Z".to_string()),
|
||||
error_message: None,
|
||||
};
|
||||
|
||||
// Should serialize successfully
|
||||
let json = serde_json::to_string(&item);
|
||||
assert!(json.is_ok());
|
||||
|
||||
let serialized = json.unwrap();
|
||||
assert!(serialized.contains("user-123"));
|
||||
assert!(serialized.contains("favorite"));
|
||||
assert!(serialized.contains("pending"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sync_queue_item_with_error() {
|
||||
let item = SyncQueueItem {
|
||||
id: 2,
|
||||
user_id: "user-789".to_string(),
|
||||
operation: "update_progress".to_string(),
|
||||
item_id: Some("item-999".to_string()),
|
||||
payload: None,
|
||||
status: "failed".to_string(),
|
||||
retry_count: 3,
|
||||
created_at: Some("2024-02-14T07:00:00Z".to_string()),
|
||||
error_message: Some("Connection timeout".to_string()),
|
||||
};
|
||||
|
||||
let json = serde_json::to_string(&item).unwrap();
|
||||
assert!(json.contains("failed"));
|
||||
assert!(json.contains("Connection timeout"));
|
||||
assert!(json.contains("3")); // retry_count
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sync_queue_item_without_optional_fields() {
|
||||
let item = SyncQueueItem {
|
||||
id: 3,
|
||||
user_id: "user-000".to_string(),
|
||||
operation: "clear_progress".to_string(),
|
||||
item_id: None,
|
||||
payload: None,
|
||||
status: "completed".to_string(),
|
||||
retry_count: 0,
|
||||
created_at: None,
|
||||
error_message: None,
|
||||
};
|
||||
|
||||
let json = serde_json::to_string(&item).unwrap();
|
||||
assert!(json.contains("completed"));
|
||||
assert!(json.contains("null") || json.contains("\"itemId\":null"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sync_status_values() {
|
||||
// Verify all expected status values
|
||||
let valid_statuses = vec!["pending", "processing", "completed", "failed"];
|
||||
|
||||
for status in valid_statuses {
|
||||
let item = SyncQueueItem {
|
||||
id: 1,
|
||||
user_id: "test".to_string(),
|
||||
operation: "test".to_string(),
|
||||
item_id: None,
|
||||
payload: None,
|
||||
status: status.to_string(),
|
||||
retry_count: 0,
|
||||
created_at: None,
|
||||
error_message: None,
|
||||
};
|
||||
|
||||
let json = serde_json::to_string(&item).unwrap();
|
||||
assert!(json.contains(status));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_query_param_generation() {
|
||||
// Test QueryParam generation for sync operations
|
||||
let user_id = "user-123".to_string();
|
||||
let operation = "favorite".to_string();
|
||||
|
||||
let params: Vec<QueryParam> = vec![
|
||||
QueryParam::String(user_id.clone()),
|
||||
QueryParam::String(operation.clone()),
|
||||
QueryParam::Null,
|
||||
QueryParam::Null,
|
||||
];
|
||||
|
||||
assert_eq!(params.len(), 4);
|
||||
assert!(matches!(params[0], QueryParam::String(_)));
|
||||
assert!(matches!(params[1], QueryParam::String(_)));
|
||||
assert!(matches!(params[2], QueryParam::Null));
|
||||
assert!(matches!(params[3], QueryParam::Null));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_retry_count_increment() {
|
||||
// Verify retry count management
|
||||
let mut item = SyncQueueItem {
|
||||
id: 1,
|
||||
user_id: "user-123".to_string(),
|
||||
operation: "favorite".to_string(),
|
||||
item_id: None,
|
||||
payload: None,
|
||||
status: "pending".to_string(),
|
||||
retry_count: 0,
|
||||
created_at: None,
|
||||
error_message: None,
|
||||
};
|
||||
|
||||
// Simulate retries
|
||||
for i in 1..=5 {
|
||||
item.retry_count = i;
|
||||
item.status = if i < 3 { "pending" } else { "failed" }.to_string();
|
||||
|
||||
assert!(item.retry_count == i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user