summaryrefslogtreecommitdiff
path: root/tests/functional-tests/17-ontology-changes.py
blob: bbbf0ff205089138f7d521221bb8f723278dbfd1 (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
#!/usr/bin/python
#
# Copyright (C) 2010, Nokia <ivan.frade@nokia.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#

"""
Stand-alone tests cases for the store, booting it with different ontology
changes and checking if the data is still there.
"""
import time

import os
import dbus # Just for the Exception
from common.utils import configuration as cfg
import unittest2 as ut
#import unittest as ut
from common.utils.system import TrackerSystemAbstraction as TrackerSystemAbstraction
from common.utils.system import UnableToBootException as UnableToBootException
from common.utils.helpers import StoreHelper as StoreHelper
from common.utils.expectedFailure import expectedFailureBug, expectedFailureJournal


RDFS_RANGE = "http://www.w3.org/2000/01/rdf-schema#range"
XSD_DATETIME = "http://www.w3.org/2001/XMLSchema#dateTime"
XSD_STRING = "http://www.w3.org/2001/XMLSchema#string"
XSD_INTEGER = "http://www.w3.org/2001/XMLSchema#integer"

TEST_PREFIX = "http://example.org/ns#"

import re
import time

class OntologyChangeTestTemplate (ut.TestCase):
    """
    Template class for the ontology changes tests. The tests are subclasses
    of this, implementing these methods:
    
       * set_ontology_dirs
       * insert_data
       * validate_status
       
    and adding a method 'test_x_y_z' to be invoked by unittest.
   
    Check doc in those methods for the specific details.
    """
        
    def get_ontology_dir (self, param):
        local = os.path.join (os.getcwd (), "test-ontologies", param)
        if (os.path.exists (local)):
            # Use local directory if available
            return local
        else:
            return os.path.join (cfg.DATADIR, "tracker-tests",
                                    "test-ontologies", param)

    def setUp (self):
        self.system = TrackerSystemAbstraction ()

    def tearDown (self):
        self.system.tracker_store_testing_stop ()

    def template_test_ontology_change (self):

        self.set_ontology_dirs ()

        
        basic_ontologies = self.get_ontology_dir (self.FIRST_ONTOLOGY_DIR)
        modified_ontologies = self.get_ontology_dir (self.SECOND_ONTOLOGY_DIR)

        self.__assert_ontology_dates (basic_ontologies, modified_ontologies)


        self.system.tracker_store_testing_start (ontodir=basic_ontologies)
        self.tracker = self.system.store

        self.insert_data ()

        try:
            # Boot the second set of ontologies
            self.system.tracker_store_restart_with_new_ontologies (modified_ontologies)
        except UnableToBootException, e:
            self.fail (str(self.__class__) + " " + str(e))

        self.validate_status ()

    def set_ontology_dirs (self):
        """
        Implement this method in the subclass setting values for:
        self.FIRST_ONTOLOGY_DIR and
        self.SECOND_ONTOLOGY_DIR
        """
        raise Exception ("Subclasses must implement 'set_ontology_dir'")

    def insert_data (self):
        """
        Put in the store some data with the FIRST ontology
        """
        raise Exception ("Subclasses must implement 'insert_data'")

    def validate_status (self):
        """
        This is called after restarting the store with the SECOND ontology
        Check that the inserted data was handled correctly and the ontology
        is up to date
        """
        raise Exception ("Subclasses must implement 'validate_status'")


    def assertInDbusResult (self, member, dbus_result, column=0):
        """
        Convenience assertion used in these tests
        """
        for row in dbus_result:
            if member == row[column]:
                return
        # This is going to fail with pretty printing
        self.assertIn (member, dbus_result) 

    def assertNotInDbusResult (self, member, dbus_result, column=0):
        """
        Convenience assertion used in these tests
        """
        for row in dbus_result:
            if member == str(row[column]):
                # This is going to fail with pretty printing
                self.fail ("'%s' wasn't supposed to be in '%s'" % (member, dbus_result))
        return

    def __assert_ontology_dates (self, first_dir, second_dir):
        """
        Asserts that 91-test.ontology in second_dir has a more recent
        modification time than in first_dir
        """
        ISO9601_REGEX = "(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z)"

        def get_ontology_date (ontology):
            for line in open (ontology, 'r'):
                if "nao:lastModified" in line:
                    getmodtime = re.compile ('nao:lastModified\ \"' + ISO9601_REGEX + '\"')
                    modtime_match = getmodtime.search (line)

                    if (modtime_match):
                        nao_date = modtime_match.group (1)
                        return time.strptime(nao_date, "%Y-%m-%dT%H:%M:%SZ")  
                    else:
                        print "something funky in", line
                    break

        first_date = get_ontology_date (os.path.join (first_dir, "91-test.ontology"))
        second_date = get_ontology_date (os.path.join (second_dir, "91-test.ontology"))
        if first_date >= second_date:
            self.fail ("nao:modifiedTime in '%s' is not more recent in the second ontology" % ("91-test.ontology"))
        

        

class PropertyRangeStringToDate (OntologyChangeTestTemplate):
    """
    Change the range of a property from string to date. There shouldn't be any data loss.
    """

    @expectedFailureJournal()
    def test_property_range_string_to_date (self):
        self.template_test_ontology_change ()

    def set_ontology_dirs (self):
        self.FIRST_ONTOLOGY_DIR = "basic"
        self.SECOND_ONTOLOGY_DIR = "property-range-string-to-date"

    def insert_data (self):
        self.instance = "test://ontology-change/property-range/string-to-date"
        self.tracker.update ("INSERT { <%s> a test:A ; test:a_string '2010-10-12T13:30:00Z' }"
                             % (self.instance))

    def validate_status (self):
        # Query the ontology itself
        result = self.tracker.query ("SELECT ?o WHERE { test:a_string rdfs:range ?o }")
        self.assertEquals (result[0][0], XSD_DATETIME)

        # Check the value is there
        result = self.tracker.query ("SELECT ?o WHERE { <%s> test:a_string ?o . }" % (self.instance))
        self.assertEquals (result[0][0], "2010-10-12T13:30:00Z")


class PropertyRangeDateToString (OntologyChangeTestTemplate):
    """
    Change the range of a property from date to string. There shouldn't be any data loss.
    """

    @expectedFailureJournal()
    def test_property_range_date_to_string (self):
        self.template_test_ontology_change ()

    def set_ontology_dirs (self):
        self.FIRST_ONTOLOGY_DIR = "property-range-string-to-date"
        self.SECOND_ONTOLOGY_DIR = "basic-future"
        
    def insert_data (self):
        self.instance = "test://ontology-change/property-range/date-to-string"
        self.tracker.update ("INSERT { <%s> a test:A ; test:a_string '2010-10-12T13:30:00Z' }"
                             % (self.instance))

    def validate_status (self):
        # Query the ontology itself
        result = self.tracker.query ("SELECT ?o WHERE { test:a_string rdfs:range ?o }")
        self.assertEquals (result[0][0], XSD_STRING)

        # Check the value is there
        result = self.tracker.query ("SELECT ?o WHERE { <%s> test:a_string ?o . }" % (self.instance))
        self.assertEquals (result[0][0], "2010-10-12T13:30:00Z")

class PropertyRangeIntToString (OntologyChangeTestTemplate):
    """
    Change the range of a property from int to string. There shouldn't be any data loss.
    """
    def test_property_range_int_to_str (self):
        self.template_test_ontology_change ()

    def set_ontology_dirs (self):
        self.FIRST_ONTOLOGY_DIR = "basic"
        self.SECOND_ONTOLOGY_DIR = "property-range-int-to-string"

    def insert_data (self):
        self.instance = "test://ontology-change/property-range/int-to-string"
        self.tracker.update ("INSERT { <%s> a test:A; test:a_int 12. }" % (self.instance))

    def validate_status (self):
        result = self.tracker.query ("SELECT ?o WHERE { test:a_int rdfs:range ?o. }")
        self.assertEquals (str(result[0][0]), XSD_STRING)

        # Check the value is there
        result = self.tracker.query ("SELECT ?o WHERE { <%s> test:a_int ?o .}" % (self.instance))
        self.assertEquals (result[0][0], "12")

class PropertyRangeStringToInt (OntologyChangeTestTemplate):
    """
    Change the range of a property from string to int. There shouldn't be any data loss.
    """

    def test_property_range_str_to_int (self):
        self.template_test_ontology_change ()

    def set_ontology_dirs (self):
        self.FIRST_ONTOLOGY_DIR = "property-range-int-to-string"
        self.SECOND_ONTOLOGY_DIR = "basic-future"

    def insert_data (self):
        self.instance = "test://ontology-change/property-range/string-to-int"
        self.tracker.update ("INSERT { <%s> a test:A; test:a_int '12'. }" % (self.instance))

    def validate_status (self):
        result = self.tracker.query ("SELECT ?o WHERE { test:a_int rdfs:range ?o. }")
        self.assertEquals (str(result[0][0]), XSD_INTEGER)

        # Check the value is there
        result = self.tracker.query ("SELECT ?o WHERE { <%s> test:a_int ?o .}" % (self.instance))
        self.assertEquals (result[0][0], "12")
        
class PropertyMaxCardinality1toN (OntologyChangeTestTemplate):
    """
    Change cardinality of a property from 1 to N. There shouldn't be any data loss
    """

    @expectedFailureJournal()
    def test_property_cardinality_1_to_n (self):
        self.template_test_ontology_change ()

    def set_ontology_dirs (self):
        #self.FIRST_ONTOLOGY_DIR = "basic"
        #self.SECOND_ONTOLOGY_DIR = "cardinality"

        self.FIRST_ONTOLOGY_DIR = "cardinality"
        self.SECOND_ONTOLOGY_DIR = "basic-future"

    def insert_data (self):
        self.instance = "test://ontology-change/cardinality/1-to-n"
        self.tracker.update ("INSERT { <%s> a test:A; test:a_n_cardinality 'some text'. }" % (self.instance))

        result = self.tracker.query ("SELECT ?o WHERE { test:a_n_cardinality nrl:maxCardinality ?o}")
        self.assertEquals (int (result[0][0]), 1)

                
    def validate_status (self):
        result = self.tracker.query ("SELECT ?o WHERE { test:a_n_cardinality nrl:maxCardinality ?o}")
        self.assertEquals (len (result), 0, "Cardinality should be 0")
        
        # Check the value is there
        result = self.tracker.query ("SELECT ?o WHERE { <%s> test:a_n_cardinality ?o .}" % (self.instance))
        self.assertEquals (str(result[0][0]), "some text")

class PropertyMaxCardinalityNto1 (OntologyChangeTestTemplate):
    """
    Change the cardinality of a property for N to 1.
    """

    @expectedFailureJournal()
    def test_property_cardinality_n_to_1 (self):
        self.template_test_ontology_change ()

    def set_ontology_dirs (self):
        self.FIRST_ONTOLOGY_DIR = "basic"
        self.SECOND_ONTOLOGY_DIR = "cardinality"

    def insert_data (self):
        self.instance = "test://ontology-change/cardinality/1-to-n"
        self.tracker.update ("INSERT { <%s> a test:A; test:a_n_cardinality 'some text'. }" % (self.instance))

        result = self.tracker.query ("SELECT ?o WHERE { test:a_n_cardinality nrl:maxCardinality ?o}")
        self.assertEquals (len (result), 0, "Cardinality should be 0")

                
    def validate_status (self):
        result = self.tracker.query ("SELECT ?o WHERE { test:a_n_cardinality nrl:maxCardinality ?o}")
        self.assertEquals (int (result[0][0]), 1, "Cardinality should be 1")
        
        # Check the value is there
        result = self.tracker.query ("SELECT ?o WHERE { <%s> test:a_n_cardinality ?o .}" % (self.instance))
        self.assertEquals (str(result[0][0]), "some text")

class ClassNotifySet (OntologyChangeTestTemplate):
    """
    Set tracker:notify to true in a class and check there is no data loss
    """
    def test_property_notify_set (self):
        self.template_test_ontology_change ()

    def set_ontology_dirs (self):
        self.FIRST_ONTOLOGY_DIR = "basic"
        self.SECOND_ONTOLOGY_DIR = "notify"

    def insert_data (self):
        self.instance = "test://ontology-change/notify/true"
        self.tracker.update ("INSERT { <%s> a test:A; test:a_string 'some text'. }" % (self.instance))


    def validate_status (self):
        result = self.tracker.query ("SELECT ?notify WHERE { test:A tracker:notify ?notify}")
        self.assertEquals (str(result[0][0]), "true")
        
        result = self.tracker.query ("SELECT ?u WHERE { ?u a test:A. }")
        self.assertEquals (str(result[0][0]), self.instance)

class ClassNotifyUnset (OntologyChangeTestTemplate):
    """
    Set tracker:notify to true in a class and check there is no data loss
    """
    def test_property_notify_set (self):
        self.template_test_ontology_change ()

    def set_ontology_dirs (self):
        self.FIRST_ONTOLOGY_DIR = "notify"
        self.SECOND_ONTOLOGY_DIR = "basic-future"

    def insert_data (self):
        self.instance = "test://ontology-change/notify/true"
        self.tracker.update ("INSERT { <%s> a test:A; test:a_string 'some text'. }" % (self.instance))


    def validate_status (self):
        result = self.tracker.query ("SELECT ?notify WHERE { test:A tracker:notify ?notify}")
        if (len (result) == 1):
            # Usually is (none) but it was "true" before so now has value.
            self.assertEquals (result[0][0], "false")
        else:
            self.assertEquals (len (result), 0)
        
        result = self.tracker.query ("SELECT ?u WHERE { ?u a test:A. }")
        self.assertEquals (str(result[0][0]), self.instance)


class PropertyIndexedSet (OntologyChangeTestTemplate):
    """
    Set tracker:indexed true to single and multiple valued properties.
    Check that instances and content of the property are still in the DB
    """
    def test_indexed_set (self):
        self.template_test_ontology_change ()

    def set_ontology_dirs (self):
        self.FIRST_ONTOLOGY_DIR = "basic"
        self.SECOND_ONTOLOGY_DIR = "indexed"

    def insert_data (self):
        # Instance with value in the single valued property
        self.instance_single_valued = "test://ontology-change/indexed/single/true"
        self.tracker.update ("INSERT { <%s> a test:A ; test:a_string 'anything 1'. }"
                             % (self.instance_single_valued))

        # Instance with value in the n valued property
        self.instance_n_valued = "test://ontology-change/indexed/multiple/true"
        self.tracker.update ("INSERT { <%s> a test:A ; test:a_n_cardinality 'anything n'. }"
                             % (self.instance_n_valued))

    def validate_status (self):
        # Check ontology and instance for the single valued property
        result = self.tracker.query ("SELECT ?indexed WHERE { test:a_string tracker:indexed ?indexed}")
        self.assertEquals (str(result[0][0]), "true")

        result = self.tracker.query ("SELECT ?content WHERE { <%s> a test:A; test:a_string ?content. }"
                                     % (self.instance_single_valued))
        self.assertEquals (str(result[0][0]), "anything 1")

        # Check ontology and instance for the multiple valued property
        result = self.tracker.query ("SELECT ?indexed WHERE { test:a_n_cardinality tracker:indexed ?indexed}")
        self.assertEquals (str(result[0][0]), "true")

        result = self.tracker.query ("SELECT ?content WHERE { <%s> a test:A; test:a_n_cardinality ?content. }"
                                     % (self.instance_n_valued))
        self.assertEquals (str(result[0][0]), "anything n")

class PropertyIndexedUnset (OntologyChangeTestTemplate):
    """
    tracker:indexed property from true to false in single and multiple valued properties.
    Check that instances and content of the property are still in the DB.
    """
    def test_indexed_unset (self):
        self.template_test_ontology_change ()

    def set_ontology_dirs (self):
        self.FIRST_ONTOLOGY_DIR = "indexed"
        self.SECOND_ONTOLOGY_DIR = "basic-future"

    def insert_data (self):
        # Instance with value in the single valued property
        self.instance_single_valued = "test://ontology-change/indexed/single/true"
        self.tracker.update ("INSERT { <%s> a test:A ; test:a_string 'anything 1'. }"
                             % (self.instance_single_valued))

        # Instance with value in the n valued property
        self.instance_n_valued = "test://ontology-change/indexed/multiple/true"
        self.tracker.update ("INSERT { <%s> a test:A ; test:a_n_cardinality 'anything n'. }"
                             % (self.instance_n_valued))

    def validate_status (self):
        #
        # NOTE: tracker:indexed can be 'false' or None. In both cases is fine.
        # 
        
        # Check ontology and instance for the single valued property
        result = self.tracker.query ("SELECT ?indexed WHERE { test:a_string tracker:indexed ?indexed}")
        self.assertEquals (str(result[0][0]), "false")

        result = self.tracker.query ("SELECT ?content WHERE { <%s> a test:A; test:a_string ?content. }"
                                     % (self.instance_single_valued))
        self.assertEquals (str(result[0][0]), "anything 1")

        # Check ontology and instance for the multiple valued property
        result = self.tracker.query ("SELECT ?indexed WHERE { test:a_n_cardinality tracker:indexed ?indexed}")
        self.assertEquals (str(result[0][0]), "false")

        result = self.tracker.query ("SELECT ?content WHERE { <%s> a test:A; test:a_n_cardinality ?content. }"
                                     % (self.instance_n_valued))
        self.assertEquals (str(result[0][0]), "anything n")

class OntologyAddClassTest (OntologyChangeTestTemplate):
    """
    Add a class in the ontology.
    """
    def test_ontology_add_class (self):
        self.template_test_ontology_change ()

    def set_ontology_dirs (self):
        self.FIRST_ONTOLOGY_DIR = "basic"
        self.SECOND_ONTOLOGY_DIR = "add-class"

    def insert_data (self):
        # No need, adding a class
        pass

    def validate_status (self):
        # check the class is there
        result = self.tracker.query ("SELECT ?k WHERE { ?k a rdfs:Class. }")
        self.assertInDbusResult (TEST_PREFIX + "D", result)

        result = self.tracker.query ("SELECT ?k WHERE { ?k a rdfs:Class. }")
        self.assertInDbusResult (TEST_PREFIX + "E", result)


class OntologyRemoveClassTest (OntologyChangeTestTemplate):
    """
    Remove a class from the ontology. With and without superclasses.
    """
    def test_ontology_remove_class (self):
        self.template_test_ontology_change ()

    def set_ontology_dirs (self):
        self.FIRST_ONTOLOGY_DIR = "add-class"
        self.SECOND_ONTOLOGY_DIR = "basic-future"

    def insert_data (self):
        self.instance_e = "test://ontology-change/removal/class/1"
        self.tracker.update ("INSERT { <%s> a test:E. }" % self.instance_e)

        self.instance_d = "test://ontology-change/removal/class/2"
        self.tracker.update ("INSERT { <%s> a test:D. }" % self.instance_d)

    def validate_status (self):
        #
        # The classes are not actually removed... so this assertions are not valid (yet?)
        #
        
        #result = self.tracker.query ("SELECT ?k WHERE { ?k a rdfs:Class. }")
        #self.assertNotInDbusResult (TEST_PREFIX + "E", result)
        #self.assertNotInDbusResult (TEST_PREFIX + "D", result)

        # D is a subclass of A, removing D should keep the A instances
        result = self.tracker.query ("SELECT ?i WHERE { ?i a test:A. }")
        self.assertEquals (result[0][0], self.instance_e)

class OntologyAddPropertyTest (OntologyChangeTestTemplate):
    """
    Add new properties in the ontology, with/without super prop and different ranges and cardinalities
    """
    def test_ontology_add_property (self):
        self.template_test_ontology_change ()

    def set_ontology_dirs (self):
        self.FIRST_ONTOLOGY_DIR = "basic"
        self.SECOND_ONTOLOGY_DIR = "add-prop"

    def insert_data (self):
        # No need, adding new properties
        pass

    def validate_status (self):
        result = self.tracker.query ("SELECT ?k WHERE { ?k a rdf:Property}")
        self.assertInDbusResult (TEST_PREFIX + "new_prop_int", result)
        self.assertInDbusResult (TEST_PREFIX + "new_prop_int_n", result)

        self.assertInDbusResult (TEST_PREFIX + "new_prop_string", result)
        self.assertInDbusResult (TEST_PREFIX + "new_prop_string_n", result)

        self.assertInDbusResult (TEST_PREFIX + "new_subprop_string", result)
        self.assertInDbusResult (TEST_PREFIX + "new_subprop_string_n", result)

class OntologyRemovePropertyTest (OntologyChangeTestTemplate):
    """
    Remove properties from the ontology, with and without super props and different ranges and cardinalities
    """
    def test_ontology_remove_property (self):
        self.template_test_ontology_change ()

    def set_ontology_dirs (self):
        self.FIRST_ONTOLOGY_DIR = "add-prop"
        self.SECOND_ONTOLOGY_DIR = "basic-future"

    def insert_data (self):
        self.instance_a = "test://ontology-change/remove/properties/1"
        self.tracker.update ("""
            INSERT { <%s> a   test:A;
                          test:a_string 'This is fine' ;
                          test:new_prop_int 7;
                          test:new_prop_int_n 3;
                          test:new_prop_string 'this is going to disappear' ;
                          test:new_prop_string_n 'same with this' .
                   }
           """ % (self.instance_a))

        self.instance_b = "test://ontology-change/remove/properties/2"
        self.tracker.update ("""
            INSERT { <%s> a   test:B;
                          test:new_subprop_string 'super-prop keeps this value';
                          test:new_subprop_string_n 'super-prop also keeps this value'.
                   }
        """ % (self.instance_b))
        self.assertTrue (self.tracker.ask ("ASK { <%s> a test:A}" % (self.instance_a)), "The instance is not there")

    def validate_status (self):
        #
        # Note: on removal basically nothing happens. The property and values are still in the DB
        #
        # Maybe we should test there forcing a db reconstruction and journal replay
        
        # First the ontology
        ## result = self.tracker.query ("SELECT ?k WHERE { ?k a rdf:Property}")
        ## self.assertNotInDbusResult (TEST_PREFIX + "new_prop_int", result)
        ## self.assertNotInDbusResult (TEST_PREFIX + "new_prop_int_n", result)

        ## self.assertNotInDbusResult (TEST_PREFIX + "new_prop_string", result)
        ## self.assertNotInDbusResult (TEST_PREFIX + "new_prop_string_n", result)

        ## self.assertNotInDbusResult (TEST_PREFIX + "new_subprop_string", result)
        ## self.assertNotInDbusResult (TEST_PREFIX + "new_subprop_string_n", result)

        # The instances are still there
        self.assertTrue (self.tracker.ask ("ASK { <%s> a test:A}" % (self.instance_a)))
        self.assertTrue (self.tracker.ask ("ASK { <%s> a test:B}" % (self.instance_b)))

        check = self.tracker.ask ("ASK { <%s> test:a_superprop 'super-prop keeps this value' }" % (self.instance_b))
        self.assertTrue (check, "This property and value should exist")
        
        check = self.tracker.ask ("ASK { <%s> test:a_superprop_n 'super-prop also keeps this value' }" % (self.instance_b))
        self.assertTrue (check, "This property and value should exist")

class DomainIndexAddTest (OntologyChangeTestTemplate):
    """
    Add tracker:domainIndex to a class and check there is no data loss.
    """
    def test_domain_index_add (self):
        self.template_test_ontology_change ()

    def set_ontology_dirs (self):
        self.FIRST_ONTOLOGY_DIR = "basic"
        self.SECOND_ONTOLOGY_DIR = "add-domainIndex"

    def insert_data (self):
        self.instance_a = "test://ontology-changes/properties/add-domain-index/a"
        self.tracker.update ("""
            INSERT { <%s> a test:B ;
                          test:a_string 'test-value' ;
                          test:a_n_cardinality 'another-test-value'. }""" % (self.instance_a))

        self.instance_b = "test://ontology-changes/properties/add-domain-index/b"
        self.tracker.update ("""
            INSERT { <%s> a test:C ;
                          test:a_string 'test-value' ;
                          test:a_n_cardinality 'another-test-value'. }""" % (self.instance_b))

    def validate_status (self):
        # Check the ontology
        has_domainIndex = self.tracker.ask ("ASK { test:B tracker:domainIndex test:a_string }")
        self.assertTrue (has_domainIndex)

        has_domainIndex = self.tracker.ask ("ASK { test:C tracker:domainIndex test:a_n_cardinality }")
        self.assertTrue (has_domainIndex)

        # Check the data
        dataok = self.tracker.ask ("ASK { <%s> test:a_string 'test-value' }" % (self.instance_a))
        self.assertTrue (dataok)

        dataok = self.tracker.ask ("ASK { <%s> test:a_n_cardinality 'another-test-value' }" % (self.instance_b))
        self.assertTrue (dataok)


class DomainIndexRemoveTest (OntologyChangeTestTemplate):
    """
    Remove tracker:domainIndex to a class and check there is no data loss.
    """
    def test_domain_index_remove (self):
        self.template_test_ontology_change ()

    def set_ontology_dirs (self):
        self.FIRST_ONTOLOGY_DIR = "add-domainIndex"
        self.SECOND_ONTOLOGY_DIR = "basic-future"

    def insert_data (self):
        self.instance_a = "test://ontology-changes/properties/add-domain-index/a"
        self.tracker.update ("""
            INSERT { <%s> a test:B ;
                          test:a_string 'test-value' ;
                          test:a_n_cardinality 'another-test-value'. }""" % (self.instance_a))

        self.instance_b = "test://ontology-changes/properties/add-domain-index/b"
        self.tracker.update ("""
            INSERT { <%s> a test:C ;
                          test:a_string 'test-value' ;
                          test:a_n_cardinality 'another-test-value'. }""" % (self.instance_b))

    def validate_status (self):
        # Check the ontology
        has_domainIndex = self.tracker.ask ("ASK { test:B tracker:domainIndex test:a_string }")
        self.assertFalse (has_domainIndex)

        has_domainIndex = self.tracker.ask ("ASK { test:C tracker:domainIndex test:a_n_cardinality }")
        self.assertFalse (has_domainIndex)

        # Check the data
        dataok = self.tracker.ask ("ASK { <%s> test:a_string 'test-value' }" % (self.instance_a))
        self.assertTrue (dataok)

        dataok = self.tracker.ask ("ASK { <%s> test:a_n_cardinality 'another-test-value' }" % (self.instance_b))
        self.assertTrue (dataok)


class SuperclassRemovalTest (OntologyChangeTestTemplate):
    """
    Remove the superclass relation between two classes
    """
    @expectedFailureJournal()
    def test_superclass_removal (self):
        self.template_test_ontology_change ()
        
    def set_ontology_dirs (self):
        self.FIRST_ONTOLOGY_DIR = "basic"
        self.SECOND_ONTOLOGY_DIR = "superclass-remove"
            
    def insert_data (self):
        is_subclass = self.tracker.ask ("ASK {test:B rdfs:subClassOf test:A}")
        self.assertTrue (is_subclass)
        
        self.instance_a = "test://ontology-changes/superclasses/remove-superclass/a"
        self.tracker.update ("""
         INSERT { <%s> a test:A . }
        """ % (self.instance_a))

        self.instance_b = "test://ontology-changes/superclasses/remove-superclass/b"
        self.tracker.update ("""
         INSERT { <%s> a test:B . }
        """ % (self.instance_b))

        result = self.tracker.count_instances ("test:B")
        self.assertEquals (result, 1)

        result = self.tracker.count_instances ("test:A")
        self.assertEquals (result, 2)

    def validate_status (self):
        is_subclass = self.tracker.ask ("ASK {test:B rdfs:subClassOf test:A}")
        self.assertFalse (is_subclass)

        result = self.tracker.count_instances ("test:B")
        self.assertEquals (result, 1)

        result = self.tracker.count_instances ("test:A")
        self.assertEquals (result, 1)

class SuperclassAdditionTest (OntologyChangeTestTemplate):
    """
    Add a superclass to a class with no superclass previously
    """
    @expectedFailureJournal()
    def test_superclass_addition (self):
        self.template_test_ontology_change ()
        
    def set_ontology_dirs (self):
        self.FIRST_ONTOLOGY_DIR = "superclass-remove"
        self.SECOND_ONTOLOGY_DIR = "basic-future"
            
    def insert_data (self):
        is_subclass = self.tracker.ask ("ASK {test:B rdfs:subClassOf test:A}")
        self.assertFalse (is_subclass)
        
        self.instance_a = "test://ontology-changes/superclasses/remove-superclass/a"
        self.tracker.update ("""
         INSERT { <%s> a test:A . }
        """ % (self.instance_a))

        self.instance_b = "test://ontology-changes/superclasses/remove-superclass/b"
        self.tracker.update ("""
         INSERT { <%s> a test:B . }
        """ % (self.instance_b))

        result = self.tracker.count_instances ("test:B")
        self.assertEquals (result, 1)

        result = self.tracker.count_instances ("test:A")
        self.assertEquals (result, 1)
        
    def validate_status (self):
        is_subclass = self.tracker.ask ("ASK {test:B rdfs:subClassOf test:A}")
        self.assertTrue (is_subclass)

        result = self.tracker.count_instances ("test:B")
        self.assertEquals (result, 1)

        result = self.tracker.count_instances ("test:A")
        self.assertEquals (result, 2)
        

class PropertyPromotionTest (OntologyChangeTestTemplate):
    """
    Move a property to the superclass
    """
    @expectedFailureJournal()
    def test_property_promotion (self):
        self.template_test_ontology_change ()
        
    def set_ontology_dirs (self):
        self.FIRST_ONTOLOGY_DIR = "basic"
        self.SECOND_ONTOLOGY_DIR = "property-promotion"
            
    def insert_data (self):
        self.instance_b = "test://ontology-change/property/promotion-to-superclass/b"
        self.tracker.update ("""
            INSERT { <%s> a test:B; test:b_property 'content-b-test'; test:b_property_n 'b-test-n'. }
           """ % (self.instance_b))

        self.instance_a = "test://ontology-change/property/promotion-to-superclass/a"
        self.assertRaises (dbus.DBusException,
                           self.tracker.update,
                           "INSERT { <%s> a test:A; test:b_property 'content-a-test'.}" % (self.instance_a))
        
    def validate_status (self):
        # This insertion should work now
        self.tracker.update ("""
           INSERT { <%s> a test:A; test:b_property 'content-a-test'.}
        """ % (self.instance_a))

        # No data loss
        result = self.tracker.query ("SELECT ?v ?w WHERE { <%s> test:b_property ?v ; test:b_property_n ?w }"
                                     % (self.instance_b))
        self.assertEquals (result [0][0], "content-b-test")
        self.assertEquals (result [0][1], "b-test-n")

class PropertyRelegationTest (OntologyChangeTestTemplate):
    """
    Move a property to the subclass
    """
    @expectedFailureJournal()
    def test_property_relegation (self):
        self.template_test_ontology_change ()
        
    def set_ontology_dirs (self):
        self.FIRST_ONTOLOGY_DIR = "property-promotion"
        self.SECOND_ONTOLOGY_DIR = "basic-future"
            
    def insert_data (self):
        self.instance_b = "test://ontology-change/property/promotion-to-superclass/b"
        self.tracker.update ("""
            INSERT { <%s> a test:B; test:b_property 'content-b-test'; test:b_property_n 'b-test-n'. }
           """ % (self.instance_b))

        self.instance_a = "test://ontology-change/property/promotion-to-superclass/a"
        self.tracker.update ("""
           INSERT { <%s> a test:A; test:b_property 'content-a-test'.}
        """ % (self.instance_a))
        
    def validate_status (self):
        # This insertion should fail now
        self.assertRaises (dbus.DBusException,
                           self.tracker.update,
                           "INSERT { <%s> a test:A; test:b_property 'content-a-test'.}" % (self.instance_a))
        # No data loss
        result = self.tracker.query ("SELECT ?v ?w WHERE { <%s> test:b_property ?v; test:b_property_n ?w }"
                                     % (self.instance_b))
        self.assertEquals (result [0][0], "content-b-test")
        self.assertEquals (result [0][1], "b-test-n")



if __name__ == "__main__":
    ut.main ()