commit-transaction.test.js 4.3 KB

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