100% coverage
This commit is contained in:
parent
12d738389b
commit
37cc9e4fdb
@ -1,10 +1,10 @@
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io;
|
||||
use std::io::{Read, Write};
|
||||
use std::path::Path;
|
||||
use flate2::Compression;
|
||||
use flate2::read::DeflateDecoder;
|
||||
use flate2::write::DeflateEncoder;
|
||||
use tempfile::tempfile;
|
||||
|
||||
pub struct TestCompression {}
|
||||
|
||||
@ -43,81 +43,38 @@ impl TestCompression {
|
||||
.expect(&format!("Unable to open target file [{}] for writing", target_file.display()));
|
||||
|
||||
let compressed_data = TestCompression::compress_string(source);
|
||||
|
||||
compressed_file
|
||||
.write_all(&compressed_data)
|
||||
.expect(&format!("Unable to write compressed data to [{}]", target_file.display()));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use tempfile::NamedTempFile;
|
||||
use super::*;
|
||||
pub fn compress_file<P: AsRef<Path>>(source: P, destination: P) -> io::Result<()> {
|
||||
// Read the source file
|
||||
let mut read_buffer = String::new();
|
||||
let mut read_file = File::open(&source)?;
|
||||
read_file.read_to_string(&mut read_buffer)?;
|
||||
|
||||
#[test]
|
||||
fn smoke() {
|
||||
assert!(true)
|
||||
// Compress the data
|
||||
let compressed_data = TestCompression::compress_string(&read_buffer);
|
||||
|
||||
// Write the compressed data to the destination file
|
||||
let mut write_file = File::create(&destination)?;
|
||||
write_file.write_all(&compressed_data)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compression_round_trip() {
|
||||
let to_compress = "The quick brown fox jumps over the lazy dog.";
|
||||
|
||||
let compressed_text = TestCompression::compress_string(to_compress);
|
||||
let decompressed_text = TestCompression::decompress_to_string(&compressed_text).unwrap();
|
||||
assert_eq!(to_compress, decompressed_text);
|
||||
pub fn compress_string_to_file(text: &str, target: &Path) {
|
||||
let compressed_text = TestCompression::compress_string(text);
|
||||
let mut writer = File::create(target).expect("Unable to create target file.");
|
||||
writer.write_all(&compressed_text).expect("Unable to write compressed data");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_compression_round_trip() {
|
||||
// compress string to file...
|
||||
let string_to_compress = "The quick brown fox jumps over the lazy dog.";
|
||||
let compressed_string = TestCompression::compress_string(string_to_compress);
|
||||
let mut temp_target = NamedTempFile::new().unwrap();
|
||||
let mut temp_reader = temp_target.reopen();
|
||||
|
||||
// ...write the compressed version to a file...
|
||||
temp_target.write_all(&compressed_string).expect("Unable to write compressed file for test");
|
||||
//
|
||||
// ...decompress from file...
|
||||
let mut compressed_read = Vec::new();
|
||||
temp_reader.unwrap().read_to_end(&mut compressed_read).expect("Unable to read compressed data for test");
|
||||
let decompresed = TestCompression::decompress_to_string(&compressed_read).expect("Unable to decompress string for test");
|
||||
//
|
||||
// ...verify its the same.
|
||||
assert_eq!(
|
||||
string_to_compress,
|
||||
decompresed
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_compression_reader() {
|
||||
// Get the 'sample text'
|
||||
let to_compress = "The quick brown fox jumps over the lazy dog.";
|
||||
|
||||
// ...write it to the temp file...
|
||||
let mut temp_file = NamedTempFile::new().expect("Unable to get temp file for test");
|
||||
temp_file.write_all(&*TestCompression::compress_string(to_compress)).expect("Unable to write compressed data to temp file for test");
|
||||
let temp2_path = temp_file.path();
|
||||
let uncompressed_text = TestCompression::load_compressed_file_to_string(temp2_path);
|
||||
|
||||
assert_eq!(uncompressed_text, to_compress);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_compression_writer() {
|
||||
let to_compress = "The quick brown fox jumps over the lazy dog.";
|
||||
|
||||
let mut temp_file = NamedTempFile::new().expect("Unable to get temp file for test.");
|
||||
TestCompression::save_string_as_compressed_data(to_compress, temp_file.path());
|
||||
|
||||
// read back the compressed text
|
||||
let mut read_buffer = Vec::new();
|
||||
temp_file.read_to_end(&mut read_buffer).expect("Unable to read compressed data back.");
|
||||
let decompressed_text = TestCompression::decompress_to_string(&read_buffer).expect("Unable to decompress text");
|
||||
assert_eq!(
|
||||
decompressed_text,
|
||||
to_compress);
|
||||
pub fn decompress_file_to_string(source: &Path) -> String {
|
||||
let mut compressed_file = File::open(source).expect("Unable to open source to compress.");
|
||||
let mut compressed_data = Vec::new();
|
||||
compressed_file.read_to_end(&mut compressed_data).expect("Unable to read compressed data");
|
||||
TestCompression::decompress_to_string(&compressed_data).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
119
tests/test_compression.rs
Normal file
119
tests/test_compression.rs
Normal file
@ -0,0 +1,119 @@
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Write};
|
||||
use tempfile::NamedTempFile;
|
||||
use trevors_utilities::test_compression::TestCompression;
|
||||
|
||||
#[test]
|
||||
fn smoke() {
|
||||
assert!(true)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compression_round_trip() {
|
||||
let to_compress = "The quick brown fox jumps over the lazy dog.";
|
||||
|
||||
;
|
||||
let decompressed_text =
|
||||
TestCompression::decompress_to_string(
|
||||
&TestCompression::compress_string(
|
||||
to_compress
|
||||
)).unwrap();
|
||||
assert_eq!(to_compress, decompressed_text);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_compression_round_trip() {
|
||||
// compress string to file...
|
||||
let string_to_compress = "The quick brown fox jumps over the lazy dog.";
|
||||
let compressed_string = TestCompression::compress_string(string_to_compress);
|
||||
let mut temp_target = NamedTempFile::new().unwrap();
|
||||
let mut temp_reader = temp_target.reopen();
|
||||
|
||||
// ...write the compressed version to a file...
|
||||
temp_target.write_all(&compressed_string).expect("Unable to write compressed file for test");
|
||||
//
|
||||
// ...decompress from file...
|
||||
let mut compressed_read = Vec::new();
|
||||
temp_reader.unwrap().read_to_end(&mut compressed_read).expect("Unable to read compressed data for test");
|
||||
let decompresed = TestCompression::decompress_to_string(&compressed_read).expect("Unable to decompress string for test");
|
||||
//
|
||||
// ...verify its the same.
|
||||
assert_eq!(
|
||||
string_to_compress,
|
||||
decompresed
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_compression_reader() {
|
||||
// Get the 'sample text'
|
||||
let to_compress = "The quick brown fox jumps over the lazy dog.";
|
||||
|
||||
// ...write it to the temp file...
|
||||
let mut temp_file = NamedTempFile::new().expect("Unable to get temp file for test");
|
||||
temp_file.write_all(&*TestCompression::compress_string(to_compress)).expect("Unable to write compressed data to temp file for test");
|
||||
let temp2_path = temp_file.path();
|
||||
let uncompressed_text = TestCompression::load_compressed_file_to_string(temp2_path);
|
||||
|
||||
assert_eq!(uncompressed_text, to_compress);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_compression_writer() {
|
||||
let to_compress = "The quick brown fox jumps over the lazy dog.";
|
||||
|
||||
let mut temp_file = NamedTempFile::new().expect("Unable to get temp file for test.");
|
||||
TestCompression::save_string_as_compressed_data(to_compress, temp_file.path());
|
||||
|
||||
// read back the compressed text
|
||||
let mut read_buffer = Vec::new();
|
||||
temp_file.read_to_end(&mut read_buffer).expect("Unable to read compressed data back.");
|
||||
let decompressed_text = TestCompression::decompress_to_string(&read_buffer).expect("Unable to decompress text");
|
||||
assert_eq!(
|
||||
decompressed_text,
|
||||
to_compress);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compress_file() {
|
||||
// create our test file.
|
||||
let test_text = "The quick brown fox jumps over the lazy dog.";
|
||||
let mut original_file = NamedTempFile::new().unwrap();
|
||||
let compressed_file = NamedTempFile::new().unwrap();
|
||||
original_file.write(test_text.as_bytes()).expect("Unable to write original file");
|
||||
|
||||
// run the method being tested
|
||||
TestCompression::compress_file(original_file.path(), compressed_file.path()).expect("Unable to compress temp file.");
|
||||
|
||||
// verify the data in the new file matches what we expect
|
||||
let compressed_text = TestCompression::compress_string(test_text);
|
||||
let mut reader_file = compressed_file.reopen().unwrap();
|
||||
|
||||
let mut result_text = Vec::new();
|
||||
reader_file.read_to_end(&mut result_text).expect("Unable to read compressed data back to verify");
|
||||
let decompressed_text = TestCompression::decompress_to_string(&result_text).unwrap();
|
||||
|
||||
assert_eq!(decompressed_text, test_text);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compress_string_to_file() {
|
||||
let to_compress = "The quick brown fox jumped over the lazy dog.";
|
||||
let compressed_text = TestCompression::compress_string(to_compress);
|
||||
let target_file = NamedTempFile::new().unwrap();
|
||||
let mut duplicate = target_file.reopen().unwrap();
|
||||
TestCompression::compress_string_to_file(to_compress, target_file.path());
|
||||
|
||||
let mut file_contents = Vec::new();
|
||||
duplicate.read_to_end(&mut file_contents).expect("Unable to read compressed file.");
|
||||
assert_eq!(file_contents, compressed_text);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decompress_file_to_string() {
|
||||
let to_compress = "The quick brown fox jumps over the lazy dog.";
|
||||
let mut temp = NamedTempFile::new().unwrap();
|
||||
TestCompression::compress_string_to_file(to_compress, temp.path());
|
||||
let result = TestCompression::decompress_file_to_string(temp.path());
|
||||
assert_eq!(to_compress, result);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user