Skip to content

Commit

Permalink
Merge pull request #1 from Chacaponquin/chaca-dev
Browse files Browse the repository at this point in the history
Finish version 1.7.2
  • Loading branch information
Chacaponquin committed Jan 31, 2024
2 parents a979409 + b1230ad commit c177736
Show file tree
Hide file tree
Showing 26 changed files with 176 additions and 138 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## chaca@1.7.2

### 🪛 Fix

- Fix problems with zip exportation in `python` and `postgresql` formats
- Fix `Internet.password` schema

## chaca@1.7.1

### 🌚 Features
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chaca",
"version": "1.7.1",
"version": "1.7.2",
"description": "Create and export mock data with your rules",
"main": "./lib/cjs/index.js",
"module": "./lib/esm/index.mjs",
Expand Down
7 changes: 6 additions & 1 deletion src/core/Export/generators/Python/PythonGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ export class PythonGenerator extends Generator {

const finalCode = this.buildFinalCode(code);
await fs.promises.writeFile(this.route, finalCode, "utf-8");
return this.route;

if (this.zip) {
return await this.createFileZip();
} else {
return this.route;
}
}

private buildFinalCode(pythonCode: string): string {
Expand Down
68 changes: 36 additions & 32 deletions src/core/Export/generators/SQL/SQLGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,42 @@ export class SQLGenerator extends Generator {
this.zip = Boolean(config.zip);
}

public async generateRelationalDataFile(
resolvers: MultiGenerateResolver,
): Promise<string> {
resolvers.getResolvers().forEach((r) => {
const allKeys = r.getKeyNodes();
allKeys.forEach((k) => this.schemasPrimaryKeys.push(k));

const allRefs = r.getRefNodes();
allRefs.forEach((r) => this.schemasForeignKeys.push(r));

const allpossibleNull = r.getPossibleNullNodes();
allpossibleNull.forEach((n) => this.schemaspossibleNull.push(n));
});

resolvers.getResolvers().forEach((r) => {
this.createData(r.getSchemaName(), r.resolve());
});

// change tables id columns
this.allTables.forEach((t) => {
t.updateIdColumnName();
});

await fs.promises.writeFile(
this.route,
this.dataGenerator.getData(),
"utf-8",
);

if (this.zip) {
return await this.createFileZip();
} else {
return this.route;
}
}

public async generateFile(data: any): Promise<string> {
let sqlData: Array<any> = [];

Expand Down Expand Up @@ -405,36 +441,4 @@ export class SQLGenerator extends Generator {
}
}
}

public async generateRelationalDataFile(
resolvers: MultiGenerateResolver,
): Promise<string> {
resolvers.getResolvers().forEach((r) => {
const allKeys = r.getKeyNodes();
allKeys.forEach((k) => this.schemasPrimaryKeys.push(k));

const allRefs = r.getRefNodes();
allRefs.forEach((r) => this.schemasForeignKeys.push(r));

const allpossibleNull = r.getPossibleNullNodes();
allpossibleNull.forEach((n) => this.schemaspossibleNull.push(n));
});

resolvers.getResolvers().forEach((r) => {
this.createData(r.getSchemaName(), r.resolve());
});

// change tables id columns
this.allTables.forEach((t) => {
t.updateIdColumnName();
});

await fs.promises.writeFile(
this.route,
this.dataGenerator.getData(),
"utf-8",
);

return this.route;
}
}
7 changes: 5 additions & 2 deletions src/schemas/internet/InternetSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,25 +195,28 @@ export class InternetSchema {

const len = typeof a.length === "number" && a.length > 0 ? a.length : 15;
const memorable = typeof a.memorable === "boolean" ? a.memorable : false;
const pattern = a.pattern instanceof RegExp ? a.pattern : /w/;
const pattern = a.pattern instanceof RegExp ? a.pattern : consonant;
const prefix = typeof a.prefix === "string" ? a.prefix : "";

const _password = (
length: number,
memorable: boolean,
pattern: RegExp,
i_pattern: RegExp,
prefix: string,
): string => {
if (prefix.length >= length) {
return prefix;
}

let pattern: RegExp = i_pattern;
if (memorable) {
if (prefix.match(consonant)) {
pattern = vowel;
} else {
pattern = consonant;
}
}

const n = this.dataTypeSchema.int().getValue({ min: 0, max: 94 }) + 33;
let char = String.fromCharCode(n);

Expand Down
5 changes: 1 addition & 4 deletions test/library/cases/car.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ describe("# Car Case Test", () => {
it("Creation", () => {
const DATA = chaca.multiGenerate(CASE_SCHEMAS, { verbose: false });
expect(DATA).toHaveProperty("Pay_Method");
expect(DATA).toHaveProperty("Situation");
expect(DATA).toHaveProperty("Category");
expect(DATA).toHaveProperty("Country");
expect(DATA).toHaveProperty("Brand");
expect(DATA).toHaveProperty("Model");
expect(DATA).toHaveProperty("Car");
expect(DATA).toHaveProperty("Driver");
expect(DATA).toHaveProperty("Turist");
expect(DATA).toHaveProperty("Tourist");
expect(DATA).toHaveProperty("Contract");
});

Expand Down
5 changes: 3 additions & 2 deletions test/library/export/csv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { checkFile } from "./utils/export-util";

const fileName = "csvExport";
const ROOT = "./data/csv";
const ext = "csv";

describe("#Export CSV test", () => {
let COMPLETE_SCHEMA_DATA: any;
Expand All @@ -26,7 +27,7 @@ describe("#Export CSV test", () => {
location: ROOT,
});

expect(checkFile(route)).toBe(true);
expect(checkFile({ route, ext: "zip" })).toBe(true);
});
});

Expand Down Expand Up @@ -70,7 +71,7 @@ describe("#Export CSV test", () => {
format: "csv",
});

expect(checkFile(route)).toBe(true);
expect(checkFile({ route, ext })).toBe(true);
});

it("Array of similar objects", async () => {
Expand Down
10 changes: 5 additions & 5 deletions test/library/export/java.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("# Java Export Test", () => {
location: ROOT,
});

expect(checkFile(route)).toBe(true);
expect(checkFile({ route, ext: "zip" })).toBe(true);
});
});

Expand All @@ -40,7 +40,7 @@ describe("# Java Export Test", () => {
location: ROOT,
});

expect(checkFile(route)).toBe(true);
expect(checkFile({ route, ext: "zip" })).toBe(true);
});

it("Simple Schema", async () => {
Expand All @@ -50,7 +50,7 @@ describe("# Java Export Test", () => {
location: ROOT,
});

expect(checkFile(route)).toBe(true);
expect(checkFile({ route, ext: "zip" })).toBe(true);
});

it("Schema Nested Objects", async () => {
Expand All @@ -60,7 +60,7 @@ describe("# Java Export Test", () => {
location: ROOT,
});

expect(checkFile(route)).toBe(true);
expect(checkFile({ route, ext: "zip" })).toBe(true);
});

it("Complete Schema", async () => {
Expand All @@ -70,7 +70,7 @@ describe("# Java Export Test", () => {
location: ROOT,
});

expect(checkFile(route)).toBe(true);
expect(checkFile({ route, ext: "zip" })).toBe(true);
});
});
});
17 changes: 9 additions & 8 deletions test/library/export/javascript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { checkFile } from "./utils/export-util";

const fileName = "javascript";
const ROOT = "./data/javascript";
const ext = "js";

describe("# Javascript Export Test", () => {
describe("Export configuration", () => {
Expand All @@ -17,7 +18,7 @@ describe("# Javascript Export Test", () => {
},
);

expect(checkFile(route)).toBe(true);
expect(checkFile({ route, ext: "zip" })).toBe(true);
});
});

Expand All @@ -29,7 +30,7 @@ describe("# Javascript Export Test", () => {
location: ROOT,
});

expect(checkFile(route)).toBe(true);
expect(checkFile({ route, ext })).toBe(true);
});

it("Export null", async () => {
Expand All @@ -39,7 +40,7 @@ describe("# Javascript Export Test", () => {
location: ROOT,
});

expect(checkFile(route)).toBe(true);
expect(checkFile({ route, ext })).toBe(true);
});

it("Export number", async () => {
Expand All @@ -49,7 +50,7 @@ describe("# Javascript Export Test", () => {
location: ROOT,
});

expect(checkFile(route)).toBe(true);
expect(checkFile({ route, ext })).toBe(true);
});

it("Export boolean", async () => {
Expand All @@ -59,7 +60,7 @@ describe("# Javascript Export Test", () => {
location: ROOT,
});

expect(checkFile(route)).toBe(true);
expect(checkFile({ route, ext })).toBe(true);
});
});

Expand All @@ -71,7 +72,7 @@ describe("# Javascript Export Test", () => {
location: ROOT,
});

expect(checkFile(route)).toBe(true);
expect(checkFile({ route, ext })).toBe(true);
});
});

Expand All @@ -83,7 +84,7 @@ describe("# Javascript Export Test", () => {
location: ROOT,
});

expect(checkFile(route)).toBe(true);
expect(checkFile({ route, ext })).toBe(true);
});
});

Expand All @@ -97,6 +98,6 @@ describe("# Javascript Export Test", () => {
},
);

expect(checkFile(route)).toBe(true);
expect(checkFile({ route, ext })).toBe(true);
});
});
12 changes: 8 additions & 4 deletions test/library/export/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ describe("# JSON Export Test", () => {
},
);

expect(checkFile(route)).toBe(true);
expect(checkFile({ route, ext: "zip" })).toBe(true);
});

it("Pass separate=false. Should create a single json file", async () => {
await chaca.exportFromSchemas(
const route = await chaca.exportFromSchemas(
[
{
documents: COUNT_DOCUMENTS,
Expand All @@ -43,17 +43,19 @@ describe("# JSON Export Test", () => {
],
{
fileName: fileName + "NotSeparate",
format: { ext: "json", separate: false, zip: false },
format: { ext: "json", separate: false, zip: true },
location: ROOT,
},
{ verbose: false },
);

expect(checkFile({ route, ext: "zip" })).toBe(true);
});

it("Pass separate=true. Should create multiple files", async () => {
const file = fileName + "Separate";

await chaca.exportFromSchemas(
const route = await chaca.exportFromSchemas(
[
{
documents: COUNT_DOCUMENTS,
Expand All @@ -78,5 +80,7 @@ describe("# JSON Export Test", () => {
},
{ verbose: false },
);

expect(checkFile({ route, ext: "zip" })).toBe(true);
});
});
Loading

0 comments on commit c177736

Please sign in to comment.