1pub use crate::migrations::*;
8
9#[cfg(test)]
10mod tests {
11 use super::*;
12 use std::fs;
13 use tempfile::TempDir;
14
15 #[tokio::test]
16 async fn test_migration_manager_creation() {
17 let temp_dir = TempDir::new().unwrap();
18 let config = MigrationConfig {
19 migrations_dir: temp_dir.path().to_path_buf(),
20 migrations_table: "test_migrations".to_string(),
21 };
22
23 let manager = MigrationManager::with_config(config);
24 let filename = manager
25 .create_migration("create_users_table")
26 .await
27 .unwrap();
28
29 assert!(filename.contains("create_users_table"));
30 assert!(filename.ends_with(".sql"));
31
32 let migration_path = temp_dir.path().join(&filename);
34 assert!(migration_path.exists());
35
36 let content = fs::read_to_string(migration_path).unwrap();
38 assert!(content.contains("Migration: create_users_table"));
39 assert!(content.contains("-- Up migration"));
40 assert!(content.contains("-- Down migration"));
41 }
42
43 #[tokio::test]
44 async fn test_load_migrations() {
45 let temp_dir = TempDir::new().unwrap();
46 let config = MigrationConfig {
47 migrations_dir: temp_dir.path().to_path_buf(),
48 migrations_table: "test_migrations".to_string(),
49 };
50
51 let manager = MigrationManager::with_config(config);
52
53 let migration1_content = "-- Migration: test1\n-- Up migration\nCREATE TABLE test1;\n-- Down migration\nDROP TABLE test1;";
55 let migration2_content = "-- Migration: test2\n-- Up migration\nCREATE TABLE test2;\n-- Down migration\nDROP TABLE test2;";
56
57 fs::write(
58 temp_dir.path().join("20240101_120000_test1.sql"),
59 migration1_content,
60 )
61 .unwrap();
62 fs::write(
63 temp_dir.path().join("20240101_130000_test2.sql"),
64 migration2_content,
65 )
66 .unwrap();
67
68 let migrations = manager.load_migrations().await.unwrap();
69 assert_eq!(migrations.len(), 2);
70 assert_eq!(migrations[0].name, "test1");
71 assert_eq!(migrations[1].name, "test2");
72 assert!(migrations[0].up_sql.contains("CREATE TABLE test1"));
73 assert!(migrations[0].down_sql.contains("DROP TABLE test1"));
74 }
75
76 #[test]
77 fn test_schema_builder() {
78 let mut builder = SchemaBuilder::new();
79 builder.create_table("users", |table| {
80 table.id("id");
81 table.string("name", Some(255));
82 table.string("email", Some(255));
83 table.timestamps();
84 table.unique(&["email"]);
85 });
86
87 let sql = builder.build();
88 assert!(sql.contains("CREATE TABLE users"));
89 assert!(sql.contains("id SERIAL PRIMARY KEY"));
90 assert!(sql.contains("name VARCHAR(255)"));
91 assert!(sql.contains("email VARCHAR(255)"));
92 assert!(sql.contains("created_at TIMESTAMP"));
93 assert!(sql.contains("UNIQUE (email)"));
94 }
95
96 #[test]
97 fn test_table_builder() {
98 let mut table = TableBuilder::new("posts");
99 table.id("id");
100 table.string("title", Some(255));
101 table.string("content", None);
102 table.integer("user_id");
103 table.timestamps();
104 table.foreign_key("user_id", "users", "id");
105
106 let sql = table.to_sql();
107 assert!(sql.contains("CREATE TABLE posts"));
108 assert!(sql.contains("id SERIAL PRIMARY KEY"));
109 assert!(sql.contains("title VARCHAR(255)"));
110 assert!(sql.contains("content TEXT"));
111 assert!(sql.contains("user_id INTEGER"));
112 assert!(sql.contains("FOREIGN KEY (user_id) REFERENCES users (id)"));
113 }
114
115 #[test]
116 fn test_migration_sql_generation() {
117 let manager = MigrationManager::new();
118
119 let create_sql = manager.create_migrations_table_sql();
121 assert!(create_sql.contains("CREATE TABLE IF NOT EXISTS elif_migrations"));
122 assert!(create_sql.contains("id VARCHAR(255) PRIMARY KEY"));
123
124 let (check_sql, params) = manager.check_migration_sql("20240101_test");
126 assert!(check_sql.contains("SELECT id FROM elif_migrations WHERE id = $1"));
127 assert_eq!(params[0], "20240101_test");
128
129 let (record_sql, params) = manager.record_migration_sql("20240101_test", 1);
131 assert!(record_sql.contains("INSERT INTO elif_migrations"));
132 assert_eq!(params[0], "20240101_test");
133 assert_eq!(params[2], "1");
134 }
135}