[tracker/sam/functional-test-fixes: 9/28] functional-tests: Allow waiting for a certain property to be set



commit 175f73cf256d3b7a44af120939678c6b1752ddd4
Author: Sam Thursfield <sam afuera me uk>
Date:   Mon Jul 21 23:07:32 2014 +0100

    functional-tests: Allow waiting for a certain property to be set
    
    Just waiting for resource creation is not enough. Sometimes the test requires
    that tracker-extract has processed the resource too. We can wait for that by
    watching for the insertion of a triple that we know is set by tracker-extract.
    
    This is necessary for the writeback tests to work.

 tests/functional-tests/common/utils/helpers.py     |   59 ++++++++++++++-----
 .../functional-tests/common/utils/writebacktest.py |   14 ++++-
 2 files changed, 54 insertions(+), 19 deletions(-)
---
diff --git a/tests/functional-tests/common/utils/helpers.py b/tests/functional-tests/common/utils/helpers.py
index 91f4de3..e878cbd 100644
--- a/tests/functional-tests/common/utils/helpers.py
+++ b/tests/functional-tests/common/utils/helpers.py
@@ -318,18 +318,26 @@ class StoreHelper (Helper):
                                                                   self._graph_updated_timeout_cb)
         self._name_source (self.graph_updated_timeout_id, 'await-change-timeout')
 
-    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
@@ -337,7 +345,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
@@ -349,18 +357,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):
@@ -368,11 +386,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)
 
@@ -513,6 +526,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 df02252..834e79b 100644
--- a/tests/functional-tests/common/utils/writebacktest.py
+++ b/tests/functional-tests/common/utils/writebacktest.py
@@ -89,9 +89,17 @@ class CommonTrackerWritebackTest (ut.TestCase):
 
         self.system.tracker_writeback_testing_start (CONF_OPTIONS)
 
-        self.system.store.await_resource_inserted('nfo:Image', self.get_test_filename_jpeg())
-        self.system.store.await_resource_inserted('nfo:Image', self.get_test_filename_tiff())
-        self.system.store.await_resource_inserted('nfo:Image', self.get_test_filename_png())
+        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!")
         


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