Skip to content

Commit

Permalink
Added e2e test for csv feature
Browse files Browse the repository at this point in the history
  • Loading branch information
worryg0d committed Nov 14, 2024
1 parent 2f0088e commit 677fc23
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
6 changes: 4 additions & 2 deletions atsc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use clap::{arg, command, Parser};
use log::{debug, error};
use std::error::Error;
use std::path::PathBuf;
use atsc::csv::{read_samples, read_samples_with_headers};
use wavbrro::wavbrro::WavBrro;

/// Processes the given input based on the provided arguments.
Expand Down Expand Up @@ -87,7 +88,8 @@ fn process_single_file(mut file_path: PathBuf, arguments: &Args) -> Result<(), B
read_samples(&file_path)?
} else {
debug!("Reading samples from csv with headers");
let headers: Vec<&str> = arguments.fields.split(",").collect();
let fields = arguments.fields.clone().unwrap();
let headers: Vec<&str> = fields.split(",").collect();
// Assuming that header[0] is a time field and header[1] is value field
read_samples_with_headers(&file_path, headers[0], headers[1])?
};
Expand Down Expand Up @@ -208,7 +210,7 @@ struct Args {
/// It assumes that the one before comma is a name of time field and the one
/// after comma is value field.
#[arg(long)]
fields: String,
fields: Option<String>,
}

#[derive(clap::ValueEnum, Default, Clone, Debug)]
Expand Down
72 changes: 72 additions & 0 deletions atsc/tests/e2e.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use atsc::utils::error::calculate_error;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use wavbrro::wavbrro::WavBrro;

Expand Down Expand Up @@ -47,6 +49,76 @@ fn test_compressor_auto_lossy() {
test_lossy_compression("auto")
}

#[test]
fn test_csv_input_compression_with_header() {
let test_dir = tempfile::tempdir().unwrap().into_path();
let filepath = test_dir.join("test-csv.csv");

let mut file = File::create(&filepath).unwrap();
let content = "time,value\n1625097600,123.45\n1625184000,678.90\n";
file.write_all(content.as_bytes()).unwrap();

run_compressor(&[
"--compressor",
"fft",
"--error",
"5",
"--csv",
"--fields=time,value",
filepath.to_str().unwrap(),
]);

run_compressor(&["-u", test_dir.join("test-csv.bro").to_str().unwrap()]);

let uncompressed_samples = WavBrro::from_file(&test_dir.join("test-csv.wbro")).unwrap();
let original_samples = vec![123.45f64, 678.9f64];

let err = calculate_error(&original_samples, &uncompressed_samples);

assert!(
err <= 0.05,
"Error: {}\nOriginal : {:?}\nUncompressed: {:?}",
err,
original_samples,
uncompressed_samples
);
}

#[test]
fn test_csv_input_compression_without_header() {
let test_dir = tempfile::tempdir().unwrap().into_path();
let filepath = test_dir.join("test-csv.csv");

let mut file = File::create(&filepath).unwrap();
let content = "123.45\n678.90\n";
file.write_all(content.as_bytes()).unwrap();

run_compressor(&[
"--compressor",
"fft",
"--error",
"5",
"--csv",
"--no-header",
filepath.to_str().unwrap(),
]);

run_compressor(&["-u", test_dir.join("test-csv.bro").to_str().unwrap()]);

let uncompressed_samples = WavBrro::from_file(&test_dir.join("test-csv.wbro")).unwrap();
let original_samples = vec![123.45f64, 678.9f64];

let err = calculate_error(&original_samples, &uncompressed_samples);

assert!(
err <= 0.05,
"Error: {}\nOriginal : {:?}\nUncompressed: {:?}",
err,
original_samples,
uncompressed_samples
);
}

fn test_lossless_compression(compressor: &str) {
test_compression_decompression_flow(compressor, 0, compare_samples_lossless)
}
Expand Down

0 comments on commit 677fc23

Please sign in to comment.