redis-cloud 0.9.5

Redis Cloud REST API client library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//! Builder-pattern fixtures for Redis Cloud API responses

use serde_json::{Value, json};

/// Fixture for building subscription responses
///
/// # Example
///
/// ```rust,ignore
/// use redis_cloud::testing::SubscriptionFixture;
///
/// let subscription = SubscriptionFixture::new(123, "Production")
///     .status("active")
///     .payment_method_type("credit-card")
///     .build();
/// ```
pub struct SubscriptionFixture {
    id: i32,
    name: String,
    status: String,
    payment_method_type: Option<String>,
    memory_storage: Option<String>,
    cloud_provider: Option<String>,
    region: Option<String>,
}

impl SubscriptionFixture {
    /// Create a new subscription fixture with required fields
    pub fn new(id: i32, name: impl Into<String>) -> Self {
        Self {
            id,
            name: name.into(),
            status: "active".to_string(),
            payment_method_type: None,
            memory_storage: None,
            cloud_provider: None,
            region: None,
        }
    }

    /// Set the subscription status
    pub fn status(mut self, status: impl Into<String>) -> Self {
        self.status = status.into();
        self
    }

    /// Set the payment method type
    pub fn payment_method_type(mut self, payment_method_type: impl Into<String>) -> Self {
        self.payment_method_type = Some(payment_method_type.into());
        self
    }

    /// Set the memory storage type
    pub fn memory_storage(mut self, memory_storage: impl Into<String>) -> Self {
        self.memory_storage = Some(memory_storage.into());
        self
    }

    /// Set the cloud provider
    pub fn cloud_provider(mut self, provider: impl Into<String>) -> Self {
        self.cloud_provider = Some(provider.into());
        self
    }

    /// Set the region
    pub fn region(mut self, region: impl Into<String>) -> Self {
        self.region = Some(region.into());
        self
    }

    /// Build the subscription as a JSON Value
    pub fn build(self) -> Value {
        let mut sub = json!({
            "id": self.id,
            "name": self.name,
            "status": self.status
        });

        if let Some(pmt) = self.payment_method_type {
            sub["paymentMethodType"] = json!(pmt);
        }
        if let Some(ms) = self.memory_storage {
            sub["memoryStorage"] = json!(ms);
        }
        if let Some(provider) = self.cloud_provider {
            sub["cloudProviders"] = json!([{
                "provider": provider,
                "regions": self.region.map(|r| json!([{"region": r}])).unwrap_or(json!([]))
            }]);
        }

        sub
    }
}

/// Fixture for building database responses
///
/// # Example
///
/// ```rust,ignore
/// use redis_cloud::testing::DatabaseFixture;
///
/// let database = DatabaseFixture::new(456, "my-redis-db")
///     .memory_limit_in_gb(1.0)
///     .protocol("redis")
///     .build();
/// ```
pub struct DatabaseFixture {
    database_id: i32,
    name: String,
    status: String,
    memory_limit_in_gb: f64,
    protocol: Option<String>,
    data_persistence: Option<String>,
    replication: Option<bool>,
    throughput_measurement: Option<Value>,
    public_endpoint: Option<String>,
    private_endpoint: Option<String>,
}

impl DatabaseFixture {
    /// Create a new database fixture with required fields
    pub fn new(database_id: i32, name: impl Into<String>) -> Self {
        Self {
            database_id,
            name: name.into(),
            status: "active".to_string(),
            memory_limit_in_gb: 1.0,
            protocol: None,
            data_persistence: None,
            replication: None,
            throughput_measurement: None,
            public_endpoint: None,
            private_endpoint: None,
        }
    }

    /// Set the database status
    pub fn status(mut self, status: impl Into<String>) -> Self {
        self.status = status.into();
        self
    }

    /// Set the memory limit in GB
    pub fn memory_limit_in_gb(mut self, limit: f64) -> Self {
        self.memory_limit_in_gb = limit;
        self
    }

    /// Set the database protocol
    pub fn protocol(mut self, protocol: impl Into<String>) -> Self {
        self.protocol = Some(protocol.into());
        self
    }

    /// Set the data persistence option
    pub fn data_persistence(mut self, persistence: impl Into<String>) -> Self {
        self.data_persistence = Some(persistence.into());
        self
    }

    /// Set whether replication is enabled
    pub fn replication(mut self, enabled: bool) -> Self {
        self.replication = Some(enabled);
        self
    }

    /// Set the throughput measurement
    pub fn throughput(mut self, by: &str, value: i32) -> Self {
        self.throughput_measurement = Some(json!({
            "by": by,
            "value": value
        }));
        self
    }

    /// Set the public endpoint
    pub fn public_endpoint(mut self, endpoint: impl Into<String>) -> Self {
        self.public_endpoint = Some(endpoint.into());
        self
    }

    /// Set the private endpoint
    pub fn private_endpoint(mut self, endpoint: impl Into<String>) -> Self {
        self.private_endpoint = Some(endpoint.into());
        self
    }

    /// Build the database as a JSON Value
    pub fn build(self) -> Value {
        let mut db = json!({
            "databaseId": self.database_id,
            "name": self.name,
            "status": self.status,
            "memoryLimitInGb": self.memory_limit_in_gb
        });

        if let Some(protocol) = self.protocol {
            db["protocol"] = json!(protocol);
        }
        if let Some(persistence) = self.data_persistence {
            db["dataPersistence"] = json!(persistence);
        }
        if let Some(replication) = self.replication {
            db["replication"] = json!(replication);
        }
        if let Some(throughput) = self.throughput_measurement {
            db["throughputMeasurement"] = throughput;
        }
        if let Some(endpoint) = self.public_endpoint {
            db["publicEndpoint"] = json!(endpoint);
        }
        if let Some(endpoint) = self.private_endpoint {
            db["privateEndpoint"] = json!(endpoint);
        }

        db
    }
}

/// Fixture for building task responses
///
/// # Example
///
/// ```rust,ignore
/// use redis_cloud::testing::TaskFixture;
///
/// let task = TaskFixture::new("task-123")
///     .command_type("subscriptionCreateRequest")
///     .status("processing-completed")
///     .resource_id(456)
///     .build();
/// ```
pub struct TaskFixture {
    task_id: String,
    command_type: Option<String>,
    status: String,
    description: Option<String>,
    resource_id: Option<i32>,
    error: Option<String>,
}

impl TaskFixture {
    /// Create a new task fixture with required fields
    pub fn new(task_id: impl Into<String>) -> Self {
        Self {
            task_id: task_id.into(),
            command_type: None,
            status: "processing-completed".to_string(),
            description: None,
            resource_id: None,
            error: None,
        }
    }

    /// Create a completed task fixture
    pub fn completed(task_id: impl Into<String>, resource_id: i32) -> Self {
        Self {
            task_id: task_id.into(),
            command_type: None,
            status: "processing-completed".to_string(),
            description: Some("Task completed successfully".to_string()),
            resource_id: Some(resource_id),
            error: None,
        }
    }

    /// Create a failed task fixture
    pub fn failed(task_id: impl Into<String>, error: impl Into<String>) -> Self {
        Self {
            task_id: task_id.into(),
            command_type: None,
            status: "processing-error".to_string(),
            description: Some("Task failed".to_string()),
            resource_id: None,
            error: Some(error.into()),
        }
    }

    /// Set the command type
    pub fn command_type(mut self, command_type: impl Into<String>) -> Self {
        self.command_type = Some(command_type.into());
        self
    }

    /// Set the task status
    pub fn status(mut self, status: impl Into<String>) -> Self {
        self.status = status.into();
        self
    }

    /// Set the task description
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Set the resource ID in the response
    pub fn resource_id(mut self, resource_id: i32) -> Self {
        self.resource_id = Some(resource_id);
        self
    }

    /// Set an error message
    pub fn error(mut self, error: impl Into<String>) -> Self {
        self.error = Some(error.into());
        self
    }

    /// Build the task as a JSON Value
    pub fn build(self) -> Value {
        let mut task = json!({
            "taskId": self.task_id,
            "status": self.status
        });

        if let Some(ct) = self.command_type {
            task["commandType"] = json!(ct);
        }
        if let Some(desc) = self.description {
            task["description"] = json!(desc);
        }

        let mut response = json!({});
        if let Some(rid) = self.resource_id {
            response["resourceId"] = json!(rid);
        }
        if let Some(err) = self.error {
            response["error"] = json!(err);
        }
        if !response.as_object().unwrap().is_empty() {
            task["response"] = response;
        }

        task
    }
}

/// Fixture for building account responses
///
/// # Example
///
/// ```rust,ignore
/// use redis_cloud::testing::AccountFixture;
///
/// let account = AccountFixture::new(12345, "My Account")
///     .marketplace_status("active")
///     .build();
/// ```
pub struct AccountFixture {
    id: i32,
    name: String,
    marketplace_status: Option<String>,
    created_timestamp: Option<String>,
    updated_timestamp: Option<String>,
}

impl AccountFixture {
    /// Create a new account fixture with required fields
    pub fn new(id: i32, name: impl Into<String>) -> Self {
        Self {
            id,
            name: name.into(),
            marketplace_status: None,
            created_timestamp: None,
            updated_timestamp: None,
        }
    }

    /// Set the marketplace status
    pub fn marketplace_status(mut self, status: impl Into<String>) -> Self {
        self.marketplace_status = Some(status.into());
        self
    }

    /// Set the created timestamp
    pub fn created_timestamp(mut self, timestamp: impl Into<String>) -> Self {
        self.created_timestamp = Some(timestamp.into());
        self
    }

    /// Set the updated timestamp
    pub fn updated_timestamp(mut self, timestamp: impl Into<String>) -> Self {
        self.updated_timestamp = Some(timestamp.into());
        self
    }

    /// Build the account as a JSON Value (wrapped in RootAccount format)
    pub fn build(self) -> Value {
        let mut account = json!({
            "id": self.id,
            "name": self.name
        });

        if let Some(status) = self.marketplace_status {
            account["marketplaceStatus"] = json!(status);
        }
        if let Some(ts) = self.created_timestamp {
            account["createdTimestamp"] = json!(ts);
        }
        if let Some(ts) = self.updated_timestamp {
            account["updatedTimestamp"] = json!(ts);
        }

        json!({
            "account": account,
            "links": []
        })
    }

    /// Build just the account object (not wrapped)
    pub fn build_account_only(self) -> Value {
        let mut account = json!({
            "id": self.id,
            "name": self.name
        });

        if let Some(status) = self.marketplace_status {
            account["marketplaceStatus"] = json!(status);
        }
        if let Some(ts) = self.created_timestamp {
            account["createdTimestamp"] = json!(ts);
        }
        if let Some(ts) = self.updated_timestamp {
            account["updatedTimestamp"] = json!(ts);
        }

        account
    }
}

/// Fixture for building user responses
///
/// # Example
///
/// ```rust,ignore
/// use redis_cloud::testing::UserFixture;
///
/// let user = UserFixture::new(1, "user@example.com")
///     .name("Test User")
///     .role("owner")
///     .build();
/// ```
pub struct UserFixture {
    id: i32,
    email: String,
    name: Option<String>,
    role: String,
    status: String,
}

impl UserFixture {
    /// Create a new user fixture with required fields
    pub fn new(id: i32, email: impl Into<String>) -> Self {
        Self {
            id,
            email: email.into(),
            name: None,
            role: "member".to_string(),
            status: "active".to_string(),
        }
    }

    /// Set the user's name
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Set the user's role
    pub fn role(mut self, role: impl Into<String>) -> Self {
        self.role = role.into();
        self
    }

    /// Set the user's status
    pub fn status(mut self, status: impl Into<String>) -> Self {
        self.status = status.into();
        self
    }

    /// Build the user as a JSON Value
    pub fn build(self) -> Value {
        let mut user = json!({
            "id": self.id,
            "email": self.email,
            "role": self.role,
            "status": self.status
        });

        if let Some(name) = self.name {
            user["name"] = json!(name);
        }

        user
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_subscription_fixture_defaults() {
        let sub = SubscriptionFixture::new(123, "Test").build();
        assert_eq!(sub["id"], 123);
        assert_eq!(sub["name"], "Test");
        assert_eq!(sub["status"], "active");
    }

    #[test]
    fn test_subscription_fixture_customized() {
        let sub = SubscriptionFixture::new(123, "Production")
            .status("pending")
            .payment_method_type("credit-card")
            .cloud_provider("AWS")
            .region("us-east-1")
            .build();

        assert_eq!(sub["id"], 123);
        assert_eq!(sub["status"], "pending");
        assert_eq!(sub["paymentMethodType"], "credit-card");
        assert_eq!(sub["cloudProviders"][0]["provider"], "AWS");
    }

    #[test]
    fn test_database_fixture_defaults() {
        let db = DatabaseFixture::new(456, "my-db").build();
        assert_eq!(db["databaseId"], 456);
        assert_eq!(db["name"], "my-db");
        assert_eq!(db["status"], "active");
        assert_eq!(db["memoryLimitInGb"], 1.0);
    }

    #[test]
    fn test_database_fixture_customized() {
        let db = DatabaseFixture::new(456, "my-db")
            .memory_limit_in_gb(2.5)
            .protocol("redis")
            .replication(true)
            .throughput("operations-per-second", 25000)
            .public_endpoint("redis-12345.c1.us-east-1.ec2.cloud.redislabs.com:12345")
            .build();

        assert_eq!(db["memoryLimitInGb"], 2.5);
        assert_eq!(db["protocol"], "redis");
        assert_eq!(db["replication"], true);
        assert_eq!(db["throughputMeasurement"]["by"], "operations-per-second");
        assert_eq!(db["throughputMeasurement"]["value"], 25000);
    }

    #[test]
    fn test_task_fixture_completed() {
        let task = TaskFixture::completed("task-123", 789).build();
        assert_eq!(task["taskId"], "task-123");
        assert_eq!(task["status"], "processing-completed");
        assert_eq!(task["response"]["resourceId"], 789);
    }

    #[test]
    fn test_task_fixture_failed() {
        let task = TaskFixture::failed("task-456", "Something went wrong").build();
        assert_eq!(task["taskId"], "task-456");
        assert_eq!(task["status"], "processing-error");
        assert_eq!(task["response"]["error"], "Something went wrong");
    }

    #[test]
    fn test_account_fixture() {
        let account = AccountFixture::new(12345, "My Account")
            .marketplace_status("active")
            .build();

        assert_eq!(account["account"]["id"], 12345);
        assert_eq!(account["account"]["name"], "My Account");
        assert_eq!(account["account"]["marketplaceStatus"], "active");
    }

    #[test]
    fn test_user_fixture() {
        let user = UserFixture::new(1, "user@example.com")
            .name("Test User")
            .role("owner")
            .build();

        assert_eq!(user["id"], 1);
        assert_eq!(user["email"], "user@example.com");
        assert_eq!(user["name"], "Test User");
        assert_eq!(user["role"], "owner");
    }
}