From bfb8cf1a13e54e217fdc4d9608a333bf818f3a05 Mon Sep 17 00:00:00 2001 From: lastlink Date: Sat, 17 Aug 2024 21:34:14 -0400 Subject: [PATCH 1/2] add more tests --- eslint.config.mjs | 7 +- package.json | 4 +- src/utils/sharedUtils.ts | 2 +- tests/utils/nosqlUtils.spec.ts | 114 +++++++++++++ tests/utils/sharedUtils.spec.ts | 272 ++++++++++++++++++++++++-------- 5 files changed, 326 insertions(+), 73 deletions(-) create mode 100644 tests/utils/nosqlUtils.spec.ts diff --git a/eslint.config.mjs b/eslint.config.mjs index 8d0f128..bb9576f 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -15,6 +15,7 @@ export default [ "@typescript-eslint/no-unsafe-function-type": "off", "@typescript-eslint/no-explicit-any": "off", "no-useless-escape": "off", + "no-control-regex": "off", "no-multiple-empty-lines": "error", "no-use-before-define": "off", "@typescript-eslint/no-non-null-assertion": "off", @@ -32,9 +33,9 @@ export default [ }, { files: ["**/__tests__/*.{j,t}s?(x)", "**/tests/**/*.spec.{j,t}s?(x)"], - env: { - jest: true, - }, + // env: { + // jest: true, + // }, }, { languageOptions: { globals: globals.browser } } ]; diff --git a/package.json b/package.json index 8b3724c..be3f986 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,8 @@ "test": "jest -c ./jest.config.ts --forceExit --verbose -i --no-cache --detectOpenHandles", "test:coverage": "jest --forceExit --coverage --verbose --detectOpenHandles", "test:watch": "jest --watchAll --detectOpenHandles", - "lint": "tsc --noEmit && eslint \"{src,client}/**/*.{js,ts}\"", - "lint:fix": "tsc --noEmit && eslint \"{src,client}/**/*.{js,ts}\" --fix", + "lint": "tsc --noEmit && eslint \"{src,client,tests}/**/*.{js,ts}\"", + "lint:fix": "tsc --noEmit && eslint \"{src,client,tests}/**/*.{js,ts}\" --fix", "create": "npm run build && npm run cli:test", "build": "tsc && npm run build:types", "build:types": "tsc -p tsconfig.json", diff --git a/src/utils/sharedUtils.ts b/src/utils/sharedUtils.ts index 6caded3..174bf27 100644 --- a/src/utils/sharedUtils.ts +++ b/src/utils/sharedUtils.ts @@ -75,7 +75,7 @@ export function dbTypeEnds(label: string): string { * @returns */ export function RemoveNameQuantifiers(name: string) { - return name.replace(/\[|\]|\(|\"|\'|\`/g, "").trim(); + return name.replace(/\[|\]|\(|\)|\"|\'|\`/g, "").trim(); } /** diff --git a/tests/utils/nosqlUtils.spec.ts b/tests/utils/nosqlUtils.spec.ts new file mode 100644 index 0000000..a6a96e6 --- /dev/null +++ b/tests/utils/nosqlUtils.spec.ts @@ -0,0 +1,114 @@ +//typescript +import { + dbToOpenApi, + GeneratePropertyModel, + ConvertOpenApiToDatabaseModel, +} from "../../src/utils/nosqlUtils"; +import { JSONSchema4, JSONSchema4TypeName } from "json-schema"; +import { + OpenApiSchemaTypeDefinition, + PartialOpenApiSchema, +} from "openapi-json-schema"; +import { + DatabaseModelResult, + TableEntity, +} from "../../src/types/sql-plugin-types"; +import { GenerateDatabaseModel } from "../../src/utils/sharedUtils"; + +describe("dbToOpenApi", () => { + it("should handle empty entities correctly", () => { + const dbResult = { + getEntities: () => ({}), + } as DatabaseModelResult; + + const result = dbToOpenApi(dbResult); + expect(result).toEqual( + expect.objectContaining({ + openapi: "3.0.0", + info: expect.anything(), + paths: {}, + components: { + schemas: {}, + }, + }) + ); + }); + + it("should process entities and populate schemas", () => { + const entities: Record = { + User: { + name: "User", + attributes: [ + { attributeName: "id", attributeType: "string" }, + { attributeName: "name", attributeType: "string" }, + ], + }, + }; + const dbResult = GenerateDatabaseModel(entities, []); + + const result = dbToOpenApi(dbResult); + expect(result.components?.schemas).toHaveProperty("User"); + expect(result.components?.schemas?.User).toEqual( + expect.objectContaining({ + type: "object", + properties: { + id: expect.any(Object), + name: expect.any(Object), + }, + }) + ); + }); +}); + +describe("GeneratePropertyModel", () => { + it("should properly construct a PropertyModel from JSONSchema", () => { + const jsonSchema: JSONSchema4 = { + type: "string", + nullable: true, + }; + const propertyName = "username"; + const tableName = "User"; + + const propertyModel = GeneratePropertyModel( + tableName, + propertyName, + jsonSchema + ); + expect(propertyModel.TableName).toEqual("`User`"); + expect(propertyModel.Name).toBe("`username`"); + expect(propertyModel.ColumnProperties).toContain("string nullable"); + }); +}); + +describe("ConvertOpenApiToDatabaseModel", () => { + it("should convert empty schemas to minimal DatabaseModel", () => { + const schemas: Record = {}; + + const model = ConvertOpenApiToDatabaseModel(schemas); + expect(model).toEqual( + expect.objectContaining({ + Dialect: "nosql", + TableList: [], + PrimaryKeyList: [], + ForeignKeyList: [], + }) + ); + }); + + it("should convert populated schemas to DatabaseModel", () => { + const schemas: Record = { + User: { + properties: { + id: { type: "string", $ref: "#/components/schemas/Id" }, + }, + type: "object", + }, + }; + + const model = ConvertOpenApiToDatabaseModel(schemas); + expect(model.TableList).toHaveLength(1); + expect(model.ForeignKeyList).toHaveLength(2); // Primary and foreign key relationships + }); +}); + +// diff --git a/tests/utils/sharedUtils.spec.ts b/tests/utils/sharedUtils.spec.ts index 7bf262b..1cb0a9b 100644 --- a/tests/utils/sharedUtils.spec.ts +++ b/tests/utils/sharedUtils.spec.ts @@ -6,9 +6,16 @@ import { getMermaidDiagramDb, removeHtml, GenerateDatabaseModel, + RemoveNameQuantifiers, + entityName, + getCommentIndexes, + generateComment, } from "../../src/utils/sharedUtils"; import { multiAssert } from "../helpers"; -import { DatabaseModelResult, TableAttribute } from "../../src/types/sql-plugin-types"; +import { + DatabaseModelResult, + TableAttribute, +} from "../../src/types/sql-plugin-types"; import { DbDefinition, DbEntityAttributesDefinition, @@ -17,81 +24,212 @@ import { DbRelationshipDefinition, } from "@funktechno/little-mermaid-2-the-sql/lib/src/types"; -import '../../src/types/drawio-types'; +import "../../src/types/drawio-types"; describe("sharedUtils.ts", () => { - it("dbTypeEnds", () => { - const result = dbTypeEnds("label"); - expect(result).toBe("`label`"); + describe("dbTypeEnds", () => { + it("dbTypeEnds 1", () => { + const result = dbTypeEnds("label"); + expect(result).toBe("`label`"); + }); + it("should wrap input with backticks", () => { + expect(dbTypeEnds("tablename")).toBe("`tablename`"); + }); }); - it("GetColumnQuantifiers", () => { - const testTheory: (() => void)[] = []; - const testTypes: Record = { - mysql: { Start: "`", End: "`" }, - ts: { Start: "`", End: "`" }, - openapi: { Start: "`", End: "`" }, - sqlserver: { Start: "[", End: "]" }, - sqlite: { Start: '"', End: '"' }, - postgres: { Start: '"', End: '"' }, - }; + describe("GetColumnQuantifiers", () => { + it("GetColumnQuantifiers 1", () => { + const testTheory: (() => void)[] = []; + const testTypes: Record = { + mysql: { Start: "`", End: "`" }, + ts: { Start: "`", End: "`" }, + openapi: { Start: "`", End: "`" }, + sqlserver: { Start: "[", End: "]" }, + sqlite: { Start: '"', End: '"' }, + postgres: { Start: '"', End: '"' }, + }; - for (const key in testTypes) { + for (const key in testTypes) { + testTheory.push(() => + expect( + GetColumnQuantifiers( + key as + | "mysql" + | "ts" + | "openapi" + | "sqlserver" + | "sqlite" + | "postgres" + ) + ).toEqual(testTypes[key]) + ); + } + let type_undefined: undefined; testTheory.push(() => - expect( - GetColumnQuantifiers( - key as - | "mysql" - | "ts" - | "openapi" - | "sqlserver" - | "sqlite" - | "postgres" - ) - ).toEqual(testTypes[key]) + expect(GetColumnQuantifiers(type_undefined)).toEqual({ + Start: '"', + End: '"', + }) ); - } - let type_undefined: undefined; - testTheory.push(() => - expect(GetColumnQuantifiers(type_undefined)).toEqual({ - Start: '"', - End: '"', - }) + + multiAssert(testTheory); + }); + test.each< + [ + ( + | "mysql" + | "sqlserver" + | "sqlite" + | "postgres" + | "openapi" + | "ts" + | undefined + ), + string, + string + ] + >([ + ["mysql", "`", "`"], + ["sqlserver", "[", "]"], + ["sqlite", '"', '"'], + ["postgres", '"', '"'], + ["openapi", "`", "`"], + ["ts", "`", "`"], + [undefined, '"', '"'], + ])( + "should return correct characters for %s", + (type, expectedStart, expectedEnd) => { + const quantifiers = GetColumnQuantifiers(type); + expect(quantifiers).toEqual({ + Start: expectedStart, + End: expectedEnd, + }); + } ); + }); + + describe("removeHtml", () => { + it("should clean HTML tags and return plain text", () => { + const input = '
Hello World!
'; + const output = removeHtml(input); + expect(output).toBe("Hello World!"); + }); + it("should clean HTML tags and return plain text 2", () => { + const expectedResult = "text only"; + const testData = `${expectedResult}`; + expect(removeHtml(testData)).toBe("text only"); + }); + }); - multiAssert(testTheory); + describe("RemoveNameQuantifiers", () => { + test.each([ + ["[name]", "name"], + ["'name'", "name"], + ['"name"', "name"], + ["`name`", "name"], + ["(name)", "name"], + ["name", "name"], + ])("should remove any quantifiers from %s", (input, expected) => { + expect(RemoveNameQuantifiers(input)).toBe(expected); + }); }); - it("removeHtml", () => { - const expectedResult = "text only"; - const testData = `${expectedResult}`; - expect(removeHtml(testData)).toBe("text only"); + + describe("getDbLabel", () => { + it("should return formatted TableAttribute object", () => { + const label = "name varchar"; + const quantifiers = { Start: "`", End: "`" }; + const attribute = getDbLabel(label, quantifiers); + expect(attribute).toEqual({ + attributeName: "name", + attributeType: "varchar", + }); + }); }); - it("getMermaidDiagramDb", () => { - const mockDrawioUI: DrawioUI = { - fileNode: null, - hideDialog:() => {}, - showDialog:(...args: any[]) => {}, - editor: { - graph: { - getModel:() => { - const cells: Record = {}; - return {cells}; - } - } as any - }, - actions: { - addAction:(name: string, action: () => void)=> {}, - get:(name: string)=> { return {funct: () => {}} } - }, - menus: { - get:(name: string) => null, - funct: (...args: any[]) => {}, - enabled: true, - addMenuItems:(menu: any, arg: any, arg2: any) => {} - } as any, - importLocalFile:(args: boolean) => {} - }; - const result = getMermaidDiagramDb(mockDrawioUI, "mysql"); - const expectedResult = GenerateDatabaseModel({}, []) - expect(result).toEqual(expectedResult); + + describe("entityName", () => { + test.each([ + [undefined, undefined, ""], + ["Desc", undefined, "/** Desc */"], + ["Desc", "Format", "/** Desc @format Format */"], + ])( + "should format entity name correctly", + (description, format, expected) => { + expect(entityName(description, format)).toBe(expected); + } + ); + }); + + describe("getCommentIndexes", () => { + const desc = "key"; + it("should return correct index positions when comments are present", () => { + const result = `--start--/**${desc}*/--end--`; + const indexes = getCommentIndexes(result); + expect(indexes).toEqual({ + beforeStart: 9, + start: 12, + end: 14, + }); + }); + + it("should return -1 for all positions when comments are absent", () => { + const result = "Some description"; + const indexes = getCommentIndexes(result); + expect(indexes).toEqual({ + beforeStart: -1, + start: -1, + end: -1, + }); + }); + }); + + describe("GenerateDatabaseModel", () => { + it("should create a DatabaseModelResult instance with entities and relationships", () => { + const entities = { table: { name: "table", attributes: [] } }; + const relationships: DbRelationshipDefinition[] = []; + const db = GenerateDatabaseModel(entities, relationships); + expect(db.getEntities()).toEqual(entities); + expect(db.getRelationships()).toEqual(relationships); + }); + it("getMermaidDiagramDb 1", () => { + const mockDrawioUI: DrawioUI = { + fileNode: null, + hideDialog: () => {}, + showDialog: (...args: any[]) => {}, + editor: { + graph: { + getModel: () => { + const cells: Record = {}; + return { cells }; + }, + } as any, + }, + actions: { + addAction: (name: string, action: () => void) => {}, + get: (name: string) => { + return { funct: () => {} }; + }, + }, + menus: { + get: (name: string) => null, + funct: (...args: any[]) => {}, + enabled: true, + addMenuItems: (menu: any, arg: any, arg2: any) => {}, + } as any, + importLocalFile: (args: boolean) => {}, + }; + const result = getMermaidDiagramDb(mockDrawioUI, "mysql"); + const expectedResult = GenerateDatabaseModel({}, []); + expect(result).toEqual(expectedResult); + }); + }); + + + describe('generateComment', () => { + test.each([ + [undefined, undefined, ''], + ["This is a comment", undefined, '/** This is a comment */'], + ["This is a comment", "extra", '/** This is a comment @format extra */'], + ])('should generate a comment string correctly', (description, format, expected) => { + expect(generateComment(description, format)).toBe(expected); + }); }); }); From 15b67eb96f8d29b3750d395f5fb2999a18cd302f Mon Sep 17 00:00:00 2001 From: lastlink Date: Sat, 17 Aug 2024 21:41:20 -0400 Subject: [PATCH 2/2] removed unused imports --- src/nosql-ts.ts | 17 ----------------- src/nosql.ts | 13 ------------- src/sql.ts | 6 ------ src/utils/nosqlUtils.ts | 4 ---- src/utils/sharedUtils.ts | 1 - tests/utils/nosqlUtils.spec.ts | 3 +-- tests/utils/sharedUtils.spec.ts | 8 -------- 7 files changed, 1 insertion(+), 51 deletions(-) diff --git a/src/nosql-ts.ts b/src/nosql-ts.ts index 8b9c141..ce3e50e 100644 --- a/src/nosql-ts.ts +++ b/src/nosql-ts.ts @@ -1,20 +1,8 @@ import { - DbDefinition, - DbRelationshipDefinition, -} from "@funktechno/little-mermaid-2-the-sql/lib/src/types"; -import { - DatabaseModelResult, - TableAttribute, - TableEntity, -} from "./types/sql-plugin-types"; -import { - DatabaseModel, ForeignKeyModel, PrimaryKeyModel, - PropertyModel, TableModel, } from "@funktechno/sqlsimpleparser/lib/types"; -import { JSONSchema4, JSONSchema4TypeName } from "json-schema"; import { convertCoreTypesToJsonSchema, convertOpenApiToCoreTypes, @@ -28,17 +16,12 @@ import { convertTypeScriptToCoreTypes } from "core-types-ts/dist/lib/ts-to-core- import { convertCoreTypesToTypeScript } from "core-types-ts"; import { CreateTableUI, - GetColumnQuantifiers, - RemoveNameQuantifiers, - dbTypeEnds, - getDbLabel, getMermaidDiagramDb, } from "./utils/sharedUtils"; import { pluginVersion } from "./utils/constants"; import { ConvertOpenApiToDatabaseModel, dbToOpenApi, - GeneratePropertyModel, } from "./utils/nosqlUtils"; import { defaultReset, defaultResetOpenApi } from "./utils/constants-nosql"; diff --git a/src/nosql.ts b/src/nosql.ts index 093a7b5..5b7f3ec 100644 --- a/src/nosql.ts +++ b/src/nosql.ts @@ -1,16 +1,8 @@ import { - DbDefinition, - DbRelationshipDefinition, -} from "@funktechno/little-mermaid-2-the-sql/lib/src/types"; -import { TableAttribute, TableEntity } from "./types/sql-plugin-types"; -import { - DatabaseModel, ForeignKeyModel, PrimaryKeyModel, - PropertyModel, TableModel, } from "@funktechno/sqlsimpleparser/lib/types"; -import { JSONSchema4, JSONSchema4TypeName } from "json-schema"; import { convertCoreTypesToJsonSchema, convertOpenApiToCoreTypes, @@ -22,17 +14,12 @@ import { } from "openapi-json-schema"; import { CreateTableUI, - GetColumnQuantifiers, - RemoveNameQuantifiers, - dbTypeEnds, - getDbLabel, getMermaidDiagramDb, } from "./utils/sharedUtils"; import { pluginVersion } from "./utils/constants"; import { ConvertOpenApiToDatabaseModel, dbToOpenApi, - GeneratePropertyModel, } from "./utils/nosqlUtils"; import { defaultResetOpenApi } from "./utils/constants-nosql"; diff --git a/src/sql.ts b/src/sql.ts index 85464d6..f04cb75 100644 --- a/src/sql.ts +++ b/src/sql.ts @@ -1,21 +1,15 @@ import { DbParser } from "@funktechno/little-mermaid-2-the-sql/lib/src/generate-sql-ddl"; import { DbDefinition, - DbRelationshipDefinition, } from "@funktechno/little-mermaid-2-the-sql/lib/src/types"; -import { TableAttribute, TableEntity } from "./types/sql-plugin-types"; import { SqlSimpleParser } from "@funktechno/sqlsimpleparser"; import { ForeignKeyModel, PrimaryKeyModel, - PropertyModel, TableModel, } from "@funktechno/sqlsimpleparser/lib/types"; import { CreateTableUI, - GetColumnQuantifiers, - RemoveNameQuantifiers, - getDbLabel, getMermaidDiagramDb, } from "./utils/sharedUtils"; import { pluginVersion } from "./utils/constants"; diff --git a/src/utils/nosqlUtils.ts b/src/utils/nosqlUtils.ts index 872dcf8..088ba95 100644 --- a/src/utils/nosqlUtils.ts +++ b/src/utils/nosqlUtils.ts @@ -1,4 +1,3 @@ -import { DbDefinition } from "@funktechno/little-mermaid-2-the-sql/lib/src/types"; import { OpenApiSchemaTypeDefinition, PartialOpenApiSchema, @@ -14,15 +13,12 @@ import { } from "./constants"; import { JSONSchema4, JSONSchema4TypeName } from "json-schema"; import { - ColumnQuantifiers, DatabaseModelResult, } from "../types/sql-plugin-types"; import { dbTypeEnds, generateComment, - GetColumnQuantifiers, getCommentIndexes, - getDbLabel, RemoveNameQuantifiers, } from "./sharedUtils"; import { diff --git a/src/utils/sharedUtils.ts b/src/utils/sharedUtils.ts index 174bf27..05b183c 100644 --- a/src/utils/sharedUtils.ts +++ b/src/utils/sharedUtils.ts @@ -1,5 +1,4 @@ import { - DbDefinition, DbRelationshipDefinition, } from "@funktechno/little-mermaid-2-the-sql/lib/src/types"; import { diff --git a/tests/utils/nosqlUtils.spec.ts b/tests/utils/nosqlUtils.spec.ts index a6a96e6..e889650 100644 --- a/tests/utils/nosqlUtils.spec.ts +++ b/tests/utils/nosqlUtils.spec.ts @@ -4,10 +4,9 @@ import { GeneratePropertyModel, ConvertOpenApiToDatabaseModel, } from "../../src/utils/nosqlUtils"; -import { JSONSchema4, JSONSchema4TypeName } from "json-schema"; +import { JSONSchema4 } from "json-schema"; import { OpenApiSchemaTypeDefinition, - PartialOpenApiSchema, } from "openapi-json-schema"; import { DatabaseModelResult, diff --git a/tests/utils/sharedUtils.spec.ts b/tests/utils/sharedUtils.spec.ts index 1cb0a9b..46d1b18 100644 --- a/tests/utils/sharedUtils.spec.ts +++ b/tests/utils/sharedUtils.spec.ts @@ -13,14 +13,6 @@ import { } from "../../src/utils/sharedUtils"; import { multiAssert } from "../helpers"; import { - DatabaseModelResult, - TableAttribute, -} from "../../src/types/sql-plugin-types"; -import { - DbDefinition, - DbEntityAttributesDefinition, - DbEntityDefinition, - DbRelSpec, DbRelationshipDefinition, } from "@funktechno/little-mermaid-2-the-sql/lib/src/types";