|
| 1 | +const query = require("../"); |
| 2 | +const { expect } = require("chai"); |
| 3 | +const { omit, cloneDeepWith } = require("lodash"); |
| 4 | + |
| 5 | +describe("Queries", () => { |
| 6 | + describe("Sync Parsing", () => { |
| 7 | + it("should return a single-item parse result for common queries", () => { |
| 8 | + const queries = ["select 1", "select null", "select ''", "select a, b"]; |
| 9 | + const results = queries.map(query.parseQuerySync); |
| 10 | + results.forEach((res) => { |
| 11 | + expect(res.stmts).to.have.lengthOf(1); |
| 12 | + }); |
| 13 | + |
| 14 | + // Do some rough asserting on the shape of the result. |
| 15 | + // These tests aren't really meant to test the parsing functionality |
| 16 | + // itself, but doing a bit for sanity doesn't hurt. |
| 17 | + const selectedDatas = results.map( |
| 18 | + (it) => it.stmts[0].stmt.SelectStmt.targetList |
| 19 | + ); |
| 20 | + |
| 21 | + expect(selectedDatas[0][0].ResTarget.val.A_Const.ival.ival).to.eq( |
| 22 | + 1 |
| 23 | + ); |
| 24 | + expect(selectedDatas[1][0].ResTarget.val.A_Const.isnull).to.eq( |
| 25 | + true |
| 26 | + ); |
| 27 | + expect(selectedDatas[2][0].ResTarget.val.A_Const.sval.sval).to.eq( |
| 28 | + "" |
| 29 | + ); |
| 30 | + expect(selectedDatas[3]).to.have.lengthOf(2); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should support parsing multiple queries", () => { |
| 34 | + const res = query.parseQuerySync("select 1; select null;"); |
| 35 | + const changedProps = [ |
| 36 | + "stmt_len", |
| 37 | + "stmt_location", |
| 38 | + "stmt.SelectStmt.targetList[0].ResTarget.location", |
| 39 | + "stmt.SelectStmt.targetList[0].ResTarget.val.A_Const.location", |
| 40 | + ]; |
| 41 | + const removeChangedProps = (stmt) => omit(stmt, changedProps); |
| 42 | + expect(res.stmts.map(removeChangedProps)).to.deep.eq([ |
| 43 | + ...query.parseQuerySync("select 1;").stmts.map(removeChangedProps), |
| 44 | + ...query.parseQuerySync("select null;").stmts.map(removeChangedProps), |
| 45 | + ]); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should not parse a bogus query", () => { |
| 49 | + expect(() => query.parseQuerySync("NOT A QUERY")).to.throw(Error); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe("Async parsing", () => { |
| 54 | + it("should return a promise resolving to same result", async () => { |
| 55 | + const testQuery = "select * from john;"; |
| 56 | + const resPromise = query.parseQuery(testQuery); |
| 57 | + const res = await resPromise; |
| 58 | + |
| 59 | + expect(resPromise).to.be.instanceof(Promise); |
| 60 | + expect(res).to.deep.eq(query.parseQuerySync(testQuery)); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should reject on bogus queries", async () => { |
| 64 | + return query.parseQuery("NOT A QUERY").then( |
| 65 | + () => { |
| 66 | + throw new Error("should have rejected"); |
| 67 | + }, |
| 68 | + (e) => { |
| 69 | + expect(e).instanceof(Error); |
| 70 | + expect(e.message).to.match(/NOT/); |
| 71 | + } |
| 72 | + ); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe("Fingerprint", () => { |
| 77 | + context("sync", () => { |
| 78 | + it("should not fingerprint a bogus query", () => { |
| 79 | + expect(() => query.fingerprintSync("NOT A QUERY")).to.throw(Error); |
| 80 | + }); |
| 81 | + |
| 82 | + it("should fingerprint a query", () => { |
| 83 | + const queries = ["select 1", "select null", "select ''", "select a, b"]; |
| 84 | + const results = queries.map(query.fingerprintSync); |
| 85 | + |
| 86 | + results.forEach((res) => { |
| 87 | + expect(res).to.have.lengthOf(16); |
| 88 | + }); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + context("async", () => { |
| 93 | + it("should not fingerprint a bogus query", () => { |
| 94 | + return query.fingerprint("NOT A QUERY").then( |
| 95 | + () => { |
| 96 | + throw new Error("should have rejected"); |
| 97 | + }, |
| 98 | + (e) => { |
| 99 | + expect(e).instanceof(Error); |
| 100 | + expect(e.message).to.match(/NOT/); |
| 101 | + } |
| 102 | + ); |
| 103 | + }); |
| 104 | + |
| 105 | + it("should fingerprint a query", async () => { |
| 106 | + const queries = ["select 1", "select null", "select ''", "select a, b"]; |
| 107 | + const results = await Promise.all(queries.map(query.fingerprint)); |
| 108 | + |
| 109 | + results.forEach((res) => { |
| 110 | + expect(res).to.have.lengthOf(16); |
| 111 | + }); |
| 112 | + }); |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
| 116 | + |
| 117 | +describe("PlPgSQL (async)", () => { |
| 118 | + it("should parse a function", async () => { |
| 119 | + const testFunction = ` |
| 120 | + CREATE FUNCTION t() RETURNS trigger AS |
| 121 | + $BODY$ |
| 122 | + DECLARE |
| 123 | + resultVal integer; |
| 124 | + finalVal integer; |
| 125 | + BEGIN |
| 126 | + resultVal = 0; |
| 127 | + IF (resultVal >= 5) |
| 128 | + THEN finalVal = 'Yes'; |
| 129 | + ELSE finalVal = 'No'; |
| 130 | + END IF; |
| 131 | + RETURN finalVal; |
| 132 | + END; |
| 133 | + $BODY$ |
| 134 | + LANGUAGE plpgsql; |
| 135 | + `; |
| 136 | + |
| 137 | + const resPromise = query.parsePlPgSQL(testFunction); |
| 138 | + const res = await resPromise; |
| 139 | + |
| 140 | + expect(resPromise).to.be.instanceof(Promise); |
| 141 | + expect(res).to.deep.have.property("0.PLpgSQL_function"); |
| 142 | + }); |
| 143 | +}); |
0 commit comments