about summary refs log tree commit diff
path: root/finito-postgres/src
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2018-09-26T16·44+0200
committerVincent Ambo <mail@tazj.in>2018-09-26T16·44+0200
commit3891ba84d5dbf335442c1c9e263823820f2a3327 (patch)
treeb5749516e57c03350b6a4d2a6374c2ce765e356d /finito-postgres/src
parent7e5592f0d186dde6e3e10be51efce2bdfc7483d0 (diff)
test(postgres): Add test for insert_machine and advance
Adds a test for the two most important functions in Finito's
PostgreSQL backend.

These actually require a local Postgres database to be available when
running. Currently the connection details are hardcoded in the test.
Diffstat (limited to 'finito-postgres/src')
-rw-r--r--finito-postgres/src/lib.rs3
-rw-r--r--finito-postgres/src/tests.rs47
2 files changed, 50 insertions, 0 deletions
diff --git a/finito-postgres/src/lib.rs b/finito-postgres/src/lib.rs
index 80200f8a43cd..5ed9693bc9b3 100644
--- a/finito-postgres/src/lib.rs
+++ b/finito-postgres/src/lib.rs
@@ -13,6 +13,9 @@ extern crate serde;
 extern crate serde_json;
 extern crate uuid;
 
+#[cfg(test)] mod tests;
+#[cfg(test)] extern crate finito_door;
+
 mod error;
 pub use error::{Result, Error};
 
diff --git a/finito-postgres/src/tests.rs b/finito-postgres/src/tests.rs
new file mode 100644
index 000000000000..55ada1329143
--- /dev/null
+++ b/finito-postgres/src/tests.rs
@@ -0,0 +1,47 @@
+use super::*;
+use finito;
+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");
+}