parse-native-query-result.test.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. var assert = require('assert');
  2. var _ = require('@sailshq/lodash');
  3. var Pack = require('../../');
  4. describe('Queryable ::', function() {
  5. describe('Parse Native Query Result', function() {
  6. var manager;
  7. var connection;
  8. // Create a manager and connection
  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. connection = report.connection;
  30. // Create a table to use for testing
  31. Pack.sendNativeQuery({
  32. connection: connection,
  33. nativeQuery: 'CREATE TABLE IF NOT EXISTS people(id serial primary key, name varchar(255));'
  34. })
  35. .exec(function(err) {
  36. if (err) {
  37. return done(err);
  38. }
  39. return done();
  40. });
  41. });
  42. });
  43. });
  44. // Afterwards destroy the test table and release the connection
  45. after(function(done) {
  46. Pack.sendNativeQuery({
  47. connection: connection,
  48. nativeQuery: 'DROP TABLE people;'
  49. })
  50. .exec(function(err) {
  51. if (err) {
  52. return done(err);
  53. }
  54. Pack.releaseConnection({
  55. connection: connection
  56. }).exec(done);
  57. });
  58. });
  59. it('should normalize SELECT query results from a native query', function(done) {
  60. Pack.sendNativeQuery({
  61. connection: connection,
  62. nativeQuery: 'SELECT * from people;'
  63. })
  64. .exec(function(err, report) {
  65. if (err) {
  66. return done(err);
  67. }
  68. var result = report.result;
  69. Pack.parseNativeQueryResult({
  70. queryType: 'select',
  71. nativeQueryResult: result
  72. })
  73. .exec(function(err, report) {
  74. if (err) {
  75. return done(err);
  76. }
  77. assert(report.result);
  78. assert(_.isArray(report.result));
  79. assert.equal(report.result.length, 0);
  80. return done();
  81. });
  82. });
  83. });
  84. it('should normalize INSERT query results from a native query', function(done) {
  85. Pack.sendNativeQuery({
  86. connection: connection,
  87. nativeQuery: 'INSERT INTO people (name) VALUES (\'hugo\');'
  88. })
  89. .exec(function(err, report) {
  90. if (err) {
  91. return done(err);
  92. }
  93. var result = report.result;
  94. Pack.parseNativeQueryResult({
  95. queryType: 'insert',
  96. nativeQueryResult: result
  97. })
  98. .exec(function(err, report) {
  99. if (err) {
  100. return done(err);
  101. }
  102. assert(report.result);
  103. assert(report.result.inserted);
  104. // We don't know what the ID will be so just check it's a number
  105. assert(_.isNumber(report.result.inserted));
  106. return done();
  107. });
  108. });
  109. });
  110. it('should normalize UPDATE query results from a native query', function(done) {
  111. Pack.sendNativeQuery({
  112. connection: connection,
  113. nativeQuery: 'INSERT INTO people (name) VALUES (\'hugo\');'
  114. })
  115. .exec(function(err, report) {
  116. if (err) {
  117. return done(err);
  118. }
  119. Pack.sendNativeQuery({
  120. connection: connection,
  121. nativeQuery: 'UPDATE people SET name = \'George\' where id = ' + report.result.insertId + ';'
  122. })
  123. .exec(function(err, report) {
  124. if (err) {
  125. return done(err);
  126. }
  127. var result = report.result;
  128. Pack.parseNativeQueryResult({
  129. queryType: 'update',
  130. nativeQueryResult: result
  131. })
  132. .exec(function(err, report) {
  133. if (err) {
  134. return done(err);
  135. }
  136. assert(report.result);
  137. assert(report.result.numRecordsUpdated);
  138. assert.equal(report.result.numRecordsUpdated, 1);
  139. return done();
  140. });
  141. });
  142. });
  143. });
  144. it('should normalize DELETE query results from a native query', function(done) {
  145. Pack.sendNativeQuery({
  146. connection: connection,
  147. nativeQuery: 'INSERT INTO people (name) VALUES (\'Sally\');'
  148. })
  149. .exec(function(err, report) {
  150. if (err) {
  151. return done(err);
  152. }
  153. // Ensure that the record inserted ok
  154. assert.equal(report.result.affectedRows, 1);
  155. Pack.sendNativeQuery({
  156. connection: connection,
  157. nativeQuery: 'DELETE FROM people WHERE name = \'Sally\';'
  158. })
  159. .exec(function(err, report) {
  160. if (err) {
  161. return done(err);
  162. }
  163. var result = report.result;
  164. Pack.parseNativeQueryResult({
  165. queryType: 'delete',
  166. nativeQueryResult: result
  167. })
  168. .exec(function(err, report) {
  169. if (err) {
  170. return done(err);
  171. }
  172. assert(report.result);
  173. assert(report.result.numRecordsDeleted);
  174. assert.equal(report.result.numRecordsDeleted, 1);
  175. return done();
  176. });
  177. });
  178. });
  179. });
  180. });
  181. });