Skip to content

Commit

Permalink
feat(test-data-generator): Output required Test Interface Actions (#657)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukehesluke authored Mar 19, 2024
1 parent 9423327 commit dd45abd
Show file tree
Hide file tree
Showing 8 changed files with 662 additions and 1,541 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"debug-tests": "cd packages/openactive-integration-tests && npm run debug",
"certificate-validator": "cd packages/openactive-integration-tests && npm run certificate-validator",
"doc-gen": "cd packages/openactive-integration-tests && npm run doc-gen",
"git-add-newly-generated-integration-test-docs": "git add \"packages/openactive-integration-tests/test/**/README.md\" packages/openactive-integration-tests/test/features/criteria-requirements.json",
"git-add-newly-generated-integration-test-docs": "git add \"packages/openactive-integration-tests/test/**/README.md\" packages/openactive-integration-tests/test/features/feature-requirements.json",
"gen-interface-criteria-types": "cd packages/test-interface-criteria && npm run gen-types",
"git-add-newly-generated-interface-criteria-types": "git add packages/test-interface-criteria/built-types",
"test-data-generator": "cd packages/openactive-integration-tests && npm run test-data-generator",
Expand Down
46 changes: 31 additions & 15 deletions packages/openactive-integration-tests/documentation/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { FeatureJsonSchema } = require('./featureJson.js');

const FEATURES_ROOT = path.join(__dirname, '..', 'test', 'features');
const INDEX_README_FILE = path.join(FEATURES_ROOT, 'README.md');
const INDEX_CRITERIA_REQUIREMENTS_JSON_FILE = path.join(FEATURES_ROOT, 'criteria-requirements.json');
const INDEX_FEATURE_REQUIREMENTS_JSON_FILE = path.join(FEATURES_ROOT, 'feature-requirements.json');
const INDEX_CATEGORIES_JSON_FILE = path.join(FEATURES_ROOT, 'categories.json');
const INDEX_TESTS_IMPLEMENTED_JSON_FILE = path.join(FEATURES_ROOT, 'tests-implemented.json');

Expand All @@ -32,12 +32,15 @@ const INDEX_TESTS_IMPLEMENTED_JSON_FILE = path.join(FEATURES_ROOT, 'tests-implem
*
* @typedef {{
* _createdByDocumentationGeneratorScript: true,
* criteriaRequirements: {
* features: {
* [featureIdentifier: string]: {
* [sellerCriteria in SellerCriteria]?: OpportunityCriteriaRequirementsObj;
* },
* },
* }} CriteriaRequirementsJson
* criteriaRequirements: {
* [sellerCriteria in SellerCriteria]?: OpportunityCriteriaRequirementsObj;
* };
* testInterfaceActionImplementationRequirements: string[];
* };
* };
* }} FeatureRequirementsJson
*
* @typedef {{
* _createdByDocumentationGeneratorScript: true,
Expand Down Expand Up @@ -145,8 +148,8 @@ writeFileSetErrorExitCodeButDontThrowIfFails(
// This file will be used by the test-data-generator script to help seed random
// mode tests.
writeFileSetErrorExitCodeButDontThrowIfFails(
INDEX_CRITERIA_REQUIREMENTS_JSON_FILE,
renderCriteraRequirementsJson(featureMetadata),
INDEX_FEATURE_REQUIREMENTS_JSON_FILE,
renderFeatureRequirementsJson(featureMetadata),
);

// Save categories information to a machine-readable (JSON) file.
Expand Down Expand Up @@ -364,21 +367,34 @@ function renderTestInterfaceActionImplementationRequirements(testInterfaceAction
/**
* @param {FeatureMetadataItem[]} features
*/
function renderCriteraRequirementsJson(features) {
/** @type {CriteriaRequirementsJson} */
function renderFeatureRequirementsJson(features) {
/** @type {FeatureRequirementsJson} */
const obj = {
_createdByDocumentationGeneratorScript: true,
criteriaRequirements: Object.fromEntries(features.map(feature => ([
features: Object.fromEntries(features.map(feature => ([
feature.identifier,
Object.fromEntries(Array.from(feature.sellerCriteriaRequirements).map(([sellerCriteria, tallyByCriteria]) => ([
sellerCriteria,
Object.fromEntries(tallyByCriteria),
]))),
{
criteriaRequirements: renderFeatureCriteriaRequirements(feature),
testInterfaceActionImplementationRequirements: [
...feature.testInterfaceActionImplementationRequirements,
],
},
]))),
};
return JSON.stringify(obj, null, 2);
}

/**
* @param {FeatureMetadataItem} feature
* @returns {FeatureRequirementsJson['features'][string]['criteriaRequirements']}
*/
function renderFeatureCriteriaRequirements(feature) {
return Object.fromEntries(Array.from(feature.sellerCriteriaRequirements).map(([sellerCriteria, tallyByCriteria]) => ([
sellerCriteria,
Object.fromEntries(tallyByCriteria),
])));
}

/**
* @param {FeatureMetadataItem[]} features
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
test-data/test-data.json
output/
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

Command line tool to generate a distribution definition of test data based on the configured features.

Test data is outputted to `./test-data/test-data.json` by default.
The data outputted by this tool can be used for the following purposes:

The data outputted by this tool can be used by a script to add sufficient test data into your [Open Booking API](https://openactive.io/open-booking-api/EditorsDraft/) implementation's database for testing when using [random mode](../README.md#userandomopportunities).
- Used by a script to add sufficient test data into your [Open Booking API](https://openactive.io/open-booking-api/EditorsDraft/) implementation's database for testing when using [random mode](../README.md#userandomopportunities).
- Used to determine which [Test Interface Actions](https://openactive.io/test-interface/#actions-endpoint) (e.g. `test:AttendeeAbsentSimulateAction`) are required to be supported by your [Open Booking API](https://openactive.io/open-booking-api/EditorsDraft/) implementation.

Test data is outputted to the following files by default:

- `./output/opportunity-test-data.json` ([more info](#opportunity-test-datajson-file-format))
- `./output/test-interface-actions.json` ([more info](#test-interface-actionsjson-file-format))

## Usage

Expand All @@ -13,10 +19,12 @@ npm run test-data-generator # all active features specified in the current confi

npm run test-data-generator -- common-error-conditions # only enough data for the common-error-conditions feature

npm run test-data-generator -- --output ./test-data.json # output to ./test-data.json
npm run test-data-generator -- --output-dir ../../../test-data-generator-output # output to ../../../test-data-generator-output
```

## Test data file format
## `opportunity-test-data.json` file format

This file can be used by a script to add sufficient test data into your [Open Booking API](https://openactive.io/open-booking-api/EditorsDraft/) implementation's database for testing when using [random mode](../README.md#userandomopportunities).

The `item` within the file format is identical to the Controlled Opportunity Creation endpoint defined within the [OpenActive Test Interface](https://openactive.io/test-interface/). This allows an implementation to use the same logic to generate data from the test data file and from the test interface.

Expand Down Expand Up @@ -71,19 +79,50 @@ The `item` within the file format is identical to the Controlled Opportunity Cre
}
```

## `test-interface-actions.json` file format

This file can be used to determine which [Test Interface Actions](https://openactive.io/test-interface/#actions-endpoint) (e.g. `test:AttendeeAbsentSimulateAction`) are required to be supported by your [Open Booking API](https://openactive.io/open-booking-api/EditorsDraft/) implementation.

This example shows that the `test:AccessChannelUpdateSimulateAction` and `test:AccessCodeUpdateSimulateAction` [Test Interface Actions](https://openactive.io/test-interface/#actions-endpoint) need to be implemented.

```json
{
"@context": [
"https://openactive.io/",
"https://openactive.io/test-interface"
],
"@type": "ItemList",
"numberOfItems": 2,
"itemListElement": [
{
"@type": "ListItem",
"item": {
"@type": "test:AccessChannelUpdateSimulateAction"
}
},
{
"@type": "ListItem",
"item": {
"@type": "test:AccessCodeUpdateSimulateAction"
}
}
]
}
```

## Use with reference implementation in development

Run the generator:
```bash
$ npm run test-data-generator

Test data file created containing a distribution with 482 items:
/example/path/test-data/test-data.json
/example/path/output/opportunity-test-data.json
```

Take the output path, and set the environment variable `OpenActiveTestDataFile` in `AppSettings.Development.json`:
```json
"OpenActiveTestDataFile": "/example/path/test-data/test-data.json"
"OpenActiveTestDataFile": "/example/path/output/opportunity-test-data.json"
```

```
Expand All @@ -94,6 +133,6 @@ dotnet run

```bash
npm run test-data-generator
export OpenActiveTestDataFile=/example/path/test-data/test-data.json
export OpenActiveTestDataFile=/example/path/output/opportunity-test-data.json
dotnet run
```
Loading

0 comments on commit dd45abd

Please sign in to comment.