]> err.no Git - dak/commitdiff
Test the DBSource and Suite relation.
authorTorsten Werner <twerner@debian.org>
Fri, 21 Jan 2011 23:05:36 +0000 (00:05 +0100)
committerTorsten Werner <twerner@debian.org>
Fri, 21 Jan 2011 23:46:57 +0000 (00:46 +0100)
Signed-off-by: Torsten Werner <twerner@debian.org>
daklib/dbconn.py
tests/dbtest_packages.py

index 6d8d3bd69b230a1a61ceec456cdfbb18a6311d31..513abb5805f1ba5f67eee0d8effee131e05a318c 100755 (executable)
@@ -2207,10 +2207,11 @@ def get_sources_from_name(source, version=None, dm_upload_allowed=None, session=
 
 __all__.append('get_sources_from_name')
 
+# FIXME: This function fails badly if it finds more than 1 source package.
 @session_wrapper
 def get_source_in_suite(source, suite, session=None):
     """
-    Returns list of DBSource objects for a combination of C{source} and C{suite}.
+    Returns a DBSource object for a combination of C{source} and C{suite}.
 
       - B{source} - source package name, eg. I{mailfilter}, I{bbdb}, I{glibc}
       - B{suite} - a suite name, eg. I{unstable}
@@ -2226,12 +2227,11 @@ def get_source_in_suite(source, suite, session=None):
 
     """
 
-    q = session.query(SrcAssociation)
-    q = q.join('source').filter_by(source=source)
-    q = q.join('suite').filter_by(suite_name=suite)
+    q = session.query(DBSource).filter_by(source = source). \
+        filter(DBSource.suites.any(Suite.suite_name == suite))
 
     try:
-        return q.one().source
+        return q.one()
     except NoResultFound:
         return None
 
index 8764bc78a4139152d790baf867cf1835711f9107..deb055d7c87ef9985414f54eb5df4125510e5ecc 100755 (executable)
@@ -4,8 +4,9 @@ from db_test import DBDakTestCase
 
 from daklib.dbconn import Architecture, Suite, get_suite_architectures, \
     get_architecture_suites, Maintainer, DBSource, Location, PoolFile, \
-    check_poolfile, get_poolfile_like_name
+    check_poolfile, get_poolfile_like_name, get_source_in_suite
 
+from sqlalchemy.orm.exc import MultipleResultsFound
 import unittest
 
 class PackageTestCase(DBDakTestCase):
@@ -100,8 +101,13 @@ class PackageTestCase(DBDakTestCase):
         self.file = {}
         self.file['hello'] = PoolFile(filename = 'main/h/hello/hello_2.2-2.dsc', \
             location = self.loc['main'], filesize = 0, md5sum = '')
+        self.file['hello_old'] = PoolFile(filename = 'main/h/hello/hello_2.2-1.dsc', \
+            location = self.loc['main'], filesize = 0, md5sum = '')
         self.file['sl'] = PoolFile(filename = 'main/s/sl/sl_3.03-16.dsc', \
             location = self.loc['main'], filesize = 0, md5sum = '')
+        self.file['python'] = PoolFile( \
+            filename = 'main/p/python2.6/python2.6_2.6.6-8.dsc', \
+            location = self.loc['main'], filesize = 0, md5sum = '')
         self.session.add_all(self.file.values())
 
     def test_poolfiles(self):
@@ -120,9 +126,10 @@ class PackageTestCase(DBDakTestCase):
         self.setup_poolfiles()
         location = self.session.query(Location)[0]
         self.assertEqual('/srv/ftp-master.debian.org/ftp/pool/', location.path)
-        self.assertEqual(2, location.files.count())
+        self.assertEqual(4, location.files.count())
         poolfile = location.files. \
-                filter(PoolFile.filename.like('%/hello/hello%')).one()
+                filter(PoolFile.filename.like('%/hello/hello%')). \
+                order_by(PoolFile.filename)[1]
         self.assertEqual('main/h/hello/hello_2.2-2.dsc', poolfile.filename)
         self.assertEqual(location, poolfile.location)
         # test get()
@@ -134,13 +141,13 @@ class PackageTestCase(DBDakTestCase):
         # TODO: deletion should cascade automatically
         self.session.delete(self.file['sl'])
         self.session.refresh(location)
-        self.assertEqual(1, location.files.count())
+        self.assertEqual(3, location.files.count())
         # please note that we intentionally do not specify 'location' here
         self.file['sl'] = PoolFile(filename = 'main/s/sl/sl_3.03-16.dsc', \
             filesize = 0, md5sum = '')
         location.files.append(self.file['sl'])
         self.session.refresh(location)
-        self.assertEqual(2, location.files.count())
+        self.assertEqual(4, location.files.count())
         # test fullpath
         self.assertEqual('/srv/ftp-master.debian.org/ftp/pool/main/s/sl/sl_3.03-16.dsc', \
             self.file['sl'].fullpath)
@@ -175,10 +182,24 @@ class PackageTestCase(DBDakTestCase):
 
         self.setup_maintainers()
         self.setup_poolfiles()
-        self.source = DBSource(source = 'hello', version = '2.2-2', \
+        self.setup_suites()
+        self.source = {}
+        self.source['hello'] = DBSource(source = 'hello', version = '2.2-2', \
             maintainer = self.maintainer['maintainer'], \
             changedby = self.maintainer['uploader'], \
             poolfile = self.file['hello'], install_date = self.now())
+        self.source['hello'].suites.append(self.suite['sid'])
+        self.source['hello_old'] = DBSource(source = 'hello', version = '2.2-1', \
+            maintainer = self.maintainer['maintainer'], \
+            changedby = self.maintainer['uploader'], \
+            poolfile = self.file['hello_old'], install_date = self.now())
+        self.source['hello_old'].suites.append(self.suite['sid'])
+        self.source['sl'] = DBSource(source = 'sl', version = '3.03-16', \
+            maintainer = self.maintainer['maintainer'], \
+            changedby = self.maintainer['uploader'], \
+            poolfile = self.file['sl'], install_date = self.now())
+        self.source['sl'].suites.append(self.suite['squeeze'])
+        self.source['sl'].suites.append(self.suite['sid'])
 
     def test_maintainers(self):
         '''
@@ -198,20 +219,51 @@ class PackageTestCase(DBDakTestCase):
         lazyguy = self.maintainer['lazyguy']
         self.assertEqual(lazyguy,
             self.session.query(Maintainer).get(lazyguy.maintainer_id))
-        self.assertEqual(maintainer.maintains_sources, [self.source])
+        self.assertEqual(3, len(maintainer.maintains_sources))
+        self.assertTrue(self.source['hello'] in maintainer.maintains_sources)
         self.assertEqual(maintainer.changed_sources, [])
         self.assertEqual(uploader.maintains_sources, [])
-        self.assertEqual(uploader.changed_sources, [self.source])
+        self.assertEqual(3, len(uploader.changed_sources))
+        self.assertTrue(self.source['sl'] in uploader.changed_sources)
         self.assertEqual(lazyguy.maintains_sources, [])
         self.assertEqual(lazyguy.changed_sources, [])
 
+    def get_source_in_suite_fail(self):
+        '''
+        This function throws the MultipleResultsFound exception because
+        get_source_in_suite is broken.
+
+        TODO: fix get_source_in_suite
+        '''
+
+        return get_source_in_suite('hello', 'sid', self.session)
+
     def test_sources(self):
-        'test relation between DBSource and PoolFile'
+        'test relation between DBSource and PoolFile or Suite'
 
         self.setup_sources()
-        self.assertEqual(self.file['hello'], self.source.poolfile)
-        self.assertEqual(self.source, self.file['hello'].source)
-        self.assertEqual(None, self.file['sl'].source)
+        # test PoolFile
+        self.assertEqual(self.file['hello'], self.source['hello'].poolfile)
+        self.assertEqual(self.source['hello'], self.file['hello'].source)
+        self.assertEqual(None, self.file['python'].source)
+        # test Suite
+        squeeze = self.session.query(Suite). \
+            filter(Suite.sources.contains(self.source['sl'])). \
+            order_by(Suite.suite_name)[1]
+        self.assertEqual(self.suite['squeeze'], squeeze)
+        self.assertEqual(1, len(squeeze.sources))
+        self.assertEqual(self.source['sl'], squeeze.sources[0])
+        sl = self.session.query(DBSource). \
+            filter(DBSource.suites.contains(self.suite['squeeze'])).one()
+        self.assertEqual(self.source['sl'], sl)
+        self.assertEqual(2, len(sl.suites))
+        self.assertTrue(self.suite['sid'] in sl.suites)
+        # test get_source_in_suite()
+        self.assertRaises(MultipleResultsFound, self.get_source_in_suite_fail)
+        self.assertEqual(None, \
+            get_source_in_suite('hello', 'squeeze', self.session))
+        self.assertEqual(self.source['sl'], \
+            get_source_in_suite('sl', 'sid', self.session))
 
 
 if __name__ == '__main__':