changeset 215:5e2983b05d4e

created first doctest tests for storage
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 08 Jun 2012 18:44:27 -0400
parents 9c7fc6899c0e
children 51acf43e421a
files python/storage.py python/tests/storage.txt
diffstat 2 files changed, 31 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/python/storage.py	Thu Jun 07 01:20:25 2012 -0400
+++ b/python/storage.py	Fri Jun 08 18:44:27 2012 -0400
@@ -56,8 +56,9 @@
 
     try:
         cursor.execute('SELECT * from prototypes order by prototype_id, trajectory_id_matched')
-    except sqlite3.OperationalError:
-        return matched_indexes
+    except sqlite3.OperationalError as err:
+        print('DB Error: {0}'.format(err))
+        return []
 
     for row in cursor:
         matched_indexes.append((row[0],row[1]))
@@ -65,18 +66,6 @@
     connection.close()
     return matched_indexes
 
-def testloadPrototypeMatchIndexesFromSqlite():
-    'TODO: write as doctest'
-    empty_list = loadPrototypeMatchIndexesFromSqlite("bidon")
-    if empty_list == []:
-        print "Empty list test Ok"
-    
-    matches=loadPrototypeMatchIndexesFromSqlite("/home/francois/Unison/École/12Été/CRSNG/TAAM-Experiments/resultats/prototypes-with-matches.sqlite")
-    if len(matches) == 66:
-        print "Matches test Ok"
-    return matches
-    
-
 def loadTrajectoriesFromSqlite(filename, trajectoryType, objectNumbers = -1):
     '''Loads nObjects or the indices in objectNumbers from the database 
     TODO: load velocities (replace table name 'positions' by 'velocities'
@@ -87,25 +76,29 @@
     connection = sqlite3.connect(filename) # add test if it open
     cursor = connection.cursor()
 
-    if trajectoryType == 'feature':
-        if type(objectNumbers) == int:
-            if objectNumbers == -1:
-                cursor.execute('SELECT * from positions order by trajectory_id, frame_number')
+    try:
+        if trajectoryType == 'feature':
+            if type(objectNumbers) == int:
+                if objectNumbers == -1:
+                    cursor.execute('SELECT * from positions order by trajectory_id, frame_number')
             else:
                 cursor.execute('SELECT * from positions where trajectory_id between 0 and {0} order by trajectory_id, frame_number'.format(objectNumbers))
         elif type(objectNumbers) == list:
             cursor.execute('SELECT * from positions where trajectory_id in ('+', '.join([str(n) for n in objectNumbers])+') order by trajectory_id, frame_number')
-    elif trajectoryType == 'object':
-        if type(objectNumbers) == int:
-            if objectNumbers == -1:
-                cursor.execute('SELECT OF.object_id, P.frame_number, avg(P.x_coordinate), avg(P.y_coordinate) from positions P, objects_features OF where P.trajectory_id = OF.trajectory_id group by object_id, frame_number')
+        elif trajectoryType == 'object':
+            if type(objectNumbers) == int:
+                if objectNumbers == -1:
+                    cursor.execute('SELECT OF.object_id, P.frame_number, avg(P.x_coordinate), avg(P.y_coordinate) from positions P, objects_features OF where P.trajectory_id = OF.trajectory_id group by object_id, frame_number')
             else:
                 cursor.execute('SELECT OF.object_id, P.frame_number, avg(P.x_coordinate), avg(P.y_coordinate) from positions P, objects_features OF where P.trajectory_id = OF.trajectory_id and trajectory_id between 0 and {0} group by object_id, frame_number'.format(objectNumbers))
         elif type(objectNumbers) == list:
             cursor.execute('SELECT OF.object_id, P.frame_number, avg(P.x_coordinate), avg(P.y_coordinate) from positions P, objects_features OF where P.trajectory_id = OF.trajectory_id and trajectory_id in ('+', '.join([str(n) for n in objectNumbers])+') group by object_id, frame_number')
-    else:
-        print('no trajectory type was chosen')
-
+        else:
+            print('no trajectory type was chosen')
+    except sqlite3.OperationalError as err:
+        print('DB Error: {0}'.format(err))
+        return []
+    
     objId = -1
     obj = None
     objects = []
@@ -209,13 +202,10 @@
         
     out.close()
 
-
-
-
-# if __name__ == "__main__":
-#     import doctest
-#     import unittest
-#     suite = doctest.DocFileSuite('tests/ubc_utils.txt')
-#     unittest.TextTestRunner().run(suite)
+if __name__ == "__main__":
+    import doctest
+    import unittest
+    suite = doctest.DocFileSuite('tests/storage.txt')
+    unittest.TextTestRunner().run(suite)
 #     #doctest.testmod()
 #     #doctest.testfile("example.txt")
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/tests/storage.txt	Fri Jun 08 18:44:27 2012 -0400
@@ -0,0 +1,8 @@
+>>> from storage import *
+
+>>> loadPrototypeMatchIndexesFromSqlite("nonexistent")
+DB Error: no such table: prototypes
+[]
+>>> loadTrajectoriesFromSqlite("nonexistent", 'feature')
+DB Error: no such table: positions
+[]