From e9b78d5fcf31ade09dc2491f8eb4817e74773295 Mon Sep 17 00:00:00 2001 From: Nikolai Schimke <94403162+NikolaiSch@users.noreply.github.com> Date: Thu, 1 Feb 2024 16:04:46 +0000 Subject: [PATCH] Refactor test_db.rs: Add create_new_stream function --- tests/test_db.rs | 143 +++++------------------------------------------ 1 file changed, 14 insertions(+), 129 deletions(-) diff --git a/tests/test_db.rs b/tests/test_db.rs index 379a9b7..e262158 100644 --- a/tests/test_db.rs +++ b/tests/test_db.rs @@ -1,5 +1,6 @@ +use anyhow::Error; use chrono::NaiveDateTime; -use diesel::SqliteConnection; +use diesel::{sqlite::Sqlite, SqliteConnection}; use scraper::db::{ self, helpers::{self, create_stream}, @@ -13,12 +14,8 @@ fn test_create_stream() -> Result { common::create_db() } - -#[test] -fn test_get_home_team() -> Result<(), anyhow::Error> { - let mut test_db = test_create_stream()?; - - let new_stream = StreamNew { +fn create_new_stream(conn: &mut SqliteConnection) -> Result<(), Error> { + let stream = StreamNew { home: "Arsenal", away: "Chelsea", sport: "Football", @@ -29,7 +26,16 @@ fn test_get_home_team() -> Result<(), anyhow::Error> { url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ", }; - create_stream(&mut test_db, &new_stream)?; + create_stream(conn, &stream)?; + + Ok(()) +} + +#[test] +fn test_get_home_team() -> Result<(), anyhow::Error> { + let mut test_db = test_create_stream()?; + + create_new_stream(&mut test_db)?; let streams = dbg!(helpers::get_streams_by_home_team(&mut test_db, "Arsenal".to_string())?); @@ -43,124 +49,3 @@ fn test_get_home_team() -> Result<(), anyhow::Error> { Ok(()) } - -// #[test] -// fn test_get_away_team() -> Result<(), anyhow::Error> { -// let mut test_db = test_create_stream()?; - -// let new_stream = StreamNew { -// home: "Arsenal", -// away: "Chelsea", -// sport: "Football", -// start_time: NaiveDateTime::from_timestamp_opt(1612128000, 0).unwrap(), -// stream_link: "https://www.youtube.com/watch?v=dQw4w9WgXcQ,https://www.youtube.com/watch?v=dQw4w9WgXcQ", -// country: "England", -// league: "Premier League", -// url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ", -// }; - -// create_stream(&mut test_db, &new_stream)?; - -// let streams = helpers::get_streams_by_away_team(&mut test_db, "Chelsea".to_string())?; - -// assert_eq!(streams.len(), 1); - - -// assert_eq!(streams[0].away, "Chelsea"); - -// helpers::delete_all_streams(&mut test_db)?; - -// assert_eq!(helpers::get_streams(&mut test_db)?.len(), 0); - -// Ok(()) -// } - -// #[test] -// fn test_get_sport() -> Result<(), anyhow::Error> { -// let mut test_db = test_create_stream()?; - -// let new_stream = StreamNew { -// home: "Arsenal", -// away: "Chelsea", -// sport: "Football", -// start_time: NaiveDateTime::from_timestamp_opt(1612128000, 0).unwrap(), -// stream_link: "https://www.youtube.com/watch?v=dQw4w9WgXcQ,https://www.youtube.com/watch?v=dQw4w9WgXcQ", -// country: "England", -// league: "Premier League", -// url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ", -// }; - -// create_stream(&mut test_db, &new_stream)?; - -// let streams = helpers::get_streams_by_sport(&mut test_db, "Football".to_string())?; - -// assert_eq!(streams.len(), 1); - -// assert_eq!(streams[0].sport, "Football"); - -// helpers::delete_all_streams(&mut test_db)?; - -// assert_eq!(helpers::get_streams(&mut test_db)?.len(), 0); - -// Ok(()) -// } - -// #[test] -// fn test_get_streams() -> Result<(), anyhow::Error> { -// let mut test_db = test_create_stream()?; - -// let new_stream = StreamNew { -// home: "Arsenal", -// away: "Chelsea", -// sport: "Football", -// start_time: NaiveDateTime::from_timestamp_opt(1612128000, 0).unwrap(), -// stream_link: "https://www.youtube.com/watch?v=dQw4w9WgXcQ,https://www.youtube.com/watch?v=dQw4w9WgXcQ", -// country: "England", -// league: "Premier League", -// url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ", -// }; - -// create_stream(&mut test_db, &new_stream)?; - -// let streams = helpers::get_streams(&mut test_db)?; - -// assert_eq!(streams.len(), 1); - -// assert_eq!(streams[0].sport, "Football"); - -// helpers::delete_all_streams(&mut test_db)?; - -// assert_eq!(helpers::get_streams(&mut test_db)?.len(), 0); - -// Ok(()) -// } - -// #[test] -// fn test_get_streams_by_id() -> Result<(), anyhow::Error> { -// let mut test_db = test_create_stream()?; - -// let new_stream = StreamNew { -// home: "Arsenal", -// away: "Chelsea", -// sport: "Football", -// start_time: NaiveDateTime::from_timestamp_opt(1612128000, 0).unwrap(), -// stream_link: "https://www.youtube.com/watch?v=dQw4w9WgXcQ,https://www.youtube.com/watch?v=dQw4w9WgXcQ", -// country: "England", -// league: "Premier League", -// url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ", -// }; - -// create_stream(&mut test_db, &new_stream)?; - -// let streams = helpers::get_streams_by_id(&mut test_db, 1)?; - -// assert_eq!(streams.len(), 1); - -// assert_eq!(streams[0].sport, "Football"); - -// helpers::delete_all_streams(&mut test_db)?; - -// assert_eq!(helpers::get_streams(&mut test_db)?.len(), 0); - -// Ok(()) -// }