Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more tests #55

Merged
merged 3 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 } }
];
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 0 additions & 17 deletions src/nosql-ts.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -28,17 +16,12 @@
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";

Expand Down Expand Up @@ -147,13 +130,13 @@

//Table Info
let foreignKeyList: ForeignKeyModel[] = [];
let primaryKeyList: PrimaryKeyModel[] = [];

Check warning on line 133 in src/nosql-ts.ts

View workflow job for this annotation

GitHub Actions / tests

'primaryKeyList' is assigned a value but never used
let tableList: TableModel[] = [];
let cells: mxCell[] = [];
let tableCell: mxCell | null = null;
let rowCell: mxCell | null = null;
let dx = 0;
let exportedTables = 0;

Check warning on line 139 in src/nosql-ts.ts

View workflow job for this annotation

GitHub Actions / tests

'exportedTables' is assigned a value but never used

//Create Base div
const divFromNOSQL = document.createElement("div");
Expand Down
13 changes: 0 additions & 13 deletions src/nosql.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -22,17 +14,12 @@
} 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";

Expand Down Expand Up @@ -128,13 +115,13 @@

//Table Info
let foreignKeyList: ForeignKeyModel[] = [];
let primaryKeyList: PrimaryKeyModel[] = [];

Check warning on line 118 in src/nosql.ts

View workflow job for this annotation

GitHub Actions / tests

'primaryKeyList' is assigned a value but never used
let tableList: TableModel[] = [];
let cells: mxCell[] = [];
let tableCell: mxCell | null = null;
let rowCell: mxCell | null = null;
let dx = 0;
let exportedTables = 0;

Check warning on line 124 in src/nosql.ts

View workflow job for this annotation

GitHub Actions / tests

'exportedTables' is assigned a value but never used

//Create Base div
const divFromNOSQL = document.createElement("div");
Expand Down
6 changes: 0 additions & 6 deletions src/sql.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -142,13 +136,13 @@

//Table Info
let foreignKeyList: ForeignKeyModel[] = [];
let primaryKeyList: PrimaryKeyModel[] = [];

Check warning on line 139 in src/sql.ts

View workflow job for this annotation

GitHub Actions / tests

'primaryKeyList' is assigned a value but never used
let tableList: TableModel[] = [];
let cells: mxCell[] = [];
let tableCell: mxCell | null = null;
let rowCell: mxCell | null = null;
let dx = 0;
let exportedTables = 0;

Check warning on line 145 in src/sql.ts

View workflow job for this annotation

GitHub Actions / tests

'exportedTables' is assigned a value but never used

//Create Base div
const divFromSQL = document.createElement("div");
Expand Down
4 changes: 0 additions & 4 deletions src/utils/nosqlUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { DbDefinition } from "@funktechno/little-mermaid-2-the-sql/lib/src/types";
import {
OpenApiSchemaTypeDefinition,
PartialOpenApiSchema,
Expand All @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions src/utils/sharedUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
DbDefinition,
DbRelationshipDefinition,
} from "@funktechno/little-mermaid-2-the-sql/lib/src/types";
import {
Expand Down Expand Up @@ -75,7 +74,7 @@ export function dbTypeEnds(label: string): string {
* @returns
*/
export function RemoveNameQuantifiers(name: string) {
return name.replace(/\[|\]|\(|\"|\'|\`/g, "").trim();
return name.replace(/\[|\]|\(|\)|\"|\'|\`/g, "").trim();
}

/**
Expand Down
113 changes: 113 additions & 0 deletions tests/utils/nosqlUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//typescript
import {
dbToOpenApi,
GeneratePropertyModel,
ConvertOpenApiToDatabaseModel,
} from "../../src/utils/nosqlUtils";
import { JSONSchema4 } from "json-schema";
import {
OpenApiSchemaTypeDefinition,
} 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<string, TableEntity> = {
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<string, OpenApiSchemaTypeDefinition> = {};

const model = ConvertOpenApiToDatabaseModel(schemas);
expect(model).toEqual(
expect.objectContaining({
Dialect: "nosql",
TableList: [],
PrimaryKeyList: [],
ForeignKeyList: [],
})
);
});

it("should convert populated schemas to DatabaseModel", () => {
const schemas: Record<string, OpenApiSchemaTypeDefinition> = {
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
});
});

//
Loading
Loading