naming strategy test
This commit is contained in:
parent
3da7cf22cd
commit
20f3e24dc8
@ -18,7 +18,8 @@ export default function modelCustomizationPhase(
|
||||
) {
|
||||
// eslint-disable-next-line global-require, import/no-dynamic-require, @typescript-eslint/no-var-requires
|
||||
const req = require(generationOptions.customNamingStrategyPath);
|
||||
namingStrategy = new req.NamingStrategy();
|
||||
// eslint-disable-next-line new-cap
|
||||
namingStrategy = new req.default();
|
||||
} else {
|
||||
namingStrategy = new NamingStrategy();
|
||||
}
|
||||
|
@ -5,6 +5,10 @@ import { RelationId } from "./models/RelationId";
|
||||
|
||||
import changeCase = require("change-case");
|
||||
|
||||
// TODO: Use function instead of class
|
||||
// TODO: Allow users to change only specific functions instead of all of them(with logging if used standard or user function)
|
||||
// TODO: Fix naming strategy relative path
|
||||
|
||||
/* eslint-disable class-methods-use-this */
|
||||
export default class NamingStrategy extends AbstractNamingStrategy {
|
||||
public relationIdName(relationId: RelationId, relation: Relation): string {
|
||||
|
@ -51,7 +51,7 @@ describe("Model customization phase", async () => {
|
||||
tscName: "PostAuthor",
|
||||
database: "",
|
||||
schema: "public",
|
||||
fileImports: ["Post"]
|
||||
fileImports: []
|
||||
},
|
||||
{
|
||||
columns: [
|
||||
@ -105,7 +105,7 @@ describe("Model customization phase", async () => {
|
||||
tscName: "Post",
|
||||
database: "",
|
||||
schema: "public",
|
||||
fileImports: ["PostAuthor"]
|
||||
fileImports: []
|
||||
}
|
||||
];
|
||||
|
||||
@ -564,4 +564,46 @@ describe("Model customization phase", async () => {
|
||||
compileGeneratedModel(generationOptions.resultsPath, [""]);
|
||||
});
|
||||
});
|
||||
it("naming strategy", async () => {
|
||||
const data = generateSampleData();
|
||||
const generationOptions = generateGenerationOptions();
|
||||
clearGenerationDir();
|
||||
|
||||
generationOptions.customNamingStrategyPath =
|
||||
"../test/modelCustomization/testNamingStrategy.ts";
|
||||
// TODO: relationId
|
||||
|
||||
const customizedModel = modelCustomizationPhase(
|
||||
data,
|
||||
generationOptions,
|
||||
{}
|
||||
);
|
||||
modelGenerationPhase(
|
||||
new IConnectionOptions(),
|
||||
generationOptions,
|
||||
customizedModel
|
||||
);
|
||||
const filesGenPath = path.resolve(resultsPath, "entities");
|
||||
const postContent = fs
|
||||
.readFileSync(path.resolve(filesGenPath, "Post_B.ts"))
|
||||
.toString();
|
||||
const postAuthorContent = fs
|
||||
.readFileSync(path.resolve(filesGenPath, "PostAuthor_B.ts"))
|
||||
.toString();
|
||||
expect(postContent).to.have.string(`@Entity("Post"`);
|
||||
expect(postContent).to.have.string(`class Post_B {`);
|
||||
expect(postContent).to.have.string(`id_C: number;`);
|
||||
expect(postContent).to.have.string(`author_A: PostAuthor_B`);
|
||||
expect(postContent).to.have.string(
|
||||
`import { PostAuthor_B } from "./PostAuthor_B";`
|
||||
);
|
||||
expect(postAuthorContent).to.have.string(`@Entity("PostAuthor"`);
|
||||
expect(postAuthorContent).to.have.string(`class PostAuthor_B`);
|
||||
expect(postAuthorContent).to.have.string(`id_C: number;`);
|
||||
expect(postAuthorContent).to.have.string(
|
||||
`import { Post_B } from "./Post_B";`
|
||||
);
|
||||
|
||||
compileGeneratedModel(generationOptions.resultsPath, [""]);
|
||||
});
|
||||
});
|
||||
|
24
test/modelCustomization/testNamingStrategy.ts
Normal file
24
test/modelCustomization/testNamingStrategy.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import StandardNamingStrategy from "../../src/NamingStrategy";
|
||||
import { RelationId } from "../../src/models/RelationId";
|
||||
import { Relation } from "../../src/models/Relation";
|
||||
|
||||
/* eslint-disable class-methods-use-this */
|
||||
export default class NamingStrategy extends StandardNamingStrategy {
|
||||
public relationIdName(relationId: RelationId, relation: Relation): string {
|
||||
return `${super.relationIdName(relationId, relation)}`;
|
||||
}
|
||||
|
||||
public relationName(relation: Relation): string {
|
||||
return `${super.relationName(relation)}_A`;
|
||||
}
|
||||
|
||||
public entityName(entityName: string): string {
|
||||
return `${super.entityName(entityName)}_B`;
|
||||
}
|
||||
|
||||
public columnName(columnName: string): string {
|
||||
return `${super.columnName(columnName)}_C`;
|
||||
}
|
||||
}
|
||||
|
||||
/* eslint-enable class-methods-use-this */
|
Loading…
Reference in New Issue
Block a user