[tracker/functional-tests-applications] functional-tests: Refactor and clear a bit applications test



commit 893002523f14184eec1f915c09b5278f43f5aa94
Author: Ivan Frade <ivan frade nokia com>
Date:   Fri Jan 28 13:33:25 2011 +0200

    functional-tests: Refactor and clear a bit applications test

 tests/functional-tests/600-applications-camera.py  |  114 ++++++++------------
 .../common/utils/applicationstest.py               |   78 +++++++++-----
 2 files changed, 93 insertions(+), 99 deletions(-)
---
diff --git a/tests/functional-tests/600-applications-camera.py b/tests/functional-tests/600-applications-camera.py
index 2fef88f..d9678f0 100755
--- a/tests/functional-tests/600-applications-camera.py
+++ b/tests/functional-tests/600-applications-camera.py
@@ -33,28 +33,9 @@ import fcntl
 
 from common.utils import configuration as cfg
 import unittest2 as ut
-from common.utils.applicationstest import CommonTrackerApplicationTest as CommonTrackerApplicationTest, APPLICATIONS_TMP_DIR, path, uri, slowcopy, slowcopy_fd
+from common.utils.applicationstest import CommonTrackerApplicationTest as CommonTrackerApplicationTest
 
 MINER_FS_IDLE_TIMEOUT = 5
-# Copy rate, 10KBps (1024b/100ms)
-SLOWCOPY_RATE = 1024
-
-# Test image
-TEST_IMAGE = "test-image-1.jpg"
-SRC_IMAGE_DIR = os.path.join (cfg.DATADIR,
-                              "tracker-tests",
-                              "data",
-                              "Images")
-SRC_IMAGE_PATH = os.path.join (SRC_IMAGE_DIR, TEST_IMAGE)
-
-# Test video
-TEST_VIDEO = "test-video.mp4"
-SRC_VIDEO_DIR = os.path.join (cfg.DATADIR,
-                              "tracker-tests",
-                              "data",
-                              "Video")
-SRC_VIDEO_PATH = os.path.join (SRC_VIDEO_DIR, TEST_VIDEO)
-
 
 class TrackerCameraApplicationTests (CommonTrackerApplicationTest):
 
@@ -69,10 +50,9 @@ class TrackerCameraApplicationTests (CommonTrackerApplicationTest):
         """
 
         fileurn = "tracker://test_camera_picture_01/" + str(random.randint (0,100))
-        filepath = path (TEST_IMAGE)
-        fileuri = uri (TEST_IMAGE)
-
-        print "Storing new image in '%s'..." % (filepath)
+        origin_filepath = os.path.join (self.get_data_dir (), self.get_test_image ())
+        dest_filepath = os.path.join (self.get_dest_dir (), self.get_test_image ())
+        dest_fileuri = "file://" + dest_filepath
 
         # Insert new resource in the store, including nie:mimeType and nie:url
         insert = """
@@ -97,23 +77,21 @@ class TrackerCameraApplicationTests (CommonTrackerApplicationTest):
         INSERT { <%s> a       rdfs:Resource ;
                       nie:url \"%s\"
         }
-        """ % (fileurn, fileurn, fileurn, fileurn, fileurn, fileurn, fileurn, fileuri)
+        """ % (fileurn, fileurn, fileurn, fileurn, fileurn, fileurn, fileurn, dest_fileuri)
         self.tracker.update (insert)
-        self.assertEquals (self.get_urn_count_by_url (fileuri), 1)
+        self.assertEquals (self.get_urn_count_by_url (dest_fileuri), 1)
 
         # Copy the image to the dest path
-        slowcopy (SRC_IMAGE_PATH, filepath, SLOWCOPY_RATE)
-        assert os.path.exists (filepath)
-        time.sleep (3)
+        self.slowcopy_file (origin_filepath, dest_filepath)
+        assert os.path.exists (dest_filepath)
         self.system.tracker_miner_fs_wait_for_idle (MINER_FS_IDLE_TIMEOUT)
-        self.assertEquals (self.get_urn_count_by_url (fileuri), 1)
+        self.assertEquals (self.get_urn_count_by_url (dest_fileuri), 1)
 
         # Clean the new file so the test directory is as before
         print "Remove and wait"
-        os.remove (filepath)
-        time.sleep (3)
+        os.remove (dest_filepath)
         self.system.tracker_miner_fs_wait_for_idle (MINER_FS_IDLE_TIMEOUT)
-        self.assertEquals (self.get_urn_count_by_url (fileuri), 0)
+        self.assertEquals (self.get_urn_count_by_url (dest_fileuri), 0)
 
 
     def test_camera_picture_02_geolocation (self):
@@ -128,14 +106,12 @@ class TrackerCameraApplicationTests (CommonTrackerApplicationTest):
         """
 
         fileurn = "tracker://test_camera_picture_02/" + str(random.randint (0,100))
-        filepath = path (TEST_IMAGE)
-        fileuri = uri (TEST_IMAGE)
+        dest_filepath = os.path.join (self.get_dest_dir (), self.get_test_image ())
+        dest_fileuri = "file://" + dest_filepath
 
         geolocationurn = "tracker://test_camera_picture_02_geolocation/" + str(random.randint (0,100))
         postaladdressurn = "tracker://test_camera_picture_02_postaladdress/" + str(random.randint (0,100))
 
-        print "Storing new image in '%s'..." % (filepath)
-
         # Insert new resource in the store, including nie:mimeType and nie:url
         insert = """
         INSERT { <%s> a nie:InformationElement,
@@ -159,13 +135,13 @@ class TrackerCameraApplicationTests (CommonTrackerApplicationTest):
         INSERT { <%s> a       rdfs:Resource ;
                       nie:url \"%s\"
         }
-        """ % (fileurn, fileurn, fileurn, fileurn, fileurn, fileurn, fileurn, fileuri)
+        """ % (fileurn, fileurn, fileurn, fileurn, fileurn, fileurn, fileurn, dest_fileuri)
         self.tracker.update (insert)
-        self.assertEquals (self.get_urn_count_by_url (fileuri), 1)
+        self.assertEquals (self.get_urn_count_by_url (dest_fileuri), 1)
 
         # FIRST, open the file for writing, and just write some garbage, to simulate that
         # we already started recording the video...
-        fdest = open (filepath, 'wb')
+        fdest = open (dest_filepath, 'wb')
         # LOCK the file, as camera-ui seems to do it
         fcntl.flock(fdest, fcntl.LOCK_EX)
 
@@ -191,21 +167,20 @@ class TrackerCameraApplicationTests (CommonTrackerApplicationTest):
         self.tracker.update (location_insert)
 
         #THIRD, start copying the image to the dest path
-        slowcopy_fd (SRC_IMAGE_PATH, filepath, fdest, SLOWCOPY_RATE)
+        original_file = os.path.join (self.get_data_dir (),self.get_test_image ())
+        self.slowcopy_file_fd (original_file, fdest)
         fdest.close ()
-        assert os.path.exists (filepath)
+        assert os.path.exists (dest_filepath)
 
         # FOURTH, ensure we have only 1 resource
-        time.sleep (3)
         self.system.tracker_miner_fs_wait_for_idle (MINER_FS_IDLE_TIMEOUT)
-        self.assertEquals (self.get_urn_count_by_url (fileuri), 1)
+        self.assertEquals (self.get_urn_count_by_url (dest_fileuri), 1)
 
         # Clean the new file so the test directory is as before
         print "Remove and wait"
-        os.remove (filepath)
-        time.sleep (3)
+        os.remove (dest_filepath)
         self.system.tracker_miner_fs_wait_for_idle (MINER_FS_IDLE_TIMEOUT)
-        self.assertEquals (self.get_urn_count_by_url (fileuri), 0)
+        self.assertEquals (self.get_urn_count_by_url (dest_fileuri), 0)
 
 
     def test_camera_video_01 (self):
@@ -219,10 +194,9 @@ class TrackerCameraApplicationTests (CommonTrackerApplicationTest):
         """
 
         fileurn = "tracker://test_camera_video_01/" + str(random.randint (0,100))
-        filepath = path (TEST_VIDEO)
-        fileuri = uri (TEST_VIDEO)
-
-        print "Storing new video in '%s'..." % (filepath)
+        origin_filepath = os.path.join (self.get_data_dir (), self.get_test_video ())
+        dest_filepath = os.path.join (self.get_dest_dir (), self.get_test_video ())
+        dest_fileuri = "file://" + dest_filepath
 
         # Insert new resource in the store, including nie:mimeType and nie:url
         insert = """
@@ -247,22 +221,21 @@ class TrackerCameraApplicationTests (CommonTrackerApplicationTest):
         INSERT { <%s> a       rdfs:Resource ;
                       nie:url \"%s\"
         }
-        """ % (fileurn, fileurn, fileurn, fileurn, fileurn, fileurn, fileurn, fileuri)
+        """ % (fileurn, fileurn, fileurn, fileurn, fileurn, fileurn, fileurn, dest_fileuri)
         self.tracker.update (insert)
-        self.assertEquals (self.get_urn_count_by_url (fileuri), 1)
+        self.assertEquals (self.get_urn_count_by_url (dest_fileuri), 1)
 
         # Copy the image to the dest path
-        slowcopy (SRC_VIDEO_PATH, filepath, SLOWCOPY_RATE)
-        assert os.path.exists (filepath)
+        self.slowcopy_file (origin_filepath, dest_filepath)
+        assert os.path.exists (dest_filepath)
         self.system.tracker_miner_fs_wait_for_idle (MINER_FS_IDLE_TIMEOUT)
-        self.assertEquals (self.get_urn_count_by_url (fileuri), 1)
+        self.assertEquals (self.get_urn_count_by_url (dest_fileuri), 1)
 
         # Clean the new file so the test directory is as before
         print "Remove and wait"
-        os.remove (filepath)
-        time.sleep (3)
+        os.remove (dest_filepath)
         self.system.tracker_miner_fs_wait_for_idle (MINER_FS_IDLE_TIMEOUT)
-        self.assertEquals (self.get_urn_count_by_url (fileuri), 0)
+        self.assertEquals (self.get_urn_count_by_url (dest_fileuri), 0)
 
 
     def test_camera_video_02_geolocation (self):
@@ -277,14 +250,13 @@ class TrackerCameraApplicationTests (CommonTrackerApplicationTest):
         """
 
         fileurn = "tracker://test_camera_video_02/" + str(random.randint (0,100))
-        filepath = path (TEST_VIDEO)
-        fileuri = uri (TEST_VIDEO)
+        origin_filepath = os.path.join (self.get_data_dir (), self.get_test_video ())
+        dest_filepath = os.path.join (self.get_dest_dir (), self.get_test_video ())
+        dest_fileuri = "file://" + dest_filepath
 
         geolocationurn = "tracker://test_camera_video_02_geolocation/" + str(random.randint (0,100))
         postaladdressurn = "tracker://test_camera_video_02_postaladdress/" + str(random.randint (0,100))
 
-        print "Storing new video in '%s'..." % (filepath)
-
         # Insert new resource in the store, including nie:mimeType and nie:url
         insert = """
         INSERT { <%s> a nie:InformationElement,
@@ -308,13 +280,13 @@ class TrackerCameraApplicationTests (CommonTrackerApplicationTest):
         INSERT { <%s> a       rdfs:Resource ;
                       nie:url \"%s\"
         }
-        """ % (fileurn, fileurn, fileurn, fileurn, fileurn, fileurn, fileurn, fileuri)
+        """ % (fileurn, fileurn, fileurn, fileurn, fileurn, fileurn, fileurn, dest_fileuri)
         self.tracker.update (insert)
-        self.assertEquals (self.get_urn_count_by_url (fileuri), 1)
+        self.assertEquals (self.get_urn_count_by_url (dest_fileuri), 1)
 
         # FIRST, open the file for writing, and just write some garbage, to simulate that
         # we already started recording the video...
-        fdest = open (filepath, 'wb')
+        fdest = open (dest_filepath, 'wb')
         # LOCK the file, as camera-ui seems to do it
         fcntl.flock(fdest, fcntl.LOCK_EX)
 
@@ -340,21 +312,21 @@ class TrackerCameraApplicationTests (CommonTrackerApplicationTest):
         self.tracker.update (location_insert)
 
         #THIRD, start copying the image to the dest path
-        slowcopy_fd (SRC_VIDEO_PATH, filepath, fdest, SLOWCOPY_RATE)
+        self.slowcopy_file_fd (origin_filepath, fdest)
         fdest.close ()
-        assert os.path.exists (filepath)
+        assert os.path.exists (dest_filepath)
 
         # FOURTH, ensure we have only 1 resource
         time.sleep (3)
         self.system.tracker_miner_fs_wait_for_idle (MINER_FS_IDLE_TIMEOUT)
-        self.assertEquals (self.get_urn_count_by_url (fileuri), 1)
+        self.assertEquals (self.get_urn_count_by_url (dest_fileuri), 1)
 
         # Clean the new file so the test directory is as before
         print "Remove and wait"
-        os.remove (filepath)
+        os.remove (dest_filepath)
         time.sleep (3)
         self.system.tracker_miner_fs_wait_for_idle (MINER_FS_IDLE_TIMEOUT)
-        self.assertEquals (self.get_urn_count_by_url (fileuri), 0)
+        self.assertEquals (self.get_urn_count_by_url (dest_fileuri), 0)
 
 if __name__ == "__main__":
 	ut.main()
diff --git a/tests/functional-tests/common/utils/applicationstest.py b/tests/functional-tests/common/utils/applicationstest.py
index adebd4c..5040327 100644
--- a/tests/functional-tests/common/utils/applicationstest.py
+++ b/tests/functional-tests/common/utils/applicationstest.py
@@ -28,29 +28,8 @@ import time
 
 APPLICATIONS_TMP_DIR = os.path.join (cfg.TEST_TMP_DIR, "test-applications-monitored")
 
-def path (filename):
-    return os.path.join (APPLICATIONS_TMP_DIR, filename)
-
-def uri (filename):
-    return "file://" + os.path.join (APPLICATIONS_TMP_DIR, filename)
-
-# Being rate defined in amount of BYTES per 100ms
-def slowcopy_fd (src, dest, fdest, rate):
-    print "Copying from '%s' to '%s' at a rate of %u bytes/100ms" % (src, dest, rate)
-    fsrc = open (src, 'rb')
-    buffer = fsrc.read (rate)
-    while (buffer != ""):
-        fdest.write (buffer)
-        time.sleep (0.1)
-        buffer = fsrc.read (rate)
-
-    fsrc.close ()
-
-def slowcopy (src, dest, rate):
-    fdest = open (dest, 'wb')
-    slowcopy_fd (src, dest, fdest, rate)
-    fdest.close ()
-
+# Copy rate, 10KBps (1024b/100ms)
+SLOWCOPY_RATE = 1024
 
 class CommonTrackerApplicationTest (ut.TestCase):
 
@@ -60,6 +39,43 @@ class CommonTrackerApplicationTest (ut.TestCase):
         """ % (url)
         return len (self.tracker.query (select))
 
+
+    def get_test_image (self):
+        TEST_IMAGE = "test-image-1.jpg"
+        return TEST_IMAGE
+
+    def get_test_video (self):
+        TEST_VIDEO = "test-video.mp4"
+        return TEST_VIDEO
+
+    def get_data_dir (self):
+        return self.datadir
+
+    def get_dest_dir (self):
+        return APPLICATIONS_TMP_DIR
+
+    def slowcopy_file_fd (self, src, fdest, rate=SLOWCOPY_RATE):
+        """
+        @rate: bytes per 100ms
+        """
+        print "Copying slowly\n '%s' to\n '%s'" % (src, fdest.name)
+        fsrc = open (src, 'rb')
+        buffer_ = fsrc.read (rate)
+        while (buffer_ != ""):
+            fdest.write (buffer_)
+            time.sleep (0.1)
+            buffer_ = fsrc.read (rate)
+        fsrc.close ()
+        
+
+    def slowcopy_file (self, src, dst, rate=SLOWCOPY_RATE):
+        """
+        @rate: bytes per 100ms
+        """
+        fdest = open (dst, 'wb')
+        self.slowcopy_file_fd (src, fdest, rate)
+        fdest.close ()
+
     @classmethod
     def setUp (self):
         # Create temp directory to monitor
@@ -67,6 +83,17 @@ class CommonTrackerApplicationTest (ut.TestCase):
             shutil.rmtree (APPLICATIONS_TMP_DIR)
         os.makedirs (APPLICATIONS_TMP_DIR)
 
+        # Use local directory if available. Installation otherwise.
+        if os.path.exists (os.path.join (os.getcwd (),
+                                         "test-apps-data")):
+            self.datadir = os.path.join (os.getcwd (),
+                                         "test-apps-data")
+        else:
+            self.datadir = os.path.join (cfg.DATADIR,
+                                         "tracker-tests",
+                                         "test-apps-data")
+
+
         self.system = TrackerSystemAbstraction ()
 
         if (os.path.exists (os.path.join (os.getcwd(),
@@ -77,9 +104,6 @@ class CommonTrackerApplicationTest (ut.TestCase):
                                     "test-configurations",
                                     "applications")
         else:
-            confdir = os.path.join (os.getcwd(),
-                                    "test-configurations",
-                                    "applications")
             confdir = os.path.join (cfg.DATADIR,
                                     "tracker-tests",
                                     "test-configurations",
@@ -92,8 +116,6 @@ class CommonTrackerApplicationTest (ut.TestCase):
         self.tracker.wait ()
 
         print "Ready to go!"
-        print "    Using configuration dir at '%s'..." % (confdir)
-        print "    Using temp dir at '%s'..." % (APPLICATIONS_TMP_DIR)
 
     @classmethod
     def tearDown (self):



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