-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP test: improve in memory test by reading from migrations folder
Docs used: - https://doc.rust-lang.org/book/ch12-02-reading-a-file.html - https://doc.rust-lang.org/std/fs/fn.read_dir.html
- Loading branch information
1 parent
95c856b
commit fa5092e
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
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,43 @@ | ||
use anyhow::{Ok, Result}; | ||
use std; | ||
|
||
fn get_first_sql_file_path() -> anyhow::Result<String> { | ||
let mut entries = std::fs::read_dir("migrations")? | ||
.map(|res| res.map(|entry| entry.path())) | ||
.collect::<Result<Vec<_>, std::io::Error>>()?; | ||
|
||
entries.sort(); | ||
|
||
let first_entry = String::from(entries[0].to_string_lossy()); | ||
|
||
Ok(first_entry) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::fs; | ||
|
||
#[test] | ||
fn read_first_sql_file_in_migrations() { | ||
let expect_migration = r#"-- Add migration script here | ||
CREATE TABLE IF NOT EXISTS contacts | ||
( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
first_name TEXT NOT NULL, | ||
last_name TEXT NOT NULL, | ||
display_name TEXT NOT NULL, | ||
email TEXT NOT NULL, | ||
phone_number TEXT NOT NULL | ||
);"#; | ||
|
||
let file_path = get_first_sql_file_path().unwrap(); | ||
|
||
println!("{:?}", file_path); | ||
|
||
let contents = | ||
fs::read_to_string(file_path).expect("Should have been able to read the file"); | ||
|
||
assert_eq!(contents, expect_migration); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
mod connection; | ||
mod contact_repo; | ||
mod metadata_repo; | ||
mod migrations; | ||
|
||
pub use connection::Connection; | ||
|
||
|