Skip to content

Commit

Permalink
test: cover transformDMMF with a test
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Korzunin committed Sep 14, 2021
1 parent 77afaa1 commit 74a9b25
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 29 deletions.
26 changes: 13 additions & 13 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 19 additions & 16 deletions src/generator/transformDMMF.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -14,20 +14,23 @@ export function transformDMMF(dmmf: DMMF.Document) {
.map((n) => `'${n}'`)
.join(', ')})`;

const scalarMorphism = morphism<Schema<ScalarProperties, DMMF.Field>>({
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<ScalarProperties, DMMF.Field>(
{
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<Schema<RelationProperties, DMMF.Field>>({
as: 'name',
Expand All @@ -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',
Expand Down
47 changes: 47 additions & 0 deletions tests/generator/transformDMMF.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 74a9b25

Please sign in to comment.