Skip to content

Commit

Permalink
add test for runtime partial update
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkime committed Jun 12, 2023
1 parent d7698ba commit ed710db
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 25 deletions.
31 changes: 27 additions & 4 deletions src/plugins/data_views/common/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ export const runtimeFieldNonCompositeFieldsSpecTypeSchema = schema.oneOf(
]
);

export const primitiveRuntimeFieldSchema = schema.object({
type: runtimeFieldNonCompositeFieldsSpecTypeSchema,
const primitiveRuntimeFieldSchemaShared = {
script: schema.maybe(
schema.object({
source: schema.string(),
Expand All @@ -44,10 +43,19 @@ export const primitiveRuntimeFieldSchema = schema.object({
min: 0,
})
),
};

export const primitiveRuntimeFieldSchema = schema.object({
type: runtimeFieldNonCompositeFieldsSpecTypeSchema,
...primitiveRuntimeFieldSchemaShared,
});

export const compositeRuntimeFieldSchema = schema.object({
type: schema.literal('composite') as Type<RuntimeType>,
const primitiveRuntimeFieldSchemaUpdate = schema.object({
type: schema.maybe(runtimeFieldNonCompositeFieldsSpecTypeSchema),
...primitiveRuntimeFieldSchemaShared,
});

const compositeRuntimeFieldSchemaShared = {
script: schema.maybe(
schema.object({
source: schema.string(),
Expand All @@ -68,13 +76,28 @@ export const compositeRuntimeFieldSchema = schema.object({
})
)
),
};

export const compositeRuntimeFieldSchema = schema.object({
type: schema.literal('composite') as Type<RuntimeType>,
...compositeRuntimeFieldSchemaShared,
});

const compositeRuntimeFieldSchemaUpdate = schema.object({
type: schema.maybe(schema.literal('composite') as Type<RuntimeType>),
...compositeRuntimeFieldSchemaShared,
});

export const runtimeFieldSchema = schema.oneOf([
primitiveRuntimeFieldSchema,
compositeRuntimeFieldSchema,
]);

export const runtimeFieldSchemaUpdate = schema.oneOf([
primitiveRuntimeFieldSchemaUpdate,
compositeRuntimeFieldSchemaUpdate,
]);

export const fieldSpecSchemaFields = {
name: schema.string({
maxLength: 1000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { DataViewsService } from '../../../../common/data_views';
import { RuntimeField } from '../../../../common/types';
import { ErrorIndexPatternFieldNotFound } from '../../../error';
import { handleErrors } from '../util/handle_errors';
import { runtimeFieldSchema } from '../../../../common/schemas';
import { runtimeFieldSchemaUpdate } from '../../../../common/schemas';
import type {
DataViewsServerPluginStart,
DataViewsServerPluginStartDependencies,
Expand Down Expand Up @@ -93,7 +93,7 @@ const updateRuntimeFieldRouteFactory =
}),
body: schema.object({
name: schema.never(),
runtimeField: runtimeFieldSchema,
runtimeField: runtimeFieldSchemaUpdate,
}),
},
response: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Side Public License, v 1.
*/

import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
import { INITIAL_REST_VERSION } from '@kbn/data-views-plugin/server/constants';
import expect from '@kbn/expect';
import { FtrProviderContext } from '../../../../ftr_provider_context';
import { configArray } from '../../constants';
Expand All @@ -29,29 +31,33 @@ export default function ({ getService }: FtrProviderContext) {
describe(config.name, () => {
it('can update an existing field', async () => {
const title = `basic_index`;
const response1 = await supertest.post(config.path).send({
override: true,
[config.serviceKey]: {
title,
runtimeFieldMap: {
runtimeFoo: {
type: 'keyword',
script: {
source: "doc['field_name'].value",
const response1 = await supertest
.post(config.path)
.set(ELASTIC_HTTP_VERSION_HEADER, INITIAL_REST_VERSION)
.send({
override: true,
[config.serviceKey]: {
title,
runtimeFieldMap: {
runtimeFoo: {
type: 'keyword',
script: {
source: "doc['field_name'].value",
},
},
},
runtimeBar: {
type: 'keyword',
script: {
source: "doc['field_name'].value",
runtimeBar: {
type: 'keyword',
script: {
source: "doc['field_name'].value",
},
},
},
},
},
});
});

const response2 = await supertest
.post(`${config.path}/${response1.body[config.serviceKey].id}/runtime_field/runtimeFoo`)
.set(ELASTIC_HTTP_VERSION_HEADER, INITIAL_REST_VERSION)
.send({
runtimeField: {
type: 'keyword',
Expand All @@ -63,9 +69,9 @@ export default function ({ getService }: FtrProviderContext) {

expect(response2.status).to.be(200);

const response3 = await supertest.get(
`${config.path}/${response1.body[config.serviceKey].id}/runtime_field/runtimeFoo`
);
const response3 = await supertest
.get(`${config.path}/${response1.body[config.serviceKey].id}/runtime_field/runtimeFoo`)
.set(ELASTIC_HTTP_VERSION_HEADER, INITIAL_REST_VERSION);

const field =
config.serviceKey === 'index_pattern' ? response3.body.field : response3.body.fields[0];
Expand All @@ -75,6 +81,24 @@ export default function ({ getService }: FtrProviderContext) {
expect(field.type).to.be('string');
expect(field.runtimeField.type).to.be('keyword');
expect(field.runtimeField.script.source).to.be("doc['something_new'].value");

// Partial update
const response4 = await supertest
.post(`${config.path}/${response1.body[config.serviceKey].id}/runtime_field/runtimeFoo`)
.set(ELASTIC_HTTP_VERSION_HEADER, INITIAL_REST_VERSION)
.send({
runtimeField: {
script: {
source: "doc['partial_update'].value",
},
},
});

expect(response4.status).to.be(200);
const field2 =
config.serviceKey === 'index_pattern' ? response4.body.field : response4.body.fields[0];

expect(field2.runtimeField.script.source).to.be("doc['partial_update'].value");
});
});
});
Expand Down

0 comments on commit ed710db

Please sign in to comment.