|
| 1 | + |
| 2 | +import { describe, it, before, after } from 'node:test'; |
| 3 | +import assert from 'node:assert'; |
| 4 | +import { createDatabaseEngine } from '../../src/core/sqlite-db'; |
| 5 | +import { CellUpdate, ModificationEntry } from '../../src/core/types'; |
| 6 | + |
| 7 | +describe('SQLite Engine Cell Update Refactoring', () => { |
| 8 | + let engine: any; |
| 9 | + |
| 10 | + before(async () => { |
| 11 | + // Initialize with empty DB |
| 12 | + const result = await createDatabaseEngine({ |
| 13 | + content: null, |
| 14 | + maxSize: 0, |
| 15 | + readOnlyMode: false |
| 16 | + }); |
| 17 | + engine = result.operations; |
| 18 | + |
| 19 | + // Setup table |
| 20 | + await engine.executeQuery("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, data TEXT)"); |
| 21 | + await engine.insertRow('users', { id: 1, name: 'Alice', data: '{"score": 10}' }); |
| 22 | + await engine.insertRow('users', { id: 2, name: 'Bob', data: '{"score": 20}' }); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should update single cell', async () => { |
| 26 | + await engine.updateCell('users', 1, 'name', 'AliceUpdated'); |
| 27 | + const result = await engine.executeQuery("SELECT name FROM users WHERE id = 1"); |
| 28 | + assert.strictEqual(result[0].rows[0][0], 'AliceUpdated'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should update single cell with JSON patch', async () => { |
| 32 | + // Apply patch to data column |
| 33 | + await engine.updateCell('users', 1, 'data', null, '{"score": 15}'); |
| 34 | + const result = await engine.executeQuery("SELECT data FROM users WHERE id = 1"); |
| 35 | + const data = JSON.parse(result[0].rows[0][0] as string); |
| 36 | + assert.strictEqual(data.score, 15); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should update multiple cells in batch', async () => { |
| 40 | + const updates: CellUpdate[] = [ |
| 41 | + { rowId: 1, column: 'name', value: 'AliceBatch' }, |
| 42 | + { rowId: 2, column: 'name', value: 'BobBatch' } |
| 43 | + ]; |
| 44 | + await engine.updateCellBatch('users', updates); |
| 45 | + |
| 46 | + const result = await engine.executeQuery("SELECT name FROM users ORDER BY id"); |
| 47 | + assert.strictEqual(result[0].rows[0][0], 'AliceBatch'); |
| 48 | + assert.strictEqual(result[0].rows[1][0], 'BobBatch'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should update multiple cells with JSON patch in batch', async () => { |
| 52 | + const updates: CellUpdate[] = [ |
| 53 | + { rowId: 1, column: 'data', value: '{"score": 25}', operation: 'json_patch' }, |
| 54 | + { rowId: 2, column: 'data', value: '{"score": 30}', operation: 'json_patch' } |
| 55 | + ]; |
| 56 | + await engine.updateCellBatch('users', updates); |
| 57 | + |
| 58 | + const result = await engine.executeQuery("SELECT data FROM users ORDER BY id"); |
| 59 | + const data1 = JSON.parse(result[0].rows[0][0] as string); |
| 60 | + const data2 = JSON.parse(result[0].rows[1][0] as string); |
| 61 | + assert.strictEqual(data1.score, 25); |
| 62 | + assert.strictEqual(data2.score, 30); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should undo batch cell update', async () => { |
| 66 | + // Setup initial state |
| 67 | + await engine.updateCell('users', 1, 'name', 'AlicePreUndo'); |
| 68 | + await engine.updateCell('users', 2, 'name', 'BobPreUndo'); |
| 69 | + |
| 70 | + // Apply batch update (to be undone) |
| 71 | + const updates: CellUpdate[] = [ |
| 72 | + { rowId: 1, column: 'name', value: 'AliceNew' }, |
| 73 | + { rowId: 2, column: 'name', value: 'BobNew' } |
| 74 | + ]; |
| 75 | + await engine.updateCellBatch('users', updates); |
| 76 | + |
| 77 | + // Verify update |
| 78 | + let result = await engine.executeQuery("SELECT name FROM users ORDER BY id"); |
| 79 | + assert.strictEqual(result[0].rows[0][0], 'AliceNew'); |
| 80 | + assert.strictEqual(result[0].rows[1][0], 'BobNew'); |
| 81 | + |
| 82 | + // Undo |
| 83 | + const mod: ModificationEntry = { |
| 84 | + modificationType: 'cell_update', |
| 85 | + targetTable: 'users', |
| 86 | + description: 'Batch update', |
| 87 | + affectedCells: [ |
| 88 | + { rowId: 1, columnName: 'name', priorValue: 'AlicePreUndo', newValue: 'AliceNew' }, |
| 89 | + { rowId: 2, columnName: 'name', priorValue: 'BobPreUndo', newValue: 'BobNew' } |
| 90 | + ] |
| 91 | + }; |
| 92 | + await engine.undoModification(mod); |
| 93 | + |
| 94 | + // Verify undo |
| 95 | + result = await engine.executeQuery("SELECT name FROM users ORDER BY id"); |
| 96 | + assert.strictEqual(result[0].rows[0][0], 'AlicePreUndo'); |
| 97 | + assert.strictEqual(result[0].rows[1][0], 'BobPreUndo'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should fail transaction on error', async () => { |
| 101 | + const updates: CellUpdate[] = [ |
| 102 | + { rowId: 1, column: 'name', value: 'AliceTrans' }, |
| 103 | + // Invalid column to force error |
| 104 | + { rowId: 2, column: 'non_existent_column', value: 'BobTrans' } |
| 105 | + ]; |
| 106 | + |
| 107 | + try { |
| 108 | + await engine.updateCellBatch('users', updates); |
| 109 | + assert.fail('Should have thrown'); |
| 110 | + } catch (e) { |
| 111 | + // Check that first update was rolled back |
| 112 | + const result = await engine.executeQuery("SELECT name FROM users WHERE id = 1"); |
| 113 | + assert.strictEqual(result[0].rows[0][0], 'AlicePreUndo'); // Value from previous test |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + it('should validate rowId in updateCell', async () => { |
| 118 | + try { |
| 119 | + await engine.updateCell('users', 'invalid', 'name', 'test'); |
| 120 | + assert.fail('Should have thrown invalid rowid'); |
| 121 | + } catch (e: any) { |
| 122 | + assert.match(e.message, /Invalid rowid/); |
| 123 | + } |
| 124 | + }); |
| 125 | + |
| 126 | + it('should validate rowId in updateCellBatch', async () => { |
| 127 | + const updates: CellUpdate[] = [ |
| 128 | + { rowId: 'invalid', column: 'name', value: 'test' } |
| 129 | + ]; |
| 130 | + try { |
| 131 | + await engine.updateCellBatch('users', updates); |
| 132 | + assert.fail('Should have thrown invalid rowid'); |
| 133 | + } catch (e: any) { |
| 134 | + assert.match(e.message, /Invalid rowid/); |
| 135 | + } |
| 136 | + }); |
| 137 | +}); |
0 commit comments