rollback-transaction.test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. var assert = require('assert');
  2. var Pack = require('../../');
  3. describe('Transactional ::', function() {
  4. describe('Rollback Transaction', function() {
  5. var manager;
  6. var connection;
  7. // Create a manager, a connection, and a table
  8. before(function(done) {
  9. // Needed to dynamically get the host using the docker container
  10. var host = process.env.MYSQL_PORT_3306_TCP_ADDR || 'localhost';
  11. Pack.createManager({
  12. connectionString: 'mysql://mp:mp@' + host + ':3306/mppg'
  13. })
  14. .exec(function(err, report) {
  15. if (err) {
  16. return done(err);
  17. }
  18. // Store the manager
  19. manager = report.manager;
  20. Pack.getConnection({
  21. manager: manager
  22. })
  23. .exec(function(err, report) {
  24. if (err) {
  25. return done(err);
  26. }
  27. // Store the connection
  28. connection = report.connection;
  29. // Create a table to use for testing
  30. Pack.sendNativeQuery({
  31. connection: connection,
  32. nativeQuery: 'CREATE TABLE IF NOT EXISTS people(id serial primary key, name varchar(255));'
  33. })
  34. .exec(function(err) {
  35. if (err) {
  36. return done(err);
  37. }
  38. return done();
  39. });
  40. });
  41. });
  42. });
  43. // Afterwards destroy the table and release the connection
  44. after(function(done) {
  45. Pack.sendNativeQuery({
  46. connection: connection,
  47. nativeQuery: 'DROP TABLE people;'
  48. })
  49. .exec(function(err) {
  50. if (err) {
  51. return done(err);
  52. }
  53. Pack.releaseConnection({
  54. connection: connection
  55. }).exec(done);
  56. });
  57. });
  58. // To Test:
  59. // * Open a transaction on connection and insert a record into the DB
  60. // * Run a query on connection and make sure the record exist
  61. // * Rollback the transaction
  62. // * Run the select query again and the record should not exist
  63. it('should perform a transaction and make sure the results are rolled back correctly', function(done) {
  64. // Start a transaction
  65. Pack.beginTransaction({
  66. connection: connection
  67. })
  68. .exec(function(err) {
  69. if (err) {
  70. return done(err);
  71. }
  72. // Insert a record using the transaction
  73. Pack.sendNativeQuery({
  74. connection: connection,
  75. nativeQuery: 'INSERT INTO people (name) VALUES (\'hugo\');'
  76. })
  77. .exec(function(err) {
  78. if (err) {
  79. return done(err);
  80. }
  81. // Query the table and ensure the record does exist
  82. Pack.sendNativeQuery({
  83. connection: connection,
  84. nativeQuery: 'SELECT * FROM people;'
  85. })
  86. .exec(function(err, report) {
  87. if (err) {
  88. return done(err);
  89. }
  90. // Ensure 1 result were returned
  91. assert.equal(report.result.rows.length, 1);
  92. // Rollback the transaction
  93. Pack.rollbackTransaction({
  94. connection: connection
  95. })
  96. .exec(function(err) {
  97. if (err) {
  98. return done(err);
  99. }
  100. // Query the table using and ensure the record doesn't exist
  101. Pack.sendNativeQuery({
  102. connection: connection,
  103. nativeQuery: 'SELECT * FROM people;'
  104. })
  105. .exec(function(err, report) {
  106. if (err) {
  107. return done(err);
  108. }
  109. // Ensure no results were returned
  110. assert.equal(report.result.rows.length, 0);
  111. return done();
  112. });
  113. });
  114. });
  115. });
  116. });
  117. });
  118. });
  119. });