[tracker/tracker-1.2] functional-tests: Allow waiting on a specific property, and fix tests that need this



commit a2088d54209bd2daf01a6370c952493b55fdabd3
Author: Sam Thursfield <sam afuera me uk>
Date:   Mon Jul 21 21:56:58 2014 +0100

    functional-tests: Allow waiting on a specific property, and fix tests that need this
    
    Resources are created when the miner process sees them, but often we
    want to wait until they have been processed by the extractor, too. We
    can achieve that by waiting for the insertion of a specific triple that
    we know is set by tracker-extract.
    
    This required adding a new parameter to the await_resource_inserted()
    function.
    
    The 310-fts-indexing and the base writeback test have been fixed to make
    use of this, instead of using tracker_miner_fs_wait_for_idle().

 tests/functional-tests/310-fts-indexing.py         |    3 +-
 tests/functional-tests/common/utils/helpers.py     |   59 ++++++++++++++-----
 .../functional-tests/common/utils/writebacktest.py |   21 ++++++-
 3 files changed, 63 insertions(+), 20 deletions(-)
---
diff --git a/tests/functional-tests/310-fts-indexing.py b/tests/functional-tests/310-fts-indexing.py
index 90e0c7d..33e2adf 100755
--- a/tests/functional-tests/310-fts-indexing.py
+++ b/tests/functional-tests/310-fts-indexing.py
@@ -59,7 +59,8 @@ class CommonMinerFTS (CommonTrackerMinerTest):
         f.write (text)
         f.close ()
         self.tracker.await_resource_inserted (rdf_class = 'nfo:Document',
-                                              url = uri (self.testfile))
+                                              url = uri (self.testfile),
+                                              required_property = 'nie:plainTextContent')
         self.tracker.reset_graph_updates_tracking ()
 
     def search_word (self, word):
diff --git a/tests/functional-tests/common/utils/helpers.py b/tests/functional-tests/common/utils/helpers.py
index d957381..607efdb 100644
--- a/tests/functional-tests/common/utils/helpers.py
+++ b/tests/functional-tests/common/utils/helpers.py
@@ -304,18 +304,26 @@ class StoreHelper (Helper):
         self.graph_updated_timeout_id = GLib.timeout_add_seconds (REASONABLE_TIMEOUT,
                                                                   self._graph_updated_timeout_cb)
 
-    def await_resource_inserted (self, rdf_class, url = None, title = None):
+    def await_resource_inserted (self, rdf_class, url = None, title = None, required_property = None):
         """
         Block until a resource matching the parameters becomes available
         """
         assert (self.inserts_match_function == None)
 
+        self.matched_resource_urn = None
+        self.matched_resource_id = None
+
+        log ("Await new %s (%i existing inserts)" % (rdf_class, len (self.inserts_list)))
+
+        if required_property is not None:
+            required_property_id = self.get_resource_id_by_uri(required_property)
+            log ("Required property %s id %i" % (required_property, required_property_id))
+
+        known_subjects = set ()
         def find_resource_insertion (inserts_list):
-            matched = False
+            matched_creation = (self.matched_resource_id is not None)
+            matched_required_property = False
             remaining_events = []
-            known_subjects = set ()
-
-            #print "Got inserts: ", inserts_list, "\n"
 
             # FIXME: this could be done in an easier way: build one query that filters
             # based on every subject id in inserts_list, and returns the id of the one
@@ -323,7 +331,7 @@ class StoreHelper (Helper):
             for insert in inserts_list:
                 id = insert[1]
 
-                if not matched and id not in known_subjects:
+                if not matched_creation and id not in known_subjects:
                     known_subjects.add (id)
 
                     where = "  ?urn a %s " % rdf_class
@@ -335,18 +343,28 @@ class StoreHelper (Helper):
                         where += "; nie:title \"%s\"" % title
 
                     query = "SELECT ?urn WHERE { %s FILTER (tracker:id(?urn) = %s)}" % (where, insert[1])
-                    #print "%s\n" % query
                     result_set = self.query (query)
-                    #print result_set, "\n\n"
 
                     if len (result_set) > 0:
-                        matched = True
+                        matched_creation = True
                         self.matched_resource_urn = result_set[0][0]
                         self.matched_resource_id = insert[1]
-
-                if not matched or id != self.matched_resource_id:
+                        log ("Matched creation of resource %s (%i)" %
+                             (self.matched_resource_urn,
+                              self.matched_resource_id))
+                        if required_property is not None:
+                            log ("Waiting for property %s (%i) to be set" %
+                                 (required_property, required_property_id))
+
+                if required_property is not None and matched_creation and not matched_required_property:
+                    if id == self.matched_resource_id and insert[2] == required_property_id:
+                        matched_required_property = True
+                        log ("Matched %s %s" % (self.matched_resource_urn, required_property))
+
+                if not matched_creation or id != self.matched_resource_id:
                     remaining_events += [insert]
 
+            matched = matched_creation if required_property is None else matched_required_property
             return matched, remaining_events
 
         def match_cb (inserts_list):
@@ -354,11 +372,6 @@ class StoreHelper (Helper):
             exit_loop = matched
             return exit_loop, remaining_events
 
-        self.matched_resource_urn = None
-        self.matched_resource_id = None
-
-        log ("Await new %s (%i existing inserts)" % (rdf_class, len (self.inserts_list)))
-
         # Check the list of previously received events for matches
         (existing_match, self.inserts_list) = find_resource_insertion (self.inserts_list)
 
@@ -499,6 +512,20 @@ class StoreHelper (Helper):
         else:
             return -1
 
+    def get_resource_id_by_uri(self, uri):
+        """
+        Get the internal ID for a given resource, identified by URI.
+        """
+        result = self.query(
+            'SELECT tracker:id(%s) WHERE { }' % uri)
+        if len(result) == 1:
+            return int (result [0][0])
+        elif len(result) == 0:
+            raise Exception ("No entry for resource %s" % uri)
+        else:
+            raise Exception ("Multiple entries for resource %s" % uri)
+
+    # FIXME: rename to get_resource_id_by_nepomuk_url !!
     def get_resource_id(self, url):
         """
         Get the internal ID for a given resource, identified by URL.
diff --git a/tests/functional-tests/common/utils/writebacktest.py 
b/tests/functional-tests/common/utils/writebacktest.py
index db756e0..834e79b 100644
--- a/tests/functional-tests/common/utils/writebacktest.py
+++ b/tests/functional-tests/common/utils/writebacktest.py
@@ -88,6 +88,18 @@ class CommonTrackerWritebackTest (ut.TestCase):
         self.system = TrackerSystemAbstraction ()
 
         self.system.tracker_writeback_testing_start (CONF_OPTIONS)
+
+        def await_resource_extraction(url):
+            # Make sure a resource has been crawled by the FS miner and by
+            # tracker-extract. The extractor adds nie:contentCreated for
+            # image resources, so know once this property is set the
+            # extraction is complete.
+            self.system.store.await_resource_inserted('nfo:Image', url=url, 
required_property='nie:contentCreated')
+
+        await_resource_extraction (self.get_test_filename_jpeg())
+        await_resource_extraction (self.get_test_filename_tiff())
+        await_resource_extraction (self.get_test_filename_png())
+
         # Returns when ready
         log ("Ready to go!")
         
@@ -97,13 +109,16 @@ class CommonTrackerWritebackTest (ut.TestCase):
         self.system.tracker_writeback_testing_stop ()
     
 
-    def get_test_filename_jpeg (self):
+    @staticmethod
+    def get_test_filename_jpeg ():
         return uri (TEST_FILE_JPEG)
 
-    def get_test_filename_tiff (self):
+    @staticmethod
+    def get_test_filename_tiff ():
         return uri (TEST_FILE_TIFF)
 
-    def get_test_filename_png (self):
+    @staticmethod
+    def get_test_filename_png ():
         return uri (TEST_FILE_PNG)
 
     def get_mtime (self, filename):


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]