Skip to content

Commit 4f7f4ef

Browse files
committed
Test refactoring complete
1 parent 4d58832 commit 4f7f4ef

File tree

2 files changed

+43
-29
lines changed

2 files changed

+43
-29
lines changed

src/docs.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ impl ColumnDoc {
2121
/// - name: `String` - the name of the column
2222
/// - doc: `Option<String>` the comment for the column
2323
#[must_use]
24-
#[allow(clippy::missing_const_for_fn)]
25-
pub fn new(name: String, doc: Option<String>) -> Self {
24+
pub const fn new(name: String, doc: Option<String>) -> Self {
2625
Self { name, doc }
2726
}
2827

@@ -34,7 +33,6 @@ impl ColumnDoc {
3433

3534
/// Getter for the field `doc`
3635
#[must_use]
37-
#[allow(clippy::missing_const_for_fn)]
3836
pub fn doc(&self) -> Option<&str> {
3937
self.doc.as_deref()
4038
}
@@ -96,14 +94,12 @@ impl TableDoc {
9694

9795
/// Getter for the `name` field
9896
#[must_use]
99-
#[allow(clippy::missing_const_for_fn)]
10097
pub fn name(&self) -> &str {
10198
&self.name
10299
}
103100

104101
/// Getter for the `doc` field
105102
#[must_use]
106-
#[allow(clippy::missing_const_for_fn)]
107103
pub fn doc(&self) -> Option<&str> {
108104
self.doc.as_deref()
109105
}
@@ -120,7 +116,6 @@ impl TableDoc {
120116

121117
/// Getter for the `columns` field
122118
#[must_use]
123-
#[allow(clippy::missing_const_for_fn)]
124119
pub fn columns(&self) -> &[ColumnDoc] {
125120
&self.columns
126121
}

src/sql_doc.rs

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,11 @@ fn generate_docs_str(content: &str) -> Result<SqlFileDoc, DocError> {
270270

271271
#[cfg(test)]
272272
mod tests {
273-
use std::{env, fs, path::PathBuf, vec};
273+
use std::{
274+
env, fs,
275+
path::{Path, PathBuf},
276+
vec,
277+
};
274278

275279
use crate::{
276280
SqlDoc,
@@ -289,8 +293,11 @@ mod tests {
289293
let (contents, expected): (Vec<_>, Vec<_>) = sample.into_iter().unzip();
290294
fs::write(&file, contents.join(""))?;
291295
let sql_doc = SqlDoc::from_path(&file).build()?;
292-
let doc = SqlDoc::new(expected.into_iter().flat_map(SqlDoc::into_tables).collect());
293-
assert_eq!(sql_doc, doc);
296+
let mut expected_tables: Vec<TableDoc> =
297+
expected.into_iter().flat_map(SqlDoc::into_tables).collect();
298+
stamp_table_paths(&mut expected_tables, &file);
299+
let expected_doc = SqlDoc::new(expected_tables);
300+
assert_eq!(sql_doc, expected_doc);
294301
let _ = fs::remove_dir_all(&base);
295302
Ok(())
296303
}
@@ -299,16 +306,16 @@ mod tests {
299306
let base = env::temp_dir().join("build_sql_doc_from_dir");
300307
let _ = fs::remove_dir_all(&base);
301308
fs::create_dir_all(&base)?;
302-
let sample = sample_sql();
303-
let (contents, expected): (Vec<_>, Vec<_>) = sample.into_iter().unzip();
304-
for (idx, contents) in contents.iter().enumerate() {
309+
let mut expected: Vec<TableDoc> = Vec::new();
310+
for (idx, (contents, doc)) in sample_sql().into_iter().enumerate() {
305311
let path = base.join(format!("test_file{idx}.sql"));
306312
fs::write(&path, contents)?;
313+
let mut tables = doc.into_tables();
314+
stamp_table_paths(&mut tables, &path);
315+
expected.extend(tables);
307316
}
308317
let sql_doc = SqlDoc::from_dir(&base).build()?;
309318
let mut actual: Vec<TableDoc> = sql_doc.into_tables();
310-
let mut expected: Vec<TableDoc> =
311-
expected.into_iter().flat_map(SqlDoc::into_tables).collect();
312319
assert_eq!(actual.len(), expected.len());
313320
sort_tables(&mut actual);
314321
sort_tables(&mut expected);
@@ -327,17 +334,19 @@ mod tests {
327334
let (contents, expected): (Vec<_>, Vec<_>) = sample.into_iter().unzip();
328335
fs::write(&file, contents.join(""))?;
329336
let sql_doc = SqlDoc::from_path(&file).build()?;
330-
let doc = SqlDoc::new(expected.into_iter().flat_map(SqlDoc::into_tables).collect());
331-
assert_eq!(sql_doc, doc);
337+
let mut expected_tables: Vec<TableDoc> =
338+
expected.into_iter().flat_map(SqlDoc::into_tables).collect();
339+
stamp_table_paths(&mut expected_tables, &file);
340+
let expected_doc = SqlDoc::new(expected_tables);
341+
assert_eq!(sql_doc, expected_doc);
332342
let table = "users";
333-
let actual_table = sql_doc.table(table)?;
334-
let expected_table = doc.table(table)?;
335-
assert_eq!(actual_table, expected_table);
343+
assert_eq!(sql_doc.table(table)?, expected_doc.table(table)?);
336344
let schema = "analytics";
337345
let schema_table = "events";
338-
let actual_schema = sql_doc.table_with_schema(schema, schema_table)?;
339-
let expected_schema = doc.table_with_schema(schema, schema_table)?;
340-
assert_eq!(actual_schema, expected_schema);
346+
assert_eq!(
347+
sql_doc.table_with_schema(schema, schema_table)?,
348+
expected_doc.table_with_schema(schema, schema_table)?
349+
);
341350
let _ = fs::remove_dir_all(&base);
342351
Ok(())
343352
}
@@ -375,6 +384,7 @@ mod tests {
375384
let duplicate_tables_err = duplicate_set.table_with_schema("schema", "duplicate");
376385
assert!(matches!(duplicate_tables_err, Err(DocError::DuplicateTablesFound { .. })));
377386
}
387+
378388
fn sort_tables(tables: &mut [TableDoc]) {
379389
tables.sort_by(|a, b| {
380390
let a_key = (a.schema().unwrap_or(""), a.name());
@@ -383,6 +393,13 @@ mod tests {
383393
});
384394
}
385395

396+
fn stamp_table_paths(tables: &mut [TableDoc], path: &Path) {
397+
let pb = path.to_path_buf();
398+
for t in tables {
399+
t.set_path(Some(pb.clone()));
400+
}
401+
}
402+
386403
fn sample_sql() -> Vec<(&'static str, SqlDoc)> {
387404
vec![
388405
(
@@ -489,6 +506,7 @@ mod tests {
489506
};
490507
assert_eq!(actual_builder, expected_builder);
491508
}
509+
492510
#[test]
493511
fn test_sql_builder_to_sql_doc() -> Result<(), Box<dyn std::error::Error>> {
494512
let base = env::temp_dir().join("sql_builder_to_sql_doc");
@@ -499,13 +517,14 @@ mod tests {
499517
let (contents, expected): (Vec<_>, Vec<_>) = sample.into_iter().unzip();
500518
fs::write(&file, contents.join(""))?;
501519
let sql_doc = SqlDoc::from_path(&file).build()?;
502-
let sql_doc_deny = SqlDoc::from_dir(&base)
503-
.deny(file.to_str().unwrap_or_else(|| panic!("unable to find file val")))
504-
.build()?;
505-
let doc_deny = SqlDoc::new(vec![]);
506-
let doc = SqlDoc::new(expected.into_iter().flat_map(SqlDoc::into_tables).collect());
507-
assert_eq!(sql_doc, doc);
508-
assert_eq!(sql_doc_deny, doc_deny);
520+
let deny_str = file.to_string_lossy().to_string();
521+
let sql_doc_deny = SqlDoc::from_dir(&base).deny(&deny_str).build()?;
522+
let mut expected_tables: Vec<TableDoc> =
523+
expected.into_iter().flat_map(SqlDoc::into_tables).collect();
524+
stamp_table_paths(&mut expected_tables, &file);
525+
let expected_doc = SqlDoc::new(expected_tables);
526+
assert_eq!(sql_doc, expected_doc);
527+
assert_eq!(sql_doc_deny, SqlDoc::new(vec![]));
509528
let _ = fs::remove_dir_all(&base);
510529
Ok(())
511530
}

0 commit comments

Comments
 (0)