-
Notifications
You must be signed in to change notification settings - Fork 216
/
test_api.rs
225 lines (182 loc) Β· 6.08 KB
/
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
use super::super::test_api::QueryEngine;
use crate::context::PrismaContext;
use quaint::{prelude::Queryable, single::Quaint};
pub type TestResult = anyhow::Result<()>;
pub struct TestApi {
provider: &'static str,
database_string: String,
connection: Quaint,
is_pgbouncer: bool,
}
impl TestApi {
pub fn connection(&self) -> &Quaint {
&self.connection
}
fn datasource(&self) -> String {
format!(
r#"
datasource my_db {{
provider = "{provider}"
url = "{url}"
}}
"#,
provider = self.provider,
url = self.database_string,
)
}
pub async fn execute_sql(&self, sql: &str) -> anyhow::Result<()> {
self.connection.execute_raw(sql, &[]).await?;
Ok(())
}
pub async fn introspect_and_start_query_engine(
&self,
) -> anyhow::Result<(DatamodelAssertions, QueryEngine)> {
let datasource = self.datasource();
let schema = introspection_core::RpcImpl::introspect_internal(datasource)
.await
.map_err(|err| anyhow::anyhow!("{:?}", err.data))?;
let context = PrismaContext::builder()
.enable_raw_queries(true)
.datamodel(schema.clone())
.force_transactions(self.is_pgbouncer)
.build()
.await
.unwrap();
eprintln!("{}", schema);
let schema = datamodel::parse_datamodel(&schema).unwrap();
Ok((DatamodelAssertions(schema), QueryEngine::new(context)))
}
}
pub struct DatamodelAssertions(datamodel::Datamodel);
impl DatamodelAssertions {
pub fn assert_model<F>(self, name: &str, assert_fn: F) -> anyhow::Result<Self>
where
F: for<'a> FnOnce(ModelAssertions<'a>) -> anyhow::Result<ModelAssertions<'a>>,
{
let model = self
.0
.find_model(name)
.ok_or_else(|| anyhow::anyhow!("Assertion error: could not find model {}", name))?;
assert_fn(ModelAssertions(model))?;
Ok(self)
}
}
pub struct ModelAssertions<'a>(&'a datamodel::dml::Model);
impl<'a> ModelAssertions<'a> {
pub fn assert_field_type(
self,
name: &str,
r#type: datamodel::dml::ScalarType,
) -> anyhow::Result<Self> {
let field = self
.0
.find_field(name)
.ok_or_else(|| anyhow::anyhow!("Assertion error: could not find field {}", name))?;
anyhow::ensure!(
field.field_type == datamodel::dml::FieldType::Base(r#type, None),
"Assertion error: expected the field {} to have type {:?}, but found {:?}",
field.name,
r#type,
&field.field_type,
);
Ok(self)
}
pub fn assert_field_enum_type(self, name: &str, enum_name: &str) -> anyhow::Result<Self> {
let field = self
.0
.find_field(name)
.ok_or_else(|| anyhow::anyhow!("Assertion error: could not find field {}", name))?;
anyhow::ensure!(
field.field_type == datamodel::dml::FieldType::Enum(enum_name.into()),
"Assertion error: expected the field {} to have enum type {:?}, but found {:?}",
field.name,
enum_name,
&field.field_type,
);
Ok(self)
}
}
pub async fn mysql_8_test_api(db_name: &str) -> TestApi {
let mysql_url = test_setup::mysql_8_url(db_name);
test_setup::create_mysql_database(&mysql_url.parse().unwrap())
.await
.unwrap();
TestApi {
connection: Quaint::new(&mysql_url).await.unwrap(),
database_string: mysql_url,
provider: "mysql",
is_pgbouncer: false,
}
}
pub async fn mysql_test_api(db_name: &str) -> TestApi {
let mysql_url = test_setup::mysql_url(db_name);
test_setup::create_mysql_database(&mysql_url.parse().unwrap())
.await
.unwrap();
TestApi {
connection: Quaint::new(&mysql_url).await.unwrap(),
database_string: mysql_url,
provider: "mysql",
is_pgbouncer: false,
}
}
pub async fn mysql_mariadb_test_api(db_name: &str) -> TestApi {
let mysql_url = test_setup::mariadb_url(db_name);
test_setup::create_mysql_database(&mysql_url.parse().unwrap())
.await
.unwrap();
TestApi {
connection: Quaint::new(&mysql_url).await.unwrap(),
database_string: mysql_url,
provider: "mysql",
is_pgbouncer: false,
}
}
pub async fn postgres_test_api(db_name: &str) -> TestApi {
let postgres_url = test_setup::postgres_10_url(db_name);
test_setup::create_postgres_database(&postgres_url.parse().unwrap())
.await
.unwrap();
TestApi {
connection: Quaint::new(&postgres_url).await.unwrap(),
database_string: postgres_url,
provider: "postgres",
is_pgbouncer: false,
}
}
pub async fn postgres9_test_api(db_name: &str) -> TestApi {
let postgres_url = test_setup::postgres_9_url(db_name);
test_setup::create_postgres_database(&postgres_url.parse().unwrap())
.await
.unwrap();
TestApi {
connection: Quaint::new(&postgres_url).await.unwrap(),
database_string: postgres_url,
provider: "postgres",
is_pgbouncer: false,
}
}
pub async fn postgres11_test_api(db_name: &str) -> TestApi {
let postgres_url = test_setup::postgres_11_url(db_name);
test_setup::create_postgres_database(&postgres_url.parse().unwrap())
.await
.unwrap();
TestApi {
connection: Quaint::new(&postgres_url).await.unwrap(),
database_string: postgres_url,
provider: "postgres",
is_pgbouncer: false,
}
}
pub async fn postgres12_test_api(db_name: &str) -> TestApi {
let postgres_url = test_setup::postgres_12_url(db_name);
test_setup::create_postgres_database(&postgres_url.parse().unwrap())
.await
.unwrap();
TestApi {
connection: Quaint::new(&postgres_url).await.unwrap(),
database_string: postgres_url,
provider: "postgres",
is_pgbouncer: false,
}
}