node-postgres/test/integration/client/transaction-tests.js

73 lines
1.8 KiB
JavaScript
Raw Normal View History

2010-12-19 04:50:30 +08:00
var helper = require(__dirname + '/test-helper');
var sink = new helper.Sink(2, function() {
helper.pg.end();
});
2010-12-19 04:50:30 +08:00
test('a single connection transaction', function() {
2013-03-08 05:57:00 +08:00
helper.pg.connect(helper.config, assert.success(function(client, done) {
2010-12-19 04:50:30 +08:00
client.query('begin');
var getZed = {
text: 'SELECT * FROM person WHERE name = $1',
values: ['Zed']
};
test('Zed should not exist in the database', function() {
client.query(getZed, assert.calls(function(err, result) {
assert.isNull(err);
assert.empty(result.rows);
}))
})
client.query("INSERT INTO person(name, age) VALUES($1, $2)", ['Zed', 270], assert.calls(function(err, result) {
assert.isNull(err)
}));
test('Zed should exist in the database', function() {
client.query(getZed, assert.calls(function(err, result) {
assert.isNull(err);
assert.equal(result.rows[0].name, 'Zed');
}))
})
client.query('rollback');
test('Zed should not exist in the database', function() {
client.query(getZed, assert.calls(function(err, result) {
assert.isNull(err);
assert.empty(result.rows);
2013-03-08 05:57:00 +08:00
done();
2010-12-19 04:50:30 +08:00
sink.add();
}))
})
}))
})
2011-07-13 12:08:16 +08:00
test('gh#36', function() {
2013-03-08 05:57:00 +08:00
helper.pg.connect(helper.config, assert.success(function(client, done) {
2011-07-13 12:08:16 +08:00
client.query("BEGIN");
client.query({
name: 'X',
text: "SELECT $1::INTEGER",
values: [0]
}, assert.calls(function(err, result) {
if(err) throw err;
assert.equal(result.rows.length, 1);
}))
client.query({
name: 'X',
text: "SELECT $1::INTEGER",
values: [0]
}, assert.calls(function(err, result) {
if(err) throw err;
assert.equal(result.rows.length, 1);
}))
client.query("COMMIT", function() {
sink.add();
2013-03-08 05:57:00 +08:00
done();
})
2013-03-08 05:57:00 +08:00
}));
2011-07-13 12:08:16 +08:00
})