MCPcopy
hub / github.com/simstudioai/sim / createMockDb

Function createMockDb

packages/testing/src/mocks/database.mock.ts:260–309  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

258 * Creates a mock database connection.
259 */
260export function createMockDb() {
261 // A `where(...)` result that is both awaitable (resolves to `[]`) and exposes
262 // `.limit`/`.orderBy`, so `select().from()[.leftJoin()].where()[.limit()]`
263 // works whether or not a terminal is chained.
264 const whereResult = () => {
265 const thenable: any = Promise.resolve([])
266 thenable.limit = vi.fn(() => Promise.resolve([]))
267 thenable.orderBy = vi.fn(() => Promise.resolve([]))
268 return thenable
269 }
270 const fromBuilder = () => ({
271 where: vi.fn(whereResult),
272 leftJoin: vi.fn(() => ({ where: vi.fn(whereResult) })),
273 innerJoin: vi.fn(() => ({ where: vi.fn(whereResult) })),
274 })
275
276 return {
277 select: vi.fn(() => ({
278 from: vi.fn(fromBuilder),
279 })),
280 selectDistinct: vi.fn(() => ({
281 from: vi.fn(fromBuilder),
282 })),
283 insert: vi.fn(() => ({
284 values: vi.fn(() => ({
285 returning: vi.fn(() => Promise.resolve([])),
286 onConflictDoUpdate: vi.fn(() => ({
287 returning: vi.fn(() => Promise.resolve([])),
288 })),
289 onConflictDoNothing: vi.fn(() => ({
290 returning: vi.fn(() => Promise.resolve([])),
291 })),
292 })),
293 })),
294 update: vi.fn(() => ({
295 set: vi.fn(() => ({
296 where: vi.fn(() => ({
297 returning: vi.fn(() => Promise.resolve([])),
298 })),
299 })),
300 })),
301 delete: vi.fn(() => ({
302 where: vi.fn(() => ({
303 returning: vi.fn(() => Promise.resolve([])),
304 })),
305 })),
306 transaction: vi.fn(async (callback) => callback(createMockDb())),
307 query: vi.fn(() => Promise.resolve([])),
308 }
309}
310
311/**
312 * Mock module for @sim/db.

Callers 1

database.mock.tsFile · 0.85

Calls 1

resolveMethod · 0.65

Tested by

no test coverage detected