feat(tazjin/finito): Check in my old Rust state-machine library
I dug through my archives for this and found a version that, while unfortunately not the latest implementation, is close enough to the real thing to show off what Finito did. This is a Postgres-backed state-machine library for complex application logic. I wrote this originally for a work purpose in a previous life, but have always wanted to apply it elsewhere, too. git-subtree-dir: users/tazjin/finito git-subtree-mainline:0380841eb1git-subtree-split:b748117225Change-Id: I0de02d6258568447a14870f1a533812a67127763
This commit is contained in:
commit
9e7b81391d
13 changed files with 1279 additions and 0 deletions
47
users/tazjin/finito/finito-postgres/src/tests.rs
Normal file
47
users/tazjin/finito/finito-postgres/src/tests.rs
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
use super::*;
|
||||
|
||||
use finito_door::*;
|
||||
use postgres::{Connection, TlsMode};
|
||||
|
||||
// TODO: read config from environment
|
||||
fn open_test_connection() -> Connection {
|
||||
Connection::connect("postgres://finito:finito@localhost/finito", TlsMode::None)
|
||||
.expect("Failed to connect to test database")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_insert_machine() {
|
||||
let conn = open_test_connection();
|
||||
let initial = DoorState::Opened;
|
||||
let door = insert_machine(&conn, initial).expect("Failed to insert door");
|
||||
let result = get_machine(&conn, &door, false).expect("Failed to fetch door");
|
||||
|
||||
assert_eq!(result, DoorState::Opened, "Inserted door state should match");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_advance() {
|
||||
let conn = open_test_connection();
|
||||
|
||||
let initial = DoorState::Opened;
|
||||
let events = vec![
|
||||
DoorEvent::Close,
|
||||
DoorEvent::Open,
|
||||
DoorEvent::Close,
|
||||
DoorEvent::Lock(1234),
|
||||
DoorEvent::Unlock(1234),
|
||||
DoorEvent::Lock(4567),
|
||||
DoorEvent::Unlock(1234),
|
||||
];
|
||||
|
||||
let door = insert_machine(&conn, initial).expect("Failed to insert door");
|
||||
|
||||
for event in events {
|
||||
advance(&conn, &door, event).expect("Failed to advance door FSM");
|
||||
}
|
||||
|
||||
let result = get_machine(&conn, &door, false).expect("Failed to fetch door");
|
||||
let expected = DoorState::Locked { code: 4567, attempts: 2 };
|
||||
|
||||
assert_eq!(result, expected, "Advanced door state should match");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue