From bd467abef7c28467728d650468fba7499a281f18 Mon Sep 17 00:00:00 2001 From: Torsten Werner Date: Sat, 22 Jan 2011 00:05:36 +0100 Subject: [PATCH] Test the DBSource and Suite relation. Signed-off-by: Torsten Werner --- daklib/dbconn.py | 10 +++--- tests/dbtest_packages.py | 76 +++++++++++++++++++++++++++++++++------- 2 files changed, 69 insertions(+), 17 deletions(-) diff --git a/daklib/dbconn.py b/daklib/dbconn.py index 6d8d3bd6..513abb58 100755 --- a/daklib/dbconn.py +++ b/daklib/dbconn.py @@ -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 diff --git a/tests/dbtest_packages.py b/tests/dbtest_packages.py index 8764bc78..deb055d7 100755 --- a/tests/dbtest_packages.py +++ b/tests/dbtest_packages.py @@ -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__': -- 2.39.5