MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / test_subscribe_negative_diffs

Function test_subscribe_negative_diffs

src/environmentd/tests/sql.rs:396–444  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

394#[mz_ore::test]
395#[allow(clippy::disallowed_methods)]
396fn test_subscribe_negative_diffs() {
397 let server = test_util::TestHarness::default().start_blocking();
398 let mut client_writes = server.connect(postgres::NoTls).unwrap();
399 let mut client_reads = server.connect(postgres::NoTls).unwrap();
400
401 client_writes
402 .batch_execute("CREATE TABLE t (data text)")
403 .unwrap();
404 client_writes.batch_execute(
405 "CREATE MATERIALIZED VIEW counts AS SELECT data AS key, COUNT(data) AS count FROM t GROUP BY data",
406 ).unwrap();
407 client_reads
408 .batch_execute(
409 "BEGIN;
410 DECLARE c CURSOR FOR SUBSCRIBE counts;",
411 )
412 .unwrap();
413
414 let data = format!("line {}", 42);
415 client_writes
416 .execute("INSERT INTO t VALUES ($1)", &[&data])
417 .unwrap();
418 let row = client_reads.query_one("FETCH ALL c", &[]).unwrap();
419
420 assert_eq!(row.get::<_, i64>("mz_diff"), 1);
421 assert_eq!(row.get::<_, String>("key"), data);
422 assert_eq!(row.get::<_, i64>("count"), 1);
423
424 // send another row with the same key, this will retract the previous
425 // count and emit an updated count
426
427 let data = format!("line {}", 42);
428 client_writes
429 .execute("INSERT INTO t VALUES ($1)", &[&data])
430 .unwrap();
431
432 let rows = client_reads.query("FETCH ALL c", &[]).unwrap();
433 let mut rows = rows.iter();
434
435 let row = rows.next().expect("missing result");
436 assert_eq!(row.get::<_, i64>("mz_diff"), -1);
437 assert_eq!(row.get::<_, String>("key"), data);
438 assert_eq!(row.get::<_, i64>("count"), 1);
439
440 let row = rows.next().expect("missing result");
441 assert_eq!(row.get::<_, i64>("mz_diff"), 1);
442 assert_eq!(row.get::<_, String>("key"), data);
443 assert_eq!(row.get::<_, i64>("count"), 2);
444}
445
446#[mz_ore::test(tokio::test(flavor = "multi_thread", worker_threads = 1))]
447#[allow(clippy::disallowed_methods)]

Callers

nothing calls this directly

Calls 10

start_blockingMethod · 0.80
unwrapMethod · 0.80
batch_executeMethod · 0.80
expectMethod · 0.80
connectMethod · 0.45
executeMethod · 0.45
query_oneMethod · 0.45
queryMethod · 0.45
iterMethod · 0.45
nextMethod · 0.45

Tested by

no test coverage detected