aboutsummaryrefslogtreecommitdiffstats
path: root/test/triple_store/sparql/test_sparql.py
blob: b1d99acfa6ecea2b6bf6e541075260b6547b5397 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961

# imports
import rdflib
import unittest

# bsie imports
from bsfs import schema as bsc
from bsfs.namespace import ns
from bsfs.query import ast
from bsfs.utils import errors, URI

# objects to test
from bsfs.triple_store.sparql.sparql import SparqlStore


## code ##

class TestSparqlStore(unittest.TestCase):
    def setUp(self):
        self.schema = bsc.from_string('''
            prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            prefix xsd: <http://www.w3.org/2001/XMLSchema#>

            prefix bsfs: <http://bsfs.ai/schema/>
            prefix bse: <http://bsfs.ai/schema/Entity#>

            bsfs:Entity rdfs:subClassOf bsfs:Node .
            bsfs:Tag rdfs:subClassOf bsfs:Node .
            bsfs:User rdfs:subClassOf bsfs:Node .
            xsd:string rdfs:subClassOf bsfs:Literal .
            bsfs:Number rdfs:subClassOf bsfs:Literal .
            bsfs:BinaryBlob rdfs:subClassOf bsfs:Literal .
            xsd:integer rdfs:subClassOf bsfs:Number .

            # non-unique literal
            bse:comment rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range xsd:string ;
                bsfs:unique "false"^^xsd:boolean .

            # unique literal
            bse:filesize rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range xsd:integer ;
                bsfs:unique "true"^^xsd:boolean .

            # non-unique node
            bse:tag rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range bsfs:Tag ;
                bsfs:unique "false"^^xsd:boolean .

            # unique node
            bse:author rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range bsfs:User ;
                bsfs:unique "true"^^xsd:boolean .

            # binary range
            bse:asset rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range bsfs:BinaryBlob .

            ''')
        self.schema_triples = {
            # schema hierarchy
            (rdflib.URIRef(ns.bsfs.Entity), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Node)),
            (rdflib.URIRef(ns.bsfs.Tag), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Node)),
            (rdflib.URIRef(ns.bsfs.User), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Node)),
            (rdflib.URIRef(ns.xsd.string), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)),
            (rdflib.URIRef(ns.bsfs.Array), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)),
            (rdflib.URIRef(ns.bsfs.BinaryBlob), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)),
            (rdflib.URIRef(ns.bsfs.Feature), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Array)),
            (rdflib.URIRef(ns.bsfs.Number), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)),
            (rdflib.URIRef(ns.bsfs.Time), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)),
            (rdflib.URIRef(ns.xsd.integer), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Number)),
            (rdflib.URIRef(ns.bse.comment), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)),
            (rdflib.URIRef(ns.bse.filesize), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)),
            (rdflib.URIRef(ns.bse.tag), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)),
            (rdflib.URIRef(ns.bse.author), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)),
            (rdflib.URIRef(ns.bse.asset), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)),
            }

    def test_essentials(self):
        store = SparqlStore.Open()
        # equality
        self.assertEqual(store, store)
        self.assertEqual(hash(store), hash(store))
        self.assertNotEqual(store, SparqlStore.Open())
        self.assertNotEqual(hash(store), hash(SparqlStore.Open()))
        # string conversion
        self.assertEqual(str(store), 'SparqlStore(uri=None)')
        self.assertEqual(repr(store), 'SparqlStore(uri=None)')
        # open
        self.assertIsInstance(SparqlStore.Open(), SparqlStore)


    def test__has_type(self):
        # setup store
        store = SparqlStore.Open()
        store.schema = bsc.from_string('''
            prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            prefix xsd: <http://www.w3.org/2001/XMLSchema#>
            prefix bsfs: <http://bsfs.ai/schema/>

            bsfs:Entity rdfs:subClassOf bsfs:Node .
            bsfs:Document rdfs:subClassOf bsfs:Entity .
            bsfs:Image rdfs:subClassOf bsfs:Entity .
            bsfs:PDF rdfs:subClassOf bsfs:Document .

            ''')
        # add some instances
        store.create(store.schema.node(ns.bsfs.Entity), {URI('http://example.com/me/entity#1234')})
        store.create(store.schema.node(ns.bsfs.Document), {URI('http://example.com/me/document#1234')})
        store.create(store.schema.node(ns.bsfs.Image), {URI('http://example.com/me/image#1234')})
        store.create(store.schema.node(ns.bsfs.PDF), {URI('http://example.com/me/pdf#1234')})

        # node_type must be in the schema
        self.assertRaises(errors.ConsistencyError, store._has_type, URI('http://example.com/me/entity#1234'), store.schema.node(ns.bsfs.Node).child(ns.bsfs.invalid))

        # returns False on inexistent nodes
        self.assertFalse(store._has_type(URI('http://example.com/me/entity#4321'), store.schema.node(ns.bsfs.Entity)))
        self.assertFalse(store._has_type(URI('http://example.com/me/document#4321'), store.schema.node(ns.bsfs.Document)))
        self.assertFalse(store._has_type(URI('http://example.com/me/image#4321'), store.schema.node(ns.bsfs.Image)))
        self.assertFalse(store._has_type(URI('http://example.com/me/pdf#4321'), store.schema.node(ns.bsfs.PDF)))

        # _has_type checks direct types
        self.assertTrue(store._has_type(URI('http://example.com/me/entity#1234'), store.schema.node(ns.bsfs.Entity)))
        self.assertTrue(store._has_type(URI('http://example.com/me/document#1234'), store.schema.node(ns.bsfs.Document)))
        self.assertTrue(store._has_type(URI('http://example.com/me/image#1234'), store.schema.node(ns.bsfs.Image)))
        self.assertTrue(store._has_type(URI('http://example.com/me/pdf#1234'), store.schema.node(ns.bsfs.PDF)))

        # _has_type checks type hierarchy
        self.assertFalse(store._has_type(URI('http://example.com/me/entity#1234'), store.schema.node(ns.bsfs.Document)))
        self.assertFalse(store._has_type(URI('http://example.com/me/entity#1234'), store.schema.node(ns.bsfs.Image)))
        self.assertFalse(store._has_type(URI('http://example.com/me/entity#1234'), store.schema.node(ns.bsfs.PDF)))

        self.assertTrue(store._has_type(URI('http://example.com/me/document#1234'), store.schema.node(ns.bsfs.Entity)))
        self.assertFalse(store._has_type(URI('http://example.com/me/document#1234'), store.schema.node(ns.bsfs.Image)))
        self.assertFalse(store._has_type(URI('http://example.com/me/document#1234'), store.schema.node(ns.bsfs.PDF)))

        self.assertTrue(store._has_type(URI('http://example.com/me/image#1234'), store.schema.node(ns.bsfs.Entity)))
        self.assertFalse(store._has_type(URI('http://example.com/me/image#1234'), store.schema.node(ns.bsfs.Document)))
        self.assertFalse(store._has_type(URI('http://example.com/me/image#1234'), store.schema.node(ns.bsfs.PDF)))

        self.assertTrue(store._has_type(URI('http://example.com/me/pdf#1234'), store.schema.node(ns.bsfs.Entity)))
        self.assertTrue(store._has_type(URI('http://example.com/me/pdf#1234'), store.schema.node(ns.bsfs.Document)))
        self.assertFalse(store._has_type(URI('http://example.com/me/pdf#1234'), store.schema.node(ns.bsfs.Image)))


    def test_schema(self):
        # setup
        store = SparqlStore.Open()
        curr = self.schema
        p_comment = curr.predicate(ns.bse.comment)
        p_filesize = curr.predicate(ns.bse.filesize)
        p_tag = curr.predicate(ns.bse.tag)
        p_author = curr.predicate(ns.bse.author)

        # migrate to an initial schema
        store.schema = curr
        # store has migrated
        self.assertEqual(store.schema, curr)

        # add some instances
        ent_ids = {URI('http://example.com/me/entity#1234'), URI('http://example.com/me/entity#4321')}
        tag_ids = {URI('http://example.com/me/tag#1234'), URI('http://example.com/me/tag#4321')}
        store.create(curr.node(ns.bsfs.Entity), ent_ids)
        store.create(curr.node(ns.bsfs.Tag), tag_ids)
        store.create(curr.node(ns.bsfs.User), {URI('http://example.com/me')})
        # add some triples
        store.set(curr.node(ns.bsfs.Entity), ent_ids, p_comment, {'foo', 'bar'})
        store.set(curr.node(ns.bsfs.Entity), ent_ids, p_filesize, {1234})
        store.set(curr.node(ns.bsfs.Entity), ent_ids, p_tag,
            {URI('http://example.com/me/tag#1234'), URI('http://example.com/me/tag#4321')})
        store.set(curr.node(ns.bsfs.Entity), ent_ids, p_author,
            {URI('http://example.com/me')})
        # check instances
        instances = self.schema_triples | {
            # node instances
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Entity)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Entity)),
            (rdflib.URIRef('http://example.com/me/tag#1234'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Tag)),
            (rdflib.URIRef('http://example.com/me/tag#4321'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Tag)),
            (rdflib.URIRef('http://example.com/me'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.User)),
            # comments
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_comment.uri), rdflib.Literal('foo', datatype=rdflib.XSD.string)),
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_comment.uri), rdflib.Literal('bar', datatype=rdflib.XSD.string)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_comment.uri), rdflib.Literal('foo', datatype=rdflib.XSD.string)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_comment.uri), rdflib.Literal('bar', datatype=rdflib.XSD.string)),
            # filesize
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_filesize.uri), rdflib.Literal('1234', datatype=rdflib.XSD.integer)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_filesize.uri), rdflib.Literal('1234', datatype=rdflib.XSD.integer)),
            # tags
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#1234')),
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#4321')),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#1234')),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#4321')),
            # author
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_author.uri), rdflib.URIRef('http://example.com/me')),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_author.uri), rdflib.URIRef('http://example.com/me')),
            }
        self.assertSetEqual(set(store._graph), instances)

        # add some classes to the schema
        curr = curr + bsc.from_string('''
            prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            prefix xsd: <http://www.w3.org/2001/XMLSchema#>
            prefix bsfs: <http://bsfs.ai/schema/>
            prefix bse: <http://bsfs.ai/schema/Entity#>
            prefix bst: <http://bsfs.ai/schema/Tag#>
            prefix bsc: <http://bsfs.ai/schema/Collection#>

            bsfs:Entity rdfs:subClassOf bsfs:Node .
            bsfs:Tag rdfs:subClassOf bsfs:Node .
            bsfs:Collection rdfs:subClassOf bsfs:Node .
            xsd:boolean rdfs:subClassOf bsfs:Literal .

            # literal
            bse:shared rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range xsd:boolean ;
                bsfs:unique "true"^^xsd:boolean .

            # node
            bse:partOf rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range bsfs:Collection ;
                bsfs:unique "false"^^xsd:boolean .

            # predicates across auxiliary node classes
            bst:usedIn rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Tag ;
                rdfs:range bsfs:Collection ;
                bsfs:unique "false"^^xsd:boolean .

            bsc:tag rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Collection ;
                rdfs:range bsfs:Tag ;
                bsfs:unique "false"^^xsd:boolean .

            bst:principal rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Tag ;
                rdfs:range bsfs:Node ;
                bsfs:unique "true"^^xsd:boolean .

            ''')
        # store migrated to the new schema
        store.schema = curr
        self.assertEqual(store.schema, curr)
        # instances have not changed
        self.assertSetEqual(set(store._graph), instances | {
            # schema hierarchy
            (rdflib.URIRef(ns.bsfs.Collection), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Node)),
            (rdflib.URIRef(ns.xsd.boolean), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)),
            (rdflib.URIRef(ns.bse.shared), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)),
            (rdflib.URIRef(ns.bse.partOf), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)),
            (rdflib.URIRef('http://bsfs.ai/schema/Tag#usedIn'), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)),
            (rdflib.URIRef('http://bsfs.ai/schema/Collection#tag'), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)),
            (rdflib.URIRef('http://bsfs.ai/schema/Tag#principal'), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)),
            })
        # add some instances of the new classes
        p_partOf = curr.predicate(ns.bse.partOf)
        p_shared = curr.predicate(ns.bse.shared)
        p_usedIn = curr.predicate('http://bsfs.ai/schema/Tag#usedIn')
        p_ctag = curr.predicate('http://bsfs.ai/schema/Collection#tag')
        p_principal = curr.predicate('http://bsfs.ai/schema/Tag#principal')
        store.create(curr.node(ns.bsfs.Collection), {URI('http://example.com/me/collection#1234'), URI('http://example.com/me/collection#4321')})
        # add some more triples
        store.set(curr.node(ns.bsfs.Entity), ent_ids, p_shared, {True})
        store.set(curr.node(ns.bsfs.Entity), ent_ids, p_partOf,
            {URI('http://example.com/me/collection#1234'), URI('http://example.com/me/collection#4321')})
        store.set(curr.node(ns.bsfs.Tag), {URI('http://example.com/me/tag#1234')}, p_usedIn,
            {URI('http://example.com/me/collection#1234')})
        store.set(curr.node(ns.bsfs.Collection), {URI('http://example.com/me/collection#4321')}, p_ctag,
            {URI('http://example.com/me/tag#1234'), URI('http://example.com/me/tag#4321')})
        store.set(curr.node(ns.bsfs.Tag), {URI('http://example.com/me/tag#1234')}, p_principal,
            {URI('http://example.com/me/collection#1234')})
        # new instances are now in the graph
        self.assertSetEqual(set(store._graph), instances | {
            # same old schema hierarchy
            (rdflib.URIRef(ns.bsfs.Collection), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Node)),
            (rdflib.URIRef(ns.xsd.boolean), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)),
            (rdflib.URIRef(ns.bse.shared), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)),
            (rdflib.URIRef(ns.bse.partOf), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)),
            (rdflib.URIRef('http://bsfs.ai/schema/Tag#usedIn'), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)),
            (rdflib.URIRef('http://bsfs.ai/schema/Collection#tag'), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)),
            (rdflib.URIRef('http://bsfs.ai/schema/Tag#principal'), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)),
            # collections
            (rdflib.URIRef('http://example.com/me/collection#1234'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Collection)),
            (rdflib.URIRef('http://example.com/me/collection#4321'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Collection)),
            # partOf
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_partOf.uri), rdflib.URIRef('http://example.com/me/collection#1234')),
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_partOf.uri), rdflib.URIRef('http://example.com/me/collection#4321')),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_partOf.uri), rdflib.URIRef('http://example.com/me/collection#1234')),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_partOf.uri), rdflib.URIRef('http://example.com/me/collection#4321')),
            # shared
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_shared.uri), rdflib.Literal('true', datatype=rdflib.XSD.boolean)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_shared.uri), rdflib.Literal('true', datatype=rdflib.XSD.boolean)),
            # auxiliary node connections
            (rdflib.URIRef('http://example.com/me/tag#1234'), rdflib.URIRef(p_usedIn.uri), rdflib.URIRef('http://example.com/me/collection#1234')),
            (rdflib.URIRef('http://example.com/me/tag#1234'), rdflib.URIRef(p_principal.uri), rdflib.URIRef('http://example.com/me/collection#1234')),
            (rdflib.URIRef('http://example.com/me/collection#4321'), rdflib.URIRef(p_ctag.uri), rdflib.URIRef('http://example.com/me/tag#1234')),
            (rdflib.URIRef('http://example.com/me/collection#4321'), rdflib.URIRef(p_ctag.uri), rdflib.URIRef('http://example.com/me/tag#4321')),
            })


        # remove some classes from the schema
        curr = bsc.from_string('''
            prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            prefix xsd: <http://www.w3.org/2001/XMLSchema#>
            prefix bsfs: <http://bsfs.ai/schema/>
            prefix bse: <http://bsfs.ai/schema/Entity#>
            prefix bst: <http://bsfs.ai/schema/Tag#>

            bsfs:Entity rdfs:subClassOf bsfs:Node .
            bsfs:Tag rdfs:subClassOf bsfs:Node .
            bsfs:User rdfs:subClassOf bsfs:Node .

            xsd:boolean rdfs:subClassOf bsfs:Literal .
            bsfs:Number rdfs:subClassOf bsfs:Literal .
            xsd:integer rdfs:subClassOf bsfs:Number .

            bse:filesize rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range xsd:integer ;
                bsfs:unique "true"^^xsd:boolean .

            bse:tag rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range bsfs:Tag ;
                bsfs:unique "false"^^xsd:boolean .

            bse:shared rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range xsd:boolean ;
                bsfs:unique "true"^^xsd:boolean .

            bst:principal rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Tag ;
                rdfs:range bsfs:Node ;
                bsfs:unique "true"^^xsd:boolean .

            # removed: bsfs:Collection
            # removed: xsd:string
            # removed: bse:comment (bsfs:Entity -> xsd:string)
            # removed: bse:partOf (bsfs:Entity -> bsfs:Collection)
            # removed: bse:author (bsfs:entity -> bsfs:User)
            # removed: bst:usedIn (bsfs:Tag -> bsfs:Collection)
            # removed: bsc:tag (bsfs:Collection -> bsfs:Tag)

            ''')
        # store migrated to the new schema
        store.schema = curr
        self.assertEqual(store.schema, curr)
        # instances of old classes were removed
        self.assertSetEqual(set(store._graph), {
            # schema hierarchy
            (rdflib.URIRef(ns.bsfs.Entity), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Node)),
            (rdflib.URIRef(ns.bsfs.Tag), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Node)),
            (rdflib.URIRef(ns.bsfs.User), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Node)),
            (rdflib.URIRef(ns.xsd.boolean), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)),
            (rdflib.URIRef(ns.bsfs.Array), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)),
            (rdflib.URIRef(ns.bsfs.BinaryBlob), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)),
            (rdflib.URIRef(ns.bsfs.Feature), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Array)),
            (rdflib.URIRef(ns.bsfs.Number), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)),
            (rdflib.URIRef(ns.bsfs.Time), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Literal)),
            (rdflib.URIRef(ns.xsd.integer), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Number)),
            (rdflib.URIRef(ns.bse.shared), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)),
            (rdflib.URIRef(ns.bse.tag), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)),
            (rdflib.URIRef(ns.bse.filesize), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)),
            (rdflib.URIRef('http://bsfs.ai/schema/Tag#principal'), rdflib.RDFS.subClassOf, rdflib.URIRef(ns.bsfs.Predicate)),
            # node instances
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Entity)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Entity)),
            (rdflib.URIRef('http://example.com/me/tag#1234'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Tag)),
            (rdflib.URIRef('http://example.com/me/tag#4321'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Tag)),
            (rdflib.URIRef('http://example.com/me'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.User)),
            # filesize
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_filesize.uri), rdflib.Literal('1234', datatype=rdflib.XSD.integer)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_filesize.uri), rdflib.Literal('1234', datatype=rdflib.XSD.integer)),
            # tags
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#1234')),
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#4321')),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#1234')),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#4321')),
            # shared
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_shared.uri), rdflib.Literal('true', datatype=rdflib.XSD.boolean)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_shared.uri), rdflib.Literal('true', datatype=rdflib.XSD.boolean)),
            })

        # can only assign schema instances
        self.assertRaises(TypeError, setattr, store, 'schema', None)
        self.assertRaises(TypeError, setattr, store, 'schema', 1234)
        self.assertRaises(TypeError, setattr, store, 'schema', 'foo')
        class Foo(): pass
        self.assertRaises(TypeError, setattr, store, 'schema', Foo())

        # cannot define features w/o known distance function
        invalid = bsc.from_string('''
            prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            prefix xsd: <http://www.w3.org/2001/XMLSchema#>
            prefix bsfs: <http://bsfs.ai/schema/>
            prefix bse: <http://bsfs.ai/schema/Entity#>

            bsfs:Array rdfs:subClassOf bsfs:Literal .
            bsfs:Feature rdfs:subClassOf bsfs:Array .

            bsfs:Colors rdfs:subClassOf bsfs:Feature ;
                bsfs:dimension "4"^^xsd:integer ;
                bsfs:distance bsfs:foobar .

            ''')
        self.assertRaises(errors.UnsupportedError, setattr, store, 'schema', invalid)

        # cannot migrate to incompatible schema
        invalid = bsc.from_string('''
            prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            prefix xsd: <http://www.w3.org/2001/XMLSchema#>
            prefix bsfs: <http://bsfs.ai/schema/>
            prefix bse: <http://bsfs.ai/schema/Entity#>

            bsfs:Entity rdfs:subClassOf bsfs:Node .
            bsfs:Tag rdfs:subClassOf bsfs:Entity . # inconsistent with previous tag definition

            bse:tag rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range bsfs:Tag ;
                bsfs:unique "false"^^xsd:boolean .

            ''')
        self.assertRaises(errors.ConsistencyError, setattr, store, 'schema', invalid)
        invalid = bsc.from_string('''
            prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
            prefix xsd: <http://www.w3.org/2001/XMLSchema#>
            prefix bsfs: <http://bsfs.ai/schema/>
            prefix bse: <http://bsfs.ai/schema/Entity#>

            bsfs:Entity rdfs:subClassOf bsfs:Node .
            bsfs:User rdfs:subClassOf bsfs:Node .

            # inconsistent predicate
            bse:tag rdfs:subClassOf bsfs:Predicate ;
                rdfs:domain bsfs:Entity ;
                rdfs:range bsfs:User;
                bsfs:unique "false"^^xsd:boolean .

            ''')
        self.assertRaises(errors.ConsistencyError, setattr, store, 'schema', invalid)


    def test_transaction(self):
        # store setup
        store = SparqlStore.Open()
        store.schema = self.schema
        p_tag = store.schema.predicate(ns.bse.tag)
        p_filesize = store.schema.predicate(ns.bse.filesize)
        # prepare node types
        ent_type = store.schema.node(ns.bsfs.Entity)
        tag_type = store.schema.node(ns.bsfs.Tag)
        ent_ids = {URI('http://example.com/me/entity#1234'), URI('http://example.com/me/entity#4321')}
        tag_ids = {URI('http://example.com/me/tag#1234'), URI('http://example.com/me/tag#4321')}
        # target instances
        instances = self.schema_triples | {
            # node instances
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Entity)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Entity)),
            (rdflib.URIRef('http://example.com/me/tag#1234'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Tag)),
            (rdflib.URIRef('http://example.com/me/tag#4321'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Tag)),
            # links
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#1234')),
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#4321')),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#1234')),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#4321')),
            }

        # add some data
        store.create(ent_type, ent_ids)
        store.create(tag_type, tag_ids)
        store.set(ent_type, ent_ids, p_tag, tag_ids)
        store.set(ent_type, ent_ids, p_filesize, {1234})
        # current transaction is visible
        self.assertSetEqual(set(store._graph), instances | {
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_filesize.uri), rdflib.Literal(1234, datatype=rdflib.XSD.integer)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_filesize.uri), rdflib.Literal(1234, datatype=rdflib.XSD.integer)),
            })

        # rollback undoes previous changes
        store.rollback()
        self.assertSetEqual(set(store._graph), self.schema_triples)

        # add some data once more
        store.create(ent_type, ent_ids)
        store.create(tag_type, tag_ids)
        store.set(ent_type, ent_ids, p_tag, tag_ids)
        store.set(ent_type, ent_ids, p_filesize, {1234})
        # current transaction is visible
        self.assertSetEqual(set(store._graph), instances | {
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_filesize.uri), rdflib.Literal(1234, datatype=rdflib.XSD.integer)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_filesize.uri), rdflib.Literal(1234, datatype=rdflib.XSD.integer)),
            })

        # commit saves changes
        store.commit()
        self.assertSetEqual(set(store._graph), instances | {
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_filesize.uri), rdflib.Literal(1234, datatype=rdflib.XSD.integer)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_filesize.uri), rdflib.Literal(1234, datatype=rdflib.XSD.integer)),
            })

        # add additional data
        store.create(ent_type, {URI('http://example.com/me/entity#hello')})
        store.set(ent_type, {URI('http://example.com/me/entity#hello')}, p_tag, tag_ids)
        store.set(ent_type, ent_ids, p_filesize, {4321})
        self.assertSetEqual(set(store._graph), instances | {
            (rdflib.URIRef('http://example.com/me/entity#hello'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Entity)),
            (rdflib.URIRef('http://example.com/me/entity#hello'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#1234')),
            (rdflib.URIRef('http://example.com/me/entity#hello'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#4321')),
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_filesize.uri), rdflib.Literal(4321, datatype=rdflib.XSD.integer)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_filesize.uri), rdflib.Literal(4321, datatype=rdflib.XSD.integer)),
            })

        # rollback undoes only changes since last commit
        store.rollback()
        self.assertSetEqual(set(store._graph), instances | {
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_filesize.uri), rdflib.Literal(1234, datatype=rdflib.XSD.integer)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_filesize.uri), rdflib.Literal(1234, datatype=rdflib.XSD.integer)),
            })

    def test_get(self):
        # store setup
        store = SparqlStore.Open()
        store.schema = self.schema
        ent_type = self.schema.node(ns.bsfs.Entity)
        tag_type = self.schema.node(ns.bsfs.Tag)
        ent_ids = {URI('http://example.com/me/entity#1234'), URI('http://example.com/me/entity#4321')}
        tag_ids = {URI('http://example.com/me/tag#1234'), URI('http://example.com/me/tag#4321')}
        store.create(ent_type, ent_ids)
        store.create(tag_type, tag_ids)
        store.set(ent_type, ent_ids, self.schema.predicate(ns.bse.tag), tag_ids)
        store.set(ent_type, {URI('http://example.com/me/entity#1234')}, self.schema.predicate(ns.bse.filesize), {1234})
        store.set(ent_type, {URI('http://example.com/me/entity#4321')}, self.schema.predicate(ns.bse.filesize), {4321})
        # node_type must be in the schema
        self.assertRaises(errors.ConsistencyError, set, store.get(self.schema.node(ns.bsfs.Node).child(ns.bsfs.Invalid), ast.filter.IsIn(ent_ids)))
        # query must be a filter expression
        class Foo(): pass
        self.assertRaises(TypeError, set, store.get(ent_type, 1234))
        self.assertRaises(TypeError, set, store.get(ent_type, '1234'))
        self.assertRaises(TypeError, set, store.get(ent_type, Foo()))
        # run some queries
        self.assertSetEqual(set(store.get(tag_type, ast.filter.IsIn(tag_ids))), tag_ids)
        self.assertSetEqual(set(store.get(ent_type, ast.filter.Any(ns.bse.tag, ast.filter.IsIn(tag_ids)))), ent_ids)
        self.assertSetEqual(set(store.get(ent_type, ast.filter.IsIn(tag_ids))), set())
        # invalid queries raise error
        self.assertRaises(errors.ConsistencyError, set, store.get(tag_type, ast.filter.Any(ns.bse.filesize, ast.filter.Equals(1234))))
        self.assertRaises(errors.BackendError, set, store.get(ent_type, ast.filter.Equals('http://example.com/me/entity#1234')))
        # run some more complex query
        q = store.get(tag_type, ast.filter.Any(ast.filter.Predicate(ns.bse.tag, reverse=True),
            ast.filter.Any(ns.bse.filesize,
                ast.filter.LessThan(2000))))
        self.assertSetEqual(set(q), tag_ids)


    def test_fetch(self):
        # store setup
        store = SparqlStore.Open()
        store.schema = self.schema
        # add instances
        ent_type = self.schema.node(ns.bsfs.Entity)
        tag_type = self.schema.node(ns.bsfs.Tag)
        ent_ids = {URI('http://example.com/me/entity#1234'), URI('http://example.com/me/entity#4321')}
        tag_ids = {URI('http://example.com/me/tag#1234'), URI('http://example.com/me/tag#4321')}
        store.create(ent_type, ent_ids)
        store.create(tag_type, tag_ids)
        store.set(ent_type, ent_ids, self.schema.predicate(ns.bse.tag), tag_ids)
        store.set(ent_type, {URI('http://example.com/me/entity#1234')}, self.schema.predicate(ns.bse.filesize), {1234})
        store.set(ent_type, {URI('http://example.com/me/entity#4321')}, self.schema.predicate(ns.bse.filesize), {4321})
        store.set(ent_type, {URI('http://example.com/me/entity#1234')}, self.schema.predicate(ns.bse.comment), {'hello world'})
        # node_type must be a node from the schema
        self.assertRaises(errors.ConsistencyError, list, store.fetch(self.schema.literal(ns.bsfs.Literal),
            ast.filter.FilterExpression(), ast.fetch.FetchExpression()))
        self.assertRaises(errors.ConsistencyError, list, store.fetch(self.schema.node(ns.bsfs.Node).child(ns.bsfs.Invalid),
            ast.filter.FilterExpression(), ast.fetch.FetchExpression()))
        # requires a filter and a fetch query
        self.assertRaises(TypeError, list, store.fetch(self.schema.node(ns.bsfs.Entity), None, ast.fetch.FetchExpression()))
        self.assertRaises(TypeError, list, store.fetch(self.schema.node(ns.bsfs.Entity), 1234, ast.fetch.FetchExpression()))
        self.assertRaises(TypeError, list, store.fetch(self.schema.node(ns.bsfs.Entity), 'hello', ast.fetch.FetchExpression()))
        self.assertRaises(TypeError, list, store.fetch(self.schema.node(ns.bsfs.Entity), ast.filter.FilterExpression(), None))
        self.assertRaises(TypeError, list, store.fetch(self.schema.node(ns.bsfs.Entity), ast.filter.FilterExpression(), 1234))
        self.assertRaises(TypeError, list, store.fetch(self.schema.node(ns.bsfs.Entity), ast.filter.FilterExpression(), 'hello'))
        # fetch emits triples
        self.assertSetEqual(set(store.fetch(self.schema.node(ns.bsfs.Entity),
            ast.filter.Is('http://example.com/me/entity#1234'),
            ast.fetch.Value(ns.bse.filesize, 'filesize'),
            )), {
            (URI('http://example.com/me/entity#1234'), 'filesize', 1234),
            })
        # fetch respects filter query
        self.assertSetEqual(set(store.fetch(self.schema.node(ns.bsfs.Entity),
            ast.filter.IsIn('http://example.com/me/entity#1234', 'http://example.com/me/entity#4321'),
            ast.fetch.Value(ns.bse.filesize, 'filesize'),
            )), {
            (URI('http://example.com/me/entity#1234'), 'filesize', 1234),
            (URI('http://example.com/me/entity#4321'), 'filesize', 4321),
            })
        # fetch ignores missing data
        self.assertSetEqual(set(store.fetch(self.schema.node(ns.bsfs.Entity),
            ast.filter.IsIn('http://example.com/me/entity#1234', 'http://example.com/me/entity#4321'),
            ast.fetch.Value(ns.bse.comment, 'comment'),
            )), {
            (URI('http://example.com/me/entity#1234'), 'comment', 'hello world'),
            })
        # fetch emits all triples
        self.assertSetEqual(set(store.fetch(self.schema.node(ns.bsfs.Entity),
            ast.filter.Is('http://example.com/me/entity#1234'),
            ast.fetch.All(
                ast.fetch.Value(ns.bse.filesize, 'filesize'),
                ast.fetch.Node(ns.bse.tag, 'tag'),
                )
            )), {
            (URI('http://example.com/me/entity#1234'), 'filesize', 1234),
            (URI('http://example.com/me/entity#1234'), 'tag', URI('http://example.com/me/tag#1234')),
            (URI('http://example.com/me/entity#1234'), 'tag', URI('http://example.com/me/tag#4321')),
            })
        # triples do not repeat
        triples = list(store.fetch(self.schema.node(ns.bsfs.Entity), ast.filter.Is('http://example.com/me/entity#1234'),
            ast.fetch.All(
                ast.fetch.Value(ns.bse.filesize, 'filesize'),
                ast.fetch.Node(ns.bse.tag, 'tag'),
                )
            ))
        self.assertEqual(len(triples), 3)

    def test_exists(self):
        # store setup
        store = SparqlStore.Open()
        store.schema = self.schema
        # prepare node types
        ent_type = store.schema.node(ns.bsfs.Entity)
        tag_type = store.schema.node(ns.bsfs.Tag)
        # create node instances
        ent_ids = {
            URI('http://example.com/me/entity#1234'),
            URI('http://example.com/me/entity#4321'),
            }
        tag_ids = {
            URI('http://example.com/me/tag#1234'),
            URI('http://example.com/me/tag#4321'),
            }
        store.create(ent_type, ent_ids)
        store.create(tag_type, tag_ids)

        # exists returns all existing nodes of the correct type
        self.assertSetEqual(ent_ids, set(store.exists(ent_type, ent_ids)))
        self.assertSetEqual(tag_ids, set(store.exists(tag_type, tag_ids)))
        # exists returns only nodes that match the type
        self.assertSetEqual(set(), set(store.exists(ent_type, tag_ids)))
        self.assertSetEqual({URI('http://example.com/me/entity#1234')}, set(store.exists(ent_type, {
            URI('http://example.com/me/tag#1234'),
            URI('http://example.com/me/entity#1234'),
            })))
        # exists returns only nodes that exist
        self.assertSetEqual(set(), set(store.exists(ent_type, {
            URI('http://example.com/me/entity#foo'),
            URI('http://example.com/me/entity#bar'),
            })))
        self.assertSetEqual({URI('http://example.com/me/entity#1234')}, set(store.exists(ent_type, {
            URI('http://example.com/me/entity#foo'),
            URI('http://example.com/me/entity#1234'),
            })))


    def test_create(self):
        # setup
        store = SparqlStore.Open()
        store.schema = self.schema

        # node type must be valid
        self.assertRaises(errors.ConsistencyError, store.create, self.schema.node(ns.bsfs.Entity).child(ns.bsfs.invalid), {
            URI('http://example.com/me/entity#1234'), URI('http://example.com/me/entity#4321')})

        # can create some nodes
        ent_type = store.schema.node(ns.bsfs.Entity)
        store.create(ent_type, {URI('http://example.com/me/entity#1234'), URI('http://example.com/me/entity#4321')})
        self.assertSetEqual(set(store._graph), self.schema_triples | {
            # instances
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Entity)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Entity)),
            })

        # existing nodes are skipped
        store.create(ent_type, {URI('http://example.com/me/entity#1234'), URI('http://example.com/me/entity#5678')})
        self.assertSetEqual(set(store._graph), self.schema_triples | {
            # previous triples
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Entity)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Entity)),
            # new triples
            (rdflib.URIRef('http://example.com/me/entity#5678'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Entity)),
            })

        # can create nodes of a different type
        tag_type = store.schema.node(ns.bsfs.Tag)
        store.create(tag_type, {URI('http://example.com/me/tag#1234'), URI('http://example.com/me/tag#4321')})
        self.assertSetEqual(set(store._graph), self.schema_triples | {
            # previous triples
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Entity)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Entity)),
            (rdflib.URIRef('http://example.com/me/entity#5678'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Entity)),
            # new triples
            (rdflib.URIRef('http://example.com/me/tag#1234'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Tag)),
            (rdflib.URIRef('http://example.com/me/tag#4321'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Tag)),
            })

        # creation does not change types of existing nodes
        tag_type = store.schema.node(ns.bsfs.Tag)
        store.create(tag_type, {URI('http://example.com/me/entity#1234'), URI('http://example.com/me/entity#4321')})
        self.assertSetEqual(set(store._graph), self.schema_triples | {
            # previous triples
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Entity)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Entity)),
            (rdflib.URIRef('http://example.com/me/entity#5678'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Entity)),
            # new triples
            (rdflib.URIRef('http://example.com/me/tag#1234'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Tag)),
            (rdflib.URIRef('http://example.com/me/tag#4321'), rdflib.RDF.type, rdflib.URIRef(ns.bsfs.Tag)),
            })


    def test_set(self):
        # store setup
        store = SparqlStore.Open()
        store.schema = self.schema
        # prepare node types
        ent_type = store.schema.node(ns.bsfs.Entity)
        user_type = store.schema.node(ns.bsfs.User)
        tag_type = store.schema.node(ns.bsfs.Tag)
        # prepare predicates
        p_filesize = store.schema.predicate(ns.bse.filesize)
        p_comment = store.schema.predicate(ns.bse.comment)
        p_author = store.schema.predicate(ns.bse.author)
        p_tag = store.schema.predicate(ns.bse.tag)
        p_invalid = store.schema.predicate(ns.bsfs.Predicate).child(ns.bsfs.foo, range=store.schema.node(ns.bsfs.Tag))
        # create node instances
        ent_ids = {
            URI('http://example.com/me/entity#1234'),
            URI('http://example.com/me/entity#4321'),
            }
        tag_ids = {
            URI('http://example.com/me/tag#1234'),
            URI('http://example.com/me/tag#4321'),
            URI('http://example.com/me/tag#foo'),
            URI('http://example.com/me/tag#bar'),
            URI('http://example.com/me/tag#foobar'),
            URI('http://example.com/me/tag#xyz'),
            }
        user_ids = {
            URI('http://example.com/me/user#1234'),
            URI('http://example.com/me/user#4321'),
            }
        store.create(ent_type, ent_ids)
        store.create(tag_type, tag_ids)
        store.create(user_type, user_ids)

        # invalid node_type is not permitted
        self.assertRaises(errors.ConsistencyError, store.set, self.schema.node(ns.bsfs.Node).child(ns.bse.foo),
            ent_ids, p_comment, {'hello world'})

        # invalid predicate is not permitted
        self.assertRaises(errors.ConsistencyError, store.set, ent_type, ent_ids, p_invalid, {'http://example.com/me/tag#1234'})

        # predicate must match node_type
        self.assertRaises(errors.ConsistencyError, store.set, tag_type, tag_ids, p_filesize, {1234})

        # empty value does not change the graph
        plen = len(store._graph)
        store.set(ent_type, ent_ids, p_filesize, [])
        store.set(ent_type, ent_ids, p_comment, [])
        store.set(ent_type, ent_ids, p_author, [])
        store.set(ent_type, ent_ids, p_tag, [])
        self.assertEqual(plen, len(store._graph))

        # cannot set multiple values on unique predicates
        self.assertRaises(ValueError, store.set, ent_type, ent_ids, p_filesize, {1234, 4321})
        self.assertRaises(ValueError, store.set, ent_type, ent_ids, p_author, {URI('http://example.com/me/user#1234'), URI('http://example.com/me/user#4321')})

        # value nodes must exist
        self.assertRaises(errors.InstanceError, store.set, ent_type, ent_ids, p_author, {URI('http://example.com/me/user#invalid')})
        self.assertRaises(errors.InstanceError, store.set, ent_type, ent_ids, p_tag, {URI('http://example.com/me/tag#invalid')})

        # value node types must be consistent with the predicate
        self.assertRaises(errors.InstanceError, store.set, ent_type, ent_ids, p_author, {URI('http://example.com/me/entity#1234')})
        self.assertRaises(errors.InstanceError, store.set, ent_type, ent_ids, p_tag, {URI('http://example.com/me/entity#1234')})

        # all value nodes must exist and be consistent
        self.assertRaises(errors.InstanceError, store.set, ent_type, ent_ids, p_tag, {
            URI('http://example.com/me/tag#1234'), URI('http://example.com/me/tag#invalid'), URI('http://example.com/me/entity#1234')})


        # set unique literal
        store.set(ent_type, ent_ids, p_filesize, {1234})
        self.assertIn(
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_filesize.uri), rdflib.Literal('1234', datatype=rdflib.XSD.integer)),
            set(store._graph))
        self.assertIn(
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_filesize.uri), rdflib.Literal('1234', datatype=rdflib.XSD.integer)),
            set(store._graph))
        # re-assigning the same node changes nothing
        store.set(ent_type, ent_ids, p_filesize, {1234})
        self.assertIn(
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_filesize.uri), rdflib.Literal('1234', datatype=rdflib.XSD.integer)),
            set(store._graph))
        self.assertIn(
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_filesize.uri), rdflib.Literal('1234', datatype=rdflib.XSD.integer)),
            set(store._graph))
        # cannot set multiple unique literals
        self.assertRaises(ValueError, store.set, ent_type, ent_ids, p_filesize, {1234, 4321}) # same test as above
        # unique literals are overwritten by set
        store.set(ent_type, ent_ids, p_filesize, {4321})
        self.assertIn(
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_filesize.uri), rdflib.Literal('4321', datatype=rdflib.XSD.integer)),
            set(store._graph))
        self.assertNotIn(
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_filesize.uri), rdflib.Literal('1234', datatype=rdflib.XSD.integer)),
            set(store._graph))
        self.assertIn(
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_filesize.uri), rdflib.Literal('4321', datatype=rdflib.XSD.integer)),
            set(store._graph))
        self.assertNotIn(
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_filesize.uri), rdflib.Literal('1234', datatype=rdflib.XSD.integer)),
            set(store._graph))

        # set non-unique literal
        store.set(ent_type, ent_ids, p_comment, {'foobar'})
        self.assertTrue(set(store._graph).issuperset({
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_comment.uri), rdflib.Literal('foobar', datatype=rdflib.XSD.string)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_comment.uri), rdflib.Literal('foobar', datatype=rdflib.XSD.string)),
            }))
        # re-assigning the same node changes nothing
        store.set(ent_type, ent_ids, p_comment, {'foobar'})
        self.assertTrue(set(store._graph).issuperset({
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_comment.uri), rdflib.Literal('foobar', datatype=rdflib.XSD.string)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_comment.uri), rdflib.Literal('foobar', datatype=rdflib.XSD.string)),
            }))
        # can set multiple non-unique literals at once
        store.set(ent_type, ent_ids, p_comment, {'foo', 'bar'})
        self.assertTrue(set(store._graph).issuperset({
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_comment.uri), rdflib.Literal('foo', datatype=rdflib.XSD.string)),
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_comment.uri), rdflib.Literal('bar', datatype=rdflib.XSD.string)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_comment.uri), rdflib.Literal('foo', datatype=rdflib.XSD.string)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_comment.uri), rdflib.Literal('bar', datatype=rdflib.XSD.string)),
            }))
        # non-unique literals are appended by set
        store.set(ent_type, ent_ids, p_comment, {'hello world'})
        self.assertTrue(set(store._graph).issuperset({
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_comment.uri), rdflib.Literal('foo', datatype=rdflib.XSD.string)),
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_comment.uri), rdflib.Literal('bar', datatype=rdflib.XSD.string)),
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_comment.uri), rdflib.Literal('hello world', datatype=rdflib.XSD.string)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_comment.uri), rdflib.Literal('foo', datatype=rdflib.XSD.string)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_comment.uri), rdflib.Literal('bar', datatype=rdflib.XSD.string)),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_comment.uri), rdflib.Literal('hello world', datatype=rdflib.XSD.string)),
            }))

        # set unique node
        store.set(ent_type, ent_ids, p_author, {URI('http://example.com/me/user#1234')})
        self.assertIn(
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_author.uri), rdflib.URIRef('http://example.com/me/user#1234')),
            set(store._graph))
        self.assertIn(
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_author.uri), rdflib.URIRef('http://example.com/me/user#1234')),
            set(store._graph))
        # re-assigning the same node changes nothing
        store.set(ent_type, ent_ids, p_author, {URI('http://example.com/me/user#1234')})
        self.assertIn(
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_author.uri), rdflib.URIRef('http://example.com/me/user#1234')),
            set(store._graph))
        self.assertIn(
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_author.uri), rdflib.URIRef('http://example.com/me/user#1234')),
            set(store._graph))
        # cannot set multiple unique nodes
        self.assertRaises(ValueError, store.set, ent_type, ent_ids, p_author, {URI('http://example.com/me/user#1234'), URI('http://example.com/me/user#4321')})
        # unique nodes are overwritten by set
        store.set(ent_type, ent_ids, p_author, {URI('http://example.com/me/user#4321')})
        self.assertIn(
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_author.uri), rdflib.URIRef('http://example.com/me/user#4321')),
            set(store._graph))
        self.assertNotIn(
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_author.uri), rdflib.URIRef('http://example.com/me/user#1234')),
            set(store._graph))
        self.assertIn(
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_author.uri), rdflib.URIRef('http://example.com/me/user#4321')),
            set(store._graph))
        self.assertNotIn(
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_author.uri), rdflib.URIRef('http://example.com/me/user#1234')),
            set(store._graph))

        # set non-unique node
        store.set(ent_type, ent_ids, p_tag, {'http://example.com/me/tag#foobar'})
        self.assertTrue(set(store._graph).issuperset({
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#foobar')),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#foobar')),
            }))
        # re-assigning the same node changes nothing
        store.set(ent_type, ent_ids, p_tag, {'http://example.com/me/tag#foobar'})
        self.assertTrue(set(store._graph).issuperset({
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#foobar')),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#foobar')),
            }))
        # can set multiple non-unique literals at once
        store.set(ent_type, ent_ids, p_tag, {'http://example.com/me/tag#1234', 'http://example.com/me/tag#4321'})
        self.assertTrue(set(store._graph).issuperset({
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#1234')),
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#4321')),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#1234')),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#4321')),
            }))
        # non-unique nodes are appended by set
        store.set(ent_type, ent_ids, p_tag, {'http://example.com/me/tag#foo', 'http://example.com/me/tag#bar'})
        self.assertTrue(set(store._graph).issuperset({
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#1234')),
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#4321')),
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#foo')),
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#bar')),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#1234')),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#4321')),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#foo')),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_tag.uri), rdflib.URIRef('http://example.com/me/tag#bar')),
            }))

        # nothing happens when no guids are given
        plen = len(store._graph)
        store.set(ent_type, set(), p_comment, {'xyz'})
        store.set(ent_type, set(), p_tag, {URI('http://example.com/me/tag#xyz')})
        self.assertEqual(plen, len(store._graph))

        # guids must be instances of node_type
        self.assertRaises(errors.InstanceError, store.set, ent_type, tag_ids, p_comment, {'xyz'})
        # inexistent guids
        self.assertRaises(errors.InstanceError, store.set, ent_type, {URI('http://example.com/me/entity#foobar')}, p_comment, {'xyz'})

        # BinaryBlob values are base64 encoded
        p_asset = store.schema.predicate(ns.bse.asset)
        store.set(ent_type, ent_ids, p_asset, {bytes(range(128)), bytes(range(128, 256))})
        blob1 = rdflib.Literal('AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn8=',
            datatype=rdflib.URIRef(ns.bsfs.BinaryBlob))
        blob2 = rdflib.Literal('gIGCg4SFhoeIiYqLjI2Oj5CRkpOUlZaXmJmam5ydnp+goaKjpKWmp6ipqqusra6vsLGys7S1tre4ubq7vL2+v8DBwsPExcbHyMnKy8zNzs/Q0dLT1NXW19jZ2tvc3d7f4OHi4+Tl5ufo6err7O3u7/Dx8vP09fb3+Pn6+/z9/v8=',
            datatype=rdflib.URIRef(ns.bsfs.BinaryBlob))
        self.assertTrue(set(store._graph).issuperset({
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_asset.uri), blob1),
            (rdflib.URIRef('http://example.com/me/entity#1234'), rdflib.URIRef(p_asset.uri), blob2),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_asset.uri), blob1),
            (rdflib.URIRef('http://example.com/me/entity#4321'), rdflib.URIRef(p_asset.uri), blob2),
            }))
        # lit.value returns the original bytes value
        self.assertSetEqual({lit.value for lit in store._graph.objects(None, rdflib.URIRef(p_asset.uri))},
            {bytes(range(128)), bytes(range(128, 256))})


## main ##

if __name__ == '__main__':
    unittest.main()

## EOF ##