Skip to content

Instantly share code, notes, and snippets.

@pires
Last active December 15, 2015 12:19
Show Gist options
  • Save pires/5259295 to your computer and use it in GitHub Desktop.
Save pires/5259295 to your computer and use it in GitHub Desktop.
Prepare a PostgreSQL URI by reading test/config.yaml. Optional parameters will take precedence, if any.
library postgresql_test;
import 'dart:async';
import 'package:unittest/unittest.dart';
import 'package:dart_config/default_server.dart' as cfg;
// load from file
Future<String> load({String user, String pwd, String host, int port, String db}){
var completer = new Completer<String>();
cfg.loadConfig('test/config.yaml').then((Map config) {
final String _host = ?host ? host : config["host"];
final int _port = ?port ? port : config["port"];
final String _user = ?user ? user : config["user"];
final String _pwd = ?pwd ? pwd : config["pwd"];
final String _db = ?db ? db : config["db"];
completer.complete("postgres://$_user:$_pwd@$_host:$_port/$_db");
},
onError: (err) => completer.completeError(err));
return completer.future;
}
library postgresql_test;
import 'dart:async';
import 'dart:io';
import 'package:unittest/unittest.dart';
import 'package:postgresql/postgresql.dart';
import 'package:postgresql/postgresql_pool.dart';
import 'load_config.dart' as cfg;
main() {
test('Connect', () {
int tout = 2 * 60 * 1000; // Should be longer than usage
Pool pool;
cfg.load()
.then((uri) => pool = new Pool(uri, timeout: tout, min: 2, max: 5))
.catchError((err) => print(err));
var pass = expectAsync0(() {});
testConnect(_) {
pool.connect().then((conn) {
print(pool);
conn.query("select 'oi';").toList()
.then(print)
.then((_) => conn.close())
.catchError((err) => print('Query error: $err'));
})
.catchError((err) => print('Connect error: $err'));
}
// Wait for initial connections to be made before starting
var timer;
pool.start().then((_) {
timer = new Timer.periodic(new Duration(milliseconds: 100), testConnect);
});
new Future.delayed(new Duration(seconds: 2), () {
timer.cancel();
pool.destroy();
print('Pool destroyed.');
pass();
exit(0); //FIXME - something is keeping the process alive.
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment