faucet-source-mongodb 1.3.0

MongoDB source connector for the faucet-stream ecosystem
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
//! Integration tests for `MongoSource::stream_pages` against a real MongoDB
//! instance via testcontainers.
//!
//! These tests require Docker. Each test boots its own container and seeds
//! its own collection so they are fully isolated and safe to run in
//! parallel.

use faucet_core::{DEFAULT_BATCH_SIZE, Source};
use faucet_source_mongodb::{MongoSource, MongoSourceConfig};
use futures::StreamExt;
use mongodb::Client;
use mongodb::bson::{Document, doc};
use std::collections::HashMap;
use std::time::Instant;
use testcontainers::{ContainerAsync, runners::AsyncRunner};
use testcontainers_modules::mongo::Mongo;

/// Start a MongoDB container and return both the container handle and a
/// connection URI. The container is kept alive by the returned handle.
async fn start_mongo() -> (ContainerAsync<Mongo>, String) {
    let container: ContainerAsync<Mongo> = Mongo::default()
        .start()
        .await
        .expect("mongo container start");
    let port = container
        .get_host_port_ipv4(27017)
        .await
        .expect("mongo port");
    let uri = format!("mongodb://127.0.0.1:{port}");
    (container, uri)
}

/// Insert `n` documents of the form `{ id: i }` for `i = 1..=n` into
/// `db.collection`.
async fn seed_docs(uri: &str, db: &str, collection: &str, n: i64) {
    let client = Client::with_uri_str(uri).await.expect("client");
    let coll = client.database(db).collection::<Document>(collection);
    let docs: Vec<Document> = (1..=n).map(|i| doc! { "id": i }).collect();
    coll.insert_many(docs).await.expect("insert_many");
}

#[tokio::test(flavor = "multi_thread")]
async fn stream_pages_chunks_documents_into_batch_sized_pages() {
    let (_container, uri) = start_mongo().await;
    seed_docs(&uri, "testdb", "events", 10_000).await;

    let config = MongoSourceConfig::new(uri, "testdb", "events").with_batch_size(1000);
    let source = MongoSource::new(config).await.expect("source new");

    let ctx: HashMap<String, serde_json::Value> = HashMap::new();
    let mut pages = source.stream_pages(&ctx, 1000);

    let mut page_count = 0;
    let mut total = 0;
    while let Some(page) = pages.next().await {
        let page = page.expect("page ok");
        page_count += 1;
        total += page.records.len();
        assert_eq!(
            page.records.len(),
            1000,
            "every page must hold exactly batch_size docs when total is a multiple"
        );
        assert!(
            page.bookmark.is_none(),
            "mongodb source has no incremental mode yet; bookmark must be None"
        );
    }
    assert_eq!(page_count, 10);
    assert_eq!(total, 10_000);
}

#[tokio::test(flavor = "multi_thread")]
async fn stream_pages_partial_final_page() {
    let (_container, uri) = start_mongo().await;
    seed_docs(&uri, "testdb", "events", 2_500).await;

    let config = MongoSourceConfig::new(uri, "testdb", "events").with_batch_size(1000);
    let source = MongoSource::new(config).await.expect("source new");

    let ctx: HashMap<String, serde_json::Value> = HashMap::new();
    let mut pages = source.stream_pages(&ctx, 1000);

    let mut sizes = Vec::new();
    while let Some(page) = pages.next().await {
        sizes.push(page.expect("page ok").records.len());
    }
    assert_eq!(sizes, vec![1000, 1000, 500]);
}

#[tokio::test(flavor = "multi_thread")]
async fn stream_pages_batch_size_zero_emits_single_page() {
    let (_container, uri) = start_mongo().await;
    seed_docs(&uri, "testdb", "events", 10_000).await;

    let config = MongoSourceConfig::new(uri, "testdb", "events").with_batch_size(0);
    let source = MongoSource::new(config).await.expect("source new");

    let ctx: HashMap<String, serde_json::Value> = HashMap::new();
    let mut pages = source.stream_pages(&ctx, 0);

    let mut collected = Vec::new();
    while let Some(page) = pages.next().await {
        collected.push(page.expect("page ok").records.len());
    }
    assert_eq!(
        collected,
        vec![10_000],
        "batch_size = 0 must drain the cursor and emit exactly one page"
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn stream_pages_empty_result_yields_no_pages() {
    let (_container, uri) = start_mongo().await;
    // Don't seed — the collection is empty.

    let config = MongoSourceConfig::new(uri, "testdb", "empty").with_batch_size(DEFAULT_BATCH_SIZE);
    let source = MongoSource::new(config).await.expect("source new");

    let ctx: HashMap<String, serde_json::Value> = HashMap::new();
    let mut pages = source.stream_pages(&ctx, DEFAULT_BATCH_SIZE);

    let mut page_count = 0;
    while let Some(page) = pages.next().await {
        let _ = page.expect("page ok");
        page_count += 1;
    }
    assert_eq!(page_count, 0);
}

#[tokio::test(flavor = "multi_thread")]
async fn stream_pages_preserves_document_contents() {
    let (_container, uri) = start_mongo().await;
    let client = Client::with_uri_str(&uri).await.expect("client");
    let coll = client.database("testdb").collection::<Document>("items");
    coll.insert_many(vec![
        doc! { "id": 1, "name": "alpha" },
        doc! { "id": 2, "name": "beta" },
        doc! { "id": 3, "name": "gamma" },
    ])
    .await
    .expect("insert_many");

    let config = MongoSourceConfig::new(uri, "testdb", "items")
        .sort(serde_json::json!({"id": 1}))
        .with_batch_size(2);
    let source = MongoSource::new(config).await.expect("source new");

    let ctx: HashMap<String, serde_json::Value> = HashMap::new();
    let mut pages = source.stream_pages(&ctx, 2);

    let mut all = Vec::new();
    while let Some(page) = pages.next().await {
        all.extend(page.expect("page ok").records);
    }
    assert_eq!(all.len(), 3);
    assert_eq!(all[0]["id"], 1);
    assert_eq!(all[0]["name"], "alpha");
    assert_eq!(all[2]["name"], "gamma");
}

/// Catches the "buffered-then-chunked" anti-pattern.
///
/// The MongoDB driver's cursor naturally streams documents in batches from
/// the server. The true-streaming impl yields a `StreamPage` after parsing
/// `batch_size` documents off the cursor; the default trait impl materialises
/// every document into a `Vec<Value>` before any page is yielded.
///
/// For a large result, the parse-and-buffer cost dominates and the
/// difference is observable: dropping the stream after the first page in the
/// streaming impl avoids parsing the remaining ~99% of documents.
#[tokio::test(flavor = "multi_thread")]
async fn stream_pages_first_page_completes_without_parsing_full_result() {
    let (_container, uri) = start_mongo().await;
    seed_docs(&uri, "testdb", "events", 100_000).await;

    let ctx: HashMap<String, serde_json::Value> = HashMap::new();

    // Full drain for reference.
    let config_full = MongoSourceConfig::new(&uri, "testdb", "events").with_batch_size(1000);
    let source = MongoSource::new(config_full).await.expect("source new");
    let start = Instant::now();
    let mut pages = source.stream_pages(&ctx, 1000);
    while let Some(page) = pages.next().await {
        let _ = page.expect("page ok");
    }
    let full_elapsed = start.elapsed();
    drop(pages);
    drop(source);

    // First page only.
    let config_first = MongoSourceConfig::new(&uri, "testdb", "events").with_batch_size(1000);
    let source = MongoSource::new(config_first).await.expect("source new");
    let start = Instant::now();
    let mut pages = source.stream_pages(&ctx, 1000);
    let first = pages
        .next()
        .await
        .expect("first page exists")
        .expect("page ok");
    let first_elapsed = start.elapsed();
    drop(pages);
    assert_eq!(first.records.len(), 1000);

    assert!(
        first_elapsed * 2 < full_elapsed,
        "first page should arrive without parsing the full result; \
         first page took {first_elapsed:?}, full drain took {full_elapsed:?}"
    );
}

// --- fetch_all(): the convenience buffering path (distinct from stream_pages) ---

#[tokio::test(flavor = "multi_thread")]
async fn fetch_all_returns_all_matching_documents() {
    let (_container, uri) = start_mongo().await;
    seed_docs(&uri, "testdb", "events", 1_234).await;

    let config = MongoSourceConfig::new(uri, "testdb", "events");
    let source = MongoSource::new(config).await.expect("source new");

    let records = source.fetch_all().await.expect("fetch_all");
    assert_eq!(records.len(), 1_234);
    // Every record carries its `id` field and a server-assigned `_id`.
    assert!(records.iter().all(|r| r.get("id").is_some()));
    assert!(records.iter().all(|r| r["_id"]["$oid"].is_string()));
}

#[tokio::test(flavor = "multi_thread")]
async fn fetch_all_applies_filter_projection_sort_and_limit() {
    let (_container, uri) = start_mongo().await;
    let client = Client::with_uri_str(&uri).await.expect("client");
    let coll = client.database("testdb").collection::<Document>("people");
    coll.insert_many(vec![
        doc! { "id": 1, "name": "alice", "active": true },
        doc! { "id": 2, "name": "bob", "active": false },
        doc! { "id": 3, "name": "carol", "active": true },
        doc! { "id": 4, "name": "dave", "active": true },
    ])
    .await
    .expect("insert_many");

    let config = MongoSourceConfig::new(uri, "testdb", "people")
        .filter(serde_json::json!({"active": true}))
        .projection(serde_json::json!({"_id": 0, "name": 1, "id": 1}))
        .sort(serde_json::json!({"id": -1}))
        .limit(2);
    let source = MongoSource::new(config).await.expect("source new");

    let records = source.fetch_all().await.expect("fetch_all");
    // filter: only active (alice, carol, dave); sort id desc → dave(4), carol(3);
    // limit 2 → two records; projection drops _id.
    assert_eq!(records.len(), 2);
    assert_eq!(records[0]["name"], "dave");
    assert_eq!(records[1]["name"], "carol");
    assert!(
        records[0].get("_id").is_none(),
        "projection must exclude _id: {:?}",
        records[0]
    );
    assert!(
        records[0].get("active").is_none(),
        "projection excludes active"
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn fetch_all_with_cursor_batch_size_and_empty_result() {
    let (_container, uri) = start_mongo().await;
    // Empty collection — exercises the zero-document branch of fetch_all.
    let config = MongoSourceConfig::new(uri, "testdb", "nothing_here").cursor_batch_size(50);
    let source = MongoSource::new(config).await.expect("source new");

    let records = source.fetch_all().await.expect("fetch_all");
    assert!(records.is_empty(), "empty collection yields no records");
}

#[tokio::test(flavor = "multi_thread")]
async fn fetch_all_preserves_bson_types_as_relaxed_extjson() {
    use mongodb::bson::DateTime;
    use mongodb::bson::oid::ObjectId;
    let (_container, uri) = start_mongo().await;
    let client = Client::with_uri_str(&uri).await.expect("client");
    let coll = client.database("testdb").collection::<Document>("typed");
    let oid = ObjectId::parse_str("64ab00112233445566778899").unwrap();
    coll.insert_one(doc! {
        "_id": oid,
        "when": DateTime::from_millis(1_000_000),
        "count": 9_000_000_000i64,
        "nothing": mongodb::bson::Bson::Null,
        "tags": ["x", "y"],
        "nested": { "inner": 5 },
    })
    .await
    .expect("insert_one");

    let config = MongoSourceConfig::new(uri, "testdb", "typed");
    let source = MongoSource::new(config).await.expect("source new");
    let records = source.fetch_all().await.expect("fetch_all");
    assert_eq!(records.len(), 1);
    let r = &records[0];
    assert_eq!(r["_id"]["$oid"], "64ab00112233445566778899");
    assert!(r["when"]["$date"].is_string(), "datetime → $date: {r:?}");
    assert_eq!(r["count"], serde_json::json!(9_000_000_000i64));
    assert_eq!(r["nothing"], serde_json::Value::Null);
    assert_eq!(r["tags"], serde_json::json!(["x", "y"]));
    assert_eq!(r["nested"]["inner"], 5);
}

// --- fetch_with_context: empty ctx delegates to fetch_all; non-empty substitutes ---

#[tokio::test(flavor = "multi_thread")]
async fn fetch_with_context_empty_delegates_to_fetch_all() {
    let (_container, uri) = start_mongo().await;
    seed_docs(&uri, "testdb", "events", 5).await;

    let config = MongoSourceConfig::new(uri, "testdb", "events");
    let source = MongoSource::new(config).await.expect("source new");

    let ctx: HashMap<String, serde_json::Value> = HashMap::new();
    let records = source
        .fetch_with_context(&ctx)
        .await
        .expect("fetch_with_context");
    assert_eq!(records.len(), 5);
}

#[tokio::test(flavor = "multi_thread")]
async fn fetch_with_context_substitutes_filter_placeholder() {
    let (_container, uri) = start_mongo().await;
    let client = Client::with_uri_str(&uri).await.expect("client");
    let coll = client.database("testdb").collection::<Document>("orders");
    coll.insert_many(vec![
        doc! { "id": 1, "region": "us" },
        doc! { "id": 2, "region": "eu" },
        doc! { "id": 3, "region": "us" },
    ])
    .await
    .expect("insert_many");

    // Placeholders use the flat `{key}` syntax over top-level context keys.
    let config = MongoSourceConfig::new(uri, "testdb", "orders")
        .filter(serde_json::json!({"region": "{region}"}))
        .sort(serde_json::json!({"id": 1}));
    let source = MongoSource::new(config).await.expect("source new");

    let mut ctx: HashMap<String, serde_json::Value> = HashMap::new();
    ctx.insert("region".into(), serde_json::json!("us"));

    let records = source
        .fetch_with_context(&ctx)
        .await
        .expect("fetch_with_context");
    // Only the two "us" docs match after substitution.
    let ids: Vec<i64> = records.iter().map(|r| r["id"].as_i64().unwrap()).collect();
    assert_eq!(ids, vec![1, 3]);
}

#[tokio::test(flavor = "multi_thread")]
async fn stream_pages_substitutes_context_into_filter() {
    use futures::StreamExt;
    let (_container, uri) = start_mongo().await;
    let client = Client::with_uri_str(&uri).await.expect("client");
    let coll = client.database("testdb").collection::<Document>("logs");
    coll.insert_many(vec![
        doc! { "id": 1, "level": "info" },
        doc! { "id": 2, "level": "error" },
        doc! { "id": 3, "level": "error" },
    ])
    .await
    .expect("insert_many");

    let config = MongoSourceConfig::new(uri, "testdb", "logs")
        .filter(serde_json::json!({"level": "{level}"}))
        .with_batch_size(10);
    let source = MongoSource::new(config).await.expect("source new");

    let mut ctx: HashMap<String, serde_json::Value> = HashMap::new();
    ctx.insert("level".into(), serde_json::json!("error"));

    let mut pages = source.stream_pages(&ctx, 10);
    let mut total = 0;
    while let Some(page) = pages.next().await {
        total += page.expect("page ok").records.len();
    }
    assert_eq!(
        total, 2,
        "only the two error docs match the substituted filter"
    );
}

// --- discover(): live catalog introspection (#211) ---

#[tokio::test(flavor = "multi_thread")]
async fn discover_enumerates_collections_with_schemas() {
    let (_container, uri) = start_mongo().await;
    let client = Client::with_uri_str(&uri).await.expect("client");
    let db = client.database("shop");
    db.collection::<Document>("orders")
        .insert_many(vec![
            doc! { "id": 1, "note": "a" },
            doc! { "id": 2, "note": "b" },
            doc! { "id": 3, "note": "c" },
        ])
        .await
        .expect("seed orders");
    db.collection::<Document>("carts")
        .insert_many(vec![doc! { "id": 1, "open": true }])
        .await
        .expect("seed carts");

    let config = MongoSourceConfig::new(uri, "shop", "orders");
    let source = MongoSource::new(config).await.expect("source new");
    assert!(source.supports_discover());
    let datasets = source.discover().await.expect("discover");

    let names: Vec<&str> = datasets.iter().map(|d| d.name.as_str()).collect();
    assert!(names.contains(&"orders"), "got: {names:?}");
    assert!(names.contains(&"carts"), "got: {names:?}");
    assert!(
        !names.iter().any(|n| n.starts_with("system.")),
        "system collections must be excluded: {names:?}"
    );

    let orders = datasets
        .iter()
        .find(|d| d.name == "orders")
        .expect("orders dataset");
    assert_eq!(orders.kind, "collection");
    assert_eq!(
        orders.config_patch,
        serde_json::json!({ "collection": "orders" })
    );
    assert_eq!(orders.estimated_rows, Some(3));
    let schema = orders.schema.as_ref().expect("schema from sampled docs");
    assert_eq!(schema["type"], "object");
    assert_eq!(schema["properties"]["id"]["type"], "integer");
    assert_eq!(schema["properties"]["note"]["type"], "string");

    let carts = datasets
        .iter()
        .find(|d| d.name == "carts")
        .expect("carts dataset");
    assert_eq!(carts.estimated_rows, Some(1));
    assert_eq!(
        carts.schema.as_ref().expect("carts schema")["properties"]["open"]["type"],
        "boolean"
    );
}

// --- instance trait methods (require a live server via new()) ---

#[tokio::test(flavor = "multi_thread")]
async fn config_schema_and_dataset_uri_via_instance() {
    let (_container, uri) = start_mongo().await;
    let config = MongoSourceConfig::new(uri.clone(), "shop", "carts");
    let source = MongoSource::new(config).await.expect("source new");

    let schema = source.config_schema();
    let props = schema
        .get("properties")
        .and_then(|p| p.as_object())
        .expect("config_schema must be a JSON object schema");
    assert!(props.contains_key("connection_uri"), "schema: {schema}");
    assert!(props.contains_key("filter"), "schema documents `filter`");
    assert!(
        props.contains_key("batch_size"),
        "schema documents `batch_size`"
    );

    let ds = source.dataset_uri();
    assert!(ds.ends_with("/shop/carts"), "dataset_uri: {ds}");
}