Skip to content

Commit

Permalink
its: Added EventStore::create
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Jan 29, 2022
1 parent 75e116a commit e1b64c2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions its/crates/adapter_sqlite/sql/insert_aggregate.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
INSERT INTO aggregates (id, version, type)
VALUES ($1, $2, $3)
2 changes: 2 additions & 0 deletions its/crates/adapter_sqlite/sql/insert_event.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
INSERT INTO events (aggregate_id, version, data)
VALUES ($1, $2, $3)
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ mod tests {
use std::path::PathBuf;

use super::*;
use sqlx::{sqlite::SqliteRow, Row};
use sqlx::{sqlite::SqliteRow, Acquire, Connection, Row};
use tempfile::tempdir;
use ulid::Ulid;

Expand Down Expand Up @@ -80,6 +80,32 @@ mod tests {
Ok(Self { connection: conn })
}

async fn create(
&mut self,
aggregate_id: Ulid,
r#type: String,
version: u64,
data: String,
) -> anyhow::Result<()> {
// TODO: transaction

sqlx::query(include_str!("../../../sql/insert_aggregate.sql"))
.bind(aggregate_id.to_string())
.bind(i64::from_be_bytes(version.to_be_bytes()))
.bind(r#type)
.fetch_all(&mut self.connection)
.await?;

sqlx::query(include_str!("../../../sql/insert_event.sql"))
.bind(aggregate_id.to_string())
.bind(i64::from_be_bytes(version.to_be_bytes()))
.bind(data)
.fetch_all(&mut self.connection)
.await?;

Ok(())
}

async fn find_aggregates(&mut self) -> anyhow::Result<Vec<AggregateRow>> {
Ok(
sqlx::query(include_str!("../../../sql/select_aggregates.sql"))
Expand Down Expand Up @@ -130,6 +156,20 @@ mod tests {
let events = event_store.find_events().await?;
assert!(events.is_empty());

let aggregate_id = Ulid::new();
let r#type = "Issue".to_string();
let version = 1;
let data = r#"{"type":"issue_created"}"#.to_string();
event_store
.create(aggregate_id, r#type, version, data)
.await?;

// TODO: improve
let aggregates = event_store.find_aggregates().await?;
assert!(!aggregates.is_empty());
let events = event_store.find_events().await?;
assert!(!events.is_empty());

Ok(())
}
}

0 comments on commit e1b64c2

Please sign in to comment.