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
		
			
				
	
	
		
			47 lines
		
	
	
	
		
			1.4 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			47 lines
		
	
	
	
		
			1.4 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| 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");
 | |
| }
 |