Skip to content

Commit

Permalink
Refactor Output into a structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Aug 30, 2024
1 parent 55e85cd commit e0b1f33
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 44 deletions.
10 changes: 8 additions & 2 deletions TESTING_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,15 @@ Consider the following chapters in [ml/model_groups](tests/plugins/ml/ml/model_g
```
As you can see, the `output` field in the first chapter saves the `model_group_id` from the response body. This value is then used in the subsequent chapters to query and delete the model group.
You can also supply defaults in both output and input values, e.g. `version: payload.version ? 0` or `${weights._version ? 0}` used in [cluster/routing/awareness/weights.yaml](tests/routing/cluster/routing/awareness/weights.yaml).
You can also supply defaults for output values, e.g. for `payload._version` used in [cluster/routing/awareness/weights.yaml](tests/routing/cluster/routing/awareness/weights.yaml).
You can also reuse output in payload expectations. See [tests/plugins/index_state_management/nodes/plugins/index_state_management.yaml](tests/plugins/index_state_management/nodes/plugins/index_state_management.yaml) for an example.
```
version:
path: payload._version
default: -1
```
You can reuse output in payload expectations. See [tests/plugins/index_state_management/nodes/plugins/index_state_management.yaml](tests/plugins/index_state_management/nodes/plugins/index_state_management.yaml) for an example.
### Managing Versions
Expand Down
16 changes: 15 additions & 1 deletion json_schemas/test_story.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,22 @@ definitions:
- `headers.<header-name>` for the headers.
type: object
additionalProperties:
type: string
anyOf:
- type: string
- $ref: '#/definitions/DetailedOutput'

DetailedOutput:
properties:
path:
type: string
default:
type:
- boolean
- number
- string
required:
- path

Version:
description: |
The semver range to execute the story or chapter against.
Expand Down
8 changes: 6 additions & 2 deletions tests/routing/cluster/decommission/awareness.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ prologues:
path: /_cluster/routing/awareness/zone/weights
method: GET
output:
version: payload._version ? -1
version:
path: payload._version
default: -1
- path: /_cluster/routing/awareness/zone/weights
method: PUT
request:
Expand All @@ -23,7 +25,9 @@ epilogues:
path: /_cluster/routing/awareness/zone/weights
method: GET
output:
version: payload._version ? -1
version:
path: payload._version
default: -1
- path: /_cluster/routing/awareness/weights
method: DELETE
request:
Expand Down
8 changes: 6 additions & 2 deletions tests/routing/cluster/decommission/status.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ prologues:
path: /_cluster/routing/awareness/zone/weights
method: GET
output:
version: payload._version ? -1
version:
path: payload._version
default: -1
- path: /_cluster/routing/awareness/zone/weights
method: PUT
request:
Expand All @@ -25,7 +27,9 @@ epilogues:
path: /_cluster/routing/awareness/zone/weights
method: GET
output:
version: payload._version ? -1
version:
path: payload._version
default: -1
- path: /_cluster/routing/awareness/weights
method: DELETE
request:
Expand Down
12 changes: 9 additions & 3 deletions tests/routing/cluster/routing/awareness/weights.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ epilogues:
path: /_cluster/routing/awareness/zone/weights
method: GET
output:
version: payload._version ? -1
version:
path: payload._version
default: -1
- path: /_cluster/routing/awareness/weights
method: DELETE
request:
Expand All @@ -21,7 +23,9 @@ chapters:
parameters:
attribute: zone
output:
version: payload._version ? -1
version:
path: payload._version
default: -1
- synopsis: Update zone routing weights.
path: /_cluster/routing/awareness/{attribute}/weights
method: PUT
Expand All @@ -42,7 +46,9 @@ chapters:
parameters:
attribute: zone
output:
version: payload._version ? -1
version:
path: payload._version
default: -1
- synopsis: Delete zone routing weights.
path: /_cluster/routing/awareness/weights
method: DELETE
Expand Down
9 changes: 4 additions & 5 deletions tools/src/tester/ChapterOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import { EvaluationWithOutput, Result } from './types/eval.types'
import { ActualResponse, type Output } from './types/story.types'
import _ from 'lodash'
import YAML from 'yaml'

export class ChapterOutput {
private outputs: Record<string, any>
Expand All @@ -30,16 +29,16 @@ export class ChapterOutput {
static extract_output_values(response: ActualResponse, output?: Output): EvaluationWithOutput {
if (!output) return { evaluation: { result: Result.SKIPPED } }
const chapter_output = new ChapterOutput({})
for (const [name, path] of Object.entries(output)) {
for (const [name, output_value] of Object.entries(output)) {
const path: string = typeof output_value === 'string' ? output_value : output_value.path
const default_value = typeof output_value === 'string' ? undefined : output_value.default
let value: any
if (path == 'payload' || path.startsWith('payload.') || path.match(/^payload\[\d*\]/)) {
if (response.payload === undefined) {
return { evaluation: { result: Result.ERROR, message: 'No payload found in response, but expected output: ' + path } }
}
value = _.get(response, path)
const rhs = path.replaceAll(' ', '').split('?', 2)
value = _.get(response, rhs[0])
if (value === undefined && rhs[1] !== undefined) value = YAML.parse(rhs[1])
if (value === undefined) value = default_value
if (value === undefined) {
return { evaluation: { result: Result.ERROR, message: `Expected to find non undefined value at \`${path}\`.` } }
}
Expand Down
2 changes: 1 addition & 1 deletion tools/src/tester/StoryOutputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class StoryOutputs {
resolve_string (str: string): any {
const ref = OutputReference.parse(str)
if (ref) {
return this.get_output_value(ref.chapter_id, ref.output_name) ?? ref.default_value
return this.get_output_value(ref.chapter_id, ref.output_name)
} else {
return str
}
Expand Down
11 changes: 3 additions & 8 deletions tools/src/tester/types/eval.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import { type ChapterOutput } from '../ChapterOutput'
import { StoryOutputs } from '../StoryOutputs'
import type { Story } from "./story.types";
import YAML from 'yaml'

export interface StoryFile {
display_path: string
Expand Down Expand Up @@ -88,19 +87,15 @@ export enum Result {
export class OutputReference {
chapter_id: string
output_name: string
default_value?: string
private constructor (chapter_id: string, output_name: string, default_value?: string) {
private constructor (chapter_id: string, output_name: string) {
this.chapter_id = chapter_id
this.output_name = output_name
this.default_value = default_value
}

static parse (str: string): OutputReference | undefined {
if (str.startsWith('${') && str.endsWith('}')) {
const spl = str.slice(2, -1).replaceAll(' ', '').split('.', 2)
const rhs = spl[1].split('?', 2)
let default_value = rhs.length >= 2 && rhs[1] !== undefined ? YAML.parse(rhs[1]) : undefined
return { chapter_id: spl[0], output_name: rhs[0], default_value }
const spl = str.slice(2, -1).split('.', 2)
return { chapter_id: spl[0], output_name: spl[1] }
}
return undefined
}
Expand Down
10 changes: 9 additions & 1 deletion tools/src/tester/types/story.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,15 @@ export interface Request {
* via the `definition` "Output".
*/
export interface Output {
[k: string]: string;
[k: string]: string | DetailedOutput;
}
/**
* This interface was referenced by `Story`'s JSON-Schema
* via the `definition` "DetailedOutput".
*/
export interface DetailedOutput {
path: string;
default?: boolean | number | string;
}
/**
* The list of distributions that support this API.
Expand Down
12 changes: 3 additions & 9 deletions tools/tests/tester/ChapterOutput.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,19 @@ describe('with an object response', () => {
})

test('uses a default numeric value', () => {
expect(ChapterOutput.extract_output_values(response, { x: 'payload.a.b.x[0] ? -1' })).toEqual(
expect(ChapterOutput.extract_output_values(response, { x: { path: 'payload.a.b.x[0]', default: -1 } })).toEqual(
passed_output({ x: -1 })
)
})

test('uses a default string value', () => {
expect(ChapterOutput.extract_output_values(response, { x: 'payload.a.b.x[0] ? a_string' })).toEqual(
expect(ChapterOutput.extract_output_values(response, { x: { path: 'payload.a.b.x[0]', default: 'a_string' } })).toEqual(
passed_output({ x: 'a_string' })
)
})

test('uses a default quoted string value', () => {
expect(ChapterOutput.extract_output_values(response, { x: 'payload.a.b.x[0] ? \'0\'' })).toEqual(
passed_output({ x: '0' })
)
})

test('does not use a default value on a numeric zero', () => {
expect(ChapterOutput.extract_output_values(response, { x: 'payload.zero ? -1' })).toEqual(
expect(ChapterOutput.extract_output_values(response, { x: { path: 'payload.zero', default: '-1' } })).toEqual(
passed_output({ x: 0 })
)
})
Expand Down
10 changes: 0 additions & 10 deletions tools/tests/tester/StoryOutputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ test('resolve_string', () => {
expect(story_outputs.resolve_string('some_str')).toEqual('some_str')
})

test('resolve_string defaults', () => {
expect(story_outputs.resolve_string('${chapter_id.n ? x}')).toEqual('x')
expect(story_outputs.resolve_string('${chapter_id.n ? 0}')).toEqual(0)
expect(story_outputs.resolve_string('${chapter_id.z ? -1}')).toEqual(0)
expect(story_outputs.resolve_string('${chapter_id.n ? a_string }')).toEqual('a_string')
expect(story_outputs.resolve_string('${chapter_id.n ? \'-1\'}')).toEqual('-1')
expect(story_outputs.resolve_string('${chapter_id.z ? a_string }')).toEqual(0)
expect(story_outputs.resolve_string('${chapter_id.z ? \'-1\'}')).toEqual(0)
})

test('resolve_value', () => {
const value = {
a: '${chapter_id.x}',
Expand Down

0 comments on commit e0b1f33

Please sign in to comment.