Skip to content

Commit e9213fe

Browse files
committed
test-store: Use AsyncPgConnection in remaining places
1 parent 68c2569 commit e9213fe

File tree

3 files changed

+58
-43
lines changed

3 files changed

+58
-43
lines changed

store/test-store/src/store.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use graph_graphql::prelude::{
2525
use graph_graphql::test_support::GraphQLMetrics;
2626
use graph_node::config::{Config, Opt};
2727
use graph_node::store_builder::StoreBuilder;
28-
use graph_store_postgres::PgConnection;
28+
use graph_store_postgres::AsyncPgConnection;
2929
use graph_store_postgres::{
3030
layout_for_tests::FAKE_NETWORK_SHARED, BlockStore as DieselBlockStore, ConnectionPool,
3131
DeploymentPlacer, Shard, SubgraphStore as DieselSubgraphStore, SubscriptionManager,
@@ -128,7 +128,7 @@ where
128128
/// Run a test with a connection into the primary database, not a full store
129129
pub async fn run_test_with_conn<F>(test: F)
130130
where
131-
F: AsyncFnOnce(&mut PgConnection),
131+
F: AsyncFnOnce(&mut AsyncPgConnection),
132132
{
133133
// Lock regardless of poisoning. This also forces sequential test execution.
134134
let _lock = match SEQ_LOCK.lock() {
@@ -137,7 +137,7 @@ where
137137
};
138138

139139
let mut conn = PRIMARY_POOL
140-
.get_sync()
140+
.get()
141141
.await
142142
.expect("failed to get connection for primary database");
143143

store/test-store/tests/postgres/relational.rs

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Test mapping of GraphQL schema to a relational schema
2-
use diesel::connection::SimpleConnection as _;
2+
use diesel_async::SimpleAsyncConnection;
33
use graph::components::store::write::{EntityModification, RowGroup};
44
use graph::data::store::scalar;
55
use graph::entity;
@@ -12,7 +12,7 @@ use graph::schema::{EntityKey, EntityType, InputSchema};
1212
use graph_store_postgres::layout_for_tests::set_account_like;
1313
use graph_store_postgres::layout_for_tests::LayoutCache;
1414
use graph_store_postgres::layout_for_tests::SqlName;
15-
use graph_store_postgres::{AsyncPgConnection, PgConnection};
15+
use graph_store_postgres::AsyncPgConnection;
1616
use hex_literal::hex;
1717
use lazy_static::lazy_static;
1818
use std::collections::BTreeSet;
@@ -240,14 +240,15 @@ lazy_static! {
240240
}
241241

242242
/// Removes test data from the database behind the store.
243-
fn remove_schema(conn: &mut PgConnection) {
243+
async fn remove_schema(conn: &mut AsyncPgConnection) {
244244
let query = format!("drop schema if exists {} cascade", NAMESPACE.as_str());
245245
conn.batch_execute(&query)
246+
.await
246247
.expect("Failed to drop test schema");
247248
}
248249

249250
async fn insert_entity_at(
250-
conn: &mut PgConnection,
251+
conn: &mut AsyncPgConnection,
251252
layout: &Layout,
252253
entity_type: &EntityType,
253254
mut entities: Vec<Entity>,
@@ -280,7 +281,7 @@ async fn insert_entity_at(
280281
}
281282

282283
async fn insert_entity(
283-
conn: &mut PgConnection,
284+
conn: &mut AsyncPgConnection,
284285
layout: &Layout,
285286
entity_type: &EntityType,
286287
entities: Vec<Entity>,
@@ -289,7 +290,7 @@ async fn insert_entity(
289290
}
290291

291292
async fn update_entity_at(
292-
conn: &mut PgConnection,
293+
conn: &mut AsyncPgConnection,
293294
layout: &Layout,
294295
entity_type: &EntityType,
295296
mut entities: Vec<Entity>,
@@ -320,7 +321,7 @@ async fn update_entity_at(
320321
}
321322

322323
async fn insert_user_entity(
323-
conn: &mut PgConnection,
324+
conn: &mut AsyncPgConnection,
324325
layout: &Layout,
325326
id: &str,
326327
entity_type: &EntityType,
@@ -388,7 +389,7 @@ fn make_user(
388389
user
389390
}
390391

391-
async fn insert_users(conn: &mut PgConnection, layout: &Layout) {
392+
async fn insert_users(conn: &mut AsyncPgConnection, layout: &Layout) {
392393
insert_user_entity(
393394
conn,
394395
layout,
@@ -443,7 +444,7 @@ async fn insert_users(conn: &mut PgConnection, layout: &Layout) {
443444
}
444445

445446
async fn update_user_entity(
446-
conn: &mut PgConnection,
447+
conn: &mut AsyncPgConnection,
447448
layout: &Layout,
448449
id: &str,
449450
entity_type: &EntityType,
@@ -475,7 +476,7 @@ async fn update_user_entity(
475476
}
476477

477478
async fn insert_pet(
478-
conn: &mut PgConnection,
479+
conn: &mut AsyncPgConnection,
479480
layout: &Layout,
480481
entity_type: &EntityType,
481482
id: &str,
@@ -491,20 +492,20 @@ async fn insert_pet(
491492
insert_entity_at(conn, layout, entity_type, vec![pet], block).await;
492493
}
493494

494-
async fn insert_pets(conn: &mut PgConnection, layout: &Layout) {
495+
async fn insert_pets(conn: &mut AsyncPgConnection, layout: &Layout) {
495496
insert_pet(conn, layout, &*DOG_TYPE, "pluto", "Pluto", 0, 0).await;
496497
insert_pet(conn, layout, &*CAT_TYPE, "garfield", "Garfield", 0, 1).await;
497498
}
498499

499-
async fn create_schema(conn: &mut PgConnection) -> Layout {
500+
async fn create_schema(conn: &mut AsyncPgConnection) -> Layout {
500501
let schema = InputSchema::parse_latest(THINGS_GQL, THINGS_SUBGRAPH_ID.clone()).unwrap();
501502
let site = make_dummy_site(
502503
THINGS_SUBGRAPH_ID.clone(),
503504
NAMESPACE.clone(),
504505
NETWORK_NAME.to_string(),
505506
);
506507
let query = format!("create schema {}", NAMESPACE.as_str());
507-
conn.batch_execute(&query).unwrap();
508+
conn.batch_execute(&query).await.unwrap();
508509

509510
Layout::create_relational_schema(conn, Arc::new(site), &schema, BTreeSet::new(), None)
510511
.await
@@ -552,11 +553,11 @@ macro_rules! assert_entity_eq {
552553
/// Test harness for running database integration tests.
553554
async fn run_test<F>(test: F)
554555
where
555-
F: AsyncFnOnce(&mut PgConnection, &Layout),
556+
F: AsyncFnOnce(&mut AsyncPgConnection, &Layout),
556557
{
557558
run_test_with_conn(async |conn| {
558559
// Reset state before starting
559-
remove_schema(conn);
560+
remove_schema(conn).await;
560561

561562
// Create the database schema
562563
let layout = create_schema(conn).await;
@@ -836,7 +837,7 @@ async fn enum_arrays() {
836837
.await
837838
}
838839

839-
async fn count_scalar_entities(conn: &mut PgConnection, layout: &Layout) -> usize {
840+
async fn count_scalar_entities(conn: &mut AsyncPgConnection, layout: &Layout) -> usize {
840841
let filter = EntityFilter::Or(vec![
841842
EntityFilter::Equal("bool".into(), true.into()),
842843
EntityFilter::Equal("bool".into(), false.into()),
@@ -980,7 +981,7 @@ async fn conflicting_entity() {
980981
// `id` is the id of an entity to create, `cat`, `dog`, and `ferret` are
981982
// the names of the types for which to check entity uniqueness
982983
async fn check(
983-
conn: &mut PgConnection,
984+
conn: &mut AsyncPgConnection,
984985
layout: &Layout,
985986
id: Value,
986987
cat: &str,
@@ -1041,10 +1042,10 @@ async fn conflicting_entity() {
10411042

10421043
#[tokio::test]
10431044
async fn revert_block() {
1044-
async fn check_fred(conn: &mut PgConnection, layout: &Layout) {
1045+
async fn check_fred(conn: &mut AsyncPgConnection, layout: &Layout) {
10451046
let id = "fred";
10461047

1047-
let set_fred = async |conn: &mut PgConnection, name, block| {
1048+
let set_fred = async |conn: &mut AsyncPgConnection, name, block| {
10481049
let fred = entity! { layout.input_schema =>
10491050
id: id,
10501051
name: name,
@@ -1057,7 +1058,7 @@ async fn revert_block() {
10571058
}
10581059
};
10591060

1060-
let assert_fred = async |conn: &mut PgConnection, name: &str| {
1061+
let assert_fred = async |conn: &mut AsyncPgConnection, name: &str| {
10611062
let fred = layout
10621063
.find(conn, &CAT_TYPE.parse_key(id).unwrap(), BLOCK_NUMBER_MAX)
10631064
.await
@@ -1082,8 +1083,8 @@ async fn revert_block() {
10821083
assert_fred(conn, "one").await;
10831084
}
10841085

1085-
async fn check_marty(conn: &mut PgConnection, layout: &Layout) {
1086-
let set_marties = async |conn: &mut PgConnection, from, to| {
1086+
async fn check_marty(conn: &mut AsyncPgConnection, layout: &Layout) {
1087+
let set_marties = async |conn: &mut AsyncPgConnection, from, to| {
10871088
for block in from..=to {
10881089
let id = format!("marty-{}", block);
10891090
let marty = entity! { layout.input_schema =>
@@ -1095,7 +1096,7 @@ async fn revert_block() {
10951096
}
10961097
};
10971098

1098-
let assert_marties = async |conn: &mut PgConnection,
1099+
let assert_marties = async |conn: &mut AsyncPgConnection,
10991100
max_block,
11001101
except: Vec<BlockNumber>| {
11011102
let id = DeploymentHash::new("QmXW3qvxV7zXnwRntpj7yoK8HZVtaraZ67uMqaLRvXdxha").unwrap();
@@ -1124,7 +1125,7 @@ async fn revert_block() {
11241125
}
11251126
};
11261127

1127-
let assert_all_marties = async |conn: &mut PgConnection, max_block| {
1128+
let assert_all_marties = async |conn: &mut AsyncPgConnection, max_block| {
11281129
assert_marties(conn, max_block, vec![]).await
11291130
};
11301131

@@ -1152,12 +1153,12 @@ async fn revert_block() {
11521153
}
11531154

11541155
struct QueryChecker<'a> {
1155-
conn: &'a mut PgConnection,
1156+
conn: &'a mut AsyncPgConnection,
11561157
layout: &'a Layout,
11571158
}
11581159

11591160
impl<'a> QueryChecker<'a> {
1160-
async fn new(conn: &'a mut PgConnection, layout: &'a Layout) -> Self {
1161+
async fn new(conn: &'a mut AsyncPgConnection, layout: &'a Layout) -> Self {
11611162
insert_users(conn, layout).await;
11621163
update_user_entity(
11631164
conn,
@@ -1912,12 +1913,12 @@ fn ferrets() -> (String, String, String, String) {
19121913
}
19131914

19141915
struct FilterChecker<'a> {
1915-
conn: &'a mut PgConnection,
1916+
conn: &'a mut AsyncPgConnection,
19161917
layout: &'a Layout,
19171918
}
19181919

19191920
impl<'a> FilterChecker<'a> {
1920-
async fn new(conn: &'a mut PgConnection, layout: &'a Layout) -> Self {
1921+
async fn new(conn: &'a mut AsyncPgConnection, layout: &'a Layout) -> Self {
19211922
let (a1, a2, a2b, a3) = ferrets();
19221923
insert_pet(conn, layout, &*FERRET_TYPE, "a1", &a1, 0, 0).await;
19231924
insert_pet(conn, layout, &*FERRET_TYPE, "a2", &a2, 0, 1).await;

store/test-store/tests/postgres/relational_bytes.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//! Test relational schemas that use `Bytes` to store ids
2-
use diesel::connection::SimpleConnection as _;
2+
use diesel_async::SimpleAsyncConnection;
33
use graph::components::store::write::RowGroup;
44
use graph::data::store::scalar;
55
use graph::data_source::CausalityRegion;
66
use graph::prelude::{BlockNumber, EntityModification, EntityQuery, MetricsRegistry, StoreError};
77
use graph::schema::{EntityKey, EntityType, InputSchema};
88
use graph::{entity, tokio};
9-
use graph_store_postgres::{AsyncPgConnection, PgConnection};
9+
use graph_store_postgres::AsyncPgConnection;
1010
use hex_literal::hex;
1111
use lazy_static::lazy_static;
1212
use std::collections::BTreeSet;
@@ -71,9 +71,10 @@ lazy_static! {
7171
}
7272

7373
/// Removes test data from the database behind the store.
74-
fn remove_test_data(conn: &mut PgConnection) {
74+
async fn remove_test_data(conn: &mut AsyncPgConnection) {
7575
let query = format!("drop schema if exists {} cascade", NAMESPACE.as_str());
7676
conn.batch_execute(&query)
77+
.await
7778
.expect("Failed to drop test schema");
7879
}
7980

@@ -137,7 +138,13 @@ async fn insert_entity(
137138
.expect(&errmsg);
138139
}
139140

140-
async fn insert_thing(conn: &mut PgConnection, layout: &Layout, id: &str, name: &str, vid: i64) {
141+
async fn insert_thing(
142+
conn: &mut AsyncPgConnection,
143+
layout: &Layout,
144+
id: &str,
145+
name: &str,
146+
vid: i64,
147+
) {
141148
insert_entity(
142149
conn,
143150
layout,
@@ -151,11 +158,11 @@ async fn insert_thing(conn: &mut PgConnection, layout: &Layout, id: &str, name:
151158
.await;
152159
}
153160

154-
async fn create_schema(conn: &mut PgConnection) -> Layout {
161+
async fn create_schema(conn: &mut AsyncPgConnection) -> Layout {
155162
let schema = InputSchema::parse_latest(THINGS_GQL, THINGS_SUBGRAPH_ID.clone()).unwrap();
156163

157164
let query = format!("create schema {}", NAMESPACE.as_str());
158-
conn.batch_execute(&query).unwrap();
165+
conn.batch_execute(&query).await.unwrap();
159166

160167
let site = make_dummy_site(
161168
THINGS_SUBGRAPH_ID.clone(),
@@ -201,11 +208,11 @@ macro_rules! assert_entity_eq {
201208

202209
async fn run_test<F>(test: F)
203210
where
204-
F: AsyncFnOnce(&mut PgConnection, &Layout),
211+
F: AsyncFnOnce(&mut AsyncPgConnection, &Layout),
205212
{
206213
run_test_with_conn(async |conn| {
207214
// Reset state before starting
208-
remove_test_data(conn);
215+
remove_test_data(conn).await;
209216

210217
// Seed database with test data
211218
let layout = create_schema(conn).await;
@@ -220,7 +227,7 @@ where
220227
async fn bad_id() {
221228
run_test(async |conn, layout| {
222229
async fn find(
223-
conn: &mut PgConnection,
230+
conn: &mut AsyncPgConnection,
224231
layout: &Layout,
225232
id: &str,
226233
) -> Result<Option<Entity>, StoreError> {
@@ -263,7 +270,11 @@ async fn bad_id() {
263270
#[tokio::test]
264271
async fn find() {
265272
run_test(async |mut conn, layout| {
266-
async fn find_entity(conn: &mut PgConnection, layout: &Layout, id: &str) -> Option<Entity> {
273+
async fn find_entity(
274+
conn: &mut AsyncPgConnection,
275+
layout: &Layout,
276+
id: &str,
277+
) -> Option<Entity> {
267278
let key = THING_TYPE.parse_key(id).unwrap();
268279
layout
269280
.find(conn, &key, BLOCK_NUMBER_MAX)
@@ -407,7 +418,10 @@ const GRANDCHILD2: &str = "0xfafa02";
407418
/// +- child2
408419
/// +- grandchild2
409420
///
410-
async fn make_thing_tree(conn: &mut PgConnection, layout: &Layout) -> (Entity, Entity, Entity) {
421+
async fn make_thing_tree(
422+
conn: &mut AsyncPgConnection,
423+
layout: &Layout,
424+
) -> (Entity, Entity, Entity) {
411425
let root = entity! { layout.input_schema =>
412426
id: ROOT,
413427
name: "root",
@@ -452,7 +466,7 @@ async fn make_thing_tree(conn: &mut PgConnection, layout: &Layout) -> (Entity, E
452466
#[tokio::test]
453467
async fn query() {
454468
async fn fetch(
455-
conn: &mut PgConnection,
469+
conn: &mut AsyncPgConnection,
456470
layout: &Layout,
457471
coll: EntityCollection,
458472
) -> Vec<String> {

0 commit comments

Comments
 (0)