Skip to content

Commit

Permalink
Reduce hardcoded connection defaults in tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos committed Oct 7, 2023
1 parent 348c6eb commit ec10cd6
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 174 deletions.
82 changes: 23 additions & 59 deletions test/connection_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import 'docker.dart';
void main() {
withPostgresServer('connection state', (server) {
test('pre-open failure', () async {
final conn = PostgreSQLConnection(
'localhost', await server.port, 'dart_test',
username: 'dart', password: 'dart');
final conn = await server.newPostgreSQLConnection();
await expectLater(
() => conn.query('SELECT 1;'),
throwsA(isA<Exception>().having(
Expand All @@ -30,9 +28,7 @@ void main() {
});

test('pre-open failure with transaction', () async {
final conn = PostgreSQLConnection(
'localhost', await server.port, 'dart_test',
username: 'dart', password: 'dart');
final conn = await server.newPostgreSQLConnection();
await expectLater(
() => conn.transaction((_) async {}),
throwsA(isA<Exception>().having(
Expand All @@ -47,9 +43,7 @@ void main() {
});

test('post-close failure', () async {
final conn = PostgreSQLConnection(
'localhost', await server.port, 'dart_test',
username: 'dart', password: 'dart');
final conn = await server.newPostgreSQLConnection();
await conn.open();
final rs = await conn.query('SELECT 1');
expect(rs.first.first, 1);
Expand All @@ -65,9 +59,7 @@ void main() {
});

test('reopen closed connection', () async {
final conn = PostgreSQLConnection(
'localhost', await server.port, 'dart_test',
username: 'dart', password: 'dart');
final conn = await server.newPostgreSQLConnection();
await conn.open();
final rs = await conn.query('SELECT 1');
expect(rs.first.first, 1);
Expand All @@ -84,12 +76,7 @@ void main() {

test('Connecting with ReplicationMode.none uses Extended Query Protocol',
() async {
final conn = PostgreSQLConnection(
'localhost',
await server.port,
'dart_test',
username: 'dart',
password: 'dart',
final conn = await server.newPostgreSQLConnection(
replicationMode: ReplicationMode.none,
);

Expand All @@ -100,12 +87,7 @@ void main() {
});

test('Connect with logical ReplicationMode.logical', () async {
final conn = PostgreSQLConnection(
'localhost',
await server.port,
'dart_test',
username: 'replication',
password: 'replication',
final conn = await server.newPostgreSQLConnection(
replicationMode: ReplicationMode.logical,
);

Expand All @@ -115,12 +97,7 @@ void main() {
});

test('IDENTIFY_SYSTEM returns system information', () async {
final conn = PostgreSQLConnection(
'localhost',
await server.port,
'dart_test',
username: 'replication',
password: 'replication',
final conn = await server.newPostgreSQLConnection(
replicationMode: ReplicationMode.logical,
);

Expand Down Expand Up @@ -149,25 +126,23 @@ void main() {
// settings in the pg_hba.conf
});

withPostgresServer('Connection lifecycle', (server) {
withPostgresServer('Connection lifecycle', initSqls: oldSchemaInit, (server) {
late PostgreSQLConnection conn;

tearDown(() async {
await conn.close();
});

test('Connect with md5 or scram-sha-256 auth required', () async {
conn = PostgreSQLConnection('localhost', await server.port, 'dart_test',
username: 'dart', password: 'dart');
conn = await server.newPostgreSQLConnection();

await conn.open();

expect(await conn.execute('select 1'), equals(1));
});

test('SSL Connect with md5 or scram-sha-256 auth required', () async {
conn = PostgreSQLConnection('localhost', await server.port, 'dart_test',
username: 'dart', password: 'dart', useSSL: true);
conn = await server.newPostgreSQLConnection(useSSL: true);

await conn.open();

Expand Down Expand Up @@ -301,7 +276,8 @@ void main() {
});
});

withPostgresServer('Successful queries over time', (server) {
withPostgresServer('Successful queries over time', initSqls: oldSchemaInit,
(server) {
late PostgreSQLConnection conn;

setUp(() async {
Expand Down Expand Up @@ -377,7 +353,8 @@ void main() {
});
});

withPostgresServer('Unintended user-error situations', (server) {
withPostgresServer('Unintended user-error situations',
initSqls: oldSchemaInit, (server) {
late PostgreSQLConnection conn;
Future? openFuture;

Expand Down Expand Up @@ -430,8 +407,7 @@ void main() {

test('SSL Starting transaction while opening connection triggers error',
() async {
conn = PostgreSQLConnection('localhost', await server.port, 'dart_test',
username: 'darttrust', useSSL: true);
conn = await server.newPostgreSQLConnection(useSSL: true);
openFuture = conn.open();

try {
Expand Down Expand Up @@ -476,8 +452,7 @@ void main() {

test('A query error maintains connectivity, allows future queries',
() async {
conn = PostgreSQLConnection('localhost', await server.port, 'dart_test',
username: 'darttrust');
conn = await server.newPostgreSQLConnection();
await conn.open();

await conn.execute('CREATE TEMPORARY TABLE t (i int unique)');
Expand All @@ -495,8 +470,7 @@ void main() {
test(
'A query error maintains connectivity, continues processing pending queries',
() async {
conn = PostgreSQLConnection('localhost', await server.port, 'dart_test',
username: 'darttrust');
conn = await server.newPostgreSQLConnection();
await conn.open();

await conn.execute('CREATE TEMPORARY TABLE t (i int unique)');
Expand Down Expand Up @@ -538,8 +512,7 @@ void main() {
test(
'A query error maintains connectivity, continues processing pending transactions',
() async {
conn = PostgreSQLConnection('localhost', await server.port, 'dart_test',
username: 'darttrust');
conn = await server.newPostgreSQLConnection();
await conn.open();

await conn.execute('CREATE TEMPORARY TABLE t (i int unique)');
Expand Down Expand Up @@ -570,8 +543,7 @@ void main() {
test(
'Building query throws error, connection continues processing pending queries',
() async {
conn = PostgreSQLConnection('localhost', await server.port, 'dart_test',
username: 'darttrust');
conn = await server.newPostgreSQLConnection();
await conn.open();

// Make some async queries that'll exit the event loop, but then fail on a query that'll die early
Expand Down Expand Up @@ -765,9 +737,7 @@ void main() {

withPostgresServer('connection', (server) {
test('If connection is closed, do not allow .execute', () async {
final conn = PostgreSQLConnection(
'localhost', await server.port, 'dart_test',
username: 'dart', password: 'dart');
final conn = await server.newPostgreSQLConnection();
try {
await conn.execute('SELECT 1');
fail('unreachable');
Expand All @@ -777,9 +747,7 @@ void main() {
});

test('If connection is closed, do not allow .query', () async {
final conn = PostgreSQLConnection(
'localhost', await server.port, 'dart_test',
username: 'dart', password: 'dart');
final conn = await server.newPostgreSQLConnection();
try {
await conn.query('SELECT 1');
fail('unreachable');
Expand All @@ -789,9 +757,7 @@ void main() {
});

test('If connection is closed, do not allow .mappedResultsQuery', () async {
final conn = PostgreSQLConnection(
'localhost', await server.port, 'dart_test',
username: 'dart', password: 'dart');
final conn = await server.newPostgreSQLConnection();
try {
await conn.mappedResultsQuery('SELECT 1');
fail('unreachable');
Expand All @@ -803,9 +769,7 @@ void main() {
test(
'Queue size, should be 0 on open, >0 if queries added and 0 again after queries executed',
() async {
final conn = PostgreSQLConnection(
'localhost', await server.port, 'dart_test',
username: 'dart', password: 'dart');
final conn = await server.newPostgreSQLConnection();
await conn.open();
expect(conn.queueSize, 0);

Expand Down
4 changes: 1 addition & 3 deletions test/decode_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ void main() {
withPostgresServer('decode', (server) {
late PostgreSQLConnection connection;
setUp(() async {
connection = PostgreSQLConnection(
'localhost', await server.port, 'dart_test',
username: 'dart', password: 'dart');
connection = await server.newPostgreSQLConnection();
await connection.open();

await connection.execute('''
Expand Down
45 changes: 33 additions & 12 deletions test/docker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'dart:io';
import 'package:docker_process/containers/postgres.dart';
import 'package:path/path.dart' as p;
import 'package:postgres/postgres_v3_experimental.dart';
import 'package:postgres/src/connection.dart';
import 'package:postgres/src/replication.dart';
import 'package:test/test.dart';

class PostgresServer {
Expand All @@ -12,19 +14,36 @@ class PostgresServer {

Future<int> get port => _port.future;

Future<PgEndpoint> dartTestEndpoint() async => PgEndpoint(
Future<PgEndpoint> get endpoint async => PgEndpoint(
host: 'localhost',
database: 'dart_test',
username: 'dart',
password: 'dart',
database: 'postgres',
username: 'postgres',
password: 'postgres',
port: await port,
);

Future<PostgreSQLConnection> newPostgreSQLConnection({
bool useSSL = false,
ReplicationMode replicationMode = ReplicationMode.none,
}) async {
final e = await endpoint;
return PostgreSQLConnection(
e.host,
e.port,
e.database,
username: e.username,
password: e.password,
useSSL: useSSL,
replicationMode: replicationMode,
);
}
}

void withPostgresServer(
String name,
void Function(PostgresServer server) fn,
) {
void Function(PostgresServer server) fn, {
Iterable<String>? initSqls,
}) {
group(name, () {
final server = PostgresServer();

Expand All @@ -36,6 +55,7 @@ void withPostgresServer(
await _startPostgresContainer(
port: port,
containerName: containerName,
initSqls: initSqls ?? const <String>[],
);

server._containerName.complete(containerName);
Expand Down Expand Up @@ -67,6 +87,7 @@ Future<int> selectFreePort() async {
Future<void> _startPostgresContainer({
required int port,
required String containerName,
required Iterable<String> initSqls,
}) async {
final isRunning = await _isPostgresContainerRunning(containerName);
if (isRunning) {
Expand Down Expand Up @@ -95,8 +116,7 @@ Future<void> _startPostgresContainer({
);

// Setup the database to support all kind of tests
// see _setupDatabaseStatements definition for details
for (final stmt in _setupDatabaseStatements) {
for (final stmt in initSqls) {
final args = [
'psql',
'-c',
Expand Down Expand Up @@ -131,10 +151,8 @@ Future<bool> _isPostgresContainerRunning(String containerName) async {
.contains(containerName);
}

// This setup supports old and new test
// This is setup is the same as the one from the old travis ci except for the
// replication user which is a new addition.
final _setupDatabaseStatements = <String>[
/// This is setup is the same as the one from the old travis ci.
const oldSchemaInit = <String>[
// create testing database
'create database dart_test;',
// create dart user
Expand All @@ -144,6 +162,9 @@ final _setupDatabaseStatements = <String>[
// create darttrust user
'create user darttrust with createdb;',
'grant all on database dart_test to darttrust;',
];

const replicationSchemaInit = <String>[
// create replication user
"create role replication with replication password 'replication' login;",
];
3 changes: 1 addition & 2 deletions test/encoding_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ late PostgreSQLConnection conn;
void main() {
withPostgresServer('Binary encoders', (server) {
setUp(() async {
conn = PostgreSQLConnection('localhost', await server.port, 'dart_test',
username: 'dart', password: 'dart');
conn = await server.newPostgreSQLConnection();
await conn.open();
});

Expand Down
5 changes: 1 addition & 4 deletions test/error_handling_test.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import 'package:postgres/postgres.dart';
import 'package:test/test.dart';

import 'docker.dart';

void main() {
withPostgresServer('error handling', (server) {
test('Reports stacktrace correctly', () async {
final conn = PostgreSQLConnection(
'localhost', await server.port, 'dart_test',
username: 'dart', password: 'dart');
final conn = await server.newPostgreSQLConnection();
await conn.open();
addTearDown(() async => conn.close());

Expand Down
4 changes: 1 addition & 3 deletions test/json_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ void main() {
late PostgreSQLConnection connection;

setUp(() async {
connection = PostgreSQLConnection(
'localhost', await server.port, 'dart_test',
username: 'dart', password: 'dart');
connection = await server.newPostgreSQLConnection();
await connection.open();

await connection.execute('''
Expand Down
Loading

0 comments on commit ec10cd6

Please sign in to comment.