Skip to content

Commit b52bc4b

Browse files
committed
test: add unit tests for stmt.iterate()
1 parent de5bdc6 commit b52bc4b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

test/parallel/test-sqlite.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,34 @@ suite('StatementSync.prototype.all()', () => {
265265
});
266266
});
267267

268+
suite('StatementSync.prototype.iterate()', () => {
269+
test('executes a query and returns an empty iterator on no results', (t) => {
270+
const db = new DatabaseSync(nextDb());
271+
const stmt = db.prepare('CREATE TABLE storage(key TEXT, val TEXT)');
272+
t.assert.deepStrictEqual(stmt.iterate().toArray(), []);
273+
});
274+
275+
test('executes a query and returns all results', (t) => {
276+
const db = new DatabaseSync(nextDb());
277+
let stmt = db.prepare('CREATE TABLE storage(key TEXT, val TEXT)');
278+
t.assert.deepStrictEqual(stmt.run(), { changes: 0, lastInsertRowid: 0 });
279+
stmt = db.prepare('INSERT INTO storage (key, val) VALUES (?, ?)');
280+
t.assert.deepStrictEqual(
281+
stmt.run('key1', 'val1'),
282+
{ changes: 1, lastInsertRowid: 1 },
283+
);
284+
t.assert.deepStrictEqual(
285+
stmt.run('key2', 'val2'),
286+
{ changes: 1, lastInsertRowid: 2 },
287+
);
288+
stmt = db.prepare('SELECT * FROM storage ORDER BY key');
289+
t.assert.deepStrictEqual(stmt.iterate().toArray(), [
290+
{ key: 'key1', val: 'val1' },
291+
{ key: 'key2', val: 'val2' },
292+
]);
293+
});
294+
})
295+
268296
suite('StatementSync.prototype.run()', () => {
269297
test('executes a query and returns change metadata', (t) => {
270298
const db = new DatabaseSync(nextDb());

0 commit comments

Comments
 (0)