@@ -7,7 +7,80 @@ import 'dart:mirrors';
7
7
import 'package:postgres/postgres.dart' ;
8
8
import 'package:test/test.dart' ;
9
9
10
+ import 'docker.dart' ;
11
+
10
12
void main () {
13
+ group ('connection state' , () {
14
+ usePostgresDocker ();
15
+
16
+ test ('pre-open failure' , () async {
17
+ final conn = PostgreSQLConnection ('localhost' , 5432 , 'dart_test' ,
18
+ username: 'dart' , password: 'dart' );
19
+ await expectLater (
20
+ () => conn.query ('SELECT 1;' ),
21
+ throwsA (isA <Exception >().having (
22
+ (e) => '$e ' ,
23
+ 'text' ,
24
+ contains (
25
+ 'Attempting to execute query, but connection is not open.' ),
26
+ )));
27
+ await conn.open ();
28
+ final rs = await conn.query ('SELECT 1' );
29
+ expect (rs.first.first, 1 );
30
+ await conn.close ();
31
+ });
32
+
33
+ test ('pre-open failure with transaction' , () async {
34
+ final conn = PostgreSQLConnection ('localhost' , 5432 , 'dart_test' ,
35
+ username: 'dart' , password: 'dart' );
36
+ await expectLater (
37
+ () => conn.transaction ((_) async {}),
38
+ throwsA (isA <Exception >().having (
39
+ (e) => '$e ' ,
40
+ 'text' ,
41
+ contains (
42
+ 'Attempting to execute query, but connection is not open.' ),
43
+ )));
44
+ await conn.open ();
45
+ await conn.transaction ((_) async {});
46
+ await conn.close ();
47
+ });
48
+
49
+ test ('post-close failure' , () async {
50
+ final conn = PostgreSQLConnection ('localhost' , 5432 , 'dart_test' ,
51
+ username: 'dart' , password: 'dart' );
52
+ await conn.open ();
53
+ final rs = await conn.query ('SELECT 1' );
54
+ expect (rs.first.first, 1 );
55
+ await conn.close ();
56
+ await expectLater (
57
+ () => conn.query ('SELECT 1;' ),
58
+ throwsA (isA <Exception >().having (
59
+ (e) => '$e ' ,
60
+ 'text' ,
61
+ contains (
62
+ 'Attempting to execute query, but connection is not open.' ),
63
+ )));
64
+ });
65
+
66
+ test ('reopen closed connection' , () async {
67
+ final conn = PostgreSQLConnection ('localhost' , 5432 , 'dart_test' ,
68
+ username: 'dart' , password: 'dart' );
69
+ await conn.open ();
70
+ final rs = await conn.query ('SELECT 1' );
71
+ expect (rs.first.first, 1 );
72
+ await conn.close ();
73
+ await expectLater (
74
+ conn.open,
75
+ throwsA (isA <Exception >().having (
76
+ (e) => '$e ' ,
77
+ 'text' ,
78
+ contains (
79
+ 'Attempting to reopen a closed connection. Create a instance instead.' ),
80
+ )));
81
+ });
82
+ });
83
+
11
84
// These tests are disabled, as we'd need to setup ci/pg_hba.conf into the CI
12
85
// postgres instance first.
13
86
// TODO: re-enable these tests after pg_hba.conf is used
0 commit comments