Debian Codegraph @

src/tests/mssql/create_table.rs

raw
1 //! Some unit tests that create create tables
2 #![allow(unused_imports)]
3
4 use crate::backend::{MsSql, SqlGenerator};
5 use crate::{types, Migration, Table};
6
7 #[test]
8 fn create_table_if_not_exists_doesnt_hit_unreachable() {
9 let mut m = Migration::new();
10 m.create_table_if_not_exists("artist", |t| {
11 t.add_column("id", types::primary());
12 t.add_column("name", types::text().nullable(true));
13 t.add_column("description", types::text().nullable(true));
14 t.add_column("pic", types::text().nullable(true));
15 t.add_column("mbid", types::text().nullable(true));
16 });
17 assert_eq!(m.make::<MsSql>(), String::from("IF NOT EXISTS (SELECT * FROM sys.tables WHERE name='artist') CREATE TABLE [artist] ([id] INT IDENTITY(1,1) PRIMARY KEY NOT NULL, [name] TEXT, [description] TEXT, [pic] TEXT, [mbid] TEXT);"));
18 }
19
20 #[test]
21 fn basic_fields() {
22 let mut m = Migration::new();
23 m.create_table("users", |t: &mut Table| {
24 t.add_column("id", types::primary());
25 t.add_column("name", types::varchar(255));
26 t.add_column("age", types::integer());
27 t.add_column("plushy_sharks_owned", types::boolean());
28 });
29
30 assert_eq!(
31 m.make::<MsSql>(),
32 String::from("CREATE TABLE [users] ([id] INT IDENTITY(1,1) PRIMARY KEY NOT NULL, [name] VARCHAR(255) NOT NULL, [age] INT NOT NULL, [plushy_sharks_owned] BIT NOT NULL);")
33 );
34 }
35
36 #[test]
37 fn basic_fields_nullable() {
38 let mut m = Migration::new();
39 m.create_table("users", |t: &mut Table| {
40 t.add_column("id", types::primary());
41 t.add_column("name", types::varchar(255).nullable(true));
42 t.add_column("age", types::integer().nullable(true));
43 t.add_column("plushy_sharks_owned", types::boolean().nullable(true));
44 });
45
46 assert_eq!(
47 m.make::<MsSql>(),
48 String::from("CREATE TABLE [users] ([id] INT IDENTITY(1,1) PRIMARY KEY NOT NULL, [name] VARCHAR(255), [age] INT, [plushy_sharks_owned] BIT);")
49 );
50 }
51 #[test]
52 fn create_multiple_tables() {
53 let mut m = Migration::new();
54 m.create_table("artist", |t| {
55 t.add_column("id", types::primary());
56 t.add_column("name", types::text());
57 t.add_column("description", types::text());
58 t.add_column("pic", types::text());
59 t.add_column("mbid", types::text());
60 });
61 m.create_table("album", |t| {
62 t.add_column("id", types::primary());
63 t.add_column("name", types::text());
64 t.add_column("pic", types::text());
65 t.add_column("mbid", types::text());
66 });
67 assert_eq!(m.make::<MsSql>(), String::from("CREATE TABLE [artist] ([id] INT IDENTITY(1,1) PRIMARY KEY NOT NULL, [name] TEXT NOT NULL, [description] TEXT NOT NULL, [pic] TEXT NOT NULL, [mbid] TEXT NOT NULL);CREATE TABLE [album] ([id] INT IDENTITY(1,1) PRIMARY KEY NOT NULL, [name] TEXT NOT NULL, [pic] TEXT NOT NULL, [mbid] TEXT NOT NULL);"));
68 }
69
70 #[test]
71 fn drop_table() {
72 let mut m = Migration::new();
73 m.drop_table("users");
74
75 assert_eq!(m.make::<MsSql>(), String::from("DROP TABLE [users];"));
76 }
77
78 #[test]
79 fn drop_table_if_exists() {
80 let mut m = Migration::new();
81 m.drop_table_if_exists("users");
82
83 assert_eq!(
84 m.make::<MsSql>(),
85 String::from("DROP TABLE IF EXISTS [users];")
86 );
87 }
88
89 #[test]
90 fn rename_table() {
91 let mut m = Migration::new();
92 m.rename_table("users", "cool_users");
93 assert_eq!(
94 m.make::<MsSql>(),
95 String::from("EXEC sp_rename 'users', 'cool_users';")
96 );
97 }