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