-
Notifications
You must be signed in to change notification settings - Fork 213
/
sync_test_api.rs
276 lines (226 loc) Β· 8.56 KB
/
sync_test_api.rs
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
pub use crate::assertions::{MigrationsAssertions, ResultSetExt, SchemaAssertion};
pub use expect_test::expect;
pub use test_macros::test_connector;
pub use test_setup::{BitFlags, Capabilities, Tags};
use crate::{commands::*, multi_engine_test_api::TestApi as RootTestApi};
use migration_connector::MigrationPersistence;
use quaint::{
prelude::{ConnectionInfo, ResultSet},
Value,
};
use sql_migration_connector::SqlMigrationConnector;
use std::{borrow::Cow, future::Future};
use tempfile::TempDir;
use test_setup::{DatasourceBlock, TestApiArgs};
pub struct TestApi {
root: RootTestApi,
connector: SqlMigrationConnector,
}
impl TestApi {
/// Initializer, called by the test macros.
pub fn new(args: TestApiArgs) -> Self {
let root = RootTestApi::new(args);
let connector = root.new_engine().connector;
TestApi { root, connector }
}
/// Plan an `applyMigrations` command
pub fn apply_migrations<'a>(&'a self, migrations_directory: &'a TempDir) -> ApplyMigrations<'a> {
ApplyMigrations::new_sync(&self.connector, migrations_directory, &self.root.rt)
}
pub fn connection_string(&self) -> &str {
self.root.connection_string()
}
pub fn connection_info(&self) -> ConnectionInfo {
self.connector.connection_info()
}
pub fn schema_name(&self) -> &str {
self.connector.schema_name()
}
/// Plan a `createMigration` command
pub fn create_migration<'a>(
&'a self,
name: &'a str,
schema: &'a str,
migrations_directory: &'a TempDir,
) -> CreateMigration<'a> {
CreateMigration::new_sync(&self.connector, name, schema, migrations_directory, &self.root.rt)
}
/// Create a temporary directory to serve as a test migrations directory.
pub fn create_migrations_directory(&self) -> TempDir {
self.root.create_migrations_directory()
}
/// Builder and assertions to call the `devDiagnostic` command.
pub fn dev_diagnostic<'a>(&'a self, migrations_directory: &'a TempDir) -> DevDiagnostic<'a> {
DevDiagnostic::new(&self.connector, migrations_directory, &self.root.rt)
}
pub fn diagnose_migration_history<'a>(&'a self, migrations_directory: &'a TempDir) -> DiagnoseMigrationHistory<'a> {
DiagnoseMigrationHistory::new_sync(&self.connector, migrations_directory, &self.root.rt)
}
pub fn dump_table(&self, table_name: &str) -> ResultSet {
let select_star =
quaint::ast::Select::from_table(self.render_table_name(table_name)).value(quaint::ast::asterisk());
self.query(select_star.into())
}
pub fn evaluate_data_loss<'a>(&'a self, migrations_directory: &'a TempDir, schema: String) -> EvaluateDataLoss<'a> {
EvaluateDataLoss::new(&self.connector, migrations_directory, schema, &self.root.rt)
}
/// Returns true only when testing on MSSQL.
pub fn is_mssql(&self) -> bool {
self.root.is_mssql()
}
/// Returns true only when testing on MariaDB.
pub fn is_mariadb(&self) -> bool {
self.root.is_mysql_mariadb()
}
/// Returns true only when testing on MySQL.
pub fn is_mysql(&self) -> bool {
self.root.is_mysql()
}
/// Returns true only when testing on MariaDB.
pub fn is_mysql_mariadb(&self) -> bool {
self.root.is_mysql_mariadb()
}
/// Returns true only when testing on MySQL 5.6.
pub fn is_mysql_5_6(&self) -> bool {
self.root.is_mysql_5_6()
}
/// Returns true only when testing on MySQL 8.
pub fn is_mysql_8(&self) -> bool {
self.root.is_mysql_8()
}
/// Returns true only when testing on postgres.
pub fn is_postgres(&self) -> bool {
self.root.is_postgres()
}
/// Returns true only when testing on sqlite.
pub fn is_sqlite(&self) -> bool {
self.root.is_sqlite()
}
/// Returns true only when testing on vitess.
pub fn is_vitess(&self) -> bool {
self.root.is_vitess()
}
/// Insert test values
pub fn insert<'a>(&'a self, table_name: &'a str) -> SingleRowInsert<'a> {
SingleRowInsert {
insert: quaint::ast::Insert::single_into(self.render_table_name(table_name)),
api: self,
}
}
pub fn list_migration_directories<'a>(&'a self, migrations_directory: &'a TempDir) -> ListMigrationDirectories<'a> {
ListMigrationDirectories::new(&self.connector, migrations_directory, &self.root.rt)
}
pub fn lower_cases_table_names(&self) -> bool {
self.root.lower_cases_table_names()
}
pub fn mark_migration_applied<'a>(
&'a self,
migration_name: impl Into<String>,
migrations_directory: &'a TempDir,
) -> MarkMigrationApplied<'a> {
MarkMigrationApplied::new(
&self.connector,
migration_name.into(),
migrations_directory,
&self.root.rt,
)
}
pub fn mark_migration_rolled_back(&self, migration_name: impl Into<String>) -> MarkMigrationRolledBack<'_> {
MarkMigrationRolledBack::new(&self.connector, migration_name.into(), &self.root.rt)
}
pub fn migration_persistence<'a>(&'a self) -> &(dyn MigrationPersistence + 'a) {
&self.connector
}
/// Assert facts about the database schema
#[track_caller]
pub fn assert_schema(&self) -> SchemaAssertion {
SchemaAssertion::new(
self.root.block_on(self.connector.describe_schema()).unwrap(),
self.root.args.tags(),
)
}
/// Block on a future
pub fn block_on<O, F: Future<Output = O>>(&self, f: F) -> O {
self.root.block_on(f)
}
/// Render a valid datasource block, including database URL.
pub fn datasource_block(&self) -> DatasourceBlock<'_> {
self.root.datasource_block()
}
pub fn datasource_block_with<'a>(&'a self, params: &'a [(&'a str, &'a str)]) -> DatasourceBlock<'a> {
self.root.args.datasource_block(self.root.connection_string(), params)
}
pub fn normalize_identifier<'a>(&self, identifier: &'a str) -> Cow<'a, str> {
if self.lower_cases_table_names() {
identifier.to_ascii_lowercase().into()
} else {
identifier.into()
}
}
/// Like quaint::Queryable::query()
#[track_caller]
pub fn query(&self, q: quaint::ast::Query<'_>) -> ResultSet {
self.root.block_on(self.connector.queryable().query(q)).unwrap()
}
/// Like quaint::Queryable::query_raw()
#[track_caller]
pub fn query_raw(&self, q: &str, params: &[Value<'static>]) -> ResultSet {
self.root
.block_on(self.connector.queryable().query_raw(q, params))
.unwrap()
}
/// Send a SQL command to the database, and expect it to succeed.
#[track_caller]
pub fn raw_cmd(&self, sql: &str) {
self.root.block_on(self.connector.queryable().raw_cmd(sql)).unwrap()
}
/// Render a table name with the required prefixing for use with quaint query building.
pub fn render_table_name<'a>(&'a self, table_name: &'a str) -> quaint::ast::Table<'a> {
if self.root.is_sqlite() {
table_name.into()
} else {
(self.connector.schema_name(), table_name).into()
}
}
/// Plan a `reset` command
pub fn reset(&self) -> Reset<'_> {
Reset::new_sync(&self.connector, &self.root.rt)
}
/// Plan a `schemaPush` command
pub fn schema_push(&self, dm: impl Into<String>) -> SchemaPush<'_> {
SchemaPush::new(&self.connector, dm.into(), &self.root.rt)
}
pub fn tags(&self) -> BitFlags<Tags> {
self.root.args.tags()
}
/// Render a valid datasource block, including database URL.
pub fn write_datasource_block(&self, out: &mut dyn std::fmt::Write) {
write!(
out,
"{}",
self.root.args.datasource_block(self.root.args.database_url(), &[])
)
.unwrap()
}
pub fn datamodel_with_provider(&self, schema: &str) -> String {
let mut out = String::with_capacity(320 + schema.len());
self.write_datasource_block(&mut out);
out.push_str(schema);
out
}
}
pub struct SingleRowInsert<'a> {
insert: quaint::ast::SingleRowInsert<'a>,
api: &'a TestApi,
}
impl<'a> SingleRowInsert<'a> {
/// Add a value to the row
pub fn value(mut self, name: &'a str, value: impl Into<quaint::ast::Expression<'a>>) -> Self {
self.insert = self.insert.value(name, value);
self
}
/// Execute the request and return the result set.
pub fn result_raw(self) -> quaint::connector::ResultSet {
self.api.query(self.insert.into())
}
}