diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2023-01-02 11:00:58 -0500 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2023-01-02 11:00:58 -0500 |
commit | 7f7e45cc3be5ea065ffd69863c566accb8b45269 (patch) | |
tree | bcbcda503f1ab783d53e81aceaa7212f4b84a5e4 | |
parent | 33dfcd18252dd8a4845cd58b93bd177ab0dffde9 (diff) |
Add failing test for DbTable
-rw-r--r-- | server/src/table.rs | 61 | ||||
-rw-r--r-- | server/tests/table_test.rs | 22 |
2 files changed, 69 insertions, 14 deletions
diff --git a/server/src/table.rs b/server/src/table.rs index cc00327..7e49c7f 100644 --- a/server/src/table.rs +++ b/server/src/table.rs @@ -1,13 +1,15 @@ use async_trait::async_trait; use protocol::{ + actions::Bid, bot::{BiddingBot, PlayingBot}, bridge_engine::{ BiddingStatePlayerView, GameState, PlayStatePlayerView, TableState, }, card::Card, - simple_bots::{AlwaysPassBiddingBot, RandomPlayingBot}, actions::Bid, + simple_bots::{AlwaysPassBiddingBot, RandomPlayingBot}, }; use rand::random; +use sqlx::PgPool; use crate::error::BridgeError; @@ -17,12 +19,12 @@ pub trait Table { async fn bid( self: Box<Self>, bid: Bid, - ) -> Result<Box<dyn Table>, BridgeError>; + ) -> Result<Box<dyn Table + Send>, BridgeError>; async fn play( self: Box<Self>, card: Card, - ) -> Result<Box<dyn Table>, BridgeError>; - async fn new_deal(self: Box<Self>) -> Result<Box<dyn Table>, BridgeError>; + ) -> Result<Box<dyn Table + Send>, BridgeError>; + async fn new_deal(self: Box<Self>) -> Result<Box<dyn Table + Send>, BridgeError>; } pub struct InMemoryTable { @@ -46,7 +48,7 @@ impl Table for InMemoryTable { async fn bid( self: Box<Self>, bid: Bid, - ) -> Result<Box<dyn Table>, BridgeError> { + ) -> Result<Box<dyn Table + Send>, BridgeError> { let game = match self.state { TableState::Game(game) => game, _ => { @@ -60,7 +62,7 @@ impl Table for InMemoryTable { async fn play( self: Box<Self>, card: Card, - ) -> Result<Box<dyn Table>, BridgeError> { + ) -> Result<Box<dyn Table + Send>, BridgeError> { let game = match self.state { TableState::Game(game) => game, _ => { @@ -71,7 +73,7 @@ impl Table for InMemoryTable { Ok(Box::new(Self { state: game.into() })) } - async fn new_deal(self: Box<Self>) -> Result<Box<dyn Table>, BridgeError> { + async fn new_deal(self: Box<Self>) -> Result<Box<dyn Table + Send>, BridgeError> { Ok(Box::new(Self { state: GameState::new(random(), random()).into(), })) @@ -80,7 +82,7 @@ impl Table for InMemoryTable { pub async fn advance_play( table: Box<dyn Table>, -) -> Result<Box<dyn Table>, BridgeError> { +) -> Result<Box<dyn Table + Send>, BridgeError> { let game = match table.state() { TableState::Game(game) => game, _ => return Err(BridgeError::InvalidRequest("no game".to_string())), @@ -108,8 +110,41 @@ pub async fn advance_play( table } -// pub struct DbTable -// { -// db: PgPool, -// pub state: TableState, -// } +pub struct DbTable { + db: PgPool, + pub inner: Box<dyn Table + Send>, +} + +impl DbTable { + pub fn new(db: PgPool) -> Self { + Self { + db, + inner: Box::new(InMemoryTable::new()), + } + } +} + +#[async_trait] +impl Table for DbTable { + fn state(&self) -> &TableState { + self.inner.state() + } + + async fn bid( + self: Box<Self>, + bid: Bid, + ) -> Result<Box<dyn Table + Send>, BridgeError> { + Ok(Box::new(Self { inner: self.inner.bid(bid).await?, ..*self })) + } + + async fn play( + self: Box<Self>, + card: Card, + ) -> Result<Box<dyn Table + Send>, BridgeError> { + Ok(Box::new(Self { inner: self.inner.play(card).await?, ..*self })) + } + + async fn new_deal(self: Box<Self>) -> Result<Box<dyn Table + Send>, BridgeError> { + Ok(Box::new(Self { inner: self.inner.new_deal().await?, ..*self })) + } +} diff --git a/server/tests/table_test.rs b/server/tests/table_test.rs index bece8ae..8b691b9 100644 --- a/server/tests/table_test.rs +++ b/server/tests/table_test.rs @@ -1,5 +1,6 @@ use protocol::bridge_engine::TableState; -use server::table::{Table, InMemoryTable}; +use rand::{thread_rng, Rng}; +use server::table::{Table, InMemoryTable, DbTable}; mod common; @@ -24,3 +25,22 @@ async fn in_memory_table() -> Result<(), anyhow::Error> { table_basic_test(Box::new(InMemoryTable::new())).await?; Ok(()) } + +#[tokio::test] +#[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?; + Ok(()) +} + +#[tokio::test] +#[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())); + for i in 0..(thread_rng().gen_range(0..200)) { + todo!() + } + Ok(()) +} |