Skip to content

Commit

Permalink
adding tests and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolai Schimke authored and Nikolai Schimke committed Feb 1, 2024
1 parent c39ded4 commit 6023d82
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 143 deletions.
94 changes: 0 additions & 94 deletions benches/browser.rs

This file was deleted.

36 changes: 0 additions & 36 deletions benches/db.rs

This file was deleted.

12 changes: 1 addition & 11 deletions src/bin/sportshub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ use diesel::sqlite::Sqlite;
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
use scraper::{db, scrape_utils, web_server_utils};

pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");

fn run_migrations(connection: &mut impl MigrationHarness<Sqlite>) -> Result<(), Error> {
connection.run_pending_migrations(MIGRATIONS).unwrap();

let mut conn = db::helpers::establish_connection()?;
db::helpers::delete_all_past_streams(&mut conn)?;

Ok(())
}

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
Expand Down Expand Up @@ -86,7 +76,7 @@ enum DataCommands {
#[rocket::main]
async fn main() {
let mut conn = db::helpers::establish_connection().unwrap();
run_migrations(&mut conn).unwrap();


let cli = Cli::parse();

Expand Down
22 changes: 20 additions & 2 deletions src/db/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,39 @@
use std::time::{Duration, Instant};

use diesel::{dsl::*, prelude::*, RunQueryDsl};
use diesel::{dsl::*, prelude::*, sqlite::Sqlite, RunQueryDsl};
use diesel_migrations::{embed_migrations, MigrationHarness};
use serde::{Deserialize, Serialize};

use super::{
models::{Stream, StreamNew},
schema,
schema::{stream, stream::dsl::*},
};

pub const MIGRATIONS: EmbeddedMigrations = diesel_migrations::embed_migrations!("migrations");

pub fn run_migrations(connection: &mut impl MigrationHarness<Sqlite>) -> Result<(), Error> {
connection.run_pending_migrations(MIGRATIONS).unwrap();

let mut conn = db::helpers::establish_connection()?;
db::helpers::delete_all_past_streams(&mut conn)?;

Ok(())
}

pub fn establish_connection() -> Result<SqliteConnection, anyhow::Error> {
let database_url = format!("{}/sports.db", std::env::temp_dir().display());

Ok(SqliteConnection::establish(&database_url)?)
}

pub fn establish_test_connection() -> Result<SqliteConnection, anyhow::Error> {
let mut conn = SqliteConnection::establish("sports.db")?;
run_migrations(&mut conn)?;

Ok(conn)
}

pub fn create_stream(conn: &mut SqliteConnection, new_stream: &StreamNew) -> Result<usize, anyhow::Error> {
Ok(diesel::insert_or_ignore_into(stream::table)
.values(new_stream)
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ pub mod query_selectors;
pub mod scrape_utils;
pub mod web_server_utils;

#[macro_use]
extern crate diesel;
#[macro_use]
extern crate rocket;
7 changes: 7 additions & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use scraper::db::helpers::establish_test_connection;


pub mod create_db() -> Result<(), Error> {
let test_db = establish_test_connection();

}
1 change: 1 addition & 0 deletions tests/test_db.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod common;

0 comments on commit 6023d82

Please sign in to comment.