Skip to content

Commit

Permalink
Merge pull request #2321 from artilleryio/bernardobridge/add-fake-dat…
Browse files Browse the repository at this point in the history
…a-plugin-to-types-dynamically

feat(schema): add artillery-plugin-fake-data to schema
  • Loading branch information
bernardobridge committed Dec 1, 2023
2 parents 4d794e3 + b1b2c80 commit 6b3611a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 16 deletions.
1 change: 1 addition & 0 deletions .github/workflows/npm-publish-artillery-types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
node-version: '18.x'
registry-url: 'https://registry.npmjs.org'
scope: '@artilleryio'
- run: PACKAGE_FOLDER_NAME=types node .github/workflows/scripts/rename-packages-to-correct-version.js
- run: npm ci
- run: npm -w '@artilleryio/types' publish --access public
env:
Expand Down
3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 22 additions & 15 deletions packages/artillery-plugin-fake-data/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
const falso = require('@ngneat/falso');

const getFalsoFunctions = () =>
Object.keys(falso).filter((funcName) => {
//functions that have the function signature we expect start with rand and aren't == rand (which takes an array)
if (!funcName.startsWith('rand') && funcName != 'rand') {
return false;
}

//don't add functions that have more than 1 argument, as we only support 1 argument
//we can look into adding support for more arguments later, but most of the functions available use 1 argument anyway
if (falso[funcName].length > 1) {
return false;
}

return true;
});

const falsoFunctions = getFalsoFunctions();

function ArtilleryPluginFakeData(script, events) {
this.script = script;
this.events = events;
Expand All @@ -8,26 +26,14 @@ function ArtilleryPluginFakeData(script, events) {
script.config['fake-data'] || script.config.plugins['fake-data'];

function falsoHandler(context, ee, next) {
for (let funcName of Object.keys(falso)) {
//functions that have the function signature we expect start with rand and aren't == rand (which takes an array
//e.g. seed,
if (!funcName.startsWith('rand') && funcName != 'rand') {
continue;
}

//don't add functions that have more than 1 argument, as we only support 1 argument
//we can look into adding support for more arguments later, but most of the functions available use 1 argument anyway
if (falso[funcName].length > 1) {
continue;
}

falsoFunctions.forEach((funcName) => {
context.funcs[`$${funcName}`] = function () {
if (pluginConfig[funcName]) {
return falso[funcName](pluginConfig[funcName]);
}
return falso[funcName]();
};
}
});

next();
}
Expand All @@ -48,5 +54,6 @@ function ArtilleryPluginFakeData(script, events) {
}

module.exports = {
Plugin: ArtilleryPluginFakeData
Plugin: ArtilleryPluginFakeData,
getFalsoFunctions
};
3 changes: 3 additions & 0 deletions packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@
"tap": "^16.3.8",
"ts-node": "^10.9.1",
"typescript-json-schema": "^0.59.0"
},
"dependencies": {
"artillery-plugin-fake-data": "*"
}
}
4 changes: 3 additions & 1 deletion packages/types/schema/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const {
const {
PublishMetricsPluginConfigSchema
} = require('./plugins/publish-metrics');
const { FakeDataPlugin } = require('./plugins/fake-data');
const { TestPhase } = require('./config/phases');

const TlsConfig = Joi.object({
Expand Down Expand Up @@ -85,7 +86,8 @@ const ArtilleryBuiltInPlugins = {
ensure: EnsurePluginConfigSchema,
apdex: ApdexPluginConfigSchema,
'metrics-by-endpoint': MetricsByEndpointPluginConfigSchema,
'publish-metrics': PublishMetricsPluginConfigSchema
'publish-metrics': PublishMetricsPluginConfigSchema,
'fake-data': FakeDataPlugin
};

const ArtilleryBuiltInPluginsInRootConfig = (({ ensure, apdex }) => ({
Expand Down
30 changes: 30 additions & 0 deletions packages/types/schema/plugins/fake-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const Joi = require('joi');
const falso = require('@ngneat/falso');
const { getFalsoFunctions } = require('artillery-plugin-fake-data');

const buildFalsoObject = () => {
let falsoJoiObject = {};

for (let funcName of getFalsoFunctions()) {
falsoJoiObject[funcName] = Joi.object()
.meta({ title: `Falso Function: ${funcName}` })
.description(
'For information on what parameters this function takes, check the falso documentation: https://ngneat.github.io/falso/docs/getting-started'
);
}

return falsoJoiObject;
};

const FakeDataPlugin = Joi.object({
...buildFalsoObject()
})
.unknown(false)
.meta({ title: 'Fake Data Plugin' })
.description(
'This plugin adds access to random realistic test data generation using falso. You can configure the parameters of each function in the config. \nFor more information, check our documentation: https://www.artillery.io/docs/reference/extensions/fake-data'
);

module.exports = {
FakeDataPlugin
};

0 comments on commit 6b3611a

Please sign in to comment.