2020import _ from 'lodash' ;
2121import { ROOT_TYPE , SavedObjectsSerializer } from '.' ;
2222import { SavedObjectsSchema } from '../schema' ;
23+ import { encodeVersion } from '../version' ;
2324
2425describe ( 'saved object conversion' , ( ) => {
2526 describe ( '#rawToSavedObject' , ( ) => {
@@ -69,7 +70,8 @@ describe('saved object conversion', () => {
6970 const actual = serializer . rawToSavedObject ( {
7071 _id : 'hello:world' ,
7172 _type : ROOT_TYPE ,
72- _version : 3 ,
73+ _seq_no : 3 ,
74+ _primary_term : 1 ,
7375 _source : {
7476 type : 'hello' ,
7577 hello : {
@@ -86,7 +88,7 @@ describe('saved object conversion', () => {
8688 const expected = {
8789 id : 'world' ,
8890 type : 'hello' ,
89- version : 3 ,
91+ version : encodeVersion ( 3 , 1 ) ,
9092 attributes : {
9193 a : 'b' ,
9294 c : 'd' ,
@@ -112,17 +114,46 @@ describe('saved object conversion', () => {
112114 expect ( actual ) . not . toHaveProperty ( 'version' ) ;
113115 } ) ;
114116
115- test ( `if specified it copies _version to version` , ( ) => {
117+ test ( `if specified it encodes _seq_no and _primary_term to version` , ( ) => {
116118 const serializer = new SavedObjectsSerializer ( new SavedObjectsSchema ( ) ) ;
117119 const actual = serializer . rawToSavedObject ( {
118120 _id : 'foo:bar' ,
119- _version : 4 ,
121+ _seq_no : 4 ,
122+ _primary_term : 1 ,
120123 _source : {
121124 type : 'foo' ,
122125 hello : { } ,
123126 } ,
124127 } ) ;
125- expect ( actual ) . toHaveProperty ( 'version' , 4 ) ;
128+ expect ( actual ) . toHaveProperty ( 'version' , encodeVersion ( 4 , 1 ) ) ;
129+ } ) ;
130+
131+ test ( `if only _seq_no is specified it throws` , ( ) => {
132+ const serializer = new SavedObjectsSerializer ( new SavedObjectsSchema ( ) ) ;
133+ expect ( ( ) =>
134+ serializer . rawToSavedObject ( {
135+ _id : 'foo:bar' ,
136+ _seq_no : 4 ,
137+ _source : {
138+ type : 'foo' ,
139+ hello : { } ,
140+ } ,
141+ } )
142+ ) . toThrowErrorMatchingInlineSnapshot ( `"_primary_term from elasticsearch must be an integer"` ) ;
143+ } ) ;
144+
145+ test ( `if only _primary_term is throws` , ( ) => {
146+ const serializer = new SavedObjectsSerializer ( new SavedObjectsSchema ( ) ) ;
147+ expect ( ( ) =>
148+ serializer . rawToSavedObject ( {
149+ _id : 'foo:bar' ,
150+ _primary_term : 1 ,
151+ _source : {
152+ type : 'foo' ,
153+ hello : { } ,
154+ } ,
155+ } )
156+ ) . toThrowErrorMatchingInlineSnapshot ( `"_seq_no from elasticsearch must be an integer"` ) ;
126157 } ) ;
127158
128159 test ( 'if specified it copies the _source.updated_at property to updated_at' , ( ) => {
@@ -206,7 +237,8 @@ describe('saved object conversion', () => {
206237 const raw = {
207238 _id : 'foo-namespace:foo:bar' ,
208239 _type : ROOT_TYPE ,
209- _version : 24 ,
240+ _primary_term : 24 ,
241+ _seq_no : 42 ,
210242 _source : {
211243 type : 'foo' ,
212244 foo : {
@@ -440,25 +472,38 @@ describe('saved object conversion', () => {
440472 expect ( actual . _source ) . not . toHaveProperty ( 'migrationVersion' ) ;
441473 } ) ;
442474
443- test ( 'it copies the version property to _version ' , ( ) => {
475+ test ( 'it decodes the version property to _seq_no and _primary_term ' , ( ) => {
444476 const serializer = new SavedObjectsSerializer ( new SavedObjectsSchema ( ) ) ;
445477 const actual = serializer . savedObjectToRaw ( {
446478 type : '' ,
447479 attributes : { } ,
448- version : 4 ,
480+ version : encodeVersion ( 1 , 2 ) ,
449481 } as any ) ;
450482
451- expect ( actual ) . toHaveProperty ( '_version' , 4 ) ;
483+ expect ( actual ) . toHaveProperty ( '_seq_no' , 1 ) ;
484+ expect ( actual ) . toHaveProperty ( '_primary_term' , 2 ) ;
452485 } ) ;
453486
454- test ( `if unspecified it doesn't add _version property ` , ( ) => {
487+ test ( `if unspecified it doesn't add _seq_no or _primary_term properties ` , ( ) => {
455488 const serializer = new SavedObjectsSerializer ( new SavedObjectsSchema ( ) ) ;
456489 const actual = serializer . savedObjectToRaw ( {
457490 type : '' ,
458491 attributes : { } ,
459492 } as any ) ;
460493
461- expect ( actual ) . not . toHaveProperty ( '_version' ) ;
494+ expect ( actual ) . not . toHaveProperty ( '_seq_no' ) ;
495+ expect ( actual ) . not . toHaveProperty ( '_primary_term' ) ;
496+ } ) ;
497+
498+ test ( `if version invalid it throws` , ( ) => {
499+ const serializer = new SavedObjectsSerializer ( new SavedObjectsSchema ( ) ) ;
500+ expect ( ( ) =>
501+ serializer . savedObjectToRaw ( {
502+ type : '' ,
503+ attributes : { } ,
504+ version : 'foo' ,
505+ } as any )
506+ ) . toThrowErrorMatchingInlineSnapshot ( `"Invalid version [foo]"` ) ;
462507 } ) ;
463508
464509 test ( 'it copies attributes to _source[type]' , ( ) => {
0 commit comments