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
//! Phase 2 integration tests: Table operations (CreateTable, DeleteTable, DescribeTable, ListTables).

use dynoxide::Database;
use dynoxide::actions::create_table::CreateTableRequest;
use dynoxide::actions::delete_table::DeleteTableRequest;
use dynoxide::actions::describe_table::DescribeTableRequest;
use dynoxide::actions::list_tables::ListTablesRequest;
use dynoxide::types::*;

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

fn simple_create_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()
    }
}

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

// ---------------------------------------------------------------------------
// CreateTable
// ---------------------------------------------------------------------------

#[test]
fn test_create_table_hash_key_only() {
    let db = make_db();
    let resp = db.create_table(simple_create_request("HashOnly")).unwrap();
    assert_eq!(resp.table_description.table_name, "HashOnly");
    assert_eq!(
        resp.table_description.table_arn,
        "arn:aws:dynamodb:dynoxide:000000000000:table/HashOnly"
    );
    assert_eq!(resp.table_description.table_status, "CREATING");
    assert_eq!(resp.table_description.key_schema.len(), 1);
    assert_eq!(resp.table_description.item_count, Some(0));
}

#[test]
fn test_create_table_hash_range_key() {
    let db = make_db();
    let resp = db
        .create_table(composite_key_create_request("Composite"))
        .unwrap();
    assert_eq!(resp.table_description.key_schema.len(), 2);
}

#[test]
fn test_create_table_with_gsi() {
    let db = make_db();
    let request = CreateTableRequest {
        table_name: "WithGSI".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,
            },
            AttributeDefinition {
                attribute_name: "gsi_pk".to_string(),
                attribute_type: ScalarAttributeType::S,
            },
        ],
        global_secondary_indexes: Some(vec![GlobalSecondaryIndex {
            index_name: "ByGsiPk".to_string(),
            key_schema: vec![KeySchemaElement {
                attribute_name: "gsi_pk".to_string(),
                key_type: KeyType::HASH,
            }],
            projection: Projection {
                projection_type: Some(ProjectionType::ALL),
                non_key_attributes: None,
            },
            provisioned_throughput: None,
        }]),
        billing_mode: None,
        provisioned_throughput: None,
        stream_specification: None,
        ..Default::default()
    };

    let resp = db.create_table(request).unwrap();
    let gsis = resp.table_description.global_secondary_indexes.unwrap();
    assert_eq!(gsis.len(), 1);
    assert_eq!(gsis[0].index_name, "ByGsiPk");
    assert_eq!(gsis[0].index_status, "CREATING");
    assert_eq!(
        gsis[0].index_arn,
        "arn:aws:dynamodb:dynoxide:000000000000:table/WithGSI/index/ByGsiPk"
    );
}

#[test]
fn test_create_table_already_exists() {
    let db = make_db();
    db.create_table(simple_create_request("Duplicate")).unwrap();

    let err = db
        .create_table(simple_create_request("Duplicate"))
        .unwrap_err();
    assert!(err.to_string().contains("Table already exists"));
}

#[test]
fn test_create_table_invalid_name_too_short() {
    let db = make_db();
    let mut req = simple_create_request("ab");
    req.table_name = "ab".to_string();

    let err = db.create_table(req).unwrap_err();
    assert!(err.to_string().contains("TableName must be at least 3"));
}

#[test]
fn test_create_table_invalid_name_bad_chars() {
    let db = make_db();
    let mut req = simple_create_request("bad name!");
    req.table_name = "bad name!".to_string();

    let err = db.create_table(req).unwrap_err();
    assert!(err.to_string().contains("tableName"));
}

#[test]
fn test_create_table_missing_key_in_definitions() {
    let db = make_db();
    let request = CreateTableRequest {
        table_name: "MissingDef".to_string(),
        key_schema: vec![
            KeySchemaElement {
                attribute_name: "pk".to_string(),
                key_type: KeyType::HASH,
            },
            KeySchemaElement {
                attribute_name: "sk".to_string(),
                key_type: KeyType::RANGE,
            },
        ],
        attribute_definitions: vec![AttributeDefinition {
            attribute_name: "pk".to_string(),
            attribute_type: ScalarAttributeType::S,
        }],
        ..Default::default()
    };

    let err = db.create_table(request).unwrap_err();
    // DynamoDB returns the generic error for 2-key schemas with missing definitions
    assert!(
        err.to_string()
            .contains("Some index key attribute have no definition")
            || err
                .to_string()
                .contains("not defined in AttributeDefinitions"),
        "Expected key-not-defined error, got: {}",
        err
    );
}

// ---------------------------------------------------------------------------
// DeleteTable
// ---------------------------------------------------------------------------

#[test]
fn test_delete_existing_table() {
    let db = make_db();
    db.create_table(simple_create_request("ToDelete")).unwrap();

    let resp = db
        .delete_table(DeleteTableRequest {
            table_name: "ToDelete".to_string(),
        })
        .unwrap();
    assert_eq!(resp.table_description.table_status, "DELETING");
    assert_eq!(
        resp.table_description.table_arn,
        "arn:aws:dynamodb:dynoxide:000000000000:table/ToDelete"
    );

    // Table should be gone
    let err = db
        .describe_table(DescribeTableRequest {
            table_name: "ToDelete".to_string(),
        })
        .unwrap_err();
    assert!(err.to_string().contains("not found"));
}

#[test]
fn test_delete_nonexistent_table() {
    let db = make_db();
    let err = db
        .delete_table(DeleteTableRequest {
            table_name: "Ghost".to_string(),
        })
        .unwrap_err();
    assert!(err.to_string().contains("not found"));
}

// ---------------------------------------------------------------------------
// DescribeTable
// ---------------------------------------------------------------------------

#[test]
fn test_describe_existing_table() {
    let db = make_db();
    db.create_table(composite_key_create_request("Described"))
        .unwrap();

    let resp = db
        .describe_table(DescribeTableRequest {
            table_name: "Described".to_string(),
        })
        .unwrap();

    assert_eq!(resp.table.table_name, "Described");
    assert_eq!(
        resp.table.table_arn,
        "arn:aws:dynamodb:dynoxide:000000000000:table/Described"
    );
    assert_eq!(resp.table.table_status, "ACTIVE");
    assert_eq!(resp.table.key_schema.len(), 2);
    assert_eq!(resp.table.attribute_definitions.len(), 2);
    assert!(resp.table.creation_date_time.is_some());
    assert_eq!(resp.table.item_count, Some(0));
}

/// #55: TableId is assigned once and stays stable, rather than changing on
/// every call. CreateTable and repeated DescribeTable calls all return the
/// same TableId.
#[test]
fn test_table_id_is_stable_across_calls() {
    let db = make_db();
    let created = db.create_table(simple_create_request("StableId")).unwrap();
    let create_id = created.table_description.table_id.clone();
    assert!(create_id.is_some(), "CreateTable should return a TableId");

    let d1 = db
        .describe_table(DescribeTableRequest {
            table_name: "StableId".to_string(),
        })
        .unwrap();
    let d2 = db
        .describe_table(DescribeTableRequest {
            table_name: "StableId".to_string(),
        })
        .unwrap();

    assert_eq!(
        d1.table.table_id, create_id,
        "DescribeTable TableId must match the CreateTable TableId"
    );
    assert_eq!(
        d2.table.table_id, d1.table.table_id,
        "TableId must be stable across repeated DescribeTable calls"
    );
}

/// #55: a dropped-and-recreated table gets a new TableId, matching AWS, even
/// when the recreate happens immediately. The id is random per incarnation, not
/// derived from table state, so a same-second recreate still changes it.
#[test]
fn test_table_id_changes_after_recreate() {
    let db = make_db();

    db.create_table(simple_create_request("RecreateId"))
        .unwrap();
    let first = db
        .describe_table(DescribeTableRequest {
            table_name: "RecreateId".to_string(),
        })
        .unwrap()
        .table
        .table_id;

    db.delete_table(DeleteTableRequest {
        table_name: "RecreateId".to_string(),
    })
    .unwrap();

    db.create_table(simple_create_request("RecreateId"))
        .unwrap();
    let second = db
        .describe_table(DescribeTableRequest {
            table_name: "RecreateId".to_string(),
        })
        .unwrap()
        .table
        .table_id;

    assert!(first.is_some() && second.is_some());
    assert_ne!(
        first, second,
        "a recreated table must be assigned a new TableId"
    );
}

#[test]
fn test_describe_nonexistent_table() {
    let db = make_db();
    let err = db
        .describe_table(DescribeTableRequest {
            table_name: "Nope".to_string(),
        })
        .unwrap_err();
    assert!(err.to_string().contains("not found"));
}

// ---------------------------------------------------------------------------
// ListTables
// ---------------------------------------------------------------------------

#[test]
fn test_list_tables_empty() {
    let db = make_db();
    let resp = db
        .list_tables(ListTablesRequest {
            exclusive_start_table_name: None,
            limit: None,
        })
        .unwrap();
    assert!(resp.table_names.is_empty());
    assert!(resp.last_evaluated_table_name.is_none());
}

#[test]
fn test_list_tables_multiple() {
    let db = make_db();
    for name in &["Alpha", "Beta", "Gamma"] {
        db.create_table(simple_create_request(name)).unwrap();
    }

    let resp = db
        .list_tables(ListTablesRequest {
            exclusive_start_table_name: None,
            limit: None,
        })
        .unwrap();
    assert_eq!(resp.table_names, vec!["Alpha", "Beta", "Gamma"]);
    assert!(resp.last_evaluated_table_name.is_none());
}

#[test]
fn test_list_tables_pagination() {
    let db = make_db();
    for name in &["Alpha", "Beta", "Gamma", "Delta"] {
        db.create_table(simple_create_request(name)).unwrap();
    }

    // Page 1
    let resp = db
        .list_tables(ListTablesRequest {
            exclusive_start_table_name: None,
            limit: Some(2),
        })
        .unwrap();
    assert_eq!(resp.table_names, vec!["Alpha", "Beta"]);
    assert_eq!(resp.last_evaluated_table_name, Some("Beta".to_string()));

    // Page 2
    let resp = db
        .list_tables(ListTablesRequest {
            exclusive_start_table_name: Some("Beta".to_string()),
            limit: Some(2),
        })
        .unwrap();
    assert_eq!(resp.table_names, vec!["Delta", "Gamma"]);
    assert!(resp.last_evaluated_table_name.is_none());
}

// ---------------------------------------------------------------------------
// JSON round-trip of request types
// ---------------------------------------------------------------------------

#[test]
fn test_create_table_request_from_json() {
    let json = r#"{
        "TableName": "FromJson",
        "KeySchema": [
            {"AttributeName": "id", "KeyType": "HASH"}
        ],
        "AttributeDefinitions": [
            {"AttributeName": "id", "AttributeType": "S"}
        ]
    }"#;

    let request: CreateTableRequest = serde_json::from_str(json).unwrap();
    let db = make_db();
    let resp = db.create_table(request).unwrap();
    assert_eq!(resp.table_description.table_name, "FromJson");
}