Skip to content

Commit

Permalink
Remove test dir (#108)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #108

# What
* Move the test into code file from the test dir

# Why
* Improve the test coverage.

Reviewed By: wenqingren

Differential Revision: D40769823

fbshipit-source-id: c88a275740ed5b0f338bd5e9008e7a66e71be0c2
  • Loading branch information
Jian Cao authored and facebook-github-bot committed Oct 27, 2022
1 parent 27bac36 commit 727f876
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 93 deletions.
76 changes: 76 additions & 0 deletions common/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,80 @@ mod test {

assert!(res.is_ok());
}

#[test]
fn test_read_csv_as_keyed_ints() {
let data = "a,1,10\n
b,2,20\n
c,3,30\n
d,4,40";

use std::io::Write;

use tempfile::NamedTempFile;
// Create a file inside of `std::env::temp_dir()`.
let mut file1 = NamedTempFile::new().unwrap();

// Write some test data to the first handle.
file1.write_all(data.as_bytes()).unwrap();
let p = file1.path().to_str().unwrap();
let r: Vec<KeyedNums<u64>> = read_csv_as_keyed_nums(p, false);
assert_eq!(r.len(), 4);
let s = r.iter().map(|x| x.key.clone()).collect::<Vec<String>>();
assert_eq!(s, vec!["a", "b", "c", "d"]);
assert_eq!(r[0].ints, vec![1, 10]);
assert_eq!(r[r.len() - 1].ints, vec![4, 40]);
}

#[test]
fn test_transpose_keyed_ints() {
let data = "a,1,10 \n
b,2,20 \n
c,3,30 \n
d,4,40";

use std::io::Write;

use tempfile::NamedTempFile;
// Create a file inside of `std::env::temp_dir()`.
let mut file1 = NamedTempFile::new().unwrap();

// Write some test data to the first handle.
file1.write_all(data.as_bytes()).unwrap();
let p = file1.path().to_str().unwrap();
let r: Vec<KeyedNums<u64>> = read_csv_as_keyed_nums(p, false);
assert_eq!(r.len(), 4);
let (keys, rows) = transpose_keyed_nums(r);
assert_eq!(keys.len(), 4);
assert_eq!(keys, vec!["a", "b", "c", "d"]);
assert_eq!(rows.len(), 2);
assert_eq!(rows[0].len(), 4);
assert_eq!(rows[0], [1, 2, 3, 4]);
assert_eq!(rows[1].len(), 4);
assert_eq!(rows[1], [10, 20, 30, 40]);
}

#[test]
fn test_read_csv_as_strings() {
let data = "a,1,10\n
b,2,20\n
c,3,30\n
d,4,40";

use std::io::Write;

use tempfile::NamedTempFile;
// Create a file inside of `std::env::temp_dir()`.
let mut file1 = NamedTempFile::new().unwrap();

// Write some test data to the first handle.
file1.write_all(data.as_bytes()).unwrap();
let p = file1.path().to_str().unwrap();
let r: Vec<Vec<String>> = read_csv_as_strings(p, false);
assert_eq!(r.len(), 4);
assert_eq!(r[0], ["a", "1", "10"]);
assert_eq!(r[1], ["b", "2", "20"]);
assert_eq!(r[2], ["c", "3", "30"]);
assert_eq!(r[3], ["d", "4", "40"]);
}
}
44 changes: 0 additions & 44 deletions common/tests/files_csv_test.rs

This file was deleted.

4 changes: 0 additions & 4 deletions common/tests/keyed_ints.csv

This file was deleted.

48 changes: 48 additions & 0 deletions protocol/src/fileio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,52 @@ mod tests {

assert_eq!(plaintext_keys.read().unwrap().len(), 3);
}

#[test]
fn test_load_data() {
let t = KeyedCSV {
headers: vec![],
records: HashMap::from([
(
String::from("a"),
vec![String::from("1"), String::from("10")],
),
(
String::from("b"),
vec![String::from("2"), String::from("20")],
),
(
String::from("c"),
vec![String::from("3"), String::from("30")],
),
(
String::from("d"),
vec![String::from("4"), String::from("40")],
),
]),
};

let plain_data = Arc::new(RwLock::new(KeyedCSV::default()));

let data = "a,1,10 \n
b,2,20 \n
c,3,30 \n
d,4,40";

use std::io::Write;

use tempfile::NamedTempFile;
// Create a file inside of `std::env::temp_dir()`.
let mut file1 = NamedTempFile::new().unwrap();

// Write some test data to the first handle.
file1.write_all(data.as_bytes()).unwrap();
let p = file1.path().to_str().unwrap();

load_data(plain_data.clone(), p, false);

let r1 = plain_data.read().unwrap();
assert_eq!(*r1.headers, t.headers);
assert_eq!((*r1).records.clone(), t.records);
}
}
41 changes: 0 additions & 41 deletions protocol/tests/fileio.rs

This file was deleted.

4 changes: 0 additions & 4 deletions protocol/tests/keyed_ints.csv

This file was deleted.

0 comments on commit 727f876

Please sign in to comment.