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

fix(typescript): correctly dump typescript files when collections name are complexe #653

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
test: add tests
SteveBunlon committed Dec 7, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 1d599e9b300387eb29ee61beeefb60fe1f07e49f
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"_special:character": {
"fields": [{
"name": "name",
"type": "String"
}],
"options": {
"timestamps": false
},
"primaryKeys": [
"_id"
],
"references": []
},
":otherSpecial*character": {
"fields": [{
"name": "age",
"type": "Number"
}],
"options": {
"timestamps": false
},
"primaryKeys": [
"_id"
],
"references": []
}
}
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import rimraf from 'rimraf';
import defaultPlan from '../../../../src/context/plan';
import AgentNodeJsDumper from '../../../../src/services/dumpers/agent-nodejs';
import languages from '../../../../src/utils/languages';
import collectionsWithSpecialCharacters from '../../analyzer/expected/mongo/db-analysis-output/collections-with-special-characters.expected.json';
import deepNestedColumn from '../../analyzer/expected/mongo/db-analysis-output/deep-nested-fields-column.expected.json';
import deepNested from '../../analyzer/expected/mongo/db-analysis-output/deep-nested-fields.expected.json';
import hasMany from '../../analyzer/expected/mongo/db-analysis-output/hasmany.expected.json';
@@ -150,6 +151,43 @@ describe('services > dumpers > agentNodejsDumper > mongoose models', () => {
'Ignoring field so:column from collection persons as it contains column and is not valid.',
);
});

it('should correctly dump interfaces and classes when collection has special characters', async () => {
expect.assertions(3);

rimraf.sync(`${appRoot}/test-output/${language.name}/mongodb/`);

await dump(language, collectionsWithSpecialCharacters);

const expectedIndex = fs.readFileSync(
`${__dirname}/expected/${language.name}/mongo-models/collection-special-characters/index.${language.fileExtension}`,
'utf-8',
);
const generatedIndex = fs.readFileSync(
`${appRoot}/test-output/${language.name}/mongodb/models/index.${language.fileExtension}`,
'utf8',
);
const expectedOtherSpecialCharacter = fs.readFileSync(
`${__dirname}/expected/${language.name}/mongo-models/collection-special-characters/other-special-character.expected.${language.fileExtension}`,
'utf-8',
);
const generatedOtherSpecialCharacter = fs.readFileSync(
`${appRoot}/test-output/${language.name}/mongodb/models/other-special-character.${language.fileExtension}`,
'utf8',
);
const expectedSpecialCharacter = fs.readFileSync(
`${__dirname}/expected/${language.name}/mongo-models/collection-special-characters/special-character.expected.${language.fileExtension}`,
'utf-8',
);
const generatedSpecialCharacter = fs.readFileSync(
`${appRoot}/test-output/${language.name}/mongodb/models/special-character.${language.fileExtension}`,
'utf8',
);

expect(generatedIndex).toStrictEqual(expectedIndex);
expect(generatedSpecialCharacter).toStrictEqual(expectedSpecialCharacter);
expect(generatedOtherSpecialCharacter).toStrictEqual(expectedOtherSpecialCharacter);
});
});
});

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const fs = require('fs');
const path = require('path');
const Mongoose = require('mongoose');

const connection = Mongoose.createConnection(process.env.DATABASE_URL);

fs
.readdirSync(__dirname)
.filter((file) => file !== 'index.js')
.forEach((file) => {
try {
const { schema, modelName, collectionName } = require(path.join(__dirname, file));
connection.model(modelName, schema, collectionName);
} catch (error) {
console.error(`Model creation error: ${error}`);
}
});

module.exports = connection;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const Mongoose = require('mongoose');

const schema = new Mongoose.Schema({
age: Number,
}, {
timestamps: false,
});

module.exports = {
collectionName: ':otherSpecial*character',
modelName: 'otherSpecialCharacter',
schema,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const Mongoose = require('mongoose');

const schema = new Mongoose.Schema({
name: String,
}, {
timestamps: false,
});

module.exports = {
collectionName: '_special:character',
modelName: 'specialCharacter',
schema,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { OtherSpecialCharacterInterface } from './other-special-character';
import type { SpecialCharacterInterface } from './special-character';

import Mongoose from 'mongoose';

import { otherSpecialCharacterSchema } from './other-special-character';
import { specialCharacterSchema } from './special-character';

const connection = Mongoose.createConnection(process.env.DATABASE_URL);

export const otherSpecialCharacter = connection.model<OtherSpecialCharacterInterface>('otherSpecialCharacter', otherSpecialCharacterSchema, ':otherSpecial*character');
export const specialCharacter = connection.model<SpecialCharacterInterface>('specialCharacter', specialCharacterSchema, '_special:character');

export default connection;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Mongoose from 'mongoose';

interface OtherSpecialCharacterInterface {
age: number;
}

const otherSpecialCharacterSchema = new Mongoose.Schema({
age: Number,
}, {
timestamps: false,
});

export { OtherSpecialCharacterInterface, otherSpecialCharacterSchema };
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Mongoose from 'mongoose';

interface SpecialCharacterInterface {
name: string;
}

const specialCharacterSchema = new Mongoose.Schema({
name: String,
}, {
timestamps: false,
});

export { SpecialCharacterInterface, specialCharacterSchema };