-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Pass the version along so we can get version conflict errors, then try and resolve if we can * Update the version post save * Refactor slightly to match setId pattern * Tests and updates to ensure the actual changes aren't clobbered * Ensure we return the id * Avoid infinite recursion, welcome suggestions on how to unit test this * Change logic for refresh fields UI button. Now it will re-call init and force a fields refresh. This ensures we pick up on field format (and other) changes * Fix a couple issues with saving field formats, #19037 * Use the right key for version
- Loading branch information
1 parent
dc16506
commit 55b8130
Showing
4 changed files
with
248 additions
and
13 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
155 changes: 155 additions & 0 deletions
155
src/ui/public/index_patterns/__tests__/_index_pattern.test.js
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,155 @@ | ||
import { IndexPatternProvider } from '../_index_pattern'; | ||
|
||
jest.mock('../../errors', () => ({ | ||
SavedObjectNotFound: jest.fn(), | ||
DuplicateField: jest.fn(), | ||
IndexPatternMissingIndices: jest.fn(), | ||
})); | ||
|
||
jest.mock('../../registry/field_formats', () => ({ | ||
fieldFormats: { | ||
getDefaultInstance: jest.fn(), | ||
} | ||
})); | ||
|
||
jest.mock('../../utils/mapping_setup', () => ({ | ||
expandShorthand: jest.fn().mockImplementation(() => ({ | ||
id: true, | ||
title: true, | ||
})) | ||
})); | ||
|
||
jest.mock('../../notify', () => ({ | ||
Notifier: jest.fn().mockImplementation(() => ({ | ||
error: jest.fn(), | ||
})), | ||
toastNotifications: { | ||
addDanger: jest.fn(), | ||
} | ||
})); | ||
|
||
jest.mock('../_format_hit', () => ({ | ||
formatHit: jest.fn().mockImplementation(() => ({ | ||
formatField: jest.fn(), | ||
})) | ||
})); | ||
|
||
jest.mock('../_get', () => ({ | ||
IndexPatternsGetProvider: jest.fn().mockImplementation(() => ({ | ||
clearCache: jest.fn(), | ||
})) | ||
})); | ||
|
||
jest.mock('../_intervals', () => ({ | ||
IndexPatternsIntervalsProvider: jest.fn(), | ||
})); | ||
|
||
jest.mock('../_field_list', () => ({ | ||
IndexPatternsFieldListProvider: jest.fn().mockImplementation((pattern) => { | ||
return { | ||
byName: { | ||
id: { value: pattern.id }, | ||
title: { value: pattern.title }, | ||
}, | ||
every: jest.fn(), | ||
}; | ||
}) | ||
})); | ||
|
||
jest.mock('../_flatten_hit', () => ({ | ||
IndexPatternsFlattenHitProvider: jest.fn(), | ||
})); | ||
|
||
jest.mock('../_pattern_cache', () => ({ | ||
IndexPatternsPatternCacheProvider: { | ||
clear: jest.fn(), | ||
} | ||
})); | ||
|
||
jest.mock('../fields_fetcher_provider', () => ({ | ||
FieldsFetcherProvider: { | ||
fetch: jest.fn().mockImplementation(() => ([])) | ||
} | ||
})); | ||
|
||
jest.mock('../unsupported_time_patterns', () => ({ | ||
IsUserAwareOfUnsupportedTimePatternProvider: jest.fn(), | ||
})); | ||
|
||
|
||
jest.mock('../../saved_objects', () => { | ||
const object = { | ||
_version: 1, | ||
_id: 'foo', | ||
attributes: { | ||
title: 'something' | ||
} | ||
}; | ||
|
||
return { | ||
SavedObjectsClientProvider: { | ||
get: async () => object, | ||
update: async (type, id, body, { version }) => { | ||
if (object._version !== version) { | ||
throw { | ||
statusCode: 409 | ||
}; | ||
} | ||
|
||
object.attributes.title = body.title; | ||
|
||
return { | ||
id: object._id, | ||
_version: ++object._version, | ||
}; | ||
} | ||
}, | ||
findObjectByTitle: jest.fn(), | ||
}; | ||
}); | ||
|
||
const Private = arg => arg; | ||
const config = { | ||
get: jest.fn(), | ||
watchAll: jest.fn(), | ||
}; | ||
const Promise = window.Promise; | ||
const confirmModalPromise = jest.fn(); | ||
const kbnUrl = { | ||
eval: jest.fn(), | ||
}; | ||
|
||
describe('IndexPattern', () => { | ||
it('should handle version conflicts', async () => { | ||
const IndexPattern = IndexPatternProvider(Private, config, Promise, confirmModalPromise, kbnUrl); // eslint-disable-line new-cap | ||
|
||
// Create a normal index pattern | ||
const pattern = new IndexPattern('foo'); | ||
await pattern.init(); | ||
|
||
expect(pattern.version).toBe(2); | ||
|
||
// Create the same one - we're going to handle concurrency | ||
const samePattern = new IndexPattern('foo'); | ||
await samePattern.init(); | ||
|
||
expect(samePattern.version).toBe(3); | ||
|
||
// This will conflict because samePattern did a save (from refreshFields) | ||
// but the resave should work fine | ||
pattern.title = 'foo2'; | ||
await pattern.save(); | ||
|
||
// This should not be able to recover | ||
samePattern.title = 'foo3'; | ||
|
||
let result; | ||
try { | ||
await samePattern.save(); | ||
} catch (err) { | ||
result = err; | ||
} | ||
|
||
expect(result.statusCode).toBe(409); | ||
}); | ||
}); |
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