@@ -881,7 +881,7 @@ suite('session extension', () => {
881
881
t . assert . strictEqual ( database1 . prepare ( select2 ) . all ( ) . length , 2 ) ; // data1 should have values in database1
882
882
} ) ;
883
883
884
- test ( 'conflict while applying changeset' , ( t ) => {
884
+ const prepareConflict = ( ) => {
885
885
const database1 = new DatabaseSync ( ':memory:' ) ;
886
886
const database2 = new DatabaseSync ( ':memory:' ) ;
887
887
@@ -896,9 +896,57 @@ suite('session extension', () => {
896
896
const insertSql = 'INSERT INTO data (key, value) VALUES (?, ?)' ;
897
897
const session = database1 . createSession ( ) ;
898
898
database1 . prepare ( insertSql ) . run ( 1 , 'hello' ) ;
899
+ database1 . prepare ( insertSql ) . run ( 2 , 'foo' ) ;
899
900
database2 . prepare ( insertSql ) . run ( 1 , 'world' ) ;
901
+ return {
902
+ database2,
903
+ changeset : session . changeset ( )
904
+ }
905
+ }
906
+
907
+ test ( 'conflict while applying changeset (default abort)' , ( t ) => {
908
+ const { database2, changeset } = prepareConflict ( ) ;
900
909
// When changeset is aborted due to a conflict, applyChangeset should return false
901
- t . assert . strictEqual ( database2 . applyChangeset ( session . changeset ( ) ) , false ) ;
910
+ t . assert . strictEqual ( database2 . applyChangeset ( changeset ) , false ) ;
911
+ t . assert . deepStrictEqual (
912
+ database2 . prepare ( 'SELECT value from data' ) . all ( ) ,
913
+ [ { value : 'world' } ] ) ; // unchanged
914
+ } ) ;
915
+
916
+ test ( 'conflict while applying changeset (explicit abort)' , ( t ) => {
917
+ const { database2, changeset } = prepareConflict ( ) ;
918
+ const result = database2 . applyChangeset ( changeset , {
919
+ onConflict : SQLITE_CHANGESET_ABORT
920
+ } ) ;
921
+ // When changeset is aborted due to a conflict, applyChangeset should return false
922
+ t . assert . strictEqual ( result , false ) ;
923
+ t . assert . deepStrictEqual (
924
+ database2 . prepare ( 'SELECT value from data' ) . all ( ) ,
925
+ [ { value : 'world' } ] ) ; // unchanged
926
+ } ) ;
927
+
928
+ test ( 'conflict while applying changeset (replacement)' , ( t ) => {
929
+ const { database2, changeset } = prepareConflict ( ) ;
930
+ const result = database2 . applyChangeset ( changeset , {
931
+ onConflict : SQLITE_CHANGESET_REPLACE
932
+ } ) ;
933
+ // Not aborted due to conflict, so should return true
934
+ t . assert . strictEqual ( result , true ) ;
935
+ t . assert . deepStrictEqual (
936
+ database2 . prepare ( 'SELECT value from data ORDER BY key' ) . all ( ) ,
937
+ [ { value : 'hello' } , { value : 'foo' } ] ) ; // replaced
938
+ } ) ;
939
+
940
+ test ( 'conflict while applying changeset (omit)' , ( t ) => {
941
+ const { database2, changeset } = prepareConflict ( ) ;
942
+ const result = database2 . applyChangeset ( changeset , {
943
+ onConflict : SQLITE_CHANGESET_OMIT
944
+ } ) ;
945
+ // Not aborted due to conflict, so should return true
946
+ t . assert . strictEqual ( result , true ) ;
947
+ t . assert . deepStrictEqual (
948
+ database2 . prepare ( 'SELECT value from data ORDER BY key ASC' ) . all ( ) ,
949
+ [ { value : 'world' } , { value : 'foo' } ] ) ; // conflicting change omitted
902
950
} ) ;
903
951
904
952
test ( 'constants are defined' , ( t ) => {
0 commit comments