From f0a0fdfa83bba1062877664d70ce97e6318c1b42 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Sat, 25 Nov 2017 03:01:21 +0200 Subject: [PATCH] Added a truncate_db helper function. --- .gitlab-ci.yml | 3 ++- hammond-data/src/database.rs | 11 +++++++++ hammond-data/src/errors.rs | 2 ++ hammond-data/src/feed.rs | 46 ++++++++++++++++++++++++++++-------- rustfmt.toml | 2 +- 5 files changed, 52 insertions(+), 12 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index cd5891d..e1fecfe 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -14,8 +14,9 @@ before_script: # kcov # - apt-get install -y libcurl4-openssl-dev libelf-dev libdw-dev cmake gcc binutils-dev libiberty-dev -# variables: +variables: # RUSTFLAGS: "-C link-dead-code" + RUST_BACKTRACE: "FULL" # Currently doesnt work. # # Build with meson diff --git a/hammond-data/src/database.rs b/hammond-data/src/database.rs index 23a1087..b45b0fa 100644 --- a/hammond-data/src/database.rs +++ b/hammond-data/src/database.rs @@ -65,3 +65,14 @@ pub fn run_migration_on(connection: &SqliteConnection) -> Result<()> { embedded_migrations::run_with_output(connection, &mut io::stdout())?; Ok(()) } + +// Reset the database into a clean state. +// Test share a Temp file db. +pub fn truncate_db() -> Result<()> { + let db = connection(); + let con = db.get()?; + con.execute("DELETE FROM episode")?; + con.execute("DELETE FROM podcast")?; + con.execute("DELETE FROM source")?; + Ok(()) +} diff --git a/hammond-data/src/errors.rs b/hammond-data/src/errors.rs index de01e77..372cc55 100644 --- a/hammond-data/src/errors.rs +++ b/hammond-data/src/errors.rs @@ -2,11 +2,13 @@ use diesel::result; use diesel::migrations::RunMigrationsError; use rss; use reqwest; +use r2d2; use std::io; error_chain! { foreign_links { + R2D2TimeoutError(r2d2::GetTimeout); DieselResultError(result::Error); DieselMigrationError(RunMigrationsError); RSSError(rss::Error); diff --git a/hammond-data/src/feed.rs b/hammond-data/src/feed.rs index 15c0aa6..f99aa2d 100644 --- a/hammond-data/src/feed.rs +++ b/hammond-data/src/feed.rs @@ -142,7 +142,7 @@ mod tests { use rss; use models::Source; - use database::connection; + use database::{connection, truncate_db}; use std::fs; use std::io::BufReader; @@ -152,6 +152,7 @@ mod tests { #[test] /// Insert feeds and update/index them. fn test_index_loop() { + truncate_db().unwrap(); let inpt = vec![ "https://request-for-explanation.github.io/podcast/rss.xml", "https://feeds.feedburner.com/InterceptedWithJeremyScahill", @@ -192,15 +193,7 @@ mod tests { ), ]; - { - // Reset the database into a clean state. - // Test share a Temp file db I think. - let db = connection(); - let con = db.get().unwrap(); - con.execute("DELETE FROM episode").unwrap(); - con.execute("DELETE FROM podcast").unwrap(); - con.execute("DELETE FROM source").unwrap(); - } + truncate_db().unwrap(); let mut feeds: Vec<_> = urls.iter() .map(|&(path, url)| { @@ -224,8 +217,41 @@ mod tests { assert_eq!(dbqueries::get_episodes().unwrap().len(), 274); } + #[test] + // Ingore this. Trying to replicate a bug + // Sometimes I get the following: + // ``` + // failures: + // ---- feed::tests::test_partial_index_podcast stdout ---- + // thread 'feed::tests::test_partial_index_podcast' panicked at 'assertion failed: + // `(left == right)` left: `Source { id: 1, uri: "https://feeds.feedburner.com/InterceptedWithJeremyScahill", last_modified: None, http_etag: None }`, + // right: `Source { id: 3, uri: "https://feeds.feedburner.com/InterceptedWithJeremyScahill", last_modified: None, http_etag: None }`', hammond-data/src/feed.rs:233:8 + // note: Run with `RUST_BACKTRACE=1` for a backtrace. + // ``` + fn foo() { + truncate_db().unwrap(); + use models::NewSource; + + let url = "https://feeds.feedburner.com/InterceptedWithJeremyScahill"; + let sources = dbqueries::get_sources().unwrap(); + println!("{:?}", sources); + + let s1 = NewSource::new_with_uri(url).into_source().unwrap(); + let sources = dbqueries::get_sources().unwrap(); + println!("{:?}", sources); + + let s2 = NewSource::new_with_uri(url).into_source().unwrap(); + + let sources = dbqueries::get_sources().unwrap(); + println!("{:?}", sources); + + assert_eq!(s1, s2); + assert_eq!(s1.id(), s2.id()); + } + #[test] fn test_partial_index_podcast() { + truncate_db().unwrap(); let url = "https://feeds.feedburner.com/InterceptedWithJeremyScahill"; let s1 = Source::from_url(url).unwrap(); diff --git a/rustfmt.toml b/rustfmt.toml index 63e38c1..3a40cc5 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -5,7 +5,7 @@ max_width = 100 comment_width = 100 wrap_comments = true error_on_line_overflow = true -error_on_line_overflow_comments = true +error_on_line_overflow_comments = false tab_spaces = 4 newline_style = "Unix" fn_call_style = "Block"