Skip to content

Commit

Permalink
Merge pull request #5 from NikolaiSch/adding-tests
Browse files Browse the repository at this point in the history
Fixing rustfmt
  • Loading branch information
NikolaiSch authored Feb 1, 2024
2 parents 8313155 + 358bf1d commit 3d5d2d6
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 208 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ harness = false
[[bench]]
name = "browser"
harness = false

[profile.release]
strip = true
opt-level = "z"
lto = true
4 changes: 1 addition & 3 deletions benches/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ fn criterion_benchmark(c: &mut Criterion) {
.measurement_time(std::time::Duration::from_secs(20));

// 1.54 seconds
group.bench_function("Create new browser basic", |b| {
b.iter(create_connection)
});
group.bench_function("Create new browser basic", |b| b.iter(create_connection));

// 0.943 seconds
group.bench_function("Create new browser headless", |b| {
Expand Down
9 changes: 9 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
blank_lines_upper_bound = 2
combine_control_expr = false
edition = "2021"
force_multiline_blocks = true
format_code_in_doc_comments = true
format_generated_files = false
imports_layout = "HorizontalVertical"
imports_granularity = "Crate"
group_imports = "StdExternalCrate"
47 changes: 20 additions & 27 deletions src/db/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,43 @@
//! Database operation helpers for sqlite, using diesel
use diesel::prelude::*;
use diesel::{prelude::*, RunQueryDsl};

use diesel::RunQueryDsl;

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

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 create_stream(
conn: &mut SqliteConnection,
new_stream: &StreamNew,
) -> Result<usize, anyhow::Error> {
Ok(diesel::insert_or_ignore_into(stream::table)
.values(new_stream)
.execute(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)
.execute(conn)?)
}

pub fn get_streams(conn: &mut SqliteConnection) -> Result<Vec<Stream>, anyhow::Error> {
Ok(stream.load::<Stream>(conn)?)
}

pub fn get_empty_streams(conn: &mut SqliteConnection) -> Result<Vec<Stream>, anyhow::Error> {
Ok(stream
.filter(schema::stream::stream_link.eq(""))
.load::<Stream>(conn)?)
Ok(stream.filter(schema::stream::stream_link.eq(""))
.load::<Stream>(conn)?)
}

pub fn get_linked_streams(conn: &mut SqliteConnection) -> Result<Vec<Stream>, anyhow::Error> {
Ok(stream
.filter(schema::stream::stream_link.ne(""))
.load::<Stream>(conn)?)
Ok(stream.filter(schema::stream::stream_link.ne(""))
.load::<Stream>(conn)?)
}

pub fn get_streams_by_id(
conn: &mut SqliteConnection,
search_id: i32,
) -> Result<Vec<Stream>, anyhow::Error> {
Ok(stream
.filter(schema::stream::id.eq(search_id))
.load::<Stream>(conn)?)
pub fn get_streams_by_id(conn: &mut SqliteConnection,
search_id: i32)
-> Result<Vec<Stream>, anyhow::Error> {
Ok(stream.filter(schema::stream::id.eq(search_id))
.load::<Stream>(conn)?)
}
22 changes: 7 additions & 15 deletions src/db/models.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//! This module contains the models for the diesel ORM
use diesel::prelude::*;
use serde::ser::SerializeStruct;
use serde::Deserialize;
use serde::Serialize;
use serde::{ser::SerializeStruct, Deserialize, Serialize};

#[derive(Debug, Queryable, Deserialize, Clone)]
pub struct Stream {
Expand Down Expand Up @@ -65,10 +63,8 @@ mod tests {
};

let serialised = serde_json::to_string(&stream).unwrap();
assert_eq!(
serialised,
"{\"id\":1,\"home\":\"home\",\"away\":\"away\",\"start_time\":\"start_time\",\"league\":\"league\",\"country\":\"country\",\"url\":\"url\",\"stream_link\":[\"stream_link\"]}"
);
assert_eq!(serialised,
"{\"id\":1,\"home\":\"home\",\"away\":\"away\",\"start_time\":\"start_time\",\"league\":\"league\",\"country\":\"country\",\"url\":\"url\",\"stream_link\":[\"stream_link\"]}");
}

#[test]
Expand All @@ -85,10 +81,8 @@ mod tests {
};

let serialised = serde_json::to_string(&stream).unwrap();
assert_eq!(
serialised,
"{\"id\":1,\"home\":\"home\",\"away\":\"away\",\"start_time\":\"start_time\",\"league\":\"league\",\"country\":\"country\",\"url\":\"url\",\"stream_link\":[\"stream_link\",\"stream_link2\"]}"
);
assert_eq!(serialised,
"{\"id\":1,\"home\":\"home\",\"away\":\"away\",\"start_time\":\"start_time\",\"league\":\"league\",\"country\":\"country\",\"url\":\"url\",\"stream_link\":[\"stream_link\",\"stream_link2\"]}");
}

#[test]
Expand All @@ -105,9 +99,7 @@ mod tests {
};

let serialised = serde_json::to_string(&stream).unwrap();
assert_eq!(
serialised,
"{\"id\":1,\"home\":\"home\",\"away\":\"away\",\"start_time\":\"start_time\",\"league\":\"league\",\"country\":\"country\",\"url\":\"url\",\"stream_link\":[\"\"]}"
);
assert_eq!(serialised,
"{\"id\":1,\"home\":\"home\",\"away\":\"away\",\"start_time\":\"start_time\",\"league\":\"league\",\"country\":\"country\",\"url\":\"url\",\"stream_link\":[\"\"]}");
}
}
116 changes: 55 additions & 61 deletions src/query_selectors.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
//! This module contains the functions to get the data from the dom of the eventlist
//! and return the data as a string
use tl::Parser;
use tl::VDom;

use thiserror::Error;
use tl::{Parser, VDom};

#[derive(Error, Debug)]
pub enum DomParseError {
Expand Down Expand Up @@ -37,24 +35,24 @@ pub enum DomParseError {
/// let parser = dom.parser();
///
/// let link = get_url_from_dom(&dom, &parser).unwrap();
/// assert_eq!(link, "https://sportshub.fan/event/ypiranga_rs_novo_hamburgo_191503337/");
/// assert_eq!(link,
/// "https://sportshub.fan/event/ypiranga_rs_novo_hamburgo_191503337/");
/// ```
pub fn get_url_from_dom(dom: &VDom<'_>, parser: &Parser<'_>) -> Result<String, anyhow::Error> {
let q = dom
.query_selector("a")
.ok_or(DomParseError::NotFound)?
.next()
.ok_or(DomParseError::NotFound)?
.get(parser)
.ok_or(DomParseError::Unknown)?
.as_tag()
.ok_or(DomParseError::NoAttributeFound)?
.attributes()
.get("href")
.ok_or(DomParseError::NoAttributeFound)?
.ok_or(DomParseError::NoAttributeFound)?
.as_utf8_str()
.to_string();
let q = dom.query_selector("a")
.ok_or(DomParseError::NotFound)?
.next()
.ok_or(DomParseError::NotFound)?
.get(parser)
.ok_or(DomParseError::Unknown)?
.as_tag()
.ok_or(DomParseError::NoAttributeFound)?
.attributes()
.get("href")
.ok_or(DomParseError::NoAttributeFound)?
.ok_or(DomParseError::NoAttributeFound)?
.as_utf8_str()
.to_string();

Ok(q)
}
Expand All @@ -81,19 +79,17 @@ pub fn get_url_from_dom(dom: &VDom<'_>, parser: &Parser<'_>) -> Result<String, a
///
/// let event_name = get_game_name_from_dom(&dom, &parser).unwrap();
/// assert_eq!(event_name, "Ypiranga RS - Novo Hamburgo");
pub fn get_game_name_from_dom(
dom: &VDom<'_>,
parser: &Parser<'_>,
) -> Result<String, anyhow::Error> {
let q = dom
.query_selector("span.mr-5")
.ok_or(DomParseError::NotFound)?
.next()
.ok_or(DomParseError::NotFound)?
.get(parser)
.ok_or(DomParseError::Unknown)?
.inner_text(parser)
.to_string();
pub fn get_game_name_from_dom(dom: &VDom<'_>,
parser: &Parser<'_>)
-> Result<String, anyhow::Error> {
let q = dom.query_selector("span.mr-5")
.ok_or(DomParseError::NotFound)?
.next()
.ok_or(DomParseError::NotFound)?
.get(parser)
.ok_or(DomParseError::Unknown)?
.inner_text(parser)
.to_string();

Ok(q)
}
Expand Down Expand Up @@ -121,15 +117,14 @@ pub fn get_game_name_from_dom(
/// let event_info = get_info_from_dom(&dom, &parser).unwrap();
/// assert_eq!(event_info, "Brazilian Campeonato Gaucho");
pub fn get_info_from_dom(dom: &VDom<'_>, parser: &Parser<'_>) -> Result<String, anyhow::Error> {
let q = dom
.query_selector("span.evdesc.event-desc")
.ok_or(DomParseError::NotFound)?
.next()
.ok_or(DomParseError::NotFound)?
.get(parser)
.ok_or(DomParseError::Unknown)?
.inner_text(parser)
.to_string();
let q = dom.query_selector("span.evdesc.event-desc")
.ok_or(DomParseError::NotFound)?
.next()
.ok_or(DomParseError::NotFound)?
.get(parser)
.ok_or(DomParseError::Unknown)?
.inner_text(parser)
.to_string();

Ok(q)
}
Expand All @@ -155,26 +150,25 @@ pub fn get_info_from_dom(dom: &VDom<'_>, parser: &Parser<'_>) -> Result<String,
/// let country = get_country_from_dom(&dom, &parser).unwrap();
/// assert_eq!(country, "brazil");
pub fn get_country_from_dom(dom: &VDom<'_>, parser: &Parser<'_>) -> Result<String, anyhow::Error> {
let q = dom
.query_selector("i.icon-competitions")
.ok_or(DomParseError::NotFound)?
.next()
.ok_or(DomParseError::NotFound)?
.get(parser)
.ok_or(DomParseError::Unknown)?
.as_tag()
.ok_or(DomParseError::NoAttributeFound)?
.attributes()
.get("style")
.ok_or(DomParseError::NoAttributeFound)?
.ok_or(DomParseError::NoAttributeFound)?
.as_utf8_str()
.split('/')
.last()
.ok_or(DomParseError::NoAttributeFound)?
.replace(");", "")
.replace(".svg", "")
.to_string();
let q = dom.query_selector("i.icon-competitions")
.ok_or(DomParseError::NotFound)?
.next()
.ok_or(DomParseError::NotFound)?
.get(parser)
.ok_or(DomParseError::Unknown)?
.as_tag()
.ok_or(DomParseError::NoAttributeFound)?
.attributes()
.get("style")
.ok_or(DomParseError::NoAttributeFound)?
.ok_or(DomParseError::NoAttributeFound)?
.as_utf8_str()
.split('/')
.last()
.ok_or(DomParseError::NoAttributeFound)?
.replace(");", "")
.replace(".svg", "")
.to_string();

Ok(q)
}
Loading

0 comments on commit 3d5d2d6

Please sign in to comment.