Skip to content

Commit 298d0ac

Browse files
committed
updated test coverage
1 parent 6408547 commit 298d0ac

5 files changed

Lines changed: 132 additions & 7 deletions

File tree

fuzz/fuzz_targets/sql_docs.rs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,67 @@ use std::str;
66
use sql_docs::SqlDoc;
77

88
fuzz_target!(|data: String| {
9-
9+
let _ = SqlDoc::from_str(&data).build();
10+
11+
let _ = SqlDoc::from_str(&data)
12+
.deny(&data)
13+
.build();
14+
15+
let _ = SqlDoc::from_str(&data)
16+
.flatten_multiline()
17+
.build();
18+
19+
let _ = SqlDoc::from_str(&data)
20+
.flatten_multiline_with("")
21+
.build();
22+
23+
let _ = SqlDoc::from_str(&data)
24+
.flatten_multiline_with(" ")
25+
.build();
26+
27+
let _ = SqlDoc::from_str(&data)
28+
.flatten_multiline_with("\n")
29+
.build();
30+
31+
let _ = SqlDoc::from_str(&data)
32+
.flatten_multiline_with(" | ")
33+
.build();
34+
35+
let _ = SqlDoc::from_str(&data)
36+
.preserve_multiline()
37+
.build();
38+
39+
let _ = SqlDoc::from_str(&data)
40+
.flatten_multiline()
41+
.preserve_multiline()
42+
.build();
43+
44+
let _ = SqlDoc::from_str(&data)
45+
.preserve_multiline()
46+
.flatten_multiline()
47+
.build();
48+
49+
let _ = SqlDoc::from_str(&data)
50+
.deny("nonexistent.sql")
51+
.flatten_multiline()
52+
.build();
53+
54+
let _ = SqlDoc::from_str(&data)
55+
.deny("nonexistent.sql")
56+
.flatten_multiline_with(" ")
57+
.build();
58+
59+
let _ = SqlDoc::from_str(&data)
60+
.deny(&data)
61+
.flatten_multiline_with("\n")
62+
.build();
63+
64+
let _ = SqlDoc::from_str(&data)
65+
.deny("a.sql")
66+
.deny("b.sql")
67+
.flatten_multiline_with(" | ")
68+
.preserve_multiline()
69+
.flatten_multiline()
70+
.build();
71+
1072
});

src/comments.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ CREATE TABLE posts (
521521
}
522522

523523
fn multiline_comments_sql() -> &'static str {
524-
r#"/* Users table stores user account information
524+
r"/* Users table stores user account information
525525
multiline */
526526
CREATE TABLE users (
527527
/* Primary key
@@ -556,7 +556,7 @@ CREATE TABLE posts (
556556
/* When the post was created
557557
multiline */
558558
published_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
559-
);"#
559+
);"
560560
}
561561

562562
fn no_comments_sql() -> &'static str {

src/docs.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ mod tests {
319319
assert_eq!(sql_doc_val_column.name(), "id");
320320
}
321321

322-
fn single_line_comments_sql() -> &'static str {
322+
fn single_line_comments_sql() -> &'static str {
323323
"-- Users table stores user account information
324324
CREATE TABLE users (
325325
-- Primary key
@@ -348,7 +348,7 @@ CREATE TABLE posts (
348348
}
349349

350350
fn multiline_comments_sql() -> &'static str {
351-
r#"/* Users table stores user account information
351+
r"/* Users table stores user account information
352352
multiline */
353353
CREATE TABLE users (
354354
/* Primary key
@@ -383,7 +383,7 @@ CREATE TABLE posts (
383383
/* When the post was created
384384
multiline */
385385
published_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
386-
);"#
386+
);"
387387
}
388388

389389
fn no_comments_sql() -> &'static str {
@@ -435,7 +435,6 @@ CREATE TABLE posts (
435435
"
436436
}
437437

438-
439438
#[test]
440439
fn generate_docs_files() -> Result<(), Box<dyn std::error::Error>> {
441440
use crate::{ast::ParsedSqlFileSet, comments::Comments, files::SqlFileSet};
@@ -827,4 +826,27 @@ CREATE TABLE posts (
827826
assert_eq!(cols[1].name(), "username");
828827
assert_eq!(cols[1].doc(), Some("login name"));
829828
}
829+
#[test]
830+
fn test_from_sql_file_doc_into_vec_table_doc_preserves_contents_and_order() {
831+
let t1 = TableDoc::new(
832+
None,
833+
"users".to_string(),
834+
Some("users doc".to_string()),
835+
vec![ColumnDoc::new("id".to_string(), Some("pk".to_string()))],
836+
None,
837+
);
838+
let t2 = TableDoc::new(
839+
Some("analytics".to_string()),
840+
"events".to_string(),
841+
None,
842+
vec![ColumnDoc::new("payload".to_string(), None)],
843+
None,
844+
);
845+
846+
let sql_file_doc = SqlFileDoc::new(vec![t1.clone(), t2.clone()]);
847+
let got: Vec<TableDoc> = Vec::from(sql_file_doc);
848+
849+
let expected = vec![t1, t2];
850+
assert_eq!(got, expected);
851+
}
830852
}

src/files.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,4 +352,31 @@ mod tests {
352352
let bad_file_list = SqlFileSet::new(Path::new(invalid_dir), &[]);
353353
assert!(bad_file_list.is_err());
354354
}
355+
#[test]
356+
fn test_from_sql_files_list_into_vec_pathbuf_preserves_contents_and_order()
357+
-> Result<(), Box<dyn std::error::Error>> {
358+
let base = env::temp_dir().join("from_impl_sql_files_list_test");
359+
let _ = fs::remove_dir_all(&base);
360+
fs::create_dir_all(&base)?;
361+
let file1 = base.join("one.sql");
362+
let file2 = base.join("two.sql");
363+
let noise = base.join("ignore.txt");
364+
fs::File::create(&file1)?;
365+
fs::File::create(&file2)?;
366+
fs::File::create(&noise)?;
367+
let sql_file_list = SqlFilesList::new(&base, &[])?;
368+
let expected: Vec<PathBuf> = sql_file_list.sql_files().to_vec();
369+
let got: Vec<PathBuf> = Vec::from(sql_file_list);
370+
assert_eq!(got, expected);
371+
let _ = fs::remove_dir_all(&base);
372+
Ok(())
373+
}
374+
#[test]
375+
fn test_sql_file_new_from_str_has_no_path_and_preserves_content() {
376+
let sql = "SELECT * FROM users;".to_string();
377+
let file = SqlFile::new_from_str(sql.clone());
378+
assert!(file.path().is_none());
379+
assert!(file.path_into_path_buf().is_none());
380+
assert_eq!(file.content(), sql);
381+
}
355382
}

src/sql_doc.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,4 +668,18 @@ mod tests {
668668
let _ = fs::remove_dir_all(&base);
669669
Ok(())
670670
}
671+
#[test]
672+
fn test_sql_doc_from_str_builds_expected_builder() {
673+
let content = "CREATE TABLE t(id INTEGER);";
674+
675+
let actual = SqlDoc::from_str(content);
676+
677+
let expected = SqlDocBuilder {
678+
source: crate::sql_doc::SqlFileDocSource::FromString(content),
679+
deny: vec![],
680+
multiline_flat: MultiFlatten::NoFlat,
681+
};
682+
683+
assert_eq!(actual, expected);
684+
}
671685
}

0 commit comments

Comments
 (0)