Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
fix: update unit tests to new cli
Browse files Browse the repository at this point in the history
  • Loading branch information
wtrocki committed Aug 21, 2020
1 parent 7a1ac22 commit 353baff
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@
}
}
}
}
}
74 changes: 43 additions & 31 deletions packages/datastore/cli/src/generate-documents/createJsonSchema.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,61 @@
import { ModelDefinition, getPrimaryKey } from "@graphback/core";
import {
isNonNullType,
GraphQLOutputType,
getNullableType
isNonNullType,
GraphQLOutputType,
getNullableType
} from "graphql";
import { convertToTsType } from "../utils";


const getFieldParameters = (fieldName: string, type: GraphQLOutputType): any => {
const options: any = {};
const options: any = {};

// TODO handle relationships
// TODO handle relationships

options.key = fieldName;
options.key = fieldName;

if (isNonNullType(type)) {
type = getNullableType(type);
options.isRequired = true;
}
if (isNonNullType(type)) {
type = getNullableType(type);
options.isRequired = true;
}

return { type: convertToTsType(type), ...options };
return { type: convertToTsType(type), ...options };
};

const getModelProperties = (model: ModelDefinition, primaryKey: string) => {
const fieldMap = model.graphqlType.getFields();

return Object.keys(fieldMap)
.map(fieldName => {
const fieldOptions = getFieldParameters(fieldName, fieldMap[fieldName].type);
if (fieldName === primaryKey) {
fieldOptions.primary = true;
}
return { [fieldName]: fieldOptions };
})
.reduce((prev, current) => ({ ...prev, ...current }), {});
const fieldMap = model.graphqlType.getFields();

const generatedProperties = Object.keys(fieldMap)
.map(fieldName => {
const fieldOptions = getFieldParameters(fieldName, fieldMap[fieldName].type);
if (fieldName === primaryKey) {
fieldOptions.primary = true;
}
return { [fieldName]: fieldOptions };
})
.reduce((prev, current) => ({ ...prev, ...current }), {});

generatedProperties._version = {
type: "string",
key: "_version",
isRequired: true
};
generatedProperties._deleted = {
type: "string",
key: "_version",
isRequired: true
};
return generatedProperties;
};

export const createJsonSchema = (model: ModelDefinition) => {
const primaryKey = getPrimaryKey(model.graphqlType).name;

return {
name: model.graphqlType.name,
version: 1,
type: "object",
primaryKey: primaryKey,
properties: getModelProperties(model, primaryKey)
};
const primaryKey = getPrimaryKey(model.graphqlType).name;

return {
name: model.graphqlType.name,
version: 1,
type: "object",
primaryKey: primaryKey,
properties: getModelProperties(model, primaryKey)
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export const createModelType = (model: ModelDefinition) => {

return `export interface ${modelName} {
${getModelProperties(model)}
_version?: number;
_deleted?: boolean;
_version: number;
_deleted: boolean;
}
export type ${modelName}Create = ${primaryKey ? `Omit<${modelName}, "${primaryKey}">` : modelName};
Expand Down
27 changes: 15 additions & 12 deletions packages/datastore/cli/tests/Cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import * as util from 'util';
import { mkdirSync, rmdirSync, readFileSync } from "fs";
import { mkdirSync, rmdirSync, readFileSync, existsSync } from "fs";
import { join } from 'path';

beforeEach(() => mkdirSync("./output/"));
beforeEach(() => {
if (!existsSync("./output/"))
mkdirSync("./output/")
});
afterEach(() => rmdirSync("./output/", { recursive: true }));

test("generate", async () => {
const exec = util.promisify(require('child_process').exec);
const cliExec = join(__dirname, '../dist/index.js');
const schemaPath = join(__dirname, './mock.graphql');
const exec = util.promisify(require('child_process').exec);
const cliExec = join(__dirname, '../bin/offix.js');
const schemaPath = join(__dirname, './mock.graphql');

await exec(`node ${cliExec} generate --schema ${schemaPath} --outputPath output/`);
const schema = readFileSync("./output/schema.json").toString();
const index = readFileSync("./output/index.ts").toString();
const types = readFileSync("./output/types.ts").toString();
await exec(`node ${cliExec} generate --schema ${schemaPath} --outputPath output/`);
const schema = readFileSync("./output/schema.json").toString();
const index = readFileSync("./output/index.ts").toString();
const types = readFileSync("./output/types.ts").toString();

expect(types).toMatchSnapshot();
expect(schema).toMatchSnapshot();
expect(index).toMatchSnapshot();
expect(types).toMatchSnapshot();
expect(schema).toMatchSnapshot();
expect(index).toMatchSnapshot();
});
28 changes: 24 additions & 4 deletions packages/datastore/cli/tests/__snapshots__/Cli.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ exports[`generate 1`] = `
title: string;
description: string;
comments: string[]
_version?: number;
_deleted?: boolean;
_version: number;
_deleted: boolean;
}
export type NoteCreate = Omit<Note, \\"_id\\">;
Expand All @@ -18,8 +18,8 @@ export interface Comment {
title: string;
description: string;
noteComment: string
_version?: number;
_deleted?: boolean;
_version: number;
_deleted: boolean;
}
export type CommentCreate = Omit<Comment, \\"_id\\">;
Expand Down Expand Up @@ -55,6 +55,16 @@ exports[`generate 2`] = `
\\"type\\": \\"string[]\\",
\\"key\\": \\"comments\\",
\\"isRequired\\": true
},
\\"_version\\": {
\\"type\\": \\"string\\",
\\"key\\": \\"_version\\",
\\"isRequired\\": true
},
\\"_deleted\\": {
\\"type\\": \\"string\\",
\\"key\\": \\"_version\\",
\\"isRequired\\": true
}
}
},
Expand Down Expand Up @@ -84,6 +94,16 @@ exports[`generate 2`] = `
\\"type\\": \\"string\\",
\\"key\\": \\"noteComment\\",
\\"isRequired\\": true
},
\\"_version\\": {
\\"type\\": \\"string\\",
\\"key\\": \\"_version\\",
\\"isRequired\\": true
},
\\"_deleted\\": {
\\"type\\": \\"string\\",
\\"key\\": \\"_version\\",
\\"isRequired\\": true
}
}
}
Expand Down

0 comments on commit 353baff

Please sign in to comment.