From 6b2d9d42216fcd0209ebcaf01cb5b5711b28b2d5 Mon Sep 17 00:00:00 2001 From: Baptiste Gelez Date: Thu, 1 Nov 2018 22:23:38 +0100 Subject: [PATCH] Tests infrastructure for plume-models (#302) First step for testing `plume-models`. I only added one test for the moment, but we should add more in future PRs. --- .travis.yml | 51 +++++++++++++++++++++++++++++++++------ Cargo.lock | 31 ++++++++++++++++++++++++ plume-common/src/utils.rs | 4 +-- plume-models/Cargo.toml | 3 +++ plume-models/tests/lib.rs | 39 ++++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+), 9 deletions(-) create mode 100644 plume-models/tests/lib.rs diff --git a/.travis.yml b/.travis.yml index dc57a1e8..569f75e9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,47 @@ language: rust -env: - matrix: - - MIGRATION_DIR=migrations/postgres FEATURES=postgres DATABASE_URL=postgres://postgres@localhost/plume - - MIGRATION_DIR=migrations/sqlite FEATURES=sqlite DATABASE_URL=plume.sqlite3 rust: - - nightly + - nightly-2018-07-17 cache: cargo -script: - - cargo build --no-default-features --features="${FEATURES}" +sudo: true +dist: trusty + +addons: + apt: + packages: + - libcurl4-openssl-dev + - libelf-dev + - libdw-dev + - cmake + - gcc + - binutils-dev + - zlib1g-dev + - libiberty-dev +jobs: + include: + - stage: test and build + env: + - RUSTFLAGS='-C link-dead-code' + script: cargo test --features sqlite --no-default-features --all + after_success: + - | + wget https://github.com/SimonKagstrom/kcov/archive/master.tar.gz && + tar xzf master.tar.gz && + cd kcov-master && + mkdir build && + cd build && + cmake .. && + make && + sudo make install && + cd ../.. && + rm -rf kcov-master && + for crate in plume plume_common plume_models plume_api plm lib; do for file in target/debug/$crate-*[^\.d]; do mkdir -p "target/cov/$(basename $file)"; kcov --exclude-pattern=/.cargo,/usr/lib --verify "target/cov/$(basename $file)" "$file"; done; done && + bash <(curl -s https://codecov.io/bash) && + echo "Uploaded code coverage" + - stage: test and build + env: + - MIGRATION_DIR=migrations/postgres FEATURES=postgres DATABASE_URL=postgres://postgres@localhost/plume + script: cargo build --no-default-features --features="${FEATURES}" + - stage: test and build + env: + - MIGRATION_DIR=migrations/sqlite FEATURES=sqlite DATABASE_URL=plume.sqlite3 + script: cargo build --no-default-features --features="${FEATURES}" diff --git a/Cargo.lock b/Cargo.lock index 7c8334b3..dfe25ebb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -536,6 +536,15 @@ dependencies = [ "syn 0.13.11 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "diesel_migrations" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "migrations_internals 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "migrations_macros 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "digest" version = "0.7.6" @@ -1217,6 +1226,24 @@ name = "memoffset" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "migrations_internals" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "diesel 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "migrations_macros" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "migrations_internals 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "mime" version = "0.2.6" @@ -1703,6 +1730,7 @@ dependencies = [ "canapi 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "diesel 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "diesel_migrations 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "guid-create 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "heck 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3143,6 +3171,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum deunicode 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "850878694b7933ca4c9569d30a34b55031b9b139ee1fc7b94a527c4ef960d690" "checksum diesel 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "164080ac16a4d1d80a50f0a623e4ddef41cb2779eee85bcc76907d340dfc98cc" "checksum diesel_derives 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "03bcaf77491f53e400d5ee3bdd57142ea4e1c47fe9217b3361ff9a76ca0e3d37" +"checksum diesel_migrations 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17b42c35d1ce9e8d57a3e7001b4127f2bc1b073a89708bb7019f5be27c991c28" "checksum digest 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "03b072242a8cbaf9c145665af9d250c59af3b958f83ed6824e13533cf76d5b90" "checksum dotenv 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c0d0a1279c96732bc6800ce6337b6a614697b0e74ae058dc03c62ebeb78b4d86" "checksum dtoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6d301140eb411af13d3115f9a562c85cc6b541ade9dfa314132244aaee7489dd" @@ -3221,6 +3250,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" "checksum memchr 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4b3629fe9fdbff6daa6c33b90f7c08355c1aca05a3d01fa8063b822fcf185f3b" "checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3" +"checksum migrations_internals 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8cf7c8c4f83fa9f47440c0b4af99973502de55e6e7b875f693bd263e03f93e7e" +"checksum migrations_macros 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "79f12499ef7353bdeca2d081bc61edd8351dac09a33af845952009b5a3d68c1a" "checksum mime 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ba626b8a6de5da682e1caa06bdb42a335aee5a84db8e5046a3e8ab17ba0a3ae0" "checksum mime 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "4b082692d3f6cf41b453af73839ce3dfc212c4411cbb2441dff80a716e38bd79" "checksum mime_guess 1.8.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2d4c0961143b8efdcfa29c3ae63281601b446a4a668165454b6c90f8024954c5" diff --git a/plume-common/src/utils.rs b/plume-common/src/utils.rs index 9ac5cec2..899c40bf 100644 --- a/plume-common/src/utils.rs +++ b/plume-common/src/utils.rs @@ -161,7 +161,7 @@ mod tests { ]; for (md, mentions) in tests { - assert_eq!(md_to_html(md).1, mentions.into_iter().map(|s| s.to_string()).collect::>()); + assert_eq!(md_to_html(md).1, mentions.into_iter().map(|s| s.to_string()).collect::>()); } } @@ -180,7 +180,7 @@ mod tests { ]; for (md, mentions) in tests { - assert_eq!(md_to_html(md).2, mentions.into_iter().map(|s| s.to_string()).collect::>()); + assert_eq!(md_to_html(md).2, mentions.into_iter().map(|s| s.to_string()).collect::>()); } } } diff --git a/plume-models/Cargo.toml b/plume-models/Cargo.toml index 095c3d01..66fe0cac 100644 --- a/plume-models/Cargo.toml +++ b/plume-models/Cargo.toml @@ -37,6 +37,9 @@ path = "../plume-common" git = "https://github.com/SergioBenitez/Rocket" rev = "55459db7732b9a240826a5c120c650f87e3372ce" +[dev-dependencies] +diesel_migrations = "1.3.0" + [features] postgres = ["diesel/postgres"] sqlite = ["diesel/sqlite"] diff --git a/plume-models/tests/lib.rs b/plume-models/tests/lib.rs new file mode 100644 index 00000000..57bdd5cc --- /dev/null +++ b/plume-models/tests/lib.rs @@ -0,0 +1,39 @@ +extern crate diesel; +#[macro_use] extern crate diesel_migrations; + +extern crate plume_models; + +use diesel::Connection; +use plume_models::{ + Connection as Conn, + instance::*, + safe_string::SafeString, +}; + +embed_migrations!("../migrations/sqlite"); + +fn db() -> Conn { + let conn = Conn::establish(":memory:").expect("Couldn't connect to the database"); + embedded_migrations::run(&conn).expect("Couldn't run migrations"); + conn +} + +#[test] +fn instance_insert() { + let conn = &db(); + Instance::insert(conn, NewInstance { + default_license: "WTFPL".to_string(), + local: true, + long_description: SafeString::new("This is my instance."), + long_description_html: "

This is my instance

".to_string(), + short_description: SafeString::new("My instance."), + short_description_html: "

My instance

".to_string(), + name: "My instance".to_string(), + open_registrations: true, + public_domain: "plu.me".to_string(), + }); + let inst = Instance::get_local(conn); + assert!(inst.is_some()); + let inst = inst.unwrap(); + assert_eq!(inst.name, "My instance".to_string()); +}