From f3eca856cfaf3348180e202bf3535b146dfa8297 Mon Sep 17 00:00:00 2001 From: rzmk Date: Wed, 29 May 2024 13:51:28 -0400 Subject: [PATCH] `tests`: add test_100 for 100.dathere.com & tests for lesson/exercise 0 --- tests/test_100.rs | 9 ++++++ tests/test_100/exercise_0.rs | 62 ++++++++++++++++++++++++++++++++++++ tests/tests.rs | 1 + 3 files changed, 72 insertions(+) create mode 100644 tests/test_100.rs create mode 100644 tests/test_100/exercise_0.rs diff --git a/tests/test_100.rs b/tests/test_100.rs new file mode 100644 index 000000000..68eb57873 --- /dev/null +++ b/tests/test_100.rs @@ -0,0 +1,9 @@ +// The tests in this file and the test_100 folder are for +// lessons and exercises in the online book "100 exercises with qsv". + +// You may read lessons and try out the exercises at: +// https://100.dathere.com + +// Warning: These tests may contain solutions to the exercises. + +mod exercise_0; diff --git a/tests/test_100/exercise_0.rs b/tests/test_100/exercise_0.rs new file mode 100644 index 000000000..80c59c065 --- /dev/null +++ b/tests/test_100/exercise_0.rs @@ -0,0 +1,62 @@ +// Lesson 0: Exploring qsv help messages and syntax +// https://100.dathere.com/lessons/0/notes.html + +use std::process; + +use crate::workdir::Workdir; + +fn setup(name: &str, command_str: &str, args: Vec<&str>) -> (Workdir, process::Command) { + let wrk = Workdir::new(name); + wrk.create( + "fruits.csv", + vec![ + svec!["fruit", "price"], + svec!["apple", "2.50"], + svec!["banana", "3.00"], + svec!["carrot", "1.50"], + ], + ); + + let mut cmd = wrk.command(command_str); + cmd.args(args); + + (wrk, cmd) +} + +// https://100.dathere.com/lessons/0/notes.html#displaying-headers-of-a-csv +#[test] +fn fruits_headers() { + let name = "fruits_headers"; + let command_str = "headers"; + let args = vec!["fruits.csv"]; + let (wrk, mut cmd) = setup(name, command_str, args); + + let got: String = wrk.stdout(&mut cmd); + let expected = r#"1 fruit +2 price"#; + assert_eq!(got, expected); +} + +// https://100.dathere.com/lessons/0/notes.html#exercise-0-total-rows +#[test] +fn fruits_count_total() { + let name = "fruits_count"; + let command_str = "count"; + let args = vec!["fruits.csv", "--no-headers"]; + let (wrk, mut cmd) = setup(name, command_str, args); + + let got: String = wrk.stdout(&mut cmd); + let expected = "4"; + assert_eq!(got, expected); +} +#[test] +fn fruits_count() { + let name = "fruits_count"; + let command_str = "count"; + let args = vec!["fruits.csv"]; + let (wrk, mut cmd) = setup(name, command_str, args); + + let got: String = wrk.stdout(&mut cmd); + let expected = "3"; + assert_eq!(got, expected); +} diff --git a/tests/tests.rs b/tests/tests.rs index 0eff56901..8487afde9 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -30,6 +30,7 @@ macro_rules! rassert_eq { mod workdir; +mod test_100; #[cfg(feature = "apply")] mod test_apply; #[cfg(feature = "datapusher_plus")]