on main help sign in
Use TOML for migrations in test framework

Previously we set up migrations directly as structs which doesn't match how users will create them. For the new test framework, we'll instead write TOML for migrations. This brings the tests closer to how an end user will interact with Reshape and also lets us write tests to match the examples in the documentation.

commita7d70f80b843da30c93af3c0ab6ff58fc68b338b authorfabianlindfors <fabian@flapplabs.se> dateSat 15 Jan 2022, 14:47 (5 years ago) parent945e637
3 files changed+72−73
tests/add_column.rs+41−42 tests/common.rs+8−4 tests/create_table.rs+23−27
tests/add_column.rs+41−42
@@ -7,51 +7,50 @@ fn add_column() {
77 let mut test = common::Test::new("Add column");
88
99 test.first_migration(
10− Migration::new("create_user_table", None).with_action(
11− CreateTableBuilder::default()
12− .name("users")
13− .primary_key(vec!["id".to_string()])
14− .columns(vec![
15− ColumnBuilder::default()
16− .name("id")
17− .data_type("INTEGER")
18− .build()
19− .unwrap(),
20− ColumnBuilder::default()
21− .name("name")
22− .data_type("TEXT")
23− .build()
24− .unwrap(),
25− ])
26− .build()
27− .unwrap(),
28− ),
10+ r#"
11+ name = "create_user_table"
12+
13+ [[actions]]
14+ type = "create_table"
15+ name = "users"
16+ primary_key = ["id"]
17+
18+ [[actions.columns]]
19+ name = "id"
20+ type = "INTEGER"
21+
22+ [[actions.columns]]
23+ name = "name"
24+ type = "TEXT"
25+ "#,
2926 );
3027
3128 test.second_migration(
32− Migration::new("add_first_and_last_name_columns", None)
33− .with_action(AddColumn {
34− table: "users".to_string(),
35− column: Column {
36− name: "first".to_string(),
37− data_type: "TEXT".to_string(),
38− nullable: false,
39− default: None,
40− generated: None,
41− },
42− up: Some("(STRING_TO_ARRAY(name, ' '))[1]".to_string()),
43− })
44− .with_action(AddColumn {
45− table: "users".to_string(),
46− column: Column {
47− name: "last".to_string(),
48− data_type: "TEXT".to_string(),
49− nullable: false,
50− default: None,
51− generated: None,
52− },
53− up: Some("(STRING_TO_ARRAY(name, ' '))[2]".to_string()),
54− }),
29+ r#"
30+ name = "add_first_and_last_name_columns"
31+
32+ [[actions]]
33+ type = "add_column"
34+ table = "users"
35+
36+ up = "(STRING_TO_ARRAY(name, ' '))[1]"
37+
38+ [actions.column]
39+ name = "first"
40+ type = "TEXT"
41+ nullable = false
42+
43+ [[actions]]
44+ type = "add_column"
45+ table = "users"
46+
47+ up = "(STRING_TO_ARRAY(name, ' '))[2]"
48+
49+ [actions.column]
50+ name = "last"
51+ type = "TEXT"
52+ nullable = false
53+ "#,
5554 );
5655
5756 test.after_first(|db| {
tests/common.rs+8−4
@@ -41,13 +41,13 @@ impl Test<'_> {
4141 }
4242 }
4343
44− pub fn first_migration(&mut self, migration: Migration) -> &mut Self {
45− self.first_migration = Some(migration);
44+ pub fn first_migration(&mut self, migration: &str) -> &mut Self {
45+ self.first_migration = Some(Self::parse_migration(migration));
4646 self
4747 }
4848
49− pub fn second_migration(&mut self, migration: Migration) -> &mut Self {
50− self.second_migration = Some(migration);
49+ pub fn second_migration(&mut self, migration: &str) -> &mut Self {
50+ self.second_migration = Some(Self::parse_migration(migration));
5151 self
5252 }
5353
@@ -70,6 +70,10 @@ impl Test<'_> {
7070 self.after_abort_fn = Some(f);
7171 self
7272 }
73+
74+ fn parse_migration(encoded: &str) -> Migration {
75+ toml::from_str(encoded).unwrap()
76+ }
7377}
7478
7579enum RunType {
tests/create_table.rs+23−27
@@ -7,33 +7,29 @@ fn create_table() {
77 let mut test = common::Test::new("Create table");
88
99 test.first_migration(
10− Migration::new("create_users_table", None).with_action(
11− CreateTableBuilder::default()
12− .name("users")
13− .primary_key(vec!["id".to_string()])
14− .columns(vec![
15− ColumnBuilder::default()
16− .name("id")
17− .data_type("INTEGER")
18− .generated("ALWAYS AS IDENTITY")
19− .build()
20− .unwrap(),
21− ColumnBuilder::default()
22− .name("name")
23− .data_type("TEXT")
24− .build()
25− .unwrap(),
26− ColumnBuilder::default()
27− .name("created_at")
28− .data_type("TIMESTAMP")
29− .nullable(false)
30− .default_value("NOW()")
31− .build()
32− .unwrap(),
33− ])
34− .build()
35− .unwrap(),
36− ),
10+ r#"
11+ name = "create_users_table"
12+
13+ [[actions]]
14+ type = "create_table"
15+ name = "users"
16+ primary_key = ["id"]
17+
18+ [[actions.columns]]
19+ name = "id"
20+ type = "INTEGER"
21+ generated = "ALWAYS AS IDENTITY"
22+
23+ [[actions.columns]]
24+ name = "name"
25+ type = "TEXT"
26+
27+ [[actions.columns]]
28+ name = "created_at"
29+ type = "TIMESTAMP"
30+ nullable = false
31+ default = "NOW()"
32+ "#,
3733 );
3834
3935 test.after_first(|db| {