[tracker] Functional tests for distance function



commit df98c3f329a868545530eb4454810cc319a8d115
Author: Ivan Frade <ivan frade nokia com>
Date:   Fri Feb 5 16:57:06 2010 +0200

    Functional tests for distance function

 tests/functional-tests/06-distance.py |  136 +++++++++++++++++++++++++++++++++
 tests/functional-tests/tests.xml      |    8 ++-
 2 files changed, 143 insertions(+), 1 deletions(-)
---
diff --git a/tests/functional-tests/06-distance.py b/tests/functional-tests/06-distance.py
new file mode 100644
index 0000000..24e67c6
--- /dev/null
+++ b/tests/functional-tests/06-distance.py
@@ -0,0 +1,136 @@
+#!/usr/bin/env python2.5
+
+# Copyright (C) 2008, Nokia (urho konttori nokia com)
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library 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
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the
+# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+# Boston, MA  02110-1301, USA.
+#
+
+
+import dbus
+import unittest
+import random
+
+TRACKER = 'org.freedesktop.Tracker1'
+TRACKER_OBJ = '/org/freedesktop/Tracker1/Resources'
+RESOURCES_IFACE = "org.freedesktop.Tracker1.Resources"
+
+POINT_COORDS = [
+    (0, 0), (1, 1), (2, 2), (3, 3), (4, 4)
+    ]
+
+class TestCoalesce (unittest.TestCase):
+
+    def setUp (self):
+        bus = dbus.SessionBus ()
+        tracker = bus.get_object (TRACKER, TRACKER_OBJ)
+        self.resources = dbus.Interface (tracker,
+                                         dbus_interface=RESOURCES_IFACE);
+
+        self.counter = 0
+        for lat, log in POINT_COORDS:
+            insert = """
+            INSERT {
+            <%s> a mlo:GeoPoint ;
+                mlo:longitude %d ;
+                mlo:latitude %d .                
+            }
+            """ % ("point://test/point/" + str(self.counter), log, lat)
+            self.resources.SparqlUpdate (insert)
+            self.counter += 1
+
+    def tearDown (self):
+        for i in range (0, self.counter):
+            delete = """
+            DELETE {
+            <%s> a rdfs:Resource.
+            }
+            """ % ("point://test/point/" + str (i))
+            self.resources.SparqlUpdate (delete)
+
+
+    def get_distance_between_points (self, sum_func, id1, id2):
+
+        assert 0 <= id1 <= len (POINT_COORDS)
+        assert 0 <= id2 <= len (POINT_COORDS)
+        assert sum_func == "cartesian" or sum_func == "haversine"
+       
+        query_1_to_2 = """
+        SELECT xsd:integer(tracker:%s-distance(?lat1,?lat2,?lon1,?lon2))
+        WHERE {
+          <point://test/point/%d> a mlo:GeoPoint ;
+             mlo:latitude ?lat1 ;
+             mlo:longitude ?lon1 .
+
+          <point://test/point/%d> a mlo:GeoPoint ;
+             mlo:latitude ?lat2 ;
+             mlo:longitude ?lon2 .
+        }
+        """ % (sum_func, id1, id2)
+        result = self.resources.SparqlQuery (query_1_to_2)
+        return int (result[0][0])
+        
+        
+    def test_distance_cartesian_symmetry (self):
+        """
+        setUp: Insert 5 points in the pre-defined coordinates
+        1. TEST: Check cartesian distance from point A to B, and from B to A
+                 (should be the same :P)
+        tearDown: Remove the test points inserted before
+        """
+        a_to_b = self.get_distance_between_points ("cartesian", 1, 2)
+        assert a_to_b == 204601
+
+        b_to_a = self.get_distance_between_points ("cartesian", 2, 1)
+        assert b_to_a == 204601
+
+        assert a_to_b == b_to_a 
+
+    def test_distance_haversine_symmetry (self):
+        """
+        setUp: Insert 5 points in the pre-defined coordinates
+        1. TEST: Check cartesian distance from point A to B, and from B to A
+                 (should be the same :P)
+        tearDown: Remove the test points inserted before
+        """
+        a_to_b = self.get_distance_between_points ("haversine", 1, 2)
+        assert a_to_b == 157225
+        
+        b_to_a = self.get_distance_between_points ("haversine", 2, 1)
+        assert b_to_a == 157225
+
+        assert a_to_b == b_to_a
+
+
+    def test_distance_cartesian_proportion (self):
+        d_1_to_2 = self.get_distance_between_points ("cartesian", 1, 2)
+        d_2_to_3 = self.get_distance_between_points ("cartesian", 2, 3)
+        d_3_to_4 = self.get_distance_between_points ("cartesian", 3, 4)
+        assert d_1_to_2 > d_2_to_3 > d_3_to_4
+
+    def test_distance_haversine_proportion (self):
+        d_1_to_2 = self.get_distance_between_points ("haversine", 1, 2)
+        d_2_to_3 = self.get_distance_between_points ("haversine", 2, 3)
+        d_3_to_4 = self.get_distance_between_points ("haversine", 3, 4)
+        assert d_1_to_2 > d_2_to_3 > d_3_to_4
+
+    def test_distance_different (self):
+        d_2_to_3h = self.get_distance_between_points ("haversine", 2, 3)
+        d_2_to_3c = self.get_distance_between_points ("cartesian", 2, 3)
+        assert d_2_to_3h < d_2_to_3c
+        
+
+if __name__ == '__main__':
+    unittest.main()
diff --git a/tests/functional-tests/tests.xml b/tests/functional-tests/tests.xml
index 6131b01..6cea3bf 100644
--- a/tests/functional-tests/tests.xml
+++ b/tests/functional-tests/tests.xml
@@ -34,11 +34,17 @@
       </case>
 
       <case name="test_coalesce" type="Functional"  level="Feature"
-            description="Group concat function in sparql" requirement="01" subfeature="">
+            description="coalesce function in sparql" requirement="01" subfeature="">
         <!-- dbus env variables defined here. This works. -->
         <step expected_result="0">. /tmp/session_bus_address.user; python2.5 /usr/share/tracker-tests/05-coalesce.py</step>
       </case>
 
+      <case name="test_distance" type="Functional"  level="Feature"
+            description="Distance between location points in sparql" requirement="01" subfeature="">
+        <!-- dbus env variables defined here. This works. -->
+        <step expected_result="0">. /tmp/session_bus_address.user; python2.5 /usr/share/tracker-tests/06-distance.py</step>
+      </case>
+
       <!-- Environments optional - tells where the tests are run -->
       <environments>
         <scratchbox>true</scratchbox>



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