forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Refactor imports using 'elasticsearch' to '@elastic/elasticsearc…
…h'. Extend 'isPopulatedOjbect()'. (elastic#95651) - Gets rid of imports from 'elasticsearch' and replaces them with '@elastic/elasticsearch'. - Extends isPopulatedObject() to allow an optional array of attributes to check if they exist. Allows us to get rid of the manual and inconsistent usages of hasOwnProperty().
- Loading branch information
Showing
50 changed files
with
383 additions
and
300 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { isPopulatedObject } from './object_utils'; | ||
|
||
describe('object_utils', () => { | ||
describe('isPopulatedObject()', () => { | ||
it('does not allow numbers', () => { | ||
expect(isPopulatedObject(0)).toBe(false); | ||
}); | ||
it('does not allow strings', () => { | ||
expect(isPopulatedObject('')).toBe(false); | ||
}); | ||
it('does not allow null', () => { | ||
expect(isPopulatedObject(null)).toBe(false); | ||
}); | ||
it('does not allow an empty object', () => { | ||
expect(isPopulatedObject({})).toBe(false); | ||
}); | ||
it('allows an object with an attribute', () => { | ||
expect(isPopulatedObject({ attribute: 'value' })).toBe(true); | ||
}); | ||
it('does not allow an object with a non-existing required attribute', () => { | ||
expect(isPopulatedObject({ attribute: 'value' }, ['otherAttribute'])).toBe(false); | ||
}); | ||
it('allows an object with an existing required attribute', () => { | ||
expect(isPopulatedObject({ attribute: 'value' }, ['attribute'])).toBe(true); | ||
}); | ||
it('allows an object with two existing required attributes', () => { | ||
expect( | ||
isPopulatedObject({ attribute1: 'value1', attribute2: 'value2' }, [ | ||
'attribute1', | ||
'attribute2', | ||
]) | ||
).toBe(true); | ||
}); | ||
it('does not allow an object with two required attributes where one does not exist', () => { | ||
expect( | ||
isPopulatedObject({ attribute1: 'value1', attribute2: 'value2' }, [ | ||
'attribute1', | ||
'otherAttribute', | ||
]) | ||
).toBe(false); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
x-pack/plugins/ml/common/util/runtime_field_utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { isRuntimeField, isRuntimeMappings } from './runtime_field_utils'; | ||
|
||
describe('ML runtime field utils', () => { | ||
describe('isRuntimeField()', () => { | ||
it('does not allow numbers', () => { | ||
expect(isRuntimeField(1)).toBe(false); | ||
}); | ||
it('does not allow null', () => { | ||
expect(isRuntimeField(null)).toBe(false); | ||
}); | ||
it('does not allow arrays', () => { | ||
expect(isRuntimeField([])).toBe(false); | ||
}); | ||
it('does not allow empty objects', () => { | ||
expect(isRuntimeField({})).toBe(false); | ||
}); | ||
it('does not allow objects with non-matching attributes', () => { | ||
expect(isRuntimeField({ someAttribute: 'someValue' })).toBe(false); | ||
expect(isRuntimeField({ type: 'wrong-type' })).toBe(false); | ||
expect(isRuntimeField({ type: 'keyword', someAttribute: 'some value' })).toBe(false); | ||
}); | ||
it('allows objects with type attribute only', () => { | ||
expect(isRuntimeField({ type: 'keyword' })).toBe(true); | ||
}); | ||
it('allows objects with both type and script attributes', () => { | ||
expect(isRuntimeField({ type: 'keyword', script: 'some script' })).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('isRuntimeMappings()', () => { | ||
it('does not allow numbers', () => { | ||
expect(isRuntimeMappings(1)).toBe(false); | ||
}); | ||
it('does not allow null', () => { | ||
expect(isRuntimeMappings(null)).toBe(false); | ||
}); | ||
it('does not allow arrays', () => { | ||
expect(isRuntimeMappings([])).toBe(false); | ||
}); | ||
it('does not allow empty objects', () => { | ||
expect(isRuntimeMappings({})).toBe(false); | ||
}); | ||
it('does not allow objects with non-object inner structure', () => { | ||
expect(isRuntimeMappings({ someAttribute: 'someValue' })).toBe(false); | ||
}); | ||
it('does not allow objects with objects with unsupported inner structure', () => { | ||
expect(isRuntimeMappings({ fieldName1: { type: 'keyword' }, fieldName2: 'someValue' })).toBe( | ||
false | ||
); | ||
expect( | ||
isRuntimeMappings({ | ||
fieldName1: { type: 'keyword' }, | ||
fieldName2: { type: 'keyword', someAttribute: 'some value' }, | ||
}) | ||
).toBe(false); | ||
expect( | ||
isRuntimeMappings({ | ||
fieldName: { type: 'long', script: 1234 }, | ||
}) | ||
).toBe(false); | ||
expect( | ||
isRuntimeMappings({ | ||
fieldName: { type: 'long', script: { someAttribute: 'some value' } }, | ||
}) | ||
).toBe(false); | ||
expect( | ||
isRuntimeMappings({ | ||
fieldName: { type: 'long', script: { source: 1234 } }, | ||
}) | ||
).toBe(false); | ||
}); | ||
|
||
it('allows object with most basic runtime mapping', () => { | ||
expect(isRuntimeMappings({ fieldName: { type: 'keyword' } })).toBe(true); | ||
}); | ||
it('allows object with multiple most basic runtime mappings', () => { | ||
expect( | ||
isRuntimeMappings({ fieldName1: { type: 'keyword' }, fieldName2: { type: 'keyword' } }) | ||
).toBe(true); | ||
}); | ||
it('allows object with runtime mappings including scripts', () => { | ||
expect( | ||
isRuntimeMappings({ | ||
fieldName1: { type: 'keyword' }, | ||
fieldName2: { type: 'keyword', script: 'some script as script' }, | ||
}) | ||
).toBe(true); | ||
expect( | ||
isRuntimeMappings({ | ||
fieldName: { type: 'long', script: { source: 'some script as source' } }, | ||
}) | ||
).toBe(true); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.