dynoxide-rs 0.11.1

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
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
use dynoxide::Database;
use dynoxide::DynoxideError;
use dynoxide::actions::batch_get_item::BatchGetItemRequest;
use dynoxide::actions::batch_write_item::BatchWriteItemRequest;
use dynoxide::actions::create_table::CreateTableRequest;
use dynoxide::actions::put_item::PutItemRequest;

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

fn create_test_table(db: &Database, name: &str) {
    let req: CreateTableRequest = serde_json::from_value(serde_json::json!({
        "TableName": name,
        "KeySchema": [
            {"AttributeName": "pk", "KeyType": "HASH"},
            {"AttributeName": "sk", "KeyType": "RANGE"}
        ],
        "AttributeDefinitions": [
            {"AttributeName": "pk", "AttributeType": "S"},
            {"AttributeName": "sk", "AttributeType": "S"}
        ],
        "BillingMode": "PAY_PER_REQUEST"
    }))
    .unwrap();
    db.create_table(req).unwrap();
}

fn put(db: &Database, table: &str, item: serde_json::Value) {
    let req: PutItemRequest = serde_json::from_value(serde_json::json!({
        "TableName": table,
        "Item": item
    }))
    .unwrap();
    db.put_item(req).unwrap();
}

// =============================================================================
// BatchGetItem tests
// =============================================================================

#[test]
fn test_batch_get_single_table() {
    let db = setup_db();
    create_test_table(&db, "Tbl");

    put(
        &db,
        "Tbl",
        serde_json::json!({"pk": {"S": "a"}, "sk": {"S": "1"}, "name": {"S": "Alice"}}),
    );
    put(
        &db,
        "Tbl",
        serde_json::json!({"pk": {"S": "b"}, "sk": {"S": "1"}, "name": {"S": "Bob"}}),
    );

    let req: BatchGetItemRequest = serde_json::from_value(serde_json::json!({
        "RequestItems": {
            "Tbl": {
                "Keys": [
                    {"pk": {"S": "a"}, "sk": {"S": "1"}},
                    {"pk": {"S": "b"}, "sk": {"S": "1"}}
                ]
            }
        }
    }))
    .unwrap();

    let resp = db.batch_get_item(req).unwrap();
    assert_eq!(resp.responses["Tbl"].len(), 2);
    assert!(resp.unprocessed_keys.is_empty());
}

#[test]
fn test_batch_get_multiple_tables() {
    let db = setup_db();
    create_test_table(&db, "TableA");
    create_test_table(&db, "TableB");

    put(
        &db,
        "TableA",
        serde_json::json!({"pk": {"S": "a"}, "sk": {"S": "1"}}),
    );
    put(
        &db,
        "TableB",
        serde_json::json!({"pk": {"S": "b"}, "sk": {"S": "1"}}),
    );

    let req: BatchGetItemRequest = serde_json::from_value(serde_json::json!({
        "RequestItems": {
            "TableA": {"Keys": [{"pk": {"S": "a"}, "sk": {"S": "1"}}]},
            "TableB": {"Keys": [{"pk": {"S": "b"}, "sk": {"S": "1"}}]}
        }
    }))
    .unwrap();

    let resp = db.batch_get_item(req).unwrap();
    assert_eq!(resp.responses["TableA"].len(), 1);
    assert_eq!(resp.responses["TableB"].len(), 1);
}

#[test]
fn test_batch_get_with_projection() {
    let db = setup_db();
    create_test_table(&db, "Tbl");

    put(
        &db,
        "Tbl",
        serde_json::json!({"pk": {"S": "a"}, "sk": {"S": "1"}, "name": {"S": "Alice"}, "age": {"N": "30"}}),
    );

    let req: BatchGetItemRequest = serde_json::from_value(serde_json::json!({
        "RequestItems": {
            "Tbl": {
                "Keys": [{"pk": {"S": "a"}, "sk": {"S": "1"}}],
                "ProjectionExpression": "#n",
                "ExpressionAttributeNames": {"#n": "name"}
            }
        }
    }))
    .unwrap();

    let resp = db.batch_get_item(req).unwrap();
    let items = &resp.responses["Tbl"];
    assert_eq!(items.len(), 1);
    // BatchGetItem does NOT auto-include key attributes in projections
    assert!(!items[0].contains_key("pk"));
    assert!(items[0].contains_key("name")); // Projected
    assert!(!items[0].contains_key("age")); // Not projected
}

#[test]
fn test_batch_get_key_not_found() {
    let db = setup_db();
    create_test_table(&db, "Tbl");

    put(
        &db,
        "Tbl",
        serde_json::json!({"pk": {"S": "a"}, "sk": {"S": "1"}}),
    );

    let req: BatchGetItemRequest = serde_json::from_value(serde_json::json!({
        "RequestItems": {
            "Tbl": {
                "Keys": [
                    {"pk": {"S": "a"}, "sk": {"S": "1"}},
                    {"pk": {"S": "missing"}, "sk": {"S": "1"}}
                ]
            }
        }
    }))
    .unwrap();

    let resp = db.batch_get_item(req).unwrap();
    assert_eq!(resp.responses["Tbl"].len(), 1); // Only found item returned
}

#[test]
fn test_batch_get_exceeds_100_keys() {
    let db = setup_db();
    create_test_table(&db, "Tbl");

    let keys: Vec<serde_json::Value> = (0..101)
        .map(|i| serde_json::json!({"pk": {"S": format!("k{}", i)}, "sk": {"S": "x"}}))
        .collect();

    let req: BatchGetItemRequest = serde_json::from_value(serde_json::json!({
        "RequestItems": {
            "Tbl": {"Keys": keys}
        }
    }))
    .unwrap();

    let err = db.batch_get_item(req).unwrap_err();
    let msg = format!("{err:?}");
    assert!(
        msg.contains("Member must have length less than or equal to 100"),
        "Got: {msg}"
    );
    assert!(msg.contains("RequestItems.Tbl.member.Keys"), "Got: {msg}");
}

// =============================================================================
// BatchWriteItem tests
// =============================================================================

#[test]
fn test_batch_write_puts_and_deletes() {
    let db = setup_db();
    create_test_table(&db, "Tbl");

    // Pre-existing item to delete
    put(
        &db,
        "Tbl",
        serde_json::json!({"pk": {"S": "del"}, "sk": {"S": "1"}}),
    );

    let req: BatchWriteItemRequest = serde_json::from_value(serde_json::json!({
        "RequestItems": {
            "Tbl": [
                {"PutRequest": {"Item": {"pk": {"S": "new1"}, "sk": {"S": "1"}, "val": {"S": "hello"}}}},
                {"PutRequest": {"Item": {"pk": {"S": "new2"}, "sk": {"S": "1"}, "val": {"S": "world"}}}},
                {"DeleteRequest": {"Key": {"pk": {"S": "del"}, "sk": {"S": "1"}}}}
            ]
        }
    }))
    .unwrap();

    let resp = db.batch_write_item(req).unwrap();
    assert!(resp.unprocessed_items.is_empty());

    // Verify items were created
    let scan = db
        .scan(serde_json::from_value(serde_json::json!({"TableName": "Tbl"})).unwrap())
        .unwrap();
    assert_eq!(scan.count, 2); // new1 and new2 (del was deleted)
}

#[test]
fn test_batch_write_multiple_tables() {
    let db = setup_db();
    create_test_table(&db, "TableA");
    create_test_table(&db, "TableB");

    let req: BatchWriteItemRequest = serde_json::from_value(serde_json::json!({
        "RequestItems": {
            "TableA": [
                {"PutRequest": {"Item": {"pk": {"S": "a"}, "sk": {"S": "1"}}}}
            ],
            "TableB": [
                {"PutRequest": {"Item": {"pk": {"S": "b"}, "sk": {"S": "1"}}}}
            ]
        }
    }))
    .unwrap();

    db.batch_write_item(req).unwrap();

    let scan_a = db
        .scan(serde_json::from_value(serde_json::json!({"TableName": "TableA"})).unwrap())
        .unwrap();
    let scan_b = db
        .scan(serde_json::from_value(serde_json::json!({"TableName": "TableB"})).unwrap())
        .unwrap();
    assert_eq!(scan_a.count, 1);
    assert_eq!(scan_b.count, 1);
}

#[test]
fn test_batch_write_exceeds_25_items() {
    let db = setup_db();
    create_test_table(&db, "Tbl");

    let items: Vec<serde_json::Value> = (0..26)
        .map(|i| {
            serde_json::json!({"PutRequest": {"Item": {"pk": {"S": format!("k{}", i)}, "sk": {"S": "x"}}}})
        })
        .collect();

    let req: BatchWriteItemRequest = serde_json::from_value(serde_json::json!({
        "RequestItems": {
            "Tbl": items
        }
    }))
    .unwrap();

    let err = db.batch_write_item(req).unwrap_err();
    let msg = format!("{err:?}");
    assert!(
        msg.contains("Member must have length less than or equal to 25"),
        "Got: {msg}"
    );
    assert!(msg.contains("at 'requestItems'"), "Got: {msg}");
}

#[test]
fn test_batch_write_item_too_large() {
    let db = setup_db();
    create_test_table(&db, "Tbl");

    // Create a string that exceeds 400KB
    let big_string = "x".repeat(400 * 1024 + 1);
    let req: BatchWriteItemRequest = serde_json::from_value(serde_json::json!({
        "RequestItems": {
            "Tbl": [
                {"PutRequest": {"Item": {"pk": {"S": "a"}, "sk": {"S": "1"}, "data": {"S": big_string}}}}
            ]
        }
    }))
    .unwrap();

    let err = db.batch_write_item(req).unwrap_err();
    assert!(format!("{err:?}").contains("Item size"), "Got: {err:?}");
}

#[test]
fn test_batch_write_nonexistent_table() {
    let db = setup_db();

    let req: BatchWriteItemRequest = serde_json::from_value(serde_json::json!({
        "RequestItems": {
            "NonExistent": [
                {"PutRequest": {"Item": {"pk": {"S": "a"}, "sk": {"S": "1"}}}}
            ]
        }
    }))
    .unwrap();

    let err = db.batch_write_item(req).unwrap_err();
    assert!(format!("{err:?}").contains("not found"), "Got: {err:?}");
}

#[test]
fn test_batch_write_keyless_put_rejected_with_400() {
    // A PutRequest whose item is missing the table key is a client error: AWS
    // rejects it with a 400 ValidationException. The duplicate-detection pass
    // previously reached extract_key_strings before validating keys, surfacing
    // a 500 InternalServerError. Mirrors the conformance assertion
    // tests/tier1/batchWriteItem/validation.test.ts —
    // "rejects a key-less item with a 400 ValidationException, not a 500".
    let db = setup_db();
    let req: CreateTableRequest = serde_json::from_value(serde_json::json!({
        "TableName": "HashTbl",
        "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
        "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
        "BillingMode": "PAY_PER_REQUEST"
    }))
    .unwrap();
    db.create_table(req).unwrap();

    let req: BatchWriteItemRequest = serde_json::from_value(serde_json::json!({
        "RequestItems": {
            "HashTbl": [
                {"PutRequest": {"Item": {"notkey": {"S": "x"}}}}
            ]
        }
    }))
    .unwrap();

    let err = db.batch_write_item(req).unwrap_err();
    assert!(
        matches!(err, DynoxideError::ValidationException(_)),
        "key-less Put must be a ValidationException, got: {err:?}"
    );
    assert_eq!(err.status_code(), 400, "must be HTTP 400, got: {err:?}");
}

// =============================================================================
// #97: BatchWriteItem collapses wrong-type / non-scalar table keys to the
// generic schema error. Captured against real AWS in eu-west-2: a batch put
// returns "The provided key element does not match the schema" for both,
// rather than PutItem's "Type mismatch for key" wording.
// =============================================================================

const BATCH_KEY_SCHEMA_MSG: &str = "The provided key element does not match the schema";

#[test]
fn test_batch_write_wrong_type_table_key_returns_schema_error() {
    let db = setup_db();
    create_test_table(&db, "Tbl");

    // pk is declared S but supplied as N.
    let req: BatchWriteItemRequest = serde_json::from_value(serde_json::json!({
        "RequestItems": {
            "Tbl": [
                {"PutRequest": {"Item": {"pk": {"N": "1"}, "sk": {"S": "a"}}}}
            ]
        }
    }))
    .unwrap();

    let err = db.batch_write_item(req).unwrap_err();
    assert!(
        matches!(&err, DynoxideError::ValidationException(m) if m == BATCH_KEY_SCHEMA_MSG),
        "wrong-type batch table key must return the generic schema error, got: {err:?}"
    );
}

#[test]
fn test_batch_write_non_scalar_table_key_returns_schema_error() {
    let db = setup_db();
    create_test_table(&db, "Tbl");

    // pk is declared S but supplied as a non-scalar (BOOL).
    let req: BatchWriteItemRequest = serde_json::from_value(serde_json::json!({
        "RequestItems": {
            "Tbl": [
                {"PutRequest": {"Item": {"pk": {"BOOL": true}, "sk": {"S": "a"}}}}
            ]
        }
    }))
    .unwrap();

    let err = db.batch_write_item(req).unwrap_err();
    assert!(
        matches!(&err, DynoxideError::ValidationException(m) if m == BATCH_KEY_SCHEMA_MSG),
        "non-scalar batch table key must return the generic schema error, got: {err:?}"
    );
}

#[test]
fn test_batch_write_wrong_type_sort_key_returns_schema_error() {
    let db = setup_db();
    create_test_table(&db, "Tbl");

    // sk is declared S but supplied as N.
    let req: BatchWriteItemRequest = serde_json::from_value(serde_json::json!({
        "RequestItems": {
            "Tbl": [
                {"PutRequest": {"Item": {"pk": {"S": "a"}, "sk": {"N": "1"}}}}
            ]
        }
    }))
    .unwrap();

    let err = db.batch_write_item(req).unwrap_err();
    assert!(
        matches!(&err, DynoxideError::ValidationException(m) if m == BATCH_KEY_SCHEMA_MSG),
        "wrong-type batch sort key must return the generic schema error, got: {err:?}"
    );
}

#[test]
fn test_put_item_wrong_type_key_keeps_type_mismatch_message() {
    // Guard: collapsing the batch wording must not change PutItem, which keeps
    // the specific "Type mismatch for key" message.
    let db = setup_db();
    create_test_table(&db, "Tbl");

    let req: PutItemRequest = serde_json::from_value(serde_json::json!({
        "TableName": "Tbl",
        "Item": {"pk": {"N": "1"}, "sk": {"S": "a"}}
    }))
    .unwrap();

    let err = db.put_item(req).unwrap_err();
    assert!(
        matches!(&err, DynoxideError::ValidationException(m) if m.contains("Type mismatch for key")),
        "PutItem must keep the specific type-mismatch message, got: {err:?}"
    );
}

#[test]
fn test_batch_get_nonexistent_table() {
    let db = setup_db();

    let req: BatchGetItemRequest = serde_json::from_value(serde_json::json!({
        "RequestItems": {
            "NonExistent": {
                "Keys": [{"pk": {"S": "a"}, "sk": {"S": "1"}}]
            }
        }
    }))
    .unwrap();

    let err = db.batch_get_item(req).unwrap_err();
    assert!(format!("{err:?}").contains("not found"), "Got: {err:?}");
}

// =============================================================================
// 16MB Batch Size Limit tests
// =============================================================================

#[test]
fn test_batch_get_returns_unprocessed_keys_over_16mb() {
    let db = setup_db();

    // Use a hash-only table for simpler key structure
    let req: CreateTableRequest = serde_json::from_value(serde_json::json!({
        "TableName": "BigTbl",
        "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
        "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
        "ProvisionedThroughput": {"ReadCapacityUnits": 5, "WriteCapacityUnits": 5}
    }))
    .unwrap();
    db.create_table(req).unwrap();

    // Each item has ~350KB data (under 400KB single-item limit).
    // 50 items * 350KB ≈ 17.5MB > 16MB.
    let big_val = "x".repeat(350 * 1024);
    for i in 0..50 {
        let item_req: PutItemRequest = serde_json::from_value(serde_json::json!({
            "TableName": "BigTbl",
            "Item": {"pk": {"S": format!("k{i}")}, "data": {"S": big_val}}
        }))
        .unwrap();
        db.put_item(item_req).unwrap();
    }

    // Request all 50 items
    let keys: Vec<serde_json::Value> = (0..50)
        .map(|i| serde_json::json!({"pk": {"S": format!("k{i}")}}))
        .collect();

    let req: BatchGetItemRequest = serde_json::from_value(serde_json::json!({
        "RequestItems": {
            "BigTbl": {"Keys": keys}
        }
    }))
    .unwrap();

    let resp = db.batch_get_item(req).unwrap();
    // Should have returned some items but not all (16MB limit hit)
    let returned = resp.responses.get("BigTbl").map_or(0, |v| v.len());
    assert!(returned > 0, "Should have returned some items");
    assert!(
        returned < 50,
        "Should not have returned all 50 items (16MB limit), got {returned}"
    );
    assert!(
        !resp.unprocessed_keys.is_empty(),
        "Should have unprocessed keys"
    );
}

#[test]
fn test_batch_write_exceeds_16mb_aggregate() {
    let db = setup_db();

    // Use a hash-only table
    let req: CreateTableRequest = serde_json::from_value(serde_json::json!({
        "TableName": "BigTbl",
        "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
        "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],
        "ProvisionedThroughput": {"ReadCapacityUnits": 5, "WriteCapacityUnits": 5}
    }))
    .unwrap();
    db.create_table(req).unwrap();

    // 25 items (max BatchWrite count) each ~350KB = ~8.75MB (under 16MB)
    // But if we create fewer large items to push over 16MB limit...
    // Actually we're limited to 25 items max in BatchWriteItem.
    // 25 * 350KB ≈ 8.75MB which is under 16MB. So let's make each item bigger.
    // We can't exceed 400KB per item, so maximum is 25 * 400KB ≈ 10MB.
    // This means it's actually impossible to exceed 16MB with BatchWriteItem's
    // 25-item limit and 400KB per-item limit. The aggregate limit matters for
    // the raw request payload including serialization overhead.
    // For testing purposes, we'll verify that a large-but-valid batch succeeds.
    let big_val = "x".repeat(300 * 1024);
    let items: Vec<serde_json::Value> = (0..25)
        .map(|i| {
            serde_json::json!({"PutRequest": {"Item": {"pk": {"S": format!("k{i}")}, "data": {"S": big_val}}}})
        })
        .collect();

    let req: BatchWriteItemRequest = serde_json::from_value(serde_json::json!({
        "RequestItems": {
            "BigTbl": items
        }
    }))
    .unwrap();

    // Should succeed — 25 * 300KB ≈ 7.5MB, under 16MB
    db.batch_write_item(req).unwrap();
}

// =============================================================================
// Empty-string key value on the batch key-only paths surfaces top-level with
// the "...are not valid..." wording, matching the single-action baseline.
// =============================================================================

const BATCH_EMPTY_KEY_MSG: &str = "One or more parameter values are not valid. The AttributeValue for a key attribute cannot contain an empty string value. Key: pk";

#[test]
fn test_batch_write_delete_empty_string_key_is_top_level_validation() {
    let db = setup_db();
    create_test_table(&db, "Tbl");
    let req: BatchWriteItemRequest = serde_json::from_value(serde_json::json!({
        "RequestItems": {
            "Tbl": [
                {"DeleteRequest": {"Key": {"pk": {"S": ""}, "sk": {"S": "a"}}}}
            ]
        }
    }))
    .unwrap();
    let err = db.batch_write_item(req).unwrap_err();
    assert_eq!(
        err.error_type(),
        "com.amazon.coral.validate#ValidationException"
    );
    assert_eq!(err.status_code(), 400, "must be HTTP 400, got: {err:?}");
    assert_eq!(err.to_string(), BATCH_EMPTY_KEY_MSG);
}

#[test]
fn test_batch_get_empty_string_key_is_top_level_validation() {
    let db = setup_db();
    create_test_table(&db, "Tbl");
    let req: BatchGetItemRequest = serde_json::from_value(serde_json::json!({
        "RequestItems": {
            "Tbl": {"Keys": [{"pk": {"S": ""}, "sk": {"S": "a"}}]}
        }
    }))
    .unwrap();
    let err = db.batch_get_item(req).unwrap_err();
    assert_eq!(
        err.error_type(),
        "com.amazon.coral.validate#ValidationException"
    );
    assert_eq!(err.status_code(), 400, "must be HTTP 400, got: {err:?}");
    assert_eq!(err.to_string(), BATCH_EMPTY_KEY_MSG);
}

// =============================================================================
// Empty-binary key value on the batch key paths surfaces top-level with the
// "...empty binary value..." message (real AWS, 2026-06-24 capture).
// =============================================================================

const BATCH_EMPTY_BINARY_MSG: &str = "One or more parameter values are not valid. The AttributeValue for a key attribute cannot contain an empty binary value. Key: pk";

fn create_binary_key_table(db: &Database, name: &str) {
    let req: CreateTableRequest = serde_json::from_value(serde_json::json!({
        "TableName": name,
        "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
        "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "B"}]
    }))
    .unwrap();
    db.create_table(req).unwrap();
}

#[test]
fn test_batch_write_delete_empty_binary_key_is_top_level_validation() {
    let db = setup_db();
    create_binary_key_table(&db, "BinTbl");
    let req: BatchWriteItemRequest = serde_json::from_value(serde_json::json!({
        "RequestItems": { "BinTbl": [{ "DeleteRequest": { "Key": { "pk": { "B": "" } } } }] }
    }))
    .unwrap();
    let err = db.batch_write_item(req).unwrap_err();
    assert_eq!(
        err.error_type(),
        "com.amazon.coral.validate#ValidationException"
    );
    assert_eq!(err.status_code(), 400, "must be HTTP 400, got: {err:?}");
    assert_eq!(err.to_string(), BATCH_EMPTY_BINARY_MSG);
}

#[test]
fn test_batch_write_put_empty_binary_key_is_top_level_validation() {
    let db = setup_db();
    create_binary_key_table(&db, "BinTbl");
    let req: BatchWriteItemRequest = serde_json::from_value(serde_json::json!({
        "RequestItems": { "BinTbl": [{ "PutRequest": { "Item": { "pk": { "B": "" } } } }] }
    }))
    .unwrap();
    let err = db.batch_write_item(req).unwrap_err();
    assert_eq!(
        err.error_type(),
        "com.amazon.coral.validate#ValidationException"
    );
    assert_eq!(err.status_code(), 400, "must be HTTP 400, got: {err:?}");
    assert_eq!(err.to_string(), BATCH_EMPTY_BINARY_MSG);
}

#[test]
fn test_batch_get_empty_binary_key_is_top_level_validation() {
    let db = setup_db();
    create_binary_key_table(&db, "BinTbl");
    let req: BatchGetItemRequest = serde_json::from_value(serde_json::json!({
        "RequestItems": { "BinTbl": { "Keys": [{ "pk": { "B": "" } }] } }
    }))
    .unwrap();
    let err = db.batch_get_item(req).unwrap_err();
    assert_eq!(
        err.error_type(),
        "com.amazon.coral.validate#ValidationException"
    );
    assert_eq!(err.status_code(), 400, "must be HTTP 400, got: {err:?}");
    assert_eq!(err.to_string(), BATCH_EMPTY_BINARY_MSG);
}