sql-code-generator
Version:
Generate code from your SQL schema and queries for type safety and development speed.
129 lines • 8.38 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const test_fns_1 = require("test-fns");
const directory_1 = require("../../__test_assets__/directory");
const fileIO_1 = require("../utils/fileIO");
const generate_1 = require("./generate");
describe('generate', () => {
describe('mysql', () => {
const testAssetPaths = {
codegenYml: `${directory_1.TEST_ASSETS_ROOT_DIR}/exampleProject/mysql/codegen.sql.yml`,
generatedTypesCode: `${directory_1.TEST_ASSETS_ROOT_DIR}/exampleProject/mysql/src/generated/fromSql/types.ts`,
generatedQueryFunctionsCode: `${directory_1.TEST_ASSETS_ROOT_DIR}/exampleProject/mysql/src/generated/fromSql/queryFunctions.ts`,
};
it('should be able to read the example config provisioned in __test_assets__', () => __awaiter(void 0, void 0, void 0, function* () {
yield (0, generate_1.generate)({
configPath: testAssetPaths.codegenYml,
});
// expect that the types code does not have compile errors
yield Promise.resolve(`${testAssetPaths.generatedTypesCode}`).then(s => __importStar(require(s)));
// expect that the query functions code does not have compile errors
const queryFunctionExports = yield Promise.resolve(`${testAssetPaths.generatedQueryFunctionsCode}`).then(s => __importStar(require(s)));
expect(queryFunctionExports).toHaveProperty('sqlQueryFindAllByName');
// expect the look right
const typesCode = (yield (0, fileIO_1.readFile)(testAssetPaths.generatedTypesCode)).toString();
expect(typesCode).toMatchSnapshot();
// expect the functions look right
const queryFunctionsCode = (yield (0, fileIO_1.readFile)(testAssetPaths.generatedQueryFunctionsCode)).toString();
expect(queryFunctionsCode).toMatchSnapshot();
}));
});
describe('postgres', () => {
const testAssetPaths = {
codegenYml: `${directory_1.TEST_ASSETS_ROOT_DIR}/exampleProject/postgres/codegen.sql.yml`,
generatedTypesCode: `${directory_1.TEST_ASSETS_ROOT_DIR}/exampleProject/postgres/src/generated/fromSql/types.ts`,
generatedQueryFunctionsCode: `${directory_1.TEST_ASSETS_ROOT_DIR}/exampleProject/postgres/src/generated/fromSql/queryFunctions.ts`,
};
it('should be able to read the example config provisioned in __test_assets__', () => __awaiter(void 0, void 0, void 0, function* () {
yield (0, generate_1.generate)({
configPath: testAssetPaths.codegenYml,
});
// expect that the types code does not have compile errors
yield Promise.resolve(`${testAssetPaths.generatedTypesCode}`).then(s => __importStar(require(s)));
// expect that the query functions code does not have compile errors
const queryFunctionExports = yield Promise.resolve(`${testAssetPaths.generatedQueryFunctionsCode}`).then(s => __importStar(require(s)));
expect(queryFunctionExports).toHaveProperty('sqlQueryFindAllByName');
// expect the look right
const typesCode = (yield (0, fileIO_1.readFile)(testAssetPaths.generatedTypesCode)).toString();
expect(typesCode).toMatchSnapshot();
// expect the functions look right
const queryFunctionsCode = (yield (0, fileIO_1.readFile)(testAssetPaths.generatedQueryFunctionsCode)).toString();
expect(queryFunctionsCode).toMatchSnapshot();
}));
});
describe('postgres-noqueries', () => {
const testAssetPaths = {
codegenYml: `${directory_1.TEST_ASSETS_ROOT_DIR}/exampleProject/postgres-noqueries/codegen.sql.yml`,
generatedTypesCode: `${directory_1.TEST_ASSETS_ROOT_DIR}/exampleProject/postgres-noqueries/src/generated/fromSql/types.ts`,
generatedQueryFunctionsCode: `${directory_1.TEST_ASSETS_ROOT_DIR}/exampleProject/postgres-noqueries/src/generated/fromSql/queryFunctions.ts`,
};
it('should be able to read the example config provisioned in __test_assets__', () => __awaiter(void 0, void 0, void 0, function* () {
yield (0, generate_1.generate)({
configPath: testAssetPaths.codegenYml,
});
// expect that the types code does not have compile errors
yield Promise.resolve(`${testAssetPaths.generatedTypesCode}`).then(s => __importStar(require(s)));
// expect that the query functions code does not get produced
const error = yield (0, test_fns_1.getError)(Promise.resolve(`${testAssetPaths.generatedQueryFunctionsCode}`).then(s => __importStar(require(s))));
expect(error.message).toContain('Cannot find module');
expect(error.message).toContain('queryFunctions.ts');
// expect the look right
const typesCode = (yield (0, fileIO_1.readFile)(testAssetPaths.generatedTypesCode)).toString();
expect(typesCode).toMatchSnapshot();
}));
});
describe('mysql-dbdump', () => {
const testAssetPaths = {
codegenYml: `${directory_1.TEST_ASSETS_ROOT_DIR}/exampleProject/mysql-dbdump/codegen.sql.yml`,
generatedTypesCode: `${directory_1.TEST_ASSETS_ROOT_DIR}/exampleProject/mysql-dbdump/src/generated/fromSql/types.ts`,
generatedQueryFunctionsCode: `${directory_1.TEST_ASSETS_ROOT_DIR}/exampleProject/mysql-dbdump/src/generated/fromSql/queryFunctions.ts`,
};
it('should be able to read the example config provisioned in __test_assets__', () => __awaiter(void 0, void 0, void 0, function* () {
yield (0, generate_1.generate)({
configPath: testAssetPaths.codegenYml,
});
// expect that the types code does not have compile errors
yield Promise.resolve(`${testAssetPaths.generatedTypesCode}`).then(s => __importStar(require(s)));
// expect that the query functions code does not get produced, since not asked for in the config
const error = yield (0, test_fns_1.getError)(Promise.resolve(`${testAssetPaths.generatedQueryFunctionsCode}`).then(s => __importStar(require(s))));
expect(error.message).toContain('Cannot find module');
expect(error.message).toContain('queryFunctions.ts');
// expect the look right
const typesCode = (yield (0, fileIO_1.readFile)(testAssetPaths.generatedTypesCode)).toString();
expect(typesCode).toMatchSnapshot();
}));
});
});
//# sourceMappingURL=generate.integration.test.js.map