Skip to content

Commit e7f0ef4

Browse files
authored
Merge pull request #55 from funktechno/f/updates
add more tests
2 parents 34c4b9a + 15b67eb commit e7f0ef4

File tree

9 files changed

+321
-118
lines changed

9 files changed

+321
-118
lines changed

eslint.config.mjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default [
1515
"@typescript-eslint/no-unsafe-function-type": "off",
1616
"@typescript-eslint/no-explicit-any": "off",
1717
"no-useless-escape": "off",
18+
"no-control-regex": "off",
1819
"no-multiple-empty-lines": "error",
1920
"no-use-before-define": "off",
2021
"@typescript-eslint/no-non-null-assertion": "off",
@@ -32,9 +33,9 @@ export default [
3233
},
3334
{
3435
files: ["**/__tests__/*.{j,t}s?(x)", "**/tests/**/*.spec.{j,t}s?(x)"],
35-
env: {
36-
jest: true,
37-
},
36+
// env: {
37+
// jest: true,
38+
// },
3839
},
3940
{ languageOptions: { globals: globals.browser } }
4041
];

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"test": "jest -c ./jest.config.ts --forceExit --verbose -i --no-cache --detectOpenHandles",
1212
"test:coverage": "jest --forceExit --coverage --verbose --detectOpenHandles",
1313
"test:watch": "jest --watchAll --detectOpenHandles",
14-
"lint": "tsc --noEmit && eslint \"{src,client}/**/*.{js,ts}\"",
15-
"lint:fix": "tsc --noEmit && eslint \"{src,client}/**/*.{js,ts}\" --fix",
14+
"lint": "tsc --noEmit && eslint \"{src,client,tests}/**/*.{js,ts}\"",
15+
"lint:fix": "tsc --noEmit && eslint \"{src,client,tests}/**/*.{js,ts}\" --fix",
1616
"create": "npm run build && npm run cli:test",
1717
"build": "tsc && npm run build:types",
1818
"build:types": "tsc -p tsconfig.json",

src/nosql-ts.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
import {
2-
DbDefinition,
3-
DbRelationshipDefinition,
4-
} from "@funktechno/little-mermaid-2-the-sql/lib/src/types";
5-
import {
6-
DatabaseModelResult,
7-
TableAttribute,
8-
TableEntity,
9-
} from "./types/sql-plugin-types";
10-
import {
11-
DatabaseModel,
122
ForeignKeyModel,
133
PrimaryKeyModel,
14-
PropertyModel,
154
TableModel,
165
} from "@funktechno/sqlsimpleparser/lib/types";
17-
import { JSONSchema4, JSONSchema4TypeName } from "json-schema";
186
import {
197
convertCoreTypesToJsonSchema,
208
convertOpenApiToCoreTypes,
@@ -28,17 +16,12 @@ import { convertTypeScriptToCoreTypes } from "core-types-ts/dist/lib/ts-to-core-
2816
import { convertCoreTypesToTypeScript } from "core-types-ts";
2917
import {
3018
CreateTableUI,
31-
GetColumnQuantifiers,
32-
RemoveNameQuantifiers,
33-
dbTypeEnds,
34-
getDbLabel,
3519
getMermaidDiagramDb,
3620
} from "./utils/sharedUtils";
3721
import { pluginVersion } from "./utils/constants";
3822
import {
3923
ConvertOpenApiToDatabaseModel,
4024
dbToOpenApi,
41-
GeneratePropertyModel,
4225
} from "./utils/nosqlUtils";
4326
import { defaultReset, defaultResetOpenApi } from "./utils/constants-nosql";
4427

src/nosql.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
import {
2-
DbDefinition,
3-
DbRelationshipDefinition,
4-
} from "@funktechno/little-mermaid-2-the-sql/lib/src/types";
5-
import { TableAttribute, TableEntity } from "./types/sql-plugin-types";
6-
import {
7-
DatabaseModel,
82
ForeignKeyModel,
93
PrimaryKeyModel,
10-
PropertyModel,
114
TableModel,
125
} from "@funktechno/sqlsimpleparser/lib/types";
13-
import { JSONSchema4, JSONSchema4TypeName } from "json-schema";
146
import {
157
convertCoreTypesToJsonSchema,
168
convertOpenApiToCoreTypes,
@@ -22,17 +14,12 @@ import {
2214
} from "openapi-json-schema";
2315
import {
2416
CreateTableUI,
25-
GetColumnQuantifiers,
26-
RemoveNameQuantifiers,
27-
dbTypeEnds,
28-
getDbLabel,
2917
getMermaidDiagramDb,
3018
} from "./utils/sharedUtils";
3119
import { pluginVersion } from "./utils/constants";
3220
import {
3321
ConvertOpenApiToDatabaseModel,
3422
dbToOpenApi,
35-
GeneratePropertyModel,
3623
} from "./utils/nosqlUtils";
3724
import { defaultResetOpenApi } from "./utils/constants-nosql";
3825

src/sql.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
import { DbParser } from "@funktechno/little-mermaid-2-the-sql/lib/src/generate-sql-ddl";
22
import {
33
DbDefinition,
4-
DbRelationshipDefinition,
54
} from "@funktechno/little-mermaid-2-the-sql/lib/src/types";
6-
import { TableAttribute, TableEntity } from "./types/sql-plugin-types";
75
import { SqlSimpleParser } from "@funktechno/sqlsimpleparser";
86
import {
97
ForeignKeyModel,
108
PrimaryKeyModel,
11-
PropertyModel,
129
TableModel,
1310
} from "@funktechno/sqlsimpleparser/lib/types";
1411
import {
1512
CreateTableUI,
16-
GetColumnQuantifiers,
17-
RemoveNameQuantifiers,
18-
getDbLabel,
1913
getMermaidDiagramDb,
2014
} from "./utils/sharedUtils";
2115
import { pluginVersion } from "./utils/constants";

src/utils/nosqlUtils.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { DbDefinition } from "@funktechno/little-mermaid-2-the-sql/lib/src/types";
21
import {
32
OpenApiSchemaTypeDefinition,
43
PartialOpenApiSchema,
@@ -14,15 +13,12 @@ import {
1413
} from "./constants";
1514
import { JSONSchema4, JSONSchema4TypeName } from "json-schema";
1615
import {
17-
ColumnQuantifiers,
1816
DatabaseModelResult,
1917
} from "../types/sql-plugin-types";
2018
import {
2119
dbTypeEnds,
2220
generateComment,
23-
GetColumnQuantifiers,
2421
getCommentIndexes,
25-
getDbLabel,
2622
RemoveNameQuantifiers,
2723
} from "./sharedUtils";
2824
import {

src/utils/sharedUtils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
DbDefinition,
32
DbRelationshipDefinition,
43
} from "@funktechno/little-mermaid-2-the-sql/lib/src/types";
54
import {
@@ -75,7 +74,7 @@ export function dbTypeEnds(label: string): string {
7574
* @returns
7675
*/
7776
export function RemoveNameQuantifiers(name: string) {
78-
return name.replace(/\[|\]|\(|\"|\'|\`/g, "").trim();
77+
return name.replace(/\[|\]|\(|\)|\"|\'|\`/g, "").trim();
7978
}
8079

8180
/**

tests/utils/nosqlUtils.spec.ts

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
//typescript
2+
import {
3+
dbToOpenApi,
4+
GeneratePropertyModel,
5+
ConvertOpenApiToDatabaseModel,
6+
} from "../../src/utils/nosqlUtils";
7+
import { JSONSchema4 } from "json-schema";
8+
import {
9+
OpenApiSchemaTypeDefinition,
10+
} from "openapi-json-schema";
11+
import {
12+
DatabaseModelResult,
13+
TableEntity,
14+
} from "../../src/types/sql-plugin-types";
15+
import { GenerateDatabaseModel } from "../../src/utils/sharedUtils";
16+
17+
describe("dbToOpenApi", () => {
18+
it("should handle empty entities correctly", () => {
19+
const dbResult = {
20+
getEntities: () => ({}),
21+
} as DatabaseModelResult;
22+
23+
const result = dbToOpenApi(dbResult);
24+
expect(result).toEqual(
25+
expect.objectContaining({
26+
openapi: "3.0.0",
27+
info: expect.anything(),
28+
paths: {},
29+
components: {
30+
schemas: {},
31+
},
32+
})
33+
);
34+
});
35+
36+
it("should process entities and populate schemas", () => {
37+
const entities: Record<string, TableEntity> = {
38+
User: {
39+
name: "User",
40+
attributes: [
41+
{ attributeName: "id", attributeType: "string" },
42+
{ attributeName: "name", attributeType: "string" },
43+
],
44+
},
45+
};
46+
const dbResult = GenerateDatabaseModel(entities, []);
47+
48+
const result = dbToOpenApi(dbResult);
49+
expect(result.components?.schemas).toHaveProperty("User");
50+
expect(result.components?.schemas?.User).toEqual(
51+
expect.objectContaining({
52+
type: "object",
53+
properties: {
54+
id: expect.any(Object),
55+
name: expect.any(Object),
56+
},
57+
})
58+
);
59+
});
60+
});
61+
62+
describe("GeneratePropertyModel", () => {
63+
it("should properly construct a PropertyModel from JSONSchema", () => {
64+
const jsonSchema: JSONSchema4 = {
65+
type: "string",
66+
nullable: true,
67+
};
68+
const propertyName = "username";
69+
const tableName = "User";
70+
71+
const propertyModel = GeneratePropertyModel(
72+
tableName,
73+
propertyName,
74+
jsonSchema
75+
);
76+
expect(propertyModel.TableName).toEqual("`User`");
77+
expect(propertyModel.Name).toBe("`username`");
78+
expect(propertyModel.ColumnProperties).toContain("string nullable");
79+
});
80+
});
81+
82+
describe("ConvertOpenApiToDatabaseModel", () => {
83+
it("should convert empty schemas to minimal DatabaseModel", () => {
84+
const schemas: Record<string, OpenApiSchemaTypeDefinition> = {};
85+
86+
const model = ConvertOpenApiToDatabaseModel(schemas);
87+
expect(model).toEqual(
88+
expect.objectContaining({
89+
Dialect: "nosql",
90+
TableList: [],
91+
PrimaryKeyList: [],
92+
ForeignKeyList: [],
93+
})
94+
);
95+
});
96+
97+
it("should convert populated schemas to DatabaseModel", () => {
98+
const schemas: Record<string, OpenApiSchemaTypeDefinition> = {
99+
User: {
100+
properties: {
101+
id: { type: "string", $ref: "#/components/schemas/Id" },
102+
},
103+
type: "object",
104+
},
105+
};
106+
107+
const model = ConvertOpenApiToDatabaseModel(schemas);
108+
expect(model.TableList).toHaveLength(1);
109+
expect(model.ForeignKeyList).toHaveLength(2); // Primary and foreign key relationships
110+
});
111+
});
112+
113+
//

0 commit comments

Comments
 (0)