All files / myorm/__tests__ mock-db.ts

84% Statements 21/25
62.5% Branches 5/8
87.5% Functions 7/8
84% Lines 21/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124                                  5x   5x 27x                 27x           27x           5x 214x   107x   107x         107x                   107x                   107x           107x               107x           107x                             5x 5x   5x 5x 5x   5x                    
import { promisify } from "util";
import sqlite3 from "sqlite3";
import * as SQLite from "expo-sqlite";
 
/**
 * Setup for a NodeJS based SQLite instance
 *
 * This allows for testing SQL queries and function calls to the ORM
 * without the need for the emulator and Expo application to be running
 *
 * Each function is replaced with an sqlite3 equivalent and return values
 * are formatted to meet expectations of regular implementation.
 *
 * The database is stored in memory.
 *
 *
 */
const sqliteDb = new sqlite3.Database(":memory:"); // Create a new temporary database in memory
 
const allAsync = (...args: [string] | [string, any[]]) => {
  Iif (args.length === 1) {
    // If no parameters are provided, run query without parameters
    return promisify(
      (query: string, callback: (err: Error | null, rows: any[]) => void) => {
        sqliteDb.all(query, callback);
      }
    )(args[0]);
  } else {
    // If parameters are provided, run query with parameters
    return promisify(
      (
        query: string,
        params: any[],
        callback: (err: Error | null, rows: any[]) => void
      ) => {
        sqliteDb.all(query, params, callback);
      }
    )(args[0], args[1]);
  }
};
 
const runAsync = (sql: string, ...params: any[]) => {
  if (params.length === 0) {
    // If no parameters are provided, run query without parameters
    return promisify(
      (query: string, callback: (err: Error | null, rows: any[]) => void) => {
        sqliteDb.run(query, callback); // Executes the query with no parameters
      }
    )(sql);
  } else {
    // If parameters are provided, run query with parameters
    return promisify(
      (
        query: string,
        params: any[],
        callback: (
          err: Error | null,
          result: { lastInsertRowId: number; changes: number }
        ) => void
      ) => {
        // Run the query with the provided parameters
        sqliteDb.run(
          query,
          params,
          (
            err: Error | null,
            result: {
              changes: number;
              lastInsertRowId: number;
            }
          ) => {
            Iif (err) {
              throw new Error(
                `Entity: callback failed in runAsync test function: ${err}`
              );
            } else {
              // After the query completes, retrieve the last insert ID
              sqliteDb.get(
                "SELECT last_insert_rowid() AS id;",
                (
                  err: any,
                  row: {
                    id: number;
                  }
                ) => {
                  Iif (err) {
                    throw new Error(
                      "Entity: callback failed in runAsync test function"
                    );
                  } else {
                    // Return the result including the lastInsertRowId
                    callback(null, {
                      ...result,
                      lastInsertRowId: row?.id,
                    });
                  }
                }
              );
            }
          }
        );
      }
    )(sql, params);
  }
};
 
const getAsync = promisify(sqliteDb.get).bind(sqliteDb);
const execAsync = promisify(sqliteDb.exec).bind(sqliteDb);
 
const execSync = sqliteDb.exec.bind(sqliteDb);
const allSync = sqliteDb.all.bind(sqliteDb);
const runSync = sqliteDb.run.bind(sqliteDb);
 
export const mockdb = {
  execAsync: execAsync,
  getAllAsync: allAsync,
  getFirstAsync: getAsync,
  runAsync: runAsync,
  execSync: execSync,
  getAllSync: allSync,
}