-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add compress-output flag and uncompress subcommand (#33)
* Add compress-output flag and uncompress subcommand * added high level test for compress/uncompress Co-authored-by: Daniel Turner <[email protected]>
- Loading branch information
Showing
7 changed files
with
195 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use std::fs::File; | ||
use std::path::PathBuf; | ||
|
||
pub fn uncompress(input_file: PathBuf, output_file: Option<PathBuf>) -> Result<(), std::io::Error> { | ||
let input = File::open(input_file)?; | ||
match output_file { | ||
Some(output) => zstd::stream::copy_decode(input, File::create(output)?), | ||
None => zstd::stream::copy_decode(input, std::io::stdout()), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::path::PathBuf; | ||
|
||
use crate::{anonymiser::anonymise, parsers::strategy_structs::TransformerOverrides}; | ||
|
||
use super::uncompress; | ||
|
||
#[test] | ||
fn compress_gives_correct_output() { | ||
let test_dir_path = PathBuf::from("test_files/compress"); | ||
std::fs::create_dir_all(&test_dir_path).unwrap(); | ||
|
||
anonymise( | ||
"test_files/dump_file.sql".to_string(), | ||
"test_files/compress/results.sql".to_string(), | ||
"test_files/strategy.json".to_string(), | ||
false, | ||
TransformerOverrides::none(), | ||
) | ||
.unwrap(); | ||
|
||
anonymise( | ||
"test_files/dump_file.sql".to_string(), | ||
"test_files/compress/results.sql.zst".to_string(), | ||
"test_files/strategy.json".to_string(), | ||
true, | ||
TransformerOverrides::none(), | ||
) | ||
.unwrap(); | ||
|
||
uncompress( | ||
PathBuf::from("test_files/compress/results.sql.zst"), | ||
Some(test_dir_path.join("uncompressed.sql")), | ||
) | ||
.unwrap(); | ||
|
||
// Can't compare actual content because of randomization, but # of lines | ||
// should be the same | ||
assert_eq!( | ||
std::fs::read_to_string("test_files/compress/results.sql") | ||
.unwrap() | ||
.lines() | ||
.count(), | ||
std::fs::read_to_string("test_files/compress/uncompressed.sql") | ||
.unwrap() | ||
.lines() | ||
.count() | ||
); | ||
|
||
std::fs::remove_dir_all(test_dir_path).unwrap(); | ||
} | ||
} |