/plugins/@grouparoo/sqlite/__tests__/integration/log-checking.ts

https://github.com/grouparoo/grouparoo · TypeScript · 77 lines · 65 code · 11 blank · 1 comment · 1 complexity · 584fd850469aacfc53717cda5d0f7165 MD5 · raw file

  1. import { GrouparooRecord, Property } from "@grouparoo/core";
  2. import { helper } from "@grouparoo/spec-helper";
  3. import path from "path";
  4. import { getConnection } from "../../src/lib/query-import/connection";
  5. import { afterData, beforeData, getConfig } from "../utils/data";
  6. process.env.GROUPAROO_INJECTED_PLUGINS = JSON.stringify({
  7. "@grouparoo/sqlite": { path: path.join(__dirname, "..", "..") },
  8. });
  9. const recordProperty = getConnection().methods.recordProperty;
  10. // these used and set by test
  11. const { appOptions, usersTableName } = getConfig();
  12. let record: GrouparooRecord;
  13. let client;
  14. async function getPropertyValue(query: string) {
  15. const propertyOptions = { query };
  16. const property = await Property.findOne();
  17. return recordProperty({
  18. connection: client,
  19. appOptions,
  20. record,
  21. propertyOptions,
  22. property,
  23. recordId: null,
  24. source: null,
  25. sourceId: null,
  26. app: null,
  27. appId: null,
  28. sourceOptions: null,
  29. sourceMapping: null,
  30. propertyId: null,
  31. propertyFilters: null,
  32. });
  33. }
  34. describe("sqlite/integration/log-checking", () => {
  35. helper.grouparooTestServer({ truncate: true, enableTestPlugin: true });
  36. const actionhero = require("actionhero");
  37. const logMock = jest.fn();
  38. Object.defineProperty(actionhero, "log", { value: logMock, writable: false });
  39. beforeAll(async () => {
  40. await helper.factories.properties();
  41. ({ client } = await beforeData());
  42. });
  43. beforeAll(async () => {
  44. record = await helper.factories.record();
  45. await record.addOrUpdateProperties({
  46. userId: [1],
  47. email: ["ejervois0@example.com"],
  48. });
  49. expect(record.id).toBeTruthy();
  50. });
  51. afterAll(async () => await afterData());
  52. it("should correctly do debug logging for sqlite queries", async () => {
  53. const sql = `SELECT first_name FROM "${usersTableName}" WHERE id = {{ userId }}`;
  54. const value = await getPropertyValue(sql);
  55. expect(value).toEqual(["Erie"]);
  56. const [sqlliteCall, debugLevel] = logMock.mock.calls.find(
  57. (c) => c[0].includes("sqlite") && c[0].includes("SELECT first_name")
  58. );
  59. expect(debugLevel).toBe("debug");
  60. expect(sqlliteCall).toEqual(
  61. `[ sqlite ] SELECT first_name FROM "USERS - '${
  62. process.env.JEST_WORKER_ID ?? 0
  63. }'" WHERE id = 1`
  64. );
  65. });
  66. });