|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { generateQueryKey, getQueryStore, QueryStore } from '../src/QueryStore'; |
| 3 | +import { AbstractPowerSyncDatabase, SQLWatchOptions } from '@powersync/common'; |
| 4 | + |
| 5 | +describe('QueryStore', () => { |
| 6 | + describe('generateQueryKey', () => { |
| 7 | + it('should generate the correct key for given inputs', () => { |
| 8 | + const sqlStatement = 'SELECT * FROM users WHERE id = ?'; |
| 9 | + const parameters = [1]; |
| 10 | + const options: SQLWatchOptions = { tables: ['users'] }; |
| 11 | + |
| 12 | + const key = generateQueryKey(sqlStatement, parameters, options); |
| 13 | + expect(key).toBe('SELECT * FROM users WHERE id = ? -- [1] -- {"tables":["users"]}'); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should generate different keys for different parameters', () => { |
| 17 | + const sqlStatement = 'SELECT * FROM users WHERE id = ?'; |
| 18 | + const parameters1 = [1]; |
| 19 | + const parameters2 = [2]; |
| 20 | + const options = {}; |
| 21 | + |
| 22 | + const key1 = generateQueryKey(sqlStatement, parameters1, options); |
| 23 | + const key2 = generateQueryKey(sqlStatement, parameters2, options); |
| 24 | + |
| 25 | + expect(key1).not.toBe(key2); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should generate different keys for different options', () => { |
| 29 | + const sqlStatement = 'SELECT * FROM users WHERE id = ?'; |
| 30 | + const parameters = [1]; |
| 31 | + const options1: SQLWatchOptions = { tables: ['users'] }; |
| 32 | + const options2: SQLWatchOptions = { tables: ['local_users'] }; |
| 33 | + |
| 34 | + const key1 = generateQueryKey(sqlStatement, parameters, options1); |
| 35 | + const key2 = generateQueryKey(sqlStatement, parameters, options2); |
| 36 | + |
| 37 | + expect(key1).not.toBe(key2); |
| 38 | + }); |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +describe('QueryStore', () => { |
| 43 | + let db: AbstractPowerSyncDatabase; |
| 44 | + let store: QueryStore; |
| 45 | + let query: any; |
| 46 | + let options: SQLWatchOptions; |
| 47 | + |
| 48 | + beforeEach(() => { |
| 49 | + db = createMockDatabase(); |
| 50 | + store = new QueryStore(db); |
| 51 | + query = {}; |
| 52 | + options = {}; |
| 53 | + }); |
| 54 | + |
| 55 | + it('should return cached query if available', () => { |
| 56 | + const key = 'test-key'; |
| 57 | + const watchedQuery1 = store.getQuery(key, query, options); |
| 58 | + const watchedQuery2 = store.getQuery(key, query, options); |
| 59 | + |
| 60 | + expect(watchedQuery1).toBe(watchedQuery2); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should create new query if not cached', () => { |
| 64 | + const key1 = 'test-key-1'; |
| 65 | + const key2 = 'test-key-2'; |
| 66 | + |
| 67 | + const watchedQuery1 = store.getQuery(key1, query, options); |
| 68 | + const watchedQuery2 = store.getQuery(key2, query, options); |
| 69 | + |
| 70 | + expect(watchedQuery1).not.toBe(watchedQuery2); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +describe('getQueryStore', () => { |
| 75 | + it('should return the same store for the same database', () => { |
| 76 | + const db = createMockDatabase(); |
| 77 | + const store1 = getQueryStore(db); |
| 78 | + const store2 = getQueryStore(db); |
| 79 | + |
| 80 | + expect(store1).toBe(store2); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should return different stores for different databases', () => { |
| 84 | + const db1 = createMockDatabase(); |
| 85 | + const db2 = createMockDatabase(); |
| 86 | + const store1 = getQueryStore(db1); |
| 87 | + const store2 = getQueryStore(db2); |
| 88 | + |
| 89 | + expect(store1).not.toBe(store2); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +function createMockDatabase() { |
| 94 | + return {} as AbstractPowerSyncDatabase; |
| 95 | +} |
0 commit comments