From 24d093a6e4edc0ce84ceaa952f3169e71d867124 Mon Sep 17 00:00:00 2001 From: Torsten Werner Date: Sat, 22 Jan 2011 11:34:05 +0100 Subject: [PATCH] Refactor get_source_in_suite(). Signed-off-by: Torsten Werner --- daklib/dbconn.py | 27 +++++++++++++++++---- tests/dbtest_packages.py | 52 +++++++++++++++++++++------------------- 2 files changed, 50 insertions(+), 29 deletions(-) diff --git a/daklib/dbconn.py b/daklib/dbconn.py index 513abb58..06960bfd 100755 --- a/daklib/dbconn.py +++ b/daklib/dbconn.py @@ -2207,7 +2207,8 @@ 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. +# FIXME: This function fails badly if it finds more than 1 source package and +# its implementation is trivial enough to be inlined. @session_wrapper def get_source_in_suite(source, suite, session=None): """ @@ -2227,9 +2228,7 @@ def get_source_in_suite(source, suite, session=None): """ - q = session.query(DBSource).filter_by(source = source). \ - filter(DBSource.suites.any(Suite.suite_name == suite)) - + q = get_suite(suite, session).get_sources(source) try: return q.one() except NoResultFound: @@ -2480,6 +2479,8 @@ SUITE_FIELDS = [ ('SuiteName', 'suite_name'), ('CopyChanges', 'copychanges'), ('OverrideSuite', 'overridesuite')] +# Why the heck don't we have any UNIQUE constraints in table suite? +# TODO: Add UNIQUE constraints for appropriate columns. class Suite(object): def __init__(self, suite_name = None, version = None): self.suite_name = suite_name @@ -2533,6 +2534,24 @@ class Suite(object): q = q.filter(Architecture.arch_string != 'all') return q.order_by(Architecture.arch_string).all() + def get_sources(self, source): + """ + Returns a query object representing DBSource that is part of C{suite}. + + - B{source} - source package name, eg. I{mailfilter}, I{bbdb}, I{glibc} + + @type source: string + @param source: source package name + + @rtype: sqlalchemy.orm.query.Query + @return: a query of DBSource + + """ + + session = object_session(self) + return session.query(DBSource).filter_by(source = source). \ + filter(DBSource.suites.contains(self)) + __all__.append('Suite') @session_wrapper diff --git a/tests/dbtest_packages.py b/tests/dbtest_packages.py index deb055d7..9af916fe 100755 --- a/tests/dbtest_packages.py +++ b/tests/dbtest_packages.py @@ -15,52 +15,45 @@ class PackageTestCase(DBDakTestCase): database. """ + def setup_suites(self): + "setup a hash of Suite objects in self.suite" + + if 'suite' in self.__dict__: + return + self.suite = {} + for suite_name in ('lenny', 'squeeze', 'sid'): + self.suite[suite_name] = Suite(suite_name = suite_name, version = '-') + self.session.add_all(self.suite.values()) + def setup_architectures(self): - "setup a hash of Architecture objects in self.arch" + "setup Architecture objects in self.arch and connect to suites" + if 'arch' in self.__dict__: + return + self.setup_suites() self.arch = {} for arch_string in ('source', 'all', 'i386', 'amd64', 'kfreebsd-i386'): self.arch[arch_string] = Architecture(arch_string) + if arch_string != 'kfreebsd-i386': + self.arch[arch_string].suites = self.suite.values() + else: + self.arch[arch_string].suites = [self.suite['squeeze'], self.suite['sid']] # hard code ids for source and all self.arch['source'].arch_id = 1 self.arch['all'].arch_id = 2 self.session.add_all(self.arch.values()) - def setup_suites(self): - "setup a hash of Suite objects in self.suite" - - self.suite = {} - for suite_name in ('lenny', 'squeeze', 'sid'): - self.suite[suite_name] = Suite(suite_name = suite_name, version = '-') - self.session.add_all(self.suite.values()) - def setUp(self): super(PackageTestCase, self).setUp() self.setup_architectures() self.setup_suites() - def connect_suite_architectures(self): - """ - Gonnect all suites and all architectures except for kfreebsd-i386 which - should not be in lenny. - """ - - for arch_string, architecture in self.arch.items(): - if arch_string != 'kfreebsd-i386': - architecture.suites = self.suite.values() - else: - architecture.suites = [self.suite['squeeze'], self.suite['sid']] - def test_suite_architecture(self): # check the id for architectures source and all self.assertEqual(1, self.arch['source'].arch_id) self.assertEqual(2, self.arch['all'].arch_id) # check the many to many relation between Suite and Architecture - self.arch['source'].suites.append(self.suite['lenny']) self.assertEqual('source', self.suite['lenny'].architectures[0]) - self.arch['source'].suites = [] - self.assertEqual([], self.suite['lenny'].architectures) - self.connect_suite_architectures() self.assertEqual(4, len(self.suite['lenny'].architectures)) self.assertEqual(3, len(self.arch['i386'].suites)) # check the function get_suite_architectures() @@ -89,6 +82,8 @@ class PackageTestCase(DBDakTestCase): def setup_locations(self): 'create some Location objects, TODO: add component' + if 'loc' in self.__dict__: + return self.loc = {} self.loc['main'] = Location(path = \ '/srv/ftp-master.debian.org/ftp/pool/') @@ -97,6 +92,8 @@ class PackageTestCase(DBDakTestCase): def setup_poolfiles(self): 'create some PoolFile objects' + if 'file' in self.__dict__: + return self.setup_locations() self.file = {} self.file['hello'] = PoolFile(filename = 'main/h/hello/hello_2.2-2.dsc', \ @@ -171,6 +168,8 @@ class PackageTestCase(DBDakTestCase): def setup_maintainers(self): 'create some Maintainer objects' + if 'maintainer' in self.__dict__: + return self.maintainer = {} self.maintainer['maintainer'] = Maintainer(name = 'Mr. Maintainer') self.maintainer['uploader'] = Maintainer(name = 'Mrs. Uploader') @@ -180,6 +179,8 @@ class PackageTestCase(DBDakTestCase): def setup_sources(self): 'create a DBSource object; but it cannot be stored in the DB yet' + if 'source' in self.__dict__: + return self.setup_maintainers() self.setup_poolfiles() self.setup_suites() @@ -200,6 +201,7 @@ class PackageTestCase(DBDakTestCase): poolfile = self.file['sl'], install_date = self.now()) self.source['sl'].suites.append(self.suite['squeeze']) self.source['sl'].suites.append(self.suite['sid']) + self.session.add_all(self.source.values()) def test_maintainers(self): ''' -- 2.39.2