dynoxide-rs 0.13.0

A lightweight, embeddable DynamoDB emulator backed by SQLite
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
use dynoxide::Database;
use dynoxide::actions::create_table::CreateTableRequest;
use dynoxide::actions::delete_table::DeleteTableRequest;
use dynoxide::actions::describe_table::DescribeTableRequest;
use dynoxide::actions::update_table::UpdateTableRequest;
use dynoxide::types::*;

fn make_db() -> Database {
    Database::memory().unwrap()
}

fn basic_request(name: &str) -> CreateTableRequest {
    CreateTableRequest {
        table_name: name.to_string(),
        key_schema: vec![KeySchemaElement {
            attribute_name: "pk".to_string(),
            key_type: KeyType::HASH,
        }],
        attribute_definitions: vec![AttributeDefinition {
            attribute_name: "pk".to_string(),
            attribute_type: ScalarAttributeType::S,
        }],
        ..Default::default()
    }
}

#[test]
fn test_create_table_with_sse_specification() {
    let db = make_db();

    let mut req = basic_request("SseTable");
    req.sse_specification = Some(SseSpecification {
        enabled: Some(true),
        sse_type: Some("KMS".to_string()),
        kms_master_key_id: Some("arn:aws:kms:us-east-1:123456789:key/my-key".to_string()),
    });

    let resp = db.create_table(req).unwrap();
    assert_eq!(resp.table_description.table_name, "SseTable");

    // Verify via DescribeTable
    let desc = db
        .describe_table(DescribeTableRequest {
            table_name: "SseTable".to_string(),
        })
        .unwrap();
    let sse = desc
        .table
        .sse_description
        .expect("SSEDescription should be present");
    assert_eq!(sse.status, "ENABLED");
    assert_eq!(sse.sse_type.as_deref(), Some("KMS"));
}

#[test]
fn test_create_table_with_table_class() {
    let db = make_db();

    let mut req = basic_request("ClassTable");
    req.table_class = Some("STANDARD_INFREQUENT_ACCESS".to_string());

    let resp = db.create_table(req).unwrap();
    let summary = resp
        .table_description
        .table_class_summary
        .expect("TableClassSummary should be present");
    assert_eq!(summary.table_class, "STANDARD_INFREQUENT_ACCESS");
}

#[test]
fn test_create_table_with_tags() {
    let db = make_db();

    let mut req = basic_request("TaggedTable");
    req.tags = Some(vec![
        Tag {
            key: "Environment".to_string(),
            value: "Production".to_string(),
        },
        Tag {
            key: "Team".to_string(),
            value: "Backend".to_string(),
        },
    ]);

    let _resp = db.create_table(req).unwrap();

    // Verify tags via ListTagsOfResource
    let tags_resp = db
        .list_tags_of_resource(
            dynoxide::actions::list_tags_of_resource::ListTagsOfResourceRequest {
                resource_arn: Some(
                    "arn:aws:dynamodb:dynoxide:000000000000:table/TaggedTable".to_string(),
                ),
            },
        )
        .unwrap();

    assert_eq!(tags_resp.tags.len(), 2);
    let keys: Vec<&str> = tags_resp.tags.iter().map(|t| t.key.as_str()).collect();
    assert!(keys.contains(&"Environment"));
    assert!(keys.contains(&"Team"));
}

#[test]
fn test_create_table_with_deletion_protection_prevents_delete() {
    let db = make_db();

    let mut req = basic_request("ProtectedTable");
    req.deletion_protection_enabled = Some(true);

    let _resp = db.create_table(req).unwrap();

    // Attempt to delete should fail
    let result = db.delete_table(DeleteTableRequest {
        table_name: "ProtectedTable".to_string(),
    });
    assert!(result.is_err());
    let err_msg = format!("{}", result.unwrap_err());
    assert!(
        err_msg.contains("deletion protection"),
        "Expected deletion protection error, got: {err_msg}"
    );
}

#[test]
fn test_update_table_disable_deletion_protection_then_delete() {
    let db = make_db();

    let mut req = basic_request("ToggleProtection");
    req.deletion_protection_enabled = Some(true);
    let _resp = db.create_table(req).unwrap();

    // Verify deletion fails
    let result = db.delete_table(DeleteTableRequest {
        table_name: "ToggleProtection".to_string(),
    });
    assert!(result.is_err());

    // Disable deletion protection via UpdateTable
    let update_req = UpdateTableRequest {
        table_name: "ToggleProtection".to_string(),
        deletion_protection_enabled: Some(false),
        ..Default::default()
    };
    let _update_resp = db.update_table(update_req).unwrap();

    // Verify DescribeTable shows disabled
    let desc = db
        .describe_table(DescribeTableRequest {
            table_name: "ToggleProtection".to_string(),
        })
        .unwrap();
    assert_eq!(desc.table.deletion_protection_enabled, Some(false));

    // Now deletion should succeed
    let result = db.delete_table(DeleteTableRequest {
        table_name: "ToggleProtection".to_string(),
    });
    assert!(result.is_ok());
}

#[test]
fn test_create_table_with_all_optional_params() {
    let db = make_db();

    let mut req = basic_request("FullTable");
    req.sse_specification = Some(SseSpecification {
        enabled: Some(true),
        sse_type: Some("KMS".to_string()),
        kms_master_key_id: None,
    });
    req.table_class = Some("STANDARD".to_string());
    req.tags = Some(vec![Tag {
        key: "App".to_string(),
        value: "test".to_string(),
    }]);
    req.deletion_protection_enabled = Some(true);

    let resp = db.create_table(req).unwrap();
    let desc = &resp.table_description;

    assert_eq!(desc.table_name, "FullTable");
    assert_eq!(desc.sse_description.as_ref().unwrap().status, "ENABLED");
    assert_eq!(
        desc.table_class_summary.as_ref().unwrap().table_class,
        "STANDARD"
    );
    assert_eq!(desc.deletion_protection_enabled, Some(true));
}

#[test]
fn test_create_table_without_optional_params_succeeds() {
    let db = make_db();

    let req = basic_request("BasicTable");
    let resp = db.create_table(req).unwrap();

    assert_eq!(resp.table_description.table_name, "BasicTable");
    // When not specified, deletion_protection_enabled is None (matching DynamoDB)
    assert!(
        resp.table_description.deletion_protection_enabled.is_none()
            || resp.table_description.deletion_protection_enabled == Some(false)
    );
    assert!(resp.table_description.sse_description.is_none());
    assert!(resp.table_description.table_class_summary.is_none());
}

#[test]
fn test_create_table_sse_enabled_only_completes_shape() {
    // Issue #44: SSESpecification { Enabled: true } with no SSEType or
    // KMSMasterKeyId must still round-trip SSEType=KMS and a KMS key ARN,
    // matching real AWS's AWS-managed-key default.
    let db = make_db();

    let mut req = basic_request("SseDefaultKey");
    req.sse_specification = Some(SseSpecification {
        enabled: Some(true),
        sse_type: None,
        kms_master_key_id: None,
    });
    db.create_table(req).unwrap();

    let desc = db
        .describe_table(DescribeTableRequest {
            table_name: "SseDefaultKey".to_string(),
        })
        .unwrap();
    let sse = desc
        .table
        .sse_description
        .expect("SSEDescription should be present");
    assert_eq!(sse.status, "ENABLED");
    assert_eq!(sse.sse_type.as_deref(), Some("KMS"));
    assert!(
        sse.kms_master_key_arn
            .as_deref()
            .is_some_and(|arn| arn.starts_with("arn:aws:kms:")),
        "expected a KMS key ARN, got: {:?}",
        sse.kms_master_key_arn
    );
}

#[test]
fn test_create_table_on_demand_throughput_round_trips() {
    // Issue #44: OnDemandThroughput set at create time must round-trip through
    // both the CreateTable response and a DescribeTable call.
    let db = make_db();

    let mut req = basic_request("OnDemandTable");
    req.billing_mode = Some("PAY_PER_REQUEST".to_string());
    req.on_demand_throughput = Some(OnDemandThroughput {
        max_read_request_units: Some(10),
        max_write_request_units: Some(5),
    });

    let resp = db.create_table(req).unwrap();
    let created = resp
        .table_description
        .on_demand_throughput
        .as_ref()
        .expect("CreateTable response should carry OnDemandThroughput");
    assert_eq!(created.max_read_request_units, Some(10));
    assert_eq!(created.max_write_request_units, Some(5));

    let desc = db
        .describe_table(DescribeTableRequest {
            table_name: "OnDemandTable".to_string(),
        })
        .unwrap();
    let odt = desc
        .table
        .on_demand_throughput
        .expect("OnDemandThroughput should round-trip via DescribeTable");
    assert_eq!(odt.max_read_request_units, Some(10));
    assert_eq!(odt.max_write_request_units, Some(5));
}

#[test]
fn test_create_table_rejects_on_demand_throughput_when_provisioned() {
    // Captured against real DynamoDB (eu-west-2, 2026-07-24): CreateTable
    // rejects OnDemandThroughput when the billing mode is PROVISIONED,
    // including the default. The message names the first present member,
    // read checked first.
    let db = make_db();

    // Default billing mode (PROVISIONED), read member present.
    let mut req = basic_request("OdtGateDefault");
    req.on_demand_throughput = Some(OnDemandThroughput {
        max_read_request_units: Some(10),
        max_write_request_units: Some(10),
    });
    let err = db.create_table(req).unwrap_err();
    assert_eq!(
        err.to_string(),
        "One or more parameter values were invalid: MaxReadRequestUnits for \
         OnDemandThroughput cannot be specified when table BillingMode is PROVISIONED."
    );

    // Explicit PROVISIONED, write member only: the message names the write member.
    let mut req = basic_request("OdtGateWrite");
    req.billing_mode = Some("PROVISIONED".to_string());
    req.provisioned_throughput = Some(ProvisionedThroughput {
        read_capacity_units: Some(5),
        write_capacity_units: Some(5),
    });
    req.on_demand_throughput = Some(OnDemandThroughput {
        max_read_request_units: None,
        max_write_request_units: Some(10),
    });
    let err = db.create_table(req).unwrap_err();
    assert_eq!(
        err.to_string(),
        "One or more parameter values were invalid: MaxWriteRequestUnits for \
         OnDemandThroughput cannot be specified when table BillingMode is PROVISIONED."
    );
}

#[test]
fn test_create_table_rejects_out_of_range_on_demand_throughput() {
    // Captured: members must be >= 1 at creation; -1 is only meaningful as a
    // removal on UpdateTable and is rejected here with the same message as 0.
    let db = make_db();

    for bad in [0i64, -1] {
        let mut req = basic_request("OdtRange");
        req.billing_mode = Some("PAY_PER_REQUEST".to_string());
        req.on_demand_throughput = Some(OnDemandThroughput {
            max_read_request_units: Some(bad),
            max_write_request_units: None,
        });
        let err = db.create_table(req).unwrap_err();
        assert_eq!(
            err.to_string(),
            "One or more parameter values were invalid: Requested MaxReadRequestUnits \
             for OnDemandThroughput for table is outside of valid range",
            "value {bad} should be out of range"
        );
    }
}

#[test]
fn test_create_table_empty_on_demand_throughput_treated_as_absent() {
    // Captured (eu-west-2, 2026-07-24): an OnDemandThroughput object with no
    // members is accepted and nothing is stored, on any billing mode.
    let db = make_db();

    let mut req = basic_request("OdtEmptyCreate");
    req.billing_mode = Some("PAY_PER_REQUEST".to_string());
    req.on_demand_throughput = Some(OnDemandThroughput {
        max_read_request_units: None,
        max_write_request_units: None,
    });
    let resp = db.create_table(req).unwrap();
    assert!(resp.table_description.on_demand_throughput.is_none());

    let desc = db
        .describe_table(DescribeTableRequest {
            table_name: "OdtEmptyCreate".to_string(),
        })
        .unwrap();
    assert!(desc.table.on_demand_throughput.is_none());

    // The empty object slips past no gate on a provisioned table either.
    let mut req = basic_request("OdtEmptyProvisioned");
    req.on_demand_throughput = Some(OnDemandThroughput {
        max_read_request_units: None,
        max_write_request_units: None,
    });
    db.create_table(req).unwrap();
}

#[test]
fn test_describe_table_omits_on_demand_throughput_when_unset() {
    // Issue #44: a table created without OnDemandThroughput must not synthesise one.
    let db = make_db();

    let resp = db.create_table(basic_request("NoOnDemand")).unwrap();
    assert!(resp.table_description.on_demand_throughput.is_none());

    let desc = db
        .describe_table(DescribeTableRequest {
            table_name: "NoOnDemand".to_string(),
        })
        .unwrap();
    assert!(desc.table.on_demand_throughput.is_none());
}

#[test]
fn test_delete_table_protected_returns_exact_aws_message() {
    // Issue #46: DeleteTable on a protected table must return the exact AWS
    // message, not the ARN-prefixed form Dynoxide used to emit.
    let db = make_db();

    let mut req = basic_request("ProtectedExactMsg");
    req.deletion_protection_enabled = Some(true);
    db.create_table(req).unwrap();

    let err = db
        .delete_table(DeleteTableRequest {
            table_name: "ProtectedExactMsg".to_string(),
        })
        .expect_err("delete of a protected table must fail");

    assert!(
        matches!(err, dynoxide::errors::DynoxideError::ValidationException(_)),
        "expected ValidationException, got: {err:?}"
    );
    assert_eq!(
        format!("{err}"),
        "Resource cannot be deleted as it is currently protected against deletion. \
         Disable deletion protection first."
    );
}