1. 1var sqlite3 = require('..');
2. 1var assert = require('assert');
3. 1
4. 1describe('error handling', function() {
5. 1 var db;
6. 1 before(function(done) {
7. 1 db = new sqlite3.Database(':memory:', done);
8. 1 });
9. 1
10. 1 it('throw when calling Database() without new', function() {
11. 1 assert.throws(function() {
12. 1 sqlite3.Database(':memory:');
13. 1 }, (/Class constructors cannot be invoked without 'new'/));
14. 1
15. 1 assert.throws(function() {
16. 1 sqlite3.Statement();
17. 1 }, (/Class constructors cannot be invoked without 'new'/));
18. 1 });
19. 1
20. 1 it('should error when calling Database#get on a missing table', function(done) {
21. 1 db.get('SELECT id, txt FROM foo', function(err, row) {
22. 1 if (err) {
23. 1 assert.equal(err.message, 'SQLITE_ERROR: no such table: foo');
24. 1 assert.equal(err.errno, sqlite3.ERROR);
25. 1 assert.equal(err.code, 'SQLITE_ERROR');
26. 1 done();
27. 0 } else {
28. 0 done(new Error('Completed query without error, but expected error'));
29. 0 }
30. 1 });
31. 1 });
32. 1
33. 1 it('Database#all prepare fail', function(done) {
34. 1 db.all('SELECT id, txt FROM foo', function(err, row) {
35. 1 if (err) {
36. 1 assert.equal(err.message, 'SQLITE_ERROR: no such table: foo');
37. 1 assert.equal(err.errno, sqlite3.ERROR);
38. 1 assert.equal(err.code, 'SQLITE_ERROR');
39. 1 done();
40. 0 } else {
41. 0 done(new Error('Completed query without error, but expected error'));
42. 0 }
43. 1 });
44. 1 });
45. 1
46. 1 it('Database#run prepare fail', function(done) {
47. 1 db.run('SELECT id, txt FROM foo', function(err, row) {
48. 1 if (err) {
49. 1 assert.equal(err.message, 'SQLITE_ERROR: no such table: foo');
50. 1 assert.equal(err.errno, sqlite3.ERROR);
51. 1 assert.equal(err.code, 'SQLITE_ERROR');
52. 1 done();
53. 0 } else {
54. 0 done(new Error('Completed query without error, but expected error'));
55. 0 }
56. 1 });
57. 1 });
58. 1
59. 1 it('Database#each prepare fail', function(done) {
60. 0 db.each('SELECT id, txt FROM foo', function(err, row) {
61. 0 assert.ok(false, "this should not be called");
62. 1 }, function(err, num) {
63. 1 if (err) {
64. 1 assert.equal(err.message, 'SQLITE_ERROR: no such table: foo');
65. 1 assert.equal(err.errno, sqlite3.ERROR);
66. 1 assert.equal(err.code, 'SQLITE_ERROR');
67. 1 done();
68. 0 } else {
69. 0 done(new Error('Completed query without error, but expected error'));
70. 0 }
71. 1 });
72. 1 });
73. 1
74. 1 it('Database#each prepare fail without completion handler', function(done) {
75. 1 db.each('SELECT id, txt FROM foo', function(err, row) {
76. 1 if (err) {
77. 1 assert.equal(err.message, 'SQLITE_ERROR: no such table: foo');
78. 1 assert.equal(err.errno, sqlite3.ERROR);
79. 1 assert.equal(err.code, 'SQLITE_ERROR');
80. 1 done();
81. 0 } else {
82. 0 done(new Error('Completed query without error, but expected error'));
83. 0 }
84. 1 });
85. 1 });
86. 1
87. 1 it('Database#get prepare fail with param binding', function(done) {
88. 1 db.get('SELECT id, txt FROM foo WHERE id = ?', 1, function(err, row) {
89. 1 if (err) {
90. 1 assert.equal(err.message, 'SQLITE_ERROR: no such table: foo');
91. 1 assert.equal(err.errno, sqlite3.ERROR);
92. 1 assert.equal(err.code, 'SQLITE_ERROR');
93. 1 done();
94. 0 } else {
95. 0 done(new Error('Completed query without error, but expected error'));
96. 0 }
97. 1 });
98. 1 });
99. 1
100. 1 it('Database#all prepare fail with param binding', function(done) {
101. 1 db.all('SELECT id, txt FROM foo WHERE id = ?', 1, function(err, row) {
102. 1 if (err) {
103. 1 assert.equal(err.message, 'SQLITE_ERROR: no such table: foo');
104. 1 assert.equal(err.errno, sqlite3.ERROR);
105. 1 assert.equal(err.code, 'SQLITE_ERROR');
106. 1 done();
107. 0 } else {
108. 0 done(new Error('Completed query without error, but expected error'));
109. 0 }
110. 1 });
111. 1 });
112. 1
113. 1 it('Database#run prepare fail with param binding', function(done) {
114. 1 db.run('SELECT id, txt FROM foo WHERE id = ?', 1, function(err, row) {
115. 1 if (err) {
116. 1 assert.equal(err.message, 'SQLITE_ERROR: no such table: foo');
117. 1 assert.equal(err.errno, sqlite3.ERROR);
118. 1 assert.equal(err.code, 'SQLITE_ERROR');
119. 1 done();
120. 0 } else {
121. 0 done(new Error('Completed query without error, but expected error'));
122. 0 }
123. 1 });
124. 1 });
125. 1
126. 1 it('Database#each prepare fail with param binding', function(done) {
127. 0 db.each('SELECT id, txt FROM foo WHERE id = ?', 1, function(err, row) {
128. 0 assert.ok(false, "this should not be called");
129. 1 }, function(err, num) {
130. 1 if (err) {
131. 1 assert.equal(err.message, 'SQLITE_ERROR: no such table: foo');
132. 1 assert.equal(err.errno, sqlite3.ERROR);
133. 1 assert.equal(err.code, 'SQLITE_ERROR');
134. 1 done();
135. 0 } else {
136. 0 done(new Error('Completed query without error, but expected error'));
137. 0 }
138. 1 });
139. 1 });
140. 1
141. 1 it('Database#each prepare fail with param binding without completion handler', function(done) {
142. 1 db.each('SELECT id, txt FROM foo WHERE id = ?', 1, function(err, row) {
143. 1 if (err) {
144. 1 assert.equal(err.message, 'SQLITE_ERROR: no such table: foo');
145. 1 assert.equal(err.errno, sqlite3.ERROR);
146. 1 assert.equal(err.code, 'SQLITE_ERROR');
147. 1 done();
148. 0 } else {
149. 0 done(new Error('Completed query without error, but expected error'));
150. 0 }
151. 1 });
152. 1 });
153. 1});
154. 1