forked from eclipse-vertx/vertx-sql-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPgTestBase.java
More file actions
69 lines (57 loc) · 2.18 KB
/
PgTestBase.java
File metadata and controls
69 lines (57 loc) · 2.18 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
/*
* Copyright (c) 2011-2026 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.pgclient;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import io.vertx.pgclient.junit.ContainerPgRule;
import io.vertx.sqlclient.PoolOptions;
import io.vertx.sqlclient.SqlClient;
import org.junit.ClassRule;
import org.junit.runner.RunWith;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
*/
@RunWith(VertxUnitRunner.class)
public abstract class PgTestBase {
protected static final String ERRCODE_QUERY_CANCELED = "57014";
@ClassRule
public static final ContainerPgRule rule = ContainerPgRule.SHARED_INSTANCE;
protected PgConnectOptions options;
protected PoolOptions poolOptions;
public void setup() throws Exception {
options = rule.options();
poolOptions = rule.poolOptions();
}
static void deleteFromTestTable(TestContext ctx, SqlClient client, Runnable completionHandler) {
client.query("DELETE FROM Test").execute(ctx.asyncAssertSuccess(result -> completionHandler.run()));
}
static void insertIntoTestTable(TestContext ctx, SqlClient client, int amount, Runnable completionHandler) {
AtomicInteger count = new AtomicInteger();
for (int i = 0;i < 10;i++) {
client
.query("INSERT INTO Test (id, val) VALUES (" + i + ", 'Whatever-" + i + "')")
.execute(ctx.asyncAssertSuccess(r1 -> {
ctx.assertEquals(1, r1.rowCount());
if (count.incrementAndGet() == amount) {
completionHandler.run();
}
}));
}
}
/**
* @return whether throwable is a PgException with the SQLSTATE code
*/
static boolean hasSqlstateCode(Throwable throwable, String code) {
return throwable instanceof PgException &&
code.equals(((PgException) throwable).getSqlState());
}
}