Skip to content

Commit 11b8757

Browse files
committed
update tests and improve types
1 parent d9fa990 commit 11b8757

File tree

6 files changed

+128
-15
lines changed

6 files changed

+128
-15
lines changed

src/mermaid/src/diagrams/er/erDb.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
DbEntityAttributesDefinition,
55
DbEntityDefinition,
66
DbRelationshipDefinition,
7+
DbRelSpec,
78
} from "../../../../types";
89
// import mermaidAPI from '../../mermaidAPI';
910
// import * as configApi from '../../../../../deps/mermaid/src/config'
@@ -69,7 +70,7 @@ const addRelationship = function (
6970
entA: string,
7071
rolA: string,
7172
entB: string,
72-
rSpec: string
73+
rSpec: DbRelSpec
7374
) {
7475
const rel: DbRelationshipDefinition = {
7576
entityA: entA,

src/types/index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,38 @@ export interface DiagramDefinition {
2424
export interface DbEntityAttributesDefinition {
2525
attributeType?: string;
2626
attributeName?: string;
27-
attributeKeyType?: string;
27+
attributeKeyType?: "PK" | "FK";
2828
attributeComment?: string;
2929
}
3030

3131
export interface DbEntityDefinition {
3232
attributes: DbEntityAttributesDefinition[];
3333
}
3434

35+
export interface DbRelSpec {
36+
cardA: string;
37+
cardB: string;
38+
relType: string;
39+
}
40+
3541
export interface DbRelationshipDefinition {
3642
entityA: string;
3743
roleA: string;
3844
entityB: string;
39-
relSpec: string;
45+
relSpec: DbRelSpec;
4046
}
4147

4248
export interface DbDefinition {
4349
Cardinality: Record<string, string>;
4450
Identification: Record<string, string>;
4551
addEntity: (name: string) => void;
46-
addAttributes: (entityName: string, attribs: any[]) => void;
52+
addAttributes: (entityName: string, attribs: DbEntityAttributesDefinition[]) => void;
4753
getEntities: () => Record<string, DbEntityDefinition>;
4854
addRelationship: (
4955
entA: string,
5056
rolA: string,
5157
entB: string,
52-
rSpec: string
58+
rSpec: DbRelSpec
5359
) => void;
5460
getRelationships: () => DbRelationshipDefinition[];
5561
clear: () => void;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"Generate DDL examples Should generate create table SQL syntax": "CREATE TABLE \"Persons\" (\n\t\"City\" varchar(255),\n\t\"Address\" varchar(255),\n\t\"FirstName\" varchar(255),\n\t\"LastName\" varchar(255),\n\t\"PersonID\" int NOT NULL,\n\tPRIMARY KEY(\"PersonID\")\n);\n\nCREATE TABLE \"Orders\" (\n\t\"PersonID\" int NOT NULL,\n\t\"OrderID\" int NOT NULL,\n\tPRIMARY KEY(\"OrderID\")\n);\n\n"
3+
}

tests/generate-sql-ddl.spec.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/src/generate-sql-ddl.spec.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { DbParser } from "../../src/generate-sql-ddl";
2+
import erDb from "../../src/mermaid/src/diagrams/er/erDb";
3+
import { DbEntityAttributesDefinition, DbRelSpec } from "../../src/types";
4+
import { getExpected, updateExpected } from "../utils/helper";
5+
6+
const dataSource = "getSQLDataDefinition";
7+
describe("Generate DDL examples", () => {
8+
it("Should generate create table SQL syntax", async () => {
9+
const db = erDb;
10+
let entityName = "Persons";
11+
db.addEntity(entityName);
12+
let attributes:DbEntityAttributesDefinition[] = [
13+
{
14+
attributeName: "PersonID",
15+
attributeKeyType: "PK",
16+
attributeType: "int",
17+
attributeComment: "NOT NULL"
18+
},
19+
{
20+
attributeName: "LastName",
21+
attributeType: "varchar255"
22+
},
23+
{
24+
attributeName: "FirstName",
25+
attributeType: "varchar255"
26+
},
27+
{
28+
attributeName: "Address",
29+
attributeType: "varchar255"
30+
},
31+
{
32+
attributeName: "City",
33+
attributeType: "varchar255"
34+
}
35+
];
36+
db.addAttributes(entityName,attributes);
37+
38+
entityName = "Orders";
39+
db.addEntity(entityName);
40+
attributes = [
41+
{
42+
attributeName: "OrderID",
43+
attributeKeyType: "PK",
44+
attributeType: "int",
45+
attributeComment: "NOT NULL"
46+
},
47+
{
48+
attributeName: "PersonID",
49+
attributeKeyType: "FK",
50+
attributeType: "int",
51+
attributeComment: "NOT NULL"
52+
}
53+
];
54+
db.addAttributes(entityName,attributes);
55+
56+
const relSpec:DbRelSpec= {
57+
cardA: "ZERO_OR_MORE",
58+
cardB: "ONLY_ONE",
59+
relType: "IDENTIFYING"
60+
};
61+
62+
db.addRelationship("Persons", entityName,`[Persons.PersonId] to [${entityName}.PersonId]`, relSpec);
63+
// `erDiagram artists { }`;
64+
const ddlSyntax = new DbParser("sqlite", db).getSQLDataDefinition();
65+
66+
const dataKey = expect.getState().currentTestName || "unknown";
67+
68+
const expectedResult = await getExpected(dataSource, dataKey);
69+
70+
if (ddlSyntax != expectedResult) {
71+
await updateExpected(dataSource, dataKey, ddlSyntax);
72+
}
73+
74+
// console.log(result);
75+
expect(ddlSyntax).toStrictEqual(expectedResult);
76+
// expect(ddlSyntax).toBe('CREATE TABLE artists');
77+
});
78+
});

tests/utils/helper.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
// import {fs} from "fs";
3+
import * as fs from "fs";
4+
// const fs = require("fs");
5+
const path = require("path");
6+
const base_DIR = path.join(__dirname, "../data/results");
7+
8+
export async function getExpectedDict<T>(testData: string): Promise<Record<string, T | null>> {
9+
const filePath = path.join(base_DIR, testData + ".json");
10+
console.log(filePath);
11+
if (await fs.existsSync(filePath)) {
12+
const expectedResults: Record<string, T> = await JSON.parse(fs.readFileSync(filePath, "utf8"));
13+
return expectedResults;
14+
} else
15+
return {};
16+
}
17+
18+
export async function getExpected<T>(testData: string, key: string): Promise<T | null> {
19+
const expectedResults = await getExpectedDict<T>(testData);
20+
if (expectedResults[key]) {
21+
return expectedResults[key];
22+
} else {
23+
return null;
24+
}
25+
}
26+
27+
export async function updateExpected<T>(testData: string, key: string, result: T | null) {
28+
const expectedResults = await getExpectedDict<T>(testData);
29+
30+
expectedResults[key] = result;
31+
32+
const filePath = path.join(base_DIR, testData + ".json");
33+
const stringResults = JSON.stringify(expectedResults, null, 4);
34+
await fs.writeFileSync(filePath, stringResults);
35+
}

0 commit comments

Comments
 (0)