pub struct DataSampleParser {
pub issues: bool,
/* private fields */
}
Expand description
Represents the Parser for sample data to be used
Fields§
§issues: bool
indicates if there were issues parsing and anlyzing the data sample
Implementations§
Source§impl DataSampleParser
impl DataSampleParser
Sourcepub fn new() -> DataSampleParser
pub fn new() -> DataSampleParser
Constructs a new DataSampleParser
#Example
extern crate test_data_generation;
use test_data_generation::data_sample_parser::DataSampleParser;
fn main() {
// initalize a new DataSampelParser
let dsp = DataSampleParser::new();
}
Sourcepub fn new_with(path: &String) -> DataSampleParser
pub fn new_with(path: &String) -> DataSampleParser
Constructs a new DataSampleParser
§Arguments
- `path: &String - The full path name (including the file name and extension) to the configuration file.
#Example
extern crate test_data_generation;
use test_data_generation::data_sample_parser::DataSampleParser;
fn main() {
// initalize a new DataSampelParser
// param: the path to the configuration file
let dsp = DataSampleParser::new_with(&String::from("./config/tdg.yaml"));
}
Sourcepub fn from_file(path: &String) -> DataSampleParser
pub fn from_file(path: &String) -> DataSampleParser
Constructs a new DataSampleParser from an exported JSON file. This is used when restoring from “archive”
§Arguments
path: &String
- The full path name of the json formatted Data Sample Parser archive file.
#Example
extern crate test_data_generation;
use test_data_generation::data_sample_parser::DataSampleParser;
fn main() {
let mut dsp = DataSampleParser::from_file(&String::from("./tests/samples/sample-00-dsp"));
assert_eq!(dsp.generate_record()[0], "OK".to_string());
}
Sourcepub fn analyze_csv_data(
&mut self,
data: &String,
delimiter: Option<u8>,
) -> Result<i32, String>
pub fn analyze_csv_data( &mut self, data: &String, delimiter: Option<u8>, ) -> Result<i32, String>
This function analyzes sample data that is a csv formatted string and returns a boolean if successful. NOTE: The csv properties are as follows: + headers are included as first line + double quote wrap text + double quote escapes is enabled + delimiter is a comma
§Arguments
data: &String
- The textual content of a csv formatted sample data file.delimiter: Option<u8>
- The delimiter to use, otherwise use the default.
§Example
extern crate test_data_generation;
use test_data_generation::data_sample_parser::DataSampleParser;
fn main() {
// initalize a new DataSampelParser
let mut dsp = DataSampleParser::new();
let mut data = String::from("");
data.push_str("\"firstname\",\"lastname\"\n");
data.push_str("\"Aaron\",\"Aaberg\"\n");
data.push_str("\"Aaron\",\"Aaby\"\n");
data.push_str("\"Abbey\",\"Aadland\"\n");
data.push_str("\"Abbie\",\"Aagaard\"\n");
data.push_str("\"Abby\",\"Aakre\"");
// Use the default delimiter (comma)
assert_eq!(dsp.analyze_csv_data(&data, None).unwrap(),1);
}
Sourcepub fn analyze_csv_file(
&mut self,
path: &String,
delimiter: Option<u8>,
) -> Result<i32, String>
pub fn analyze_csv_file( &mut self, path: &String, delimiter: Option<u8>, ) -> Result<i32, String>
This function analyzes sample data that is a csv formatted file and returns a boolean if successful. NOTE: The csv properties are as follows: + headers are included as first line + double quote wrap text + double quote escapes is enabled + delimiter is a comma
§Arguments
path: &String
- The full path name of the csv formatted sample data file.delimiter: Option<u8>
- The delimiter to use, otherwise use the default.
§Example
extern crate test_data_generation;
use test_data_generation::data_sample_parser::DataSampleParser;
fn main() {
// initalize a new DataSampelParser
let mut dsp = DataSampleParser::new();
// Use the default delimiter (comma)
assert_eq!(dsp.analyze_csv_file(&String::from("./tests/samples/sample-01.csv"), None).unwrap(),1);
}
Sourcepub fn demo_date(&self) -> String
pub fn demo_date(&self) -> String
This function generates date as strings using the a demo
profile
§Example
extern crate test_data_generation;
use test_data_generation::data_sample_parser::DataSampleParser;
fn main() {
// initalize a new DataSampelParser
let dsp = DataSampleParser::new();
// generate some test data using the demo functions
println!("generate date:{}", dsp.demo_date());
}
Sourcepub fn demo_person_name(&self) -> String
pub fn demo_person_name(&self) -> String
This function generates people’s names as strings using the a demo
profile
§Example
extern crate test_data_generation;
use test_data_generation::data_sample_parser::DataSampleParser;
fn main() {
// initalize a new DataSampelParser
let dsp = DataSampleParser::new();
// generate some test data using the demo functions
println!("generate date:{}", dsp.demo_person_name());
}
Sourcepub fn extract_headers(&mut self) -> Vec<String>
pub fn extract_headers(&mut self) -> Vec<String>
This function returns a vector of header names
§Example
extern crate test_data_generation;
use test_data_generation::data_sample_parser::DataSampleParser;
fn main() {
// initalize a new DataSampelParser
let mut dsp = DataSampleParser::new();
dsp.analyze_csv_file(&String::from("./tests/samples/sample-01.csv"), None).unwrap();
let headers = dsp.extract_headers();
assert_eq!(headers.len(), 2);
}
Sourcepub fn generate_by_field_name(&mut self, field: String) -> String
pub fn generate_by_field_name(&mut self, field: String) -> String
This function generates test data for the specified field name.
§Arguments
field: String
- The name of the field (e.g.: firstname) the represents the profile to use when generating the test data.
§Example
extern crate test_data_generation;
use test_data_generation::data_sample_parser::DataSampleParser;
fn main() {
// initalize a new DataSampelParser
let mut dsp = DataSampleParser::new();
dsp.analyze_csv_file(&String::from("./tests/samples/sample-01.csv"), None).unwrap();
println!("Generated data for first name {}",dsp.generate_by_field_name("firstname".to_string()));
}
Sourcepub fn generate_record(&mut self) -> Vec<String>
pub fn generate_record(&mut self) -> Vec<String>
This function Vec of generates test data fields.
§Example
extern crate test_data_generation;
use test_data_generation::data_sample_parser::DataSampleParser;
fn main() {
// initalize a new DataSampelParser
let mut dsp = DataSampleParser::new();
dsp.analyze_csv_file(&String::from("./tests/samples/sample-01.csv"), None).unwrap();
println!("Generated data record: {:?}",dsp.generate_record());
}
Sourcepub fn generate_csv(
&mut self,
row_count: u32,
path: &String,
delimiter: Option<u8>,
) -> Result<(), Box<dyn Error>>
pub fn generate_csv( &mut self, row_count: u32, path: &String, delimiter: Option<u8>, ) -> Result<(), Box<dyn Error>>
This function creates a csv file of generated test data. Prior to calling this funciton, you need to call the analyze_csv_file() function. NOTE: The csv properties are as follows: + headers are included as first line + double quotes wrap text + double quote escapes is enabled + delimiter is a comma
§Arguments
row_count: u32
- The number of rows to generate.path: &String
- The full path name where to save the csv file.delimiter: Option<u8>
- The delimiter to use, otherwise use the default.
§Example
extern crate test_data_generation;
use test_data_generation::data_sample_parser::DataSampleParser;
fn main() {
// initalize a new DataSampelParser
let mut dsp = DataSampleParser::new();
dsp.analyze_csv_file(&String::from("./tests/samples/sample-01.csv"), None).unwrap();
dsp.generate_csv(100, &String::from("./tests/samples/generated-01.csv"), None).unwrap();
}
Sourcepub fn levenshtein_distance(
&mut self,
control: &String,
experiment: &String,
) -> usize
pub fn levenshtein_distance( &mut self, control: &String, experiment: &String, ) -> usize
This function calculates the levenshtein distance between 2 strings. See: https://crates.io/crates/levenshtein
§Arguments
control: &String
- The string to compare against. This would be the real data from the data sample.experiment: &String
- The string to compare. This would be the generated data for which you want to find the distance.
#Example
extern crate test_data_generation;
use test_data_generation::data_sample_parser::DataSampleParser;
fn main() {
// analyze the dataset
let mut dsp = DataSampleParser::new();
assert_eq!(dsp.levenshtein_distance(&"kitten".to_string(), &"sitting".to_string()), 3 as usize);
}
Sourcepub fn realistic_test(&mut self, control: &String, experiment: &String) -> f64
pub fn realistic_test(&mut self, control: &String, experiment: &String) -> f64
This function calculates the percent difference between 2 strings.
§Arguments
control: &String
- The string to compare against. This would be the real data from the data sample.experiment: &String
- The string to compare. This would be the generated data for which you want to find the percent difference.
#Example
extern crate test_data_generation;
use test_data_generation::data_sample_parser::DataSampleParser;
fn main() {
// analyze the dataset
let mut dsp = DataSampleParser::new();
assert_eq!(dsp.realistic_test(&"kitten".to_string(), &"sitting".to_string()), 76.92307692307692 as f64);
}
Sourcepub fn running_with_issues(&self) -> &bool
pub fn running_with_issues(&self) -> &bool
This function returns a boolean that indicates if the data sample parsing had issues
§Example
extern crate test_data_generation;
use test_data_generation::data_sample_parser::DataSampleParser;
fn main() {
// initalize a new DataSampelParser
// param: the path to the configuration file is wrong
let dsp = DataSampleParser::new_with(&String::from("./target/debug/config/tdg.yaml"));
// generate some test data using the demo functions
assert_eq!(dsp.running_with_issues(), &false);
}
Sourcepub fn save(&mut self, path: &String) -> Result<bool, Error>
pub fn save(&mut self, path: &String) -> Result<bool, Error>
This function saves (exports) the DataSampleParser to a JSON file. This is useful when you wish to reuse the algorithm to generate more test data later.
§Arguments
field: &String
- The full path of the export file , excluding the file extension, (e.g.: “./test/data/custom-names”).
#Errors If this function encounters any form of I/O or other error, an error variant will be returned. Otherwise, the function returns Ok(true).
#Example
extern crate test_data_generation;
use test_data_generation::data_sample_parser::DataSampleParser;
fn main() {
// analyze the dataset
let mut dsp = DataSampleParser::new();
dsp.analyze_csv_file(&String::from("./tests/samples/sample-00.csv"), None).unwrap();
assert_eq!(dsp.save(&String::from("./tests/samples/sample-00-dsp")).unwrap(), true);
}