From 74a9b25fdb879efe145d955e02e594f023440451 Mon Sep 17 00:00:00 2001 From: Victor Korzunin Date: Tue, 14 Sep 2021 23:24:52 +0200 Subject: [PATCH] test: cover transformDMMF with a test --- .github/workflows/release.yml | 26 +++++++-------- src/generator/transformDMMF.ts | 35 +++++++++++--------- tests/generator/transformDMMF.test.ts | 47 +++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 29 deletions(-) create mode 100644 tests/generator/transformDMMF.test.ts diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 70ba33b..5586029 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -20,19 +20,19 @@ jobs: - run: npm install - run: npm test - run: npm run build --if-present - # coverage: - # needs: test - # runs-on: ubuntu-latest - # steps: - # - uses: actions/checkout@v2 - # - uses: actions/setup-node@v1 - # with: - # node-version: 14 - # - run: npm install - # - run: npm run test:coverage - # - uses: coverallsapp/github-action@master - # with: - # github-token: ${{ secrets.GITHUB_TOKEN }} + coverage: + needs: test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: 14 + - run: npm install + - run: npm run test:coverage + - uses: coverallsapp/github-action@master + with: + github-token: ${{ secrets.GITHUB_TOKEN }} publish: needs: test runs-on: ubuntu-latest diff --git a/src/generator/transformDMMF.ts b/src/generator/transformDMMF.ts index 5d8869e..943bc85 100644 --- a/src/generator/transformDMMF.ts +++ b/src/generator/transformDMMF.ts @@ -1,4 +1,4 @@ -import { morphism, Schema } from 'morphism'; +import { morphism, Schema, createSchema } from 'morphism'; import R from 'ramda'; import { included, isNotEmpty, notIncluded } from './helpers'; @@ -14,20 +14,23 @@ export function transformDMMF(dmmf: DMMF.Document) { .map((n) => `'${n}'`) .join(', ')})`; - const scalarMorphism = morphism>({ - isList: 'isList', - hasDefaultValue: 'hasDefaultValue', - default: 'default', - isId: 'isId', - isUnique: 'isUnique', - fieldName: 'name', - type: (field: DMMF.Field) => - field.kind === 'scalar' - ? R.prop(field.type, PrismaTypeToSequelizeType) - : enumValuesToString(R.prop(field.type, enumIndex)), - allowNull: { path: 'isRequired', fn: R.not }, - isAutoincrement: R.allPass([R.prop('hasDefaultValue'), R.pathEq(['default', 'name'], 'autoincrement')]), - }); + const scalarSchema = createSchema( + { + isList: 'isList', + hasDefaultValue: 'hasDefaultValue', + default: 'default', + isId: 'isId', + isUnique: 'isUnique', + fieldName: 'name', + type: (field: DMMF.Field) => + field.kind === 'scalar' + ? R.prop(field.type, PrismaTypeToSequelizeType) + : enumValuesToString(R.prop(field.type, enumIndex)), + allowNull: { path: 'isRequired', fn: R.not }, + isAutoincrement: R.allPass([R.prop('hasDefaultValue'), R.pathEq(['default', 'name'], 'autoincrement')]), + }, + { undefinedValues: { strip: true } } + ); const relationMorphism = morphism>({ as: 'name', @@ -48,7 +51,7 @@ export function transformDMMF(dmmf: DMMF.Document) { R.propSatisfies(notIncluded(['createdAt', 'updatedAt', 'deletedAt']), 'name'), ]), fields - ).map(scalarMorphism), + ).map(morphism(scalarSchema)), }, belongsToFields: { path: 'fields', diff --git a/tests/generator/transformDMMF.test.ts b/tests/generator/transformDMMF.test.ts new file mode 100644 index 0000000..8ca5375 --- /dev/null +++ b/tests/generator/transformDMMF.test.ts @@ -0,0 +1,47 @@ +import { getDMMF } from '@prisma/sdk'; +import { PrismaTypeToSequelizeType } from '../../src/generator/properties'; + +import { transformDMMF } from '../../src/generator/transformDMMF'; + +describe('given transformDMMF,', () => { + it('should transform properly.', async () => { + expect.assertions(1); + + const datamodel = ` + model User { + id String @id + } + `; + const expectedProperties = { + models: [ + { + modelName: 'User', + dbName: null, + scalarFields: [ + { + fieldName: 'id', + allowNull: false, + hasDefaultValue: false, + isAutoincrement: false, + isId: true, + isList: false, + isUnique: false, + type: PrismaTypeToSequelizeType.String, + }, + ], + belongsToFields: [], + hasManyFields: [], + hasOneFields: [], + hasCreatedAt: false, + hasUpdatedAt: false, + hasDeletedAt: false, + }, + ], + }; + + const dmmf = await getDMMF({ datamodel }); + const result = transformDMMF(dmmf); + + expect(result).toStrictEqual(expectedProperties); + }); +});