mod book;
mod person;
use chrono::Utc;
use std::{borrow::Cow, thread, time};
use uuid::Uuid;
#[test]
fn test_projector() {
let mut my_book = book::Book {
uuid: Uuid::new_v4(),
some_number: 42,
author: person::Person {
uuid: Uuid::new_v4(),
first_name: String::from("Alex"),
last_name: String::from("Example"),
},
};
let mut books = crate::Projector::<book::Book>::new();
println!("Empty projector:");
println!("{:?}\n", books.get_projection());
assert_eq!(books.get_projection().len(), 0);
books
.push(crate::Event::create(Cow::Owned(my_book.clone())))
.unwrap();
println!("Projector after creating new book:");
println!("{:?}\n", books.get_projection());
assert_eq!(books.get_projection().get(0).unwrap().some_number, 42);
let timestamp: crate::Timestamp = Utc::now();
thread::sleep(time::Duration::from_millis(1));
my_book.some_number = 123;
books
.push(crate::Event::update(Cow::Borrowed(&my_book)))
.unwrap();
println!("Projector after updating the book:");
println!("{:?}\n", books.get_projection());
assert_eq!(books.get_projection().get(0).unwrap().some_number, 123);
println!("Projector before the book was updated:");
println!("{:?}\n", books.project_at(×tamp));
assert_eq!(
books
.project_at(×tamp)
.unwrap()
.get(0)
.unwrap()
.some_number,
42
);
}
#[test]
fn test_snapshots() {
let mut my_book = book::Book {
uuid: Uuid::new_v4(),
some_number: 42,
author: person::Person {
uuid: Uuid::new_v4(),
first_name: String::from("Alex"),
last_name: String::from("Example"),
},
};
let mut books = crate::Projector::<book::Book>::new();
println!("Empty projector:");
println!("{:?}\n", books.get_projection());
assert_eq!(books.get_projection().len(), 0);
books
.push(crate::Event::create(Cow::Owned(my_book.clone())))
.unwrap();
println!("Projector after creating new book:");
println!("{:?}\n", books.get_projection());
assert_eq!(books.get_projection().get(0).unwrap().some_number, 42);
let timestamp: crate::Timestamp = Utc::now();
thread::sleep(time::Duration::from_millis(1));
books.make_snapshot();
my_book.some_number = 123;
books
.push(crate::Event::update(Cow::Owned(my_book.clone())))
.unwrap();
println!("Projector after updating the book:");
println!("{:?}\n", books.get_projection());
assert_eq!(books.get_projection().get(0).unwrap().some_number, 123);
println!("Projector before the book was updated:");
println!("{:?}\n", books.project_at(×tamp));
assert_eq!(
books
.project_at(×tamp)
.unwrap()
.get(0)
.unwrap()
.some_number,
42
);
}
#[test]
fn test_many_change_after_snapshots() {
let mut my_book = book::Book {
uuid: Uuid::new_v4(),
some_number: 42,
author: person::Person {
uuid: Uuid::new_v4(),
first_name: String::from("Alex"),
last_name: String::from("Example"),
},
};
let mut books = crate::Projector::<book::Book>::new();
println!("Empty projector:");
println!("{:?}\n", books.get_projection());
assert_eq!(books.get_projection().len(), 0);
books
.push(crate::Event::create(Cow::Owned(my_book.clone())))
.unwrap();
println!("Projector after creating new book:");
println!("{:?}\n", books.get_projection());
assert_eq!(books.get_projection().get(0).unwrap().some_number, 42);
let timestamp: crate::Timestamp = Utc::now();
thread::sleep(time::Duration::from_millis(1));
books.make_snapshot();
my_book.some_number = 123;
books
.push(crate::Event::update(Cow::Owned(my_book.clone())))
.unwrap();
let timestamp_2: crate::Timestamp = Utc::now();
thread::sleep(time::Duration::from_millis(1));
thread::sleep(time::Duration::from_millis(1));
books.make_snapshot(); thread::sleep(time::Duration::from_millis(1));
books.make_snapshot();
my_book.some_number = 321;
books
.push(crate::Event::update(Cow::Owned(my_book.clone())))
.unwrap();
println!("Projector after updating the book:");
println!("{:?}\n", books.get_projection());
assert_eq!(books.get_projection().get(0).unwrap().some_number, 321);
println!("Projector before the book was updated:");
println!("{:?}\n", books.project_at(×tamp));
assert_eq!(
books
.project_at(×tamp)
.unwrap()
.get(0)
.unwrap()
.some_number,
42
);
println!("Projector before the book was updated again:");
println!("{:?}\n", books.project_at(×tamp));
assert_eq!(
books
.project_at(×tamp_2)
.unwrap()
.get(0)
.unwrap()
.some_number,
123
);
println!("Projector after updating the book:");
println!("{:?}\n", books.get_projection());
assert_eq!(books.get_projection().get(0).unwrap().some_number, 321);
println!("Projector before the book was updated:");
println!("{:?}\n", books.project_at(×tamp));
assert_eq!(
books
.project_at(×tamp)
.unwrap()
.get(0)
.unwrap()
.some_number,
42
);
println!("Projector before the book was updated again:");
println!("{:?}\n", books.project_at(×tamp));
assert_eq!(
books
.project_at(×tamp_2)
.unwrap()
.get(0)
.unwrap()
.some_number,
123
);
println!("Projector after updating the book:");
println!("{:?}\n", books.get_projection());
assert_eq!(books.get_projection().get(0).unwrap().some_number, 321);
println!("Projector before the book was updated:");
println!("{:?}\n", books.project_at(×tamp));
assert_eq!(
books
.project_at(×tamp)
.unwrap()
.get(0)
.unwrap()
.some_number,
42
);
println!("Projector before the book was updated again:");
println!("{:?}\n", books.project_at(×tamp));
assert_eq!(
books
.project_at(×tamp_2)
.unwrap()
.get(0)
.unwrap()
.some_number,
123
);
println!("Projector after updating the book:");
println!("{:?}\n", books.get_projection());
assert_eq!(books.get_projection().get(0).unwrap().some_number, 321);
println!("Projector before the book was updated:");
println!("{:?}\n", books.project_at(×tamp));
assert_eq!(
books
.project_at(×tamp)
.unwrap()
.get(0)
.unwrap()
.some_number,
42
);
println!("Projector before the book was updated again:");
println!("{:?}\n", books.project_at(×tamp));
assert_eq!(
books
.project_at(×tamp_2)
.unwrap()
.get(0)
.unwrap()
.some_number,
123
);
}