|
| 1 | +import assert from 'assert'; |
| 2 | +import config from './config.ts'; |
| 3 | +import { |
| 4 | + createRxDatabase, |
| 5 | + randomToken, |
| 6 | + addRxPlugin, |
| 7 | + prepareQuery, |
| 8 | + normalizeMangoQuery, |
| 9 | + fillWithDefaultSettings, |
| 10 | + clone, |
| 11 | + RxJsonSchema |
| 12 | +} from '../../plugins/core/index.mjs'; |
| 13 | +import { |
| 14 | + getRxStorageLocalstorage, |
| 15 | + getLocalStorageMock |
| 16 | +} from '../../plugins/storage-localstorage/index.mjs'; |
| 17 | +import { wrappedValidateAjvStorage } from '../../plugins/validate-ajv/index.mjs'; |
| 18 | +import { RxDBAttachmentsPlugin } from '../../plugins/attachments/index.mjs'; |
| 19 | + |
| 20 | +addRxPlugin(RxDBAttachmentsPlugin); |
| 21 | + |
| 22 | +/** |
| 23 | + * Tests specific to the localstorage storage where the behavior |
| 24 | + * cannot be observed through the generic rx-storage-implementations |
| 25 | + * test suite. |
| 26 | + */ |
| 27 | +describe('rx-storage-localstorage.test.ts', () => { |
| 28 | + if (config.storage.name !== 'localstorage') { |
| 29 | + return; |
| 30 | + } |
| 31 | + it('remove() must delete all localStorage keys for the database, including attachments', async () => { |
| 32 | + const mock = getLocalStorageMock(); |
| 33 | + const storage = wrappedValidateAjvStorage({ |
| 34 | + storage: getRxStorageLocalstorage({ localStorage: mock }) |
| 35 | + }); |
| 36 | + const dbName = randomToken(10); |
| 37 | + const db = await createRxDatabase({ |
| 38 | + name: dbName, |
| 39 | + storage, |
| 40 | + eventReduce: false |
| 41 | + }); |
| 42 | + |
| 43 | + const cols = await db.addCollections({ |
| 44 | + docs: { |
| 45 | + schema: { |
| 46 | + version: 0, |
| 47 | + primaryKey: 'id', |
| 48 | + type: 'object', |
| 49 | + properties: { |
| 50 | + id: { type: 'string', maxLength: 100 }, |
| 51 | + name: { type: 'string', maxLength: 100 } |
| 52 | + }, |
| 53 | + required: ['id', 'name'], |
| 54 | + attachments: {} |
| 55 | + } |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + const doc = await cols.docs.insert({ id: 'att-test', name: 'Test' }); |
| 60 | + await doc.putAttachment({ |
| 61 | + id: 'myfile.txt', |
| 62 | + data: new Blob(['hello attachment'], { type: 'text/plain' }), |
| 63 | + type: 'text/plain' |
| 64 | + }); |
| 65 | + |
| 66 | + function getDbKeys(): string[] { |
| 67 | + const keys: string[] = []; |
| 68 | + for (let i = 0; i < mock.length; i++) { |
| 69 | + const key = mock.key(i); |
| 70 | + if (key && key.includes(dbName)) { |
| 71 | + keys.push(key); |
| 72 | + } |
| 73 | + } |
| 74 | + return keys; |
| 75 | + } |
| 76 | + |
| 77 | + const keysBefore = getDbKeys(); |
| 78 | + assert.ok( |
| 79 | + keysBefore.some(k => k.includes('attachment')), |
| 80 | + 'should have attachment keys before remove' |
| 81 | + ); |
| 82 | + |
| 83 | + await db.remove(); |
| 84 | + |
| 85 | + const keysAfter = getDbKeys(); |
| 86 | + assert.strictEqual( |
| 87 | + keysAfter.length, |
| 88 | + 0, |
| 89 | + 'remove() must clean up all keys, but found leaked: ' + keysAfter.join(', ') |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + /** |
| 94 | + * count() must respect skip and limit and return the |
| 95 | + * same amount as query().documents.length . |
| 96 | + * The generic query-correctness suite skips count checks |
| 97 | + * when limit/skip is present because some storages implement |
| 98 | + * count() with a different contract, so this regression test |
| 99 | + * lives here. |
| 100 | + */ |
| 101 | + it('count() must return the same amount as query().documents.length (with limit)', async () => { |
| 102 | + const dbName = randomToken(10); |
| 103 | + const db = await createRxDatabase({ |
| 104 | + name: dbName, |
| 105 | + storage: config.storage.getStorage(), |
| 106 | + eventReduce: false |
| 107 | + }); |
| 108 | + const rawSchema: RxJsonSchema<{ id: string; age: number; }> = { |
| 109 | + version: 0, |
| 110 | + primaryKey: 'id', |
| 111 | + type: 'object', |
| 112 | + properties: { |
| 113 | + id: { type: 'string', maxLength: 100 }, |
| 114 | + age: { |
| 115 | + type: 'integer', |
| 116 | + minimum: 0, |
| 117 | + maximum: 150, |
| 118 | + multipleOf: 1 |
| 119 | + } |
| 120 | + }, |
| 121 | + required: ['id', 'age'], |
| 122 | + indexes: ['age'] |
| 123 | + }; |
| 124 | + const cols = await db.addCollections({ humans: { schema: rawSchema } }); |
| 125 | + const col = cols.humans; |
| 126 | + |
| 127 | + await col.bulkInsert( |
| 128 | + new Array(10).fill(0).map((_, i) => ({ id: 'doc-' + i, age: 20 + i })) |
| 129 | + ); |
| 130 | + |
| 131 | + const schema = fillWithDefaultSettings(clone(rawSchema)); |
| 132 | + const preparedQuery = prepareQuery( |
| 133 | + schema, |
| 134 | + normalizeMangoQuery(schema, { |
| 135 | + selector: { |
| 136 | + _deleted: { $eq: false }, |
| 137 | + age: { $gte: 20 } |
| 138 | + } as any, |
| 139 | + sort: [ |
| 140 | + { _deleted: 'asc' as const }, |
| 141 | + { age: 'asc' as const }, |
| 142 | + { id: 'asc' as const } |
| 143 | + ], |
| 144 | + skip: 0, |
| 145 | + limit: 3 |
| 146 | + }) |
| 147 | + ); |
| 148 | + |
| 149 | + const queryResult = await col.storageInstance.query(preparedQuery); |
| 150 | + const countResult = await col.storageInstance.count(preparedQuery); |
| 151 | + |
| 152 | + assert.strictEqual( |
| 153 | + countResult.count, |
| 154 | + queryResult.documents.length, |
| 155 | + 'count() must equal query().documents.length' |
| 156 | + ); |
| 157 | + assert.strictEqual(countResult.count, 3, 'count should be limited to 3'); |
| 158 | + |
| 159 | + await db.close(); |
| 160 | + }); |
| 161 | +}); |
0 commit comments