|
| 1 | +import * as fs from "fs"; |
| 2 | +import { SqlSimpleParser } from "../../src/index"; |
| 3 | +import { getExpected, updateExpected } from "../utils/helper"; |
| 4 | +const dataSource = "ToModel"; |
| 5 | +describe("Example Sql Parsing", () => { |
| 6 | + it("Run Parser Sqlite Ex", async () => { |
| 7 | + const filePath = "examples/chinook-database-2.0.1_sqlite.sql"; |
| 8 | + // load sql |
| 9 | + var sql = await fs.readFileSync(filePath, "utf8"); |
| 10 | + // console.log(sql); |
| 11 | + |
| 12 | + // run parser |
| 13 | + const parser = new SqlSimpleParser("sqlite"); |
| 14 | + |
| 15 | + // get models |
| 16 | + const result = parser.feed(sql).ToModel(); |
| 17 | + |
| 18 | + // write to json file |
| 19 | + const dataKey = expect.getState().currentTestName || "unknown"; |
| 20 | + |
| 21 | + const expectedResult = await getExpected(dataSource, dataKey); |
| 22 | + |
| 23 | + if (result != expectedResult) { |
| 24 | + await updateExpected(dataSource, dataKey, result); |
| 25 | + } |
| 26 | + |
| 27 | + // console.log(result); |
| 28 | + expect(result).toStrictEqual(expectedResult); |
| 29 | + // await fs.writeFileSync( |
| 30 | + // "output-sqlite.json", |
| 31 | + // JSON.stringify(models, null, "\t") |
| 32 | + // ); |
| 33 | + |
| 34 | + expect(1).toBeTruthy(); |
| 35 | + }); |
| 36 | + it("Run Parser mysql Ex", async () => { |
| 37 | + const filePath = "examples/adventureworks_mysql.sql"; |
| 38 | + // load sql |
| 39 | + var sql = await fs.readFileSync(filePath, "utf8"); |
| 40 | + // console.log(sql); |
| 41 | + |
| 42 | + // run parser |
| 43 | + const parser = new SqlSimpleParser("mysql"); |
| 44 | + |
| 45 | + // get models |
| 46 | + const result = parser.feed(sql).ToModel(); |
| 47 | + |
| 48 | + // write to json file |
| 49 | + const dataKey = expect.getState().currentTestName || "unknown"; |
| 50 | + |
| 51 | + const expectedResult = await getExpected(dataSource, dataKey); |
| 52 | + |
| 53 | + if (result != expectedResult) { |
| 54 | + await updateExpected(dataSource, dataKey, result); |
| 55 | + } |
| 56 | + |
| 57 | + // console.log(result); |
| 58 | + expect(result).toStrictEqual(expectedResult); |
| 59 | + |
| 60 | + // write to json file |
| 61 | + // await fs.writeFileSync( |
| 62 | + // "output-mysql.json", |
| 63 | + // JSON.stringify(models, null, "\t") |
| 64 | + // ); |
| 65 | + expect(1).toBeTruthy(); |
| 66 | + }); |
| 67 | + it("Run Parser postgres Ex", async () => { |
| 68 | + const filePath = "examples/adventureworks_pg.sql"; |
| 69 | + // load sql |
| 70 | + var sql = await fs.readFileSync(filePath, "utf8"); |
| 71 | + // console.log(sql); |
| 72 | + |
| 73 | + // run parser |
| 74 | + const parser = new SqlSimpleParser("postgres"); |
| 75 | + |
| 76 | + // get models |
| 77 | + const result = parser.feed(sql).ToModel(); |
| 78 | + |
| 79 | + // write to json file |
| 80 | + const dataKey = expect.getState().currentTestName || "unknown"; |
| 81 | + |
| 82 | + const expectedResult = await getExpected(dataSource, dataKey); |
| 83 | + |
| 84 | + if (result != expectedResult) { |
| 85 | + await updateExpected(dataSource, dataKey, result); |
| 86 | + } |
| 87 | + |
| 88 | + // console.log(result); |
| 89 | + expect(result).toStrictEqual(expectedResult); |
| 90 | + |
| 91 | + // write to json file |
| 92 | + // await fs.writeFileSync( |
| 93 | + // "output-pg.json", |
| 94 | + // JSON.stringify(models, null, "\t") |
| 95 | + // ); |
| 96 | + expect(1).toBeTruthy(); |
| 97 | + }); |
| 98 | + it("Run Parser sqlserver Ex", async () => { |
| 99 | + const filePath = "examples/adventureworks_mssql.sql"; |
| 100 | + // load sql |
| 101 | + var sql = await fs.readFileSync(filePath, "utf8"); |
| 102 | + // console.log(sql); |
| 103 | + |
| 104 | + // run parser |
| 105 | + const parser = new SqlSimpleParser("sqlserver"); |
| 106 | + |
| 107 | + // get models |
| 108 | + const result = parser.feed(sql).ToModel(); |
| 109 | + |
| 110 | + // write to json file |
| 111 | + const dataKey = expect.getState().currentTestName || "unknown"; |
| 112 | + |
| 113 | + const expectedResult = await getExpected(dataSource, dataKey); |
| 114 | + |
| 115 | + if (result != expectedResult) { |
| 116 | + await updateExpected(dataSource, dataKey, result); |
| 117 | + } |
| 118 | + |
| 119 | + // console.log(result); |
| 120 | + expect(result).toStrictEqual(expectedResult); |
| 121 | + |
| 122 | + // write to json file |
| 123 | + // await fs.writeFileSync( |
| 124 | + // "output-mssql.json", |
| 125 | + // JSON.stringify(models, null, "\t") |
| 126 | + // ); |
| 127 | + expect(1).toBeTruthy(); |
| 128 | + }); |
| 129 | + |
| 130 | + it("Run Parser postgres simple", async () => { |
| 131 | + // load sql |
| 132 | + var sql = `CREATE TABLE "humanresources_department" ( |
| 133 | + "departmentid" serial NOT NULL, |
| 134 | + "name" Name NOT NULL, |
| 135 | + "groupname" Name NOT NULL, |
| 136 | + "modifieddate" timestamp NOT NULL, |
| 137 | + PRIMARY KEY("departmentid") |
| 138 | + );`; |
| 139 | + // console.log(sql); |
| 140 | + |
| 141 | + // run parser |
| 142 | + const parser = new SqlSimpleParser("postgres"); |
| 143 | + |
| 144 | + // get models |
| 145 | + const result = parser.feed(sql).ToModel(); |
| 146 | + |
| 147 | + // write to json file |
| 148 | + const dataKey = expect.getState().currentTestName || "unknown"; |
| 149 | + |
| 150 | + const expectedResult = await getExpected(dataSource, dataKey); |
| 151 | + |
| 152 | + if (result != expectedResult) { |
| 153 | + await updateExpected(dataSource, dataKey, result); |
| 154 | + } |
| 155 | + |
| 156 | + // console.log(result); |
| 157 | + expect(result).toStrictEqual(expectedResult); |
| 158 | + |
| 159 | + // write to json file |
| 160 | + // await fs.writeFileSync( |
| 161 | + // "output-pg_simple.json", |
| 162 | + // JSON.stringify(models, null, "\t") |
| 163 | + // ); |
| 164 | + expect(1).toBeTruthy(); |
| 165 | + }); |
| 166 | +}); |
0 commit comments