forked from Billuc/cigogne
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_test.gleam
More file actions
254 lines (217 loc) · 5.93 KB
/
database_test.gleam
File metadata and controls
254 lines (217 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import cigogne/config
import cigogne/internal/database
import cigogne/migration
import envoy
import gleam/erlang/process
import gleam/int
import gleam/list
import gleam/option
import gleam/result
import pog
// Modify these constants to match your local database setup
const db_host = "localhost"
const db_port = 5432
const db_user = "billuc"
const db_password = option.Some("mysecretpassword")
const db_database = "cigogne_test"
const schema = "test"
const migration_table = "migs"
fn db_url() {
"postgres://"
<> db_user
<> case db_password {
option.Some(password) -> ":" <> password
option.None -> ""
}
<> "@"
<> db_host
<> ":"
<> db_port |> int.to_string
<> "/"
<> db_database
}
pub fn init_with_envvar_test() {
envoy.set("DATABASE_URL", db_url())
let config =
config.Config(
config.EnvVarConfig,
config.MigrationTableConfig(
option.Some(schema),
option.Some(migration_table),
),
config.MigrationsConfig(
"cigogne",
option.Some("test/migrations"),
[],
option.None,
),
)
let init_res = database.init(config)
envoy.unset("DATABASE_URL")
let assert Ok(init_res) = init_res
assert init_res |> database.migrations_table_exists |> result.is_ok()
assert init_res.migrations_table == migration_table
assert init_res.db_schema == schema
}
pub fn init_with_postgres_envvar_test() {
envoy.set("PGHOST", db_host)
envoy.set("PGUSER", db_user)
envoy.set("PGPASSWORD", db_password |> option.unwrap(""))
envoy.set("PGDATABASE", db_database)
envoy.set("PGPORT", db_port |> int.to_string)
let config =
config.Config(
config.EnvVarConfig,
config.MigrationTableConfig(
option.Some(schema),
option.Some(migration_table),
),
config.MigrationsConfig(
"cigogne",
option.Some("test/migrations"),
[],
option.None,
),
)
let init_res = database.init(config)
envoy.unset("PGHOST")
envoy.unset("PGUSER")
envoy.unset("PGPASSWORD")
envoy.unset("PGDATABASE")
envoy.unset("PGPORT")
let assert Ok(init_res) = init_res
assert init_res |> database.migrations_table_exists |> result.is_ok()
assert init_res.migrations_table == migration_table
assert init_res.db_schema == schema
}
pub fn init_with_url_test() {
let config =
config.Config(
config.UrlDbConfig(db_url()),
config.MigrationTableConfig(
option.Some(schema),
option.Some(migration_table),
),
config.MigrationsConfig(
"cigogne",
option.Some("test/migrations"),
[],
option.None,
),
)
let init_res = database.init(config)
let assert Ok(init_res) = init_res
assert init_res.migrations_table == migration_table
assert init_res.db_schema == schema
}
pub fn init_with_detailed_config_test() {
let config =
config.Config(
config.DetailedDbConfig(
host: option.Some("localhost"),
user: option.Some(db_user),
password: db_password,
port: option.Some(5432),
name: option.Some(db_database),
),
config.MigrationTableConfig(
option.Some(schema),
option.Some(migration_table),
),
config.MigrationsConfig(
"cigogne",
option.Some("test/migrations"),
[],
option.None,
),
)
let init_res = database.init(config)
let assert Ok(init_res) = init_res
assert init_res.migrations_table == migration_table
assert init_res.db_schema == schema
}
pub fn init_with_connection_test() {
let name = process.new_name("cigogne_test")
let assert Ok(conf) = pog.url_config(name, db_url())
let assert Ok(actor) = pog.start(conf)
let config =
config.Config(
config.ConnectionDbConfig(actor.data),
config.MigrationTableConfig(
option.Some(schema),
option.Some(migration_table),
),
config.MigrationsConfig(
"cigogne",
option.Some("test/migrations"),
[],
option.None,
),
)
let init_res = database.init(config)
let assert Ok(init_res) = init_res
assert init_res.migrations_table == migration_table
assert init_res.db_schema == schema
}
pub fn migration_table_exists_after_zero_test() {
let config =
config.Config(
config.UrlDbConfig(db_url()),
config.MigrationTableConfig(
option.Some(schema),
option.Some(migration_table),
),
config.MigrationsConfig(
"cigogne",
option.Some("test/migrations"),
[],
option.None,
),
)
let assert Ok(init_res) = database.init(config)
let assert Ok(_) =
init_res
|> database.apply_cigogne_zero()
let assert Ok(exists) = init_res |> database.migrations_table_exists()
assert exists
}
pub fn apply_get_rollback_migrations_test() {
let mig_1 =
migration.create_zero_migration(
"test1",
["create table test.my_table (id serial primary key, name text);"],
["drop table test.my_table;"],
)
let mig_2 =
migration.create_zero_migration(
"test2",
["create table test.test_table_2 (id serial primary key);"],
["drop table test.test_table_2;"],
)
let config =
config.Config(
config.UrlDbConfig(db_url()),
config.MigrationTableConfig(
option.Some(schema),
option.Some(migration_table),
),
config.MigrationsConfig(
"cigogne",
option.Some("test/migrations"),
[],
option.None,
),
)
let assert Ok(db) = database.init(config)
let assert Ok(_) = database.apply_cigogne_zero(db)
let applied_res =
database.apply_migration(db, mig_1)
|> result.try(fn(_) { database.apply_migration(db, mig_2) })
|> result.try(fn(_) { database.get_applied_migrations(db) })
let assert Ok(applied) = applied_res
let rb_res =
database.rollback_migration(db, mig_2)
|> result.try(fn(_) { database.rollback_migration(db, mig_1) })
let assert Ok(_) = rb_res
assert applied |> list.length() == 3
}