| 101 | }); |
| 102 | |
| 103 | export function tests() { |
| 104 | describe('common_cache', () => { |
| 105 | beforeEach(async (ctx) => { |
| 106 | const { db, dbGlobalCached } = ctx.cachedSqlite; |
| 107 | await db.run(sql`drop table if exists users`); |
| 108 | await db.run(sql`drop table if exists posts`); |
| 109 | await db.$cache?.invalidate({ tables: 'users' }); |
| 110 | await dbGlobalCached.$cache?.invalidate({ tables: 'users' }); |
| 111 | // public users |
| 112 | await db.run( |
| 113 | sql` |
| 114 | create table users ( |
| 115 | id integer primary key AUTOINCREMENT, |
| 116 | name text not null, |
| 117 | verified integer not null default 0, |
| 118 | jsonb text, |
| 119 | created_at integer |
| 120 | ) |
| 121 | `, |
| 122 | ); |
| 123 | await db.run( |
| 124 | sql` |
| 125 | create table posts ( |
| 126 | id integer primary key AUTOINCREMENT, |
| 127 | description text not null, |
| 128 | user_id int |
| 129 | ) |
| 130 | `, |
| 131 | ); |
| 132 | }); |
| 133 | |
| 134 | test('test force invalidate', async (ctx) => { |
| 135 | const { db } = ctx.cachedSqlite; |
| 136 | |
| 137 | const spyInvalidate = vi.spyOn(db.$cache, 'invalidate'); |
| 138 | await db.$cache?.invalidate({ tables: 'users' }); |
| 139 | expect(spyInvalidate).toHaveBeenCalledTimes(1); |
| 140 | }); |
| 141 | |
| 142 | test('default global config - no cache should be hit', async (ctx) => { |
| 143 | const { db } = ctx.cachedSqlite; |
| 144 | |
| 145 | // @ts-expect-error |
| 146 | const spyPut = vi.spyOn(db.$cache, 'put'); |
| 147 | // @ts-expect-error |
| 148 | const spyGet = vi.spyOn(db.$cache, 'get'); |
| 149 | // @ts-expect-error |
| 150 | const spyInvalidate = vi.spyOn(db.$cache, 'onMutate'); |
| 151 | |
| 152 | await db.select().from(usersTable); |
| 153 | |
| 154 | expect(spyPut).toHaveBeenCalledTimes(0); |
| 155 | expect(spyGet).toHaveBeenCalledTimes(0); |
| 156 | expect(spyInvalidate).toHaveBeenCalledTimes(0); |
| 157 | }); |
| 158 | |
| 159 | test('default global config + enable cache on select: get, put', async (ctx) => { |
| 160 | const { db } = ctx.cachedSqlite; |