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