|
| 1 | +import { expect } from "chai"; |
| 2 | +import { TestClazz } from "./models/test-clazz"; |
| 3 | +import { Crud } from "../crud/crud"; |
| 4 | +import { getMapper } from "./mappers-table-new"; |
| 5 | +import { TestClazzRef } from "./models/test-clazz-ref"; |
| 6 | +import { Cidade } from "./models/cidade"; |
| 7 | +import { Uf } from "./models/uf"; |
| 8 | +import { SQLiteDatabase } from "./database/sqlite-database"; |
| 9 | +import { Ddl } from "../ddl/ddl"; |
| 10 | +import { async } from "rxjs/internal/scheduler/async"; |
| 11 | + |
| 12 | +describe("Compile", () => { |
| 13 | + let crud: Crud; |
| 14 | + let ddl: Ddl; |
| 15 | + |
| 16 | + beforeEach(async () => { |
| 17 | + const mapper = getMapper(); |
| 18 | + |
| 19 | + const database = await new SQLiteDatabase().init(); |
| 20 | + crud = new Crud(database, mapper, false); |
| 21 | + ddl = new Ddl(database, mapper, false); |
| 22 | + |
| 23 | + await ddl.create(Cidade).execute().toPromise(); |
| 24 | + await ddl.create(Uf).execute().toPromise(); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(async () => { |
| 28 | + await ddl.drop(Cidade).execute().toPromise(); |
| 29 | + await ddl.drop(Uf).execute().toPromise(); |
| 30 | + }); |
| 31 | + |
| 32 | + // const crud = new Crud({} as any, getMapper()); |
| 33 | + |
| 34 | + const createQueryTestClazzForTest = () => crud.query(TestClazz).select(x => x.internalKey); |
| 35 | + const createQueryCidadeForTest = () => crud.query(Cidade).select(x => x.nome); |
| 36 | + |
| 37 | + it("simple", () => { |
| 38 | + const query = createQueryTestClazzForTest(); |
| 39 | + const result = query.compile(); |
| 40 | + expect(result[0].params.length).to.equal(0); |
| 41 | + expect(result[0].query).to.equal("SELECT tes.internalKey AS internalKey FROM TestClazz AS tes"); |
| 42 | + }); |
| 43 | + |
| 44 | + it("simple with parameter", () => { |
| 45 | + const query = createQueryTestClazzForTest(); |
| 46 | + query.where(where => where.equal(x => x.numero, 13)); |
| 47 | + const result = query.compile(); |
| 48 | + expect(result[0].params.length).to.equal(1); |
| 49 | + expect(result[0].params[0]).to.equal(13); |
| 50 | + expect(result[0].query).to.equal("SELECT tes.internalKey AS internalKey FROM TestClazz AS tes WHERE tes.numero = ?"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("simple with join and parameter", () => { |
| 54 | + const query = createQueryTestClazzForTest(); |
| 55 | + query.where(where => where.equal(x => x.numero, 13)); |
| 56 | + query.join(TestClazzRef, |
| 57 | + on => on.equal(x => x.id, query.ref(x => x.referenceTest.id)), |
| 58 | + join => { |
| 59 | + join.select(x => x.description); |
| 60 | + join.where(where => where.equal(x => x.id, 144)); |
| 61 | + }); |
| 62 | + const result = query.compile(); |
| 63 | + expect(result[0].params.length).to.equal(2); |
| 64 | + expect(result[0].params[0]).to.equal(13); |
| 65 | + expect(result[0].params[1]).to.equal(144); |
| 66 | + expect(result[0].query).to.equal("SELECT tes.internalKey AS internalKey, tes0.description AS tes0_description FROM TestClazz AS tes LEFT JOIN TestClazzRef AS tes0 ON (tes0.id = tes.referenceTest_id) WHERE tes.numero = ? AND tes0.id = ?"); |
| 67 | + }); |
| 68 | + |
| 69 | + it("simple with join, parameter and where default", () => { |
| 70 | + const query = createQueryCidadeForTest(); |
| 71 | + query.where(where => where.less(x => x.population, 20)); |
| 72 | + query.join(Uf, |
| 73 | + on => on.equal(x => x.codeImport, query.ref(x => x.uf.codeImport)), |
| 74 | + join => { |
| 75 | + join.select(x => x.nome); |
| 76 | + join.where(where => where.greatAndEqual(x => x.population, 200)); |
| 77 | + }); |
| 78 | + const result = query.compile(); |
| 79 | + expect(result[0].params.length).to.equal(3); |
| 80 | + expect(result[0].params[0]).to.equal(20); |
| 81 | + expect(result[0].params[1]).to.equal(200); |
| 82 | + expect(result[0].params[2]).to.equal(0); |
| 83 | + expect(result[0].query).to.equal("SELECT cid.nome AS nome, uf.nome AS uf_nome FROM Cidade AS cid LEFT JOIN Uf AS uf ON (uf.codeImport = cid.uf_codeImport) WHERE cid.population < ? AND uf.population >= ? AND (cid.population > ?)"); |
| 84 | + }); |
| 85 | + |
| 86 | + it("multi", () => { |
| 87 | + const query = createQueryTestClazzForTest(); |
| 88 | + const result1 = query.compile(); |
| 89 | + expect(result1[0].params.length).to.equal(0); |
| 90 | + expect(result1[0].query).to.equal("SELECT tes.internalKey AS internalKey FROM TestClazz AS tes"); |
| 91 | + const result2 = query.compile(); |
| 92 | + expect(result2[0].params.length).to.equal(0); |
| 93 | + expect(result2[0].query).to.equal("SELECT tes.internalKey AS internalKey FROM TestClazz AS tes"); |
| 94 | + }); |
| 95 | + |
| 96 | + it("multi with parameter", () => { |
| 97 | + const query = createQueryTestClazzForTest(); |
| 98 | + query.where(where => where.equal(x => x.numero, 13)); |
| 99 | + const result1 = query.compile(); |
| 100 | + expect(result1[0].params.length).to.equal(1); |
| 101 | + expect(result1[0].params[0]).to.equal(13); |
| 102 | + expect(result1[0].query).to.equal("SELECT tes.internalKey AS internalKey FROM TestClazz AS tes WHERE tes.numero = ?"); |
| 103 | + const result2 = query.compile(); |
| 104 | + expect(result2[0].params.length).to.equal(1); |
| 105 | + expect(result2[0].params[0]).to.equal(13); |
| 106 | + expect(result2[0].query).to.equal("SELECT tes.internalKey AS internalKey FROM TestClazz AS tes WHERE tes.numero = ?"); |
| 107 | + }); |
| 108 | + |
| 109 | + it("multi with join and parameter", () => { |
| 110 | + const query = createQueryTestClazzForTest(); |
| 111 | + query.where(where => where.equal(x => x.numero, 13)); |
| 112 | + query.join(TestClazzRef, |
| 113 | + on => on.equal(x => x.id, query.ref(x => x.referenceTest.id)), |
| 114 | + join => { |
| 115 | + join.select(x => x.description); |
| 116 | + join.where(where => where.equal(x => x.id, 144)); |
| 117 | + }); |
| 118 | + const result1 = query.compile(); |
| 119 | + expect(result1[0].params.length).to.equal(2); |
| 120 | + expect(result1[0].params[0]).to.equal(13); |
| 121 | + expect(result1[0].params[1]).to.equal(144); |
| 122 | + expect(result1[0].query).to.equal("SELECT tes.internalKey AS internalKey, tes0.description AS tes0_description FROM TestClazz AS tes LEFT JOIN TestClazzRef AS tes0 ON (tes0.id = tes.referenceTest_id) WHERE tes.numero = ? AND tes0.id = ?"); |
| 123 | + |
| 124 | + const result2 = query.compile(); |
| 125 | + expect(result2[0].params.length).to.equal(2); |
| 126 | + expect(result2[0].params[0]).to.equal(13); |
| 127 | + expect(result2[0].params[1]).to.equal(144); |
| 128 | + expect(result2[0].query).to.equal("SELECT tes.internalKey AS internalKey, tes0.description AS tes0_description FROM TestClazz AS tes LEFT JOIN TestClazzRef AS tes0 ON (tes0.id = tes.referenceTest_id) WHERE tes.numero = ? AND tes0.id = ?"); |
| 129 | + }); |
| 130 | + |
| 131 | + it("multi with join, parameter and where default", async () => { |
| 132 | + const query = createQueryCidadeForTest(); |
| 133 | + query.where(where => where.less(x => x.population, 20)); |
| 134 | + query.join(Uf, |
| 135 | + on => on.equal(x => x.codeImport, query.ref(x => x.uf.codeImport)), |
| 136 | + join => { |
| 137 | + join.enableQueryFilters(); |
| 138 | + join.select(x => x.nome); |
| 139 | + join.where(where => where.greatAndEqual(x => x.population, 200)); |
| 140 | + }); |
| 141 | + const result1 = query.compile(); |
| 142 | + expect(result1[0].params.length).to.equal(3); |
| 143 | + expect(result1[0].params[0]).to.equal(20); |
| 144 | + expect(result1[0].params[1]).to.equal(200); |
| 145 | + expect(result1[0].params[2]).to.equal(0); |
| 146 | + expect(result1[0].query).to.equal("SELECT cid.nome AS nome, uf.nome AS uf_nome FROM Cidade AS cid LEFT JOIN Uf AS uf ON (uf.codeImport = cid.uf_codeImport) WHERE cid.population < ? AND uf.population >= ? AND (cid.population > ?)"); |
| 147 | + |
| 148 | + // const resultExecute = await query.toList().toPromise(); |
| 149 | + // console.log(resultExecute); |
| 150 | + const result2 = query.compile(); |
| 151 | + expect(result2[0].params.length).to.equal(3); |
| 152 | + expect(result2[0].params[0]).to.equal(20); |
| 153 | + expect(result2[0].params[1]).to.equal(200); |
| 154 | + expect(result2[0].params[2]).to.equal(0); |
| 155 | + expect(result2[0].query).to.equal("SELECT cid.nome AS nome, uf.nome AS uf_nome FROM Cidade AS cid LEFT JOIN Uf AS uf ON (uf.codeImport = cid.uf_codeImport) WHERE cid.population < ? AND uf.population >= ? AND (cid.population > ?)"); |
| 156 | + }); |
| 157 | + |
| 158 | +}); |
0 commit comments