summaryrefslogtreecommitdiff
path: root/server/tests/table_test.rs
diff options
context:
space:
mode:
authorKjetil Orbekk <kj@orbekk.com>2023-01-02 11:58:49 -0500
committerKjetil Orbekk <kj@orbekk.com>2023-01-02 11:58:49 -0500
commit8696dcc0afdc0896081405df13c887384500961b (patch)
treeeea3f0d0534795611ba4c05dcc943ff9af364465 /server/tests/table_test.rs
parent2d50ae0b134366b875fa54912422b2484dd70d9b (diff)
Insert table in db on creation
Diffstat (limited to 'server/tests/table_test.rs')
-rw-r--r--server/tests/table_test.rs50
1 files changed, 44 insertions, 6 deletions
diff --git a/server/tests/table_test.rs b/server/tests/table_test.rs
index 8b691b9..87547c3 100644
--- a/server/tests/table_test.rs
+++ b/server/tests/table_test.rs
@@ -1,12 +1,18 @@
use protocol::bridge_engine::TableState;
use rand::{thread_rng, Rng};
-use server::table::{Table, InMemoryTable, DbTable};
+use server::{
+ error::BridgeError,
+ table::{DbTable, InMemoryTable, Table},
+};
+use uuid::Uuid;
mod common;
-async fn table_basic_test(table: Box<dyn Table>) -> Result<(), anyhow::Error> {
+async fn table_basic_test(
+ mut table: Box<dyn Table + Send>,
+) -> Result<(), anyhow::Error> {
assert!(matches!(table.state(), TableState::Unknown));
- let mut table = table.new_deal().await?;
+ table = table.new_deal().await?;
assert!(matches!(table.state(), TableState::Game(_)));
while matches!(table.state(), TableState::Game(_)) {
table = server::table::advance_play(table).await?;
@@ -18,6 +24,16 @@ async fn table_basic_test(table: Box<dyn Table>) -> Result<(), anyhow::Error> {
Ok(())
}
+async fn advance_table(
+ table: Box<dyn Table + Send>,
+) -> Result<Box<dyn Table + Send>, BridgeError> {
+ match table.state() {
+ TableState::Unknown => panic!("unexpected state"),
+ TableState::Game(g) => server::table::advance_play(table).await,
+ TableState::Result(g) => table.new_deal().await,
+ }
+}
+
#[tokio::test]
#[ignore]
async fn in_memory_table() -> Result<(), anyhow::Error> {
@@ -30,7 +46,27 @@ async fn in_memory_table() -> Result<(), anyhow::Error> {
#[ignore]
async fn db_table() -> Result<(), anyhow::Error> {
let db = common::TestDb::new().await;
- table_basic_test(Box::new(DbTable::new(db.db().clone()))).await?;
+ table_basic_test(Box::new(
+ DbTable::new(db.db().clone(), Uuid::new_v4()).await?,
+ ))
+ .await?;
+ Ok(())
+}
+
+// Table testing:
+// Creating a table for a specific owner.
+// Recreating a table with the same owner should return the same table.
+
+#[tokio::test]
+#[ignore]
+async fn db_table_idempotent() -> Result<(), anyhow::Error> {
+ let db = common::TestDb::new().await;
+ let uuid = Uuid::new_v4();
+ let table1: Box<dyn Table + Send> =
+ Box::new(DbTable::new(db.db().clone(), uuid).await?);
+ let table2: Box<dyn Table + Send> =
+ Box::new(DbTable::new(db.db().clone(), uuid).await?);
+ assert_eq!(table1.state(), table2.state());
Ok(())
}
@@ -38,9 +74,11 @@ async fn db_table() -> Result<(), anyhow::Error> {
#[ignore]
async fn db_table_persistence() -> Result<(), anyhow::Error> {
let db = common::TestDb::new().await;
- let mut table = Box::new(DbTable::new(db.db().clone()));
+ let mut table: Box<dyn Table + Send> =
+ Box::new(DbTable::new(db.db().clone(), Uuid::new_v4()).await?);
+ table = table.new_deal().await?;
for i in 0..(thread_rng().gen_range(0..200)) {
- todo!()
+ table = advance_table(table).await?;
}
Ok(())
}