Skip to content

Commit

Permalink
refactor: rename migration.rs to fake_db.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonribble committed Oct 6, 2024
1 parent fa5092e commit 6d9666e
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/db/fake_db.rs
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);
}
}

0 comments on commit 6d9666e

Please sign in to comment.