Skip to content

Commit

Permalink
fix: add tests and fix middle syntax error
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielc committed Dec 4, 2024
1 parent c296b8a commit 1fe06af
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM public.ecr.aws/r5b3e0r5/3box/rust-builder:latest as builder
FROM public.ecr.aws/r5b3e0r5/3box/rust-builder:latest AS builder

RUN mkdir -p /home/builder/rust-ceramic
WORKDIR /home/builder/rust-ceramic
Expand Down
17 changes: 9 additions & 8 deletions event-svc/src/store/sql/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ impl EventQuery {
/// Requires binding 1 parameter. Finds the `BlockRow` values needed to rebuild the event
/// Looks up the event by the EventID (ie order_key).
pub fn value_blocks_by_order_key_one() -> &'static str {
r#"SELECT
r#"SELECT
eb.codec, eb.root, b.multihash, b.bytes
FROM ceramic_one_event_block eb
FROM ceramic_one_event_block eb
JOIN ceramic_one_block b on b.multihash = eb.block_multihash
JOIN ceramic_one_event e on e.cid = eb.event_cid
WHERE e.order_key = $1
Expand Down Expand Up @@ -129,7 +129,7 @@ impl EventQuery {
}

pub fn new_delivered_events_id_only() -> &'static str {
r#"SELECT
r#"SELECT
cid, COALESCE(delivered, 0) as "new_highwater_mark"
FROM ceramic_one_event
WHERE delivered >= $1 -- we return delivered+1 so we must match it next search
Expand All @@ -139,8 +139,8 @@ impl EventQuery {

/// Returns the max delivered value in the event table
pub fn max_delivered() -> &'static str {
r#"SELECT
COALESCE(MAX(delivered), 0) as res
r#"SELECT
COALESCE(MAX(delivered), 0) as res
FROM ceramic_one_event;"#
}

Expand Down Expand Up @@ -214,7 +214,7 @@ impl ReconQuery {
TOTAL(ahash_4) & 0xFFFFFFFF as ahash_4, TOTAL(ahash_5) & 0xFFFFFFFF as ahash_5,
TOTAL(ahash_6) & 0xFFFFFFFF as ahash_6, TOTAL(ahash_7) & 0xFFFFFFFF as ahash_7,
COUNT(1) as count
FROM ceramic_one_event
FROM ceramic_one_event
WHERE order_key >= $1 AND order_key < $2;"#
}
}
Expand Down Expand Up @@ -252,9 +252,10 @@ impl ReconQuery {
order_key >= $1 AND order_key < $2
ORDER BY
order_key ASC
LIMIT
1
OFFSET
$3
LIMIT 1;"#
$3;"#
}

pub fn count(db: SqlBackend) -> &'static str {
Expand Down
90 changes: 90 additions & 0 deletions event-svc/src/tests/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,96 @@ macro_rules! test_with_dbs {
}
}

test_with_dbs!(
range_query,
range_query,
[
"delete from ceramic_one_event_block",
"delete from ceramic_one_event",
"delete from ceramic_one_block",
]
);

async fn range_query<S>(store: S)
where
S: recon::Store<Key = EventId, Hash = Sha256a>,
{
let (model, events) = get_events_return_model().await;
let init_cid = events[0].key.cid().unwrap();
let min_id = event_id_min(&init_cid, &model);
let max_id = event_id_max(&init_cid, &model);
recon::Store::insert_many(&store, &events, NodeKey::random().id())
.await
.unwrap();
let values: Vec<EventId> = recon::Store::range(&store, &min_id..&max_id)
.await
.unwrap()
.collect();

let mut expected: Vec<_> = events.into_iter().map(|item| item.key).collect();
expected.sort();
assert_eq!(expected, values);
}

test_with_dbs!(
first_query,
first_query,
[
"delete from ceramic_one_event_block",
"delete from ceramic_one_event",
"delete from ceramic_one_block",
]
);

async fn first_query<S>(store: S)
where
S: recon::Store<Key = EventId, Hash = Sha256a> + std::marker::Sync,
{
let (model, events) = get_events_return_model().await;
let init_cid = events[0].key.cid().unwrap();
let min_id = event_id_min(&init_cid, &model);
let max_id = event_id_max(&init_cid, &model);
recon::Store::insert_many(&store, &events, NodeKey::random().id())
.await
.unwrap();
let first = recon::Store::first(&store, &min_id..&max_id).await.unwrap();

// Sort events into expected because event ids are not sorted in log order
let mut expected: Vec<_> = events.into_iter().map(|item| item.key).collect();
expected.sort();
assert_eq!(Some(expected[0].clone()), first);
}
test_with_dbs!(
middle_query,
middle_query,
[
"delete from ceramic_one_event_block",
"delete from ceramic_one_event",
"delete from ceramic_one_block",
]
);

async fn middle_query<S>(store: S)
where
S: recon::Store<Key = EventId, Hash = Sha256a>,
{
let (model, events) = get_events_return_model().await;
let init_cid = events[0].key.cid().unwrap();
let min_id = event_id_min(&init_cid, &model);
let max_id = event_id_max(&init_cid, &model);
recon::Store::insert_many(&store, &events, NodeKey::random().id())
.await
.unwrap();
let middle = recon::Store::middle(&store, &min_id..&max_id)
.await
.unwrap();

// Sort events into expected because event ids are not sorted in log order
let mut expected: Vec<_> = events.into_iter().map(|item| item.key).collect();
expected.sort();
assert_eq!(Some(expected[expected.len() / 2].clone()), middle);
}

test_with_dbs!(
double_insert,
double_insert,
Expand Down
27 changes: 27 additions & 0 deletions event-svc/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@ pub(crate) fn build_event_id(cid: &Cid, init: &Cid, model: &StreamId) -> EventId
.build()
}

// The EventId that is the minumum of all possible random event ids for that stream
pub(crate) fn event_id_min(init: &Cid, model: &StreamId) -> EventId {
EventId::builder()
.with_network(&Network::DevUnstable)
.with_sep(SEP_KEY, &model.to_vec())
.with_controller(CONTROLLER)
.with_init(init)
.with_min_event()
.build_fencepost()
}
// The EventId that is the maximum of all possible random event ids for that stream
pub(crate) fn event_id_max(init: &Cid, model: &StreamId) -> EventId {
EventId::builder()
.with_network(&Network::DevUnstable)
.with_sep(SEP_KEY, &model.to_vec())
.with_controller(CONTROLLER)
.with_init(init)
.with_max_event()
.build_fencepost()
}

pub(crate) fn random_cid() -> Cid {
let mut data = [0u8; 8];
rand::Rng::fill(&mut ::rand::thread_rng(), &mut data);
Expand Down Expand Up @@ -212,6 +233,12 @@ async fn get_init_plus_n_events_with_model(
events
}

pub(crate) async fn get_events_return_model() -> (StreamId, Vec<ReconItem<EventId>>) {
let model = StreamId::document(random_cid());
let events = get_init_plus_n_events_with_model(&model, 3).await;
(model, events)
}

// builds init -> data -> data that are a stream (will be a different stream each call)
pub(crate) async fn get_events() -> Vec<ReconItem<EventId>> {
let model = StreamId::document(random_cid());
Expand Down
6 changes: 3 additions & 3 deletions interest-svc/src/store/sql/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ impl ReconQuery {
order_key >= $1 AND order_key < $2
ORDER BY
order_key ASC
OFFSET
$3
LIMIT
1;"#
1
OFFSET
$3;"#
}

pub fn count(db: SqlBackend) -> &'static str {
Expand Down
106 changes: 76 additions & 30 deletions interest-svc/src/tests/interest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ async fn access_interest_model(store: impl InterestService) {
}

test_with_dbs!(
test_hash_range_query,
test_hash_range_query,
hash_range_query,
hash_range_query,
["delete from ceramic_one_interest"]
);

async fn test_hash_range_query<S>(store: S)
async fn hash_range_query<S>(store: S)
where
S: recon::Store<Key = Interest, Hash = Sha256a>,
{
Expand Down Expand Up @@ -134,46 +134,92 @@ where
}

test_with_dbs!(
test_range_query,
test_range_query,
range_query,
range_query,
["delete from ceramic_one_interest"]
);

async fn test_range_query<S>(store: S)
async fn range_query<S>(store: S)
where
S: recon::Store<Key = Interest, Hash = Sha256a>,
{
let interest_0 = random_interest(None, None);
let interest_1 = random_interest(None, None);
let mut interests: Vec<_> = (0..10).map(|_| random_interest(None, None)).collect();
let items: Vec<_> = interests
.iter()
.map(|interest| ReconItem::new(interest.clone(), Vec::new()))
.collect();

recon::Store::insert_many(
&store,
&[ReconItem::new(interest_0.clone(), Vec::new())],
NodeKey::random().id(),
)
.await
.unwrap();
recon::Store::insert_many(
&store,
&[ReconItem::new(interest_1.clone(), Vec::new())],
NodeKey::random().id(),
)
.await
.unwrap();
recon::Store::insert_many(&store, &items, NodeKey::random().id())
.await
.unwrap();
let ids = recon::Store::range(&store, &random_interest_min()..&random_interest_max())
.await
.unwrap();
let interests = ids.collect::<BTreeSet<Interest>>();
assert_eq!(BTreeSet::from_iter([interest_0, interest_1]), interests);
let mut ids: Vec<Interest> = ids.collect();
interests.sort();
ids.sort();
assert_eq!(interests, ids);
}

test_with_dbs!(
first_query,
first_query,
["delete from ceramic_one_interest"]
);

async fn first_query<S>(store: S)
where
S: recon::Store<Key = Interest, Hash = Sha256a> + Sync,
{
let mut interests: Vec<_> = (0..10).map(|_| random_interest(None, None)).collect();
let items: Vec<_> = interests
.iter()
.map(|interest| ReconItem::new(interest.clone(), Vec::new()))
.collect();

recon::Store::insert_many(&store, &items, NodeKey::random().id())
.await
.unwrap();
let first = recon::Store::first(&store, &random_interest_min()..&random_interest_max())
.await
.unwrap();
interests.sort();
assert_eq!(Some(interests[0].clone()), first);
}

test_with_dbs!(
middle_query,
middle_query,
["delete from ceramic_one_interest"]
);

async fn middle_query<S>(store: S)
where
S: recon::Store<Key = Interest, Hash = Sha256a> + Sync,
{
let mut interests: Vec<_> = (0..10).map(|_| random_interest(None, None)).collect();
let items: Vec<_> = interests
.iter()
.map(|interest| ReconItem::new(interest.clone(), Vec::new()))
.collect();

recon::Store::insert_many(&store, &items, NodeKey::random().id())
.await
.unwrap();
let middle = recon::Store::middle(&store, &random_interest_min()..&random_interest_max())
.await
.unwrap();
interests.sort();
assert_eq!(Some(interests[interests.len() / 2].clone()), middle);
}

test_with_dbs!(
test_double_insert,
test_double_insert,
double_insert,
double_insert,
["delete from ceramic_one_interest"]
);

async fn test_double_insert<S>(store: S)
async fn double_insert<S>(store: S)
where
S: recon::Store<Key = Interest, Hash = Sha256a>,
{
Expand All @@ -200,12 +246,12 @@ where
}

test_with_dbs!(
test_value_for_key,
test_value_for_key,
value_for_key,
value_for_key,
["delete from ceramic_one_interest"]
);

async fn test_value_for_key<S>(store: S)
async fn value_for_key<S>(store: S)
where
S: recon::Store<Key = Interest, Hash = Sha256a>,
{
Expand Down

0 comments on commit 1fe06af

Please sign in to comment.