ingraph 0.1.0

A declaration-driven indexing framework: records and nodes are written as Rust types, and the storage rows, the dataflow between them and the GraphQL query surface are emitted from the declaration.
Documentation
use super::super::{Positions, RecordedPositions, Track};

#[test]
fn a_track_names_the_reader_and_what_it_reads() {
  let track = Track::new("keyframes", "scene::store");
  assert_eq!(track.node(), "keyframes");
  assert_eq!(track.source(), "scene::store");
  assert_eq!(track.to_string(), "keyframes/scene::store");
}

#[tokio::test]
async fn an_unadvanced_track_has_no_position() {
  let mut positions: RecordedPositions<i64> = RecordedPositions::new();
  assert!(positions.is_empty());
  assert_eq!(
    positions
      .position_async(&Track::new("a", "b"))
      .await
      .expect("in memory"),
    None
  );
}

#[tokio::test]
async fn advancing_replaces_the_track_rather_than_appending_to_it() {
  let mut positions = RecordedPositions::new();
  let track = Track::new("keyframes", "scene::store");
  positions
    .advance_async(&track, 4i64, &[])
    .await
    .expect("in memory");
  positions
    .advance_async(&track, 9i64, &[])
    .await
    .expect("in memory");
  assert_eq!(positions.len(), 1, "one track, one row");
  assert_eq!(
    positions.position_async(&track).await.expect("in memory"),
    Some(9)
  );
}

#[tokio::test]
async fn two_readers_of_one_source_keep_independent_positions() {
  // Which is what makes re-enabling a node free: it resumes from ITS own position, and the other
  // reader of the same store never noticed it was away.
  let mut positions = RecordedPositions::new();
  let first = Track::new("keyframes", "scene::store");
  let second = Track::new("captions", "scene::store");
  positions
    .advance_async(&first, 10i64, &[])
    .await
    .expect("in memory");
  positions
    .advance_async(&second, 3i64, &[])
    .await
    .expect("in memory");
  assert_eq!(positions.len(), 2);
  assert_eq!(
    positions.position_async(&first).await.expect("in memory"),
    Some(10)
  );
  assert_eq!(
    positions.position_async(&second).await.expect("in memory"),
    Some(3)
  );
}

#[cfg(feature = "sqlite")]
mod floor {
  use sqlx::Connection as _;

  use super::super::super::{Positions, SqlitePositions, Track};

  /// A bare `SqliteConnection` and never a `Pool`: constructing a pool spawns a reaper through
  /// sqlx's runtime shim and panics asking for a feature this crate deliberately does not enable.
  async fn open() -> SqlitePositions {
    let conn = sqlx::sqlite::SqliteConnection::connect("sqlite::memory:")
      .await
      .expect("an in-memory database");
    SqlitePositions::open(conn).await.expect("the table")
  }

  #[tokio::test]
  async fn opening_twice_is_the_same_table() {
    let mut positions = open().await;
    let track = Track::new("keyframes", "scene");
    positions
      .advance_async(&track, 7, &[])
      .await
      .expect("the write");

    // `CREATE TABLE IF NOT EXISTS` on a connection that already has the table, plus the row it
    // wrote: re-opening is what a restart does, and it must not lose a position.
    let conn = positions.into_connection();
    let mut positions = SqlitePositions::open(conn).await.expect("the table again");
    assert_eq!(
      positions.position_async(&track).await.expect("the read"),
      Some(7)
    );
  }

  #[tokio::test]
  async fn an_advance_is_a_keyed_replace_and_never_a_second_row() {
    let mut positions = open().await;
    let track = Track::new("keyframes", "scene");
    for to in [1, 2, 3, 3, 9] {
      positions
        .advance_async(&track, to, &[])
        .await
        .expect("the write");
    }
    assert_eq!(
      positions.position_async(&track).await.expect("the read"),
      Some(9)
    );

    // The count is the assertion: the upsert is what makes a replayed advance write the same row,
    // which is the storage law this layer runs on applied to the cursor itself.
    use sqlx::Row as _;
    let mut conn = positions.into_connection();
    let rows: i64 = sqlx::query("SELECT COUNT(*) FROM ingraph_position")
      .fetch_one(&mut conn)
      .await
      .expect("the count")
      .get(0);
    assert_eq!(rows, 1);
  }

  #[tokio::test]
  async fn tracks_are_addressed_by_both_halves() {
    let mut positions = open().await;
    let unit_a = Track::new("a", "scene");
    let unit_b = Track::new("b", "scene");
    let other_source = Track::new("a", "ocr");
    positions
      .advance_async(&unit_a, 1, &[])
      .await
      .expect("write");
    positions
      .advance_async(&unit_b, 2, &[])
      .await
      .expect("write");
    positions
      .advance_async(&other_source, 3, &[])
      .await
      .expect("write");

    assert_eq!(
      positions.position_async(&unit_a).await.expect("read"),
      Some(1)
    );
    assert_eq!(
      positions.position_async(&unit_b).await.expect("read"),
      Some(2)
    );
    assert_eq!(
      positions.position_async(&other_source).await.expect("read"),
      Some(3)
    );
    assert_eq!(
      positions
        .position_async(&Track::new("c", "scene"))
        .await
        .expect("read"),
      None
    );
  }
}