|
2 | 2 | * rowset.test.ts - test rowset container |
3 | 3 | */ |
4 | 4 |
|
5 | | -import { SQLiteCloudRowset } from '../src/index' |
| 5 | +import { SQLiteCloudRowset, SQLiteCloudRow } from '../src/index' |
6 | 6 | import { SQLiteCloudConnection } from '../src/connection' |
7 | 7 | import { CHINOOK_DATABASE_URL, getChinookConnection, getChinookConfig } from './shared' |
8 | 8 |
|
@@ -34,6 +34,58 @@ describe('rowset', () => { |
34 | 34 | }) |
35 | 35 | }) |
36 | 36 |
|
| 37 | + it('implements .map', done => { |
| 38 | + const connection = getChinookConnection() |
| 39 | + connection.sendCommands('SELECT * FROM tracks LIMIT 10;', (error, rowset) => { |
| 40 | + expect(rowset).toBeInstanceOf(SQLiteCloudRowset) |
| 41 | + expect(rowset.numberOfColumns).toBe(9) |
| 42 | + expect(rowset.numberOfRows).toBe(10) |
| 43 | + expect(Array.isArray(rowset)).toBeTruthy() |
| 44 | + expect(rowset).toHaveLength(10) |
| 45 | + |
| 46 | + rowset.map((row: SQLiteCloudRow) => { |
| 47 | + expect(row).toBeInstanceOf(SQLiteCloudRow) |
| 48 | + expect(Object.keys(row)).toHaveLength(9) |
| 49 | + }) |
| 50 | + |
| 51 | + connection.close() |
| 52 | + done() |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + it('implements .filter', done => { |
| 57 | + const connection = getChinookConnection() |
| 58 | + connection.sendCommands('SELECT * FROM tracks LIMIT 10;', (error, rowset) => { |
| 59 | + expect(rowset).toBeInstanceOf(SQLiteCloudRowset) |
| 60 | + expect(rowset.numberOfColumns).toBe(9) |
| 61 | + expect(rowset.numberOfRows).toBe(10) |
| 62 | + expect(Array.isArray(rowset)).toBeTruthy() |
| 63 | + expect(rowset).toHaveLength(10) |
| 64 | + |
| 65 | + const filtered = rowset.filter((row: SQLiteCloudRow) => row.AlbumId === 1) |
| 66 | + expect(filtered).toBeInstanceOf(SQLiteCloudRowset) |
| 67 | + expect(filtered.numberOfColumns).toBe(9) |
| 68 | + expect(filtered.numberOfRows).toBe(6) |
| 69 | + |
| 70 | + connection.close() |
| 71 | + done() |
| 72 | + }) |
| 73 | + }) |
| 74 | + |
| 75 | + it('implements .reduce', done => { |
| 76 | + const connection = getChinookConnection() |
| 77 | + connection.sendCommands('SELECT * FROM invoices;', (error, rowset) => { |
| 78 | + expect(rowset).toBeInstanceOf(SQLiteCloudRowset) |
| 79 | + |
| 80 | + // doing "SELECT sum(Total) FROM invoices" the wrong way (not using SQL) to test reduce... |
| 81 | + const total = rowset.reduce((acc: number, row: SQLiteCloudRow) => acc + (row?.Total ? (row.Total as number) : 0), 0) |
| 82 | + expect(Math.floor(total)).toBe(2328) |
| 83 | + |
| 84 | + connection.close() |
| 85 | + done() |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
37 | 89 | it('can be sliced like an array', done => { |
38 | 90 | const connection = getChinookConnection() |
39 | 91 | connection.sendCommands('SELECT * FROM tracks LIMIT 50;', (error, rowset) => { |
|
0 commit comments