Skip to content

More tests #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions test/connection_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,80 @@ import 'dart:mirrors';
import 'package:postgres/postgres.dart';
import 'package:test/test.dart';

import 'docker.dart';

void main() {
group('connection state', () {
usePostgresDocker();

test('pre-open failure', () async {
final conn = PostgreSQLConnection('localhost', 5432, 'dart_test',
username: 'dart', password: 'dart');
await expectLater(
() => conn.query('SELECT 1;'),
throwsA(isA<Exception>().having(
(e) => '$e',
'text',
contains(
'Attempting to execute query, but connection is not open.'),
)));
await conn.open();
final rs = await conn.query('SELECT 1');
expect(rs.first.first, 1);
await conn.close();
});

test('pre-open failure with transaction', () async {
final conn = PostgreSQLConnection('localhost', 5432, 'dart_test',
username: 'dart', password: 'dart');
await expectLater(
() => conn.transaction((_) async {}),
throwsA(isA<Exception>().having(
(e) => '$e',
'text',
contains(
'Attempting to execute query, but connection is not open.'),
)));
await conn.open();
await conn.transaction((_) async {});
await conn.close();
});

test('post-close failure', () async {
final conn = PostgreSQLConnection('localhost', 5432, 'dart_test',
username: 'dart', password: 'dart');
await conn.open();
final rs = await conn.query('SELECT 1');
expect(rs.first.first, 1);
await conn.close();
await expectLater(
() => conn.query('SELECT 1;'),
throwsA(isA<Exception>().having(
(e) => '$e',
'text',
contains(
'Attempting to execute query, but connection is not open.'),
)));
});

test('reopen closed connection', () async {
final conn = PostgreSQLConnection('localhost', 5432, 'dart_test',
username: 'dart', password: 'dart');
await conn.open();
final rs = await conn.query('SELECT 1');
expect(rs.first.first, 1);
await conn.close();
await expectLater(
conn.open,
throwsA(isA<Exception>().having(
(e) => '$e',
'text',
contains(
'Attempting to reopen a closed connection. Create a instance instead.'),
)));
});
});

// These tests are disabled, as we'd need to setup ci/pg_hba.conf into the CI
// postgres instance first.
// TODO: re-enable these tests after pg_hba.conf is used
Expand Down