Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions eventually-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ tokio = { version = "0.2", features = ["sync"] }
tokio-postgres = "0.5"
femme = "2.1.0"
anyhow = "1.0"

[dev-dependencies]
reqwest = { version = "0.10", features = ["json"] }
lazy_static = "1.4"
8 changes: 8 additions & 0 deletions eventually-test/src/bin/eventually-test-api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use envconfig::Envconfig;

use eventually_test::config::Config;

fn main() -> anyhow::Result<()> {
let config = Config::init()?;
smol::run(eventually_test::run(config))
}
2 changes: 1 addition & 1 deletion eventually-test/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use envconfig::Envconfig;
use envconfig_derive::Envconfig;

#[derive(Envconfig)]
pub(crate) struct Config {
pub struct Config {
#[envconfig(from = "DB_HOST", default = "localhost")]
pub db_host: String,

Expand Down
14 changes: 3 additions & 11 deletions eventually-test/src/main.rs → eventually-test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
mod api;
mod config;
mod order;
pub mod config;
pub mod order;
mod state;

use std::sync::Arc;

use envconfig::Envconfig;

use eventually::aggregate::Optional;
use eventually::inmemory::EventStoreBuilder;
use eventually::{AggregateRootBuilder, Repository};
Expand All @@ -16,13 +14,7 @@ use tokio::sync::RwLock;
use crate::config::Config;
use crate::order::OrderAggregate;

fn main() -> anyhow::Result<()> {
smol::run(run())
}

async fn run() -> anyhow::Result<()> {
let config = Config::init()?;

pub async fn run(config: Config) -> anyhow::Result<()> {
femme::with_level(config.log_level);

// Aggregate target: in this case it's empty, but usually it would use
Expand Down
25 changes: 24 additions & 1 deletion eventually-test/src/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl OrderItems {
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(tag = "state")]
pub enum OrderState {
Editable { updated_at: DateTime<Utc> },
Expand All @@ -52,12 +52,35 @@ pub enum OrderState {

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Order {
#[serde(skip_serializing)]
id: String,
created_at: DateTime<Utc>,
items: Vec<OrderItem>,
state: OrderState,
}

impl Order {
pub fn created_at(&self) -> DateTime<Utc> {
self.created_at
}

pub fn items(&self) -> &Vec<OrderItem> {
&self.items
}

pub fn state(&self) -> OrderState {
self.state
}

pub fn is_editable(&self) -> bool {
if let OrderState::Editable { .. } = self.state {
return true;
}

false
}
}

#[derive(Debug)]
pub enum OrderCommand {
Create,
Expand Down
56 changes: 56 additions & 0 deletions eventually-test/tests/acceptance_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::sync::atomic::AtomicBool;
use std::sync::Once;
use std::thread;

use chrono::Utc;

use envconfig::Envconfig;

use lazy_static::lazy_static;

use eventually_test::config::Config;
use eventually_test::order::Order;

static START: Once = Once::new();

lazy_static! {
static ref SERVER_STARTED: AtomicBool = AtomicBool::default();
}

fn setup() {
START.call_once(|| {
thread::spawn(move || {
let config = Config::init().unwrap();
SERVER_STARTED.store(true, std::sync::atomic::Ordering::SeqCst);

smol::run(eventually_test::run(config));
});
});

while !SERVER_STARTED.load(std::sync::atomic::Ordering::SeqCst) {}
}

#[test]
fn it_creates_an_order_successfully() {
setup();

smol::run(async {
let url = format!("http://localhost:8080/orders/test/create");
let client = reqwest::Client::new();

let start = Utc::now();

let root: Order = client
.post(&url)
.send()
.await
.unwrap()
.json()
.await
.unwrap();

assert!(root.created_at() >= start);
assert!(root.is_editable());
assert!(root.items().is_empty());
});
}