@@ -7,6 +7,8 @@ import IndexedFormula from '../../src/store'
7
7
import NamedNode from '../../src/named-node'
8
8
import { RDFArrayRemove } from '../../src/utils-js'
9
9
import DataFactory from '../../src/factories/rdflib-data-factory'
10
+ import Collection from "../../src/collection" ;
11
+ import Variable from "../../src/variable" ;
10
12
11
13
describe ( 'IndexedFormula' , ( ) => {
12
14
const g0 = NamedNode . fromValue ( 'https://example.com/graph0' )
@@ -340,4 +342,73 @@ describe('IndexedFormula', () => {
340
342
expect ( store1 . holdsStatement ( store0 ) ) . to . be . true ( )
341
343
} )
342
344
} ) ;
345
+
346
+ describe ( 'applyPatch' , ( ) => {
347
+ it ( 'patches insert' , function ( ) {
348
+ const store = new IndexedFormula ( )
349
+ store . add ( [ triple1 , triple2 , triple3 ] . map ( t => DataFactory . st ( t . subject , t . predicate , t . object , g0 ) ) )
350
+ let collection = new Collection ( ) ;
351
+ collection . append ( DataFactory . st ( s1 , p2 , o3 ) )
352
+
353
+ store . applyPatch ( {
354
+ insert : collection ,
355
+ where : { statements : [
356
+ DataFactory . st ( new Variable ( "var1" ) , p1 , o1 ) ,
357
+ DataFactory . st ( s2 , new Variable ( "var2" ) , o2 ) ,
358
+ DataFactory . st ( s3 , p3 , new Variable ( "var3" ) )
359
+ ] , optional : [ ] , constraints : [ ] }
360
+ } , g0 , ( err ) => { expect ( err ) . to . be . empty } )
361
+
362
+ expect ( store . statements . length ) . to . eq ( 4 )
363
+ } ) ;
364
+ it ( 'patches delete' , function ( ) {
365
+ const store = new IndexedFormula ( )
366
+ store . add ( [ triple1 , triple2 , triple3 , triple4 ] . map ( t => DataFactory . st ( t . subject , t . predicate , t . object , g0 ) ) )
367
+ let collection = new Collection ( ) ;
368
+ collection . append ( DataFactory . st ( new Variable ( "var1" ) , new Variable ( "var2" ) , new Variable ( "var3" ) ) )
369
+
370
+ store . applyPatch ( {
371
+ delete : collection ,
372
+ where : { statements : [
373
+ DataFactory . st ( new Variable ( "var1" ) , p1 , o1 ) ,
374
+ DataFactory . st ( s2 , new Variable ( "var2" ) , o2 ) ,
375
+ DataFactory . st ( s3 , p3 , new Variable ( "var3" ) )
376
+ ] , optional : [ ] , constraints : [ ] }
377
+ } , g0 , ( err ) => { expect ( err ) . to . be . empty } )
378
+
379
+ expect ( store . statements . length ) . to . eq ( 3 )
380
+ } ) ;
381
+ it ( "patches nothing when the where clause doesn't match" , function ( ) {
382
+ const store = new IndexedFormula ( )
383
+ store . add ( [ triple1 , triple2 , triple3 ] )
384
+ let collection = new Collection ( ) ;
385
+ collection . append ( DataFactory . st ( new Variable ( "var1" ) , new Variable ( "var1" ) , new Variable ( "var1" ) ) )
386
+
387
+ store . applyPatch ( {
388
+ insert : collection ,
389
+ where : { statements : [
390
+ DataFactory . st ( new Variable ( "var1" ) , p1 , o1 ) ,
391
+ DataFactory . st ( s2 , new Variable ( "var2" ) , o2 ) ,
392
+ DataFactory . st ( s3 , p3 , new Variable ( "var3" ) )
393
+ ] , optional : [ ] , constraints : [ ] }
394
+ } , g0 , ( err ) => { expect ( err ) . to . be . empty } )
395
+
396
+ expect ( store . statements . length ) . to . eq ( 3 )
397
+ } ) ;
398
+ it ( 'patches nothing when the where clause matches more than one' , function ( ) {
399
+ const store = new IndexedFormula ( )
400
+ store . add ( [ triple1 , triple2 , triple3 ] )
401
+ let collection = new Collection ( ) ;
402
+ collection . append ( DataFactory . st ( s1 , p2 , o3 ) )
403
+
404
+ store . applyPatch ( {
405
+ insert : collection ,
406
+ where : { statements : [
407
+ DataFactory . st ( new Variable ( "var1" ) , new Variable ( "var2" ) , new Variable ( "var3" ) ) ,
408
+ ] , optional : [ ] , constraints : [ ] }
409
+ } , g0 , ( err ) => { expect ( err ) . to . be . empty } )
410
+
411
+ expect ( store . statements . length ) . to . eq ( 3 )
412
+ } ) ;
413
+ } )
343
414
} )
0 commit comments