]> git.decadent.org.uk Git - dak.git/blobdiff - tests/dbtest_packages.py
Refactor PackageTestCase.
[dak.git] / tests / dbtest_packages.py
index 97cc8b10cacd497c9968b4dff70a15c70c8b9a3e..e97253213c38ad8ea4366967a0f1b74329e72659 100755 (executable)
@@ -3,7 +3,8 @@
 from db_test import DBDakTestCase
 
 from daklib.dbconn import Architecture, Suite, get_suite_architectures, \
-    get_architecture_suites
+    get_architecture_suites, Maintainer, DBSource, Location, PoolFile, \
+    check_poolfile, get_poolfile_like_name
 
 import unittest
 
@@ -22,21 +23,15 @@ class PackageTestCase(DBDakTestCase):
         # hard code ids for source and all
         self.arch['source'].arch_id = 1
         self.arch['all'].arch_id = 2
-        for _, architecture in self.arch.items():
-            self.session.add(architecture)
-            self.session.flush()
-            self.session.refresh(architecture)
+        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'):
-            suite = Suite(suite_name = suite_name, version = '-')
-            self.suite[suite_name] = suite
-            self.session.add(suite)
-            self.session.flush()
-            self.session.refresh(suite)
+            self.suite[suite_name] = Suite(suite_name = suite_name, version = '-')
+        self.session.add_all(self.suite.values())
 
     def setUp(self):
         super(PackageTestCase, self).setUp()
@@ -90,5 +85,137 @@ class PackageTestCase(DBDakTestCase):
         self.assertEqual(2, len(suites))
         self.assertTrue(self.suite['lenny'] not in suites)
 
+    def setup_locations(self):
+        'create some Location objects, TODO: add component'
+
+        self.loc = {}
+        self.loc['main'] = Location(path = \
+            '/srv/ftp-master.debian.org/ftp/pool/')
+        self.session.add(self.loc['main'])
+
+    def setup_poolfiles(self):
+        'create some PoolFile objects'
+
+        self.setup_locations()
+        self.file = {}
+        self.file['hello'] = PoolFile(filename = 'main/h/hello/hello_2.2-2.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.session.add_all(self.file.values())
+
+    def test_poolfiles(self):
+        '''
+        Test the relation of the classes PoolFile and Location.
+
+        The code needs some explaination. The property Location.files is not a
+        list as in other relations because such a list would become rather
+        huge. It is a query object that can be queried, filtered, and iterated
+        as usual.  But list like methods like append() and remove() are
+        supported as well which allows code like:
+
+        somelocation.files.append(somefile)
+        '''
+
+        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())
+        poolfile = location.files. \
+                filter(PoolFile.filename.like('%/hello/hello%')).one()
+        self.assertEqual('main/h/hello/hello_2.2-2.dsc', poolfile.filename)
+        self.assertEqual(location, poolfile.location)
+        # test get()
+        self.assertEqual(poolfile, \
+                self.session.query(PoolFile).get(poolfile.file_id))
+        self.assertEqual(None, self.session.query(PoolFile).get(-1))
+        # test remove() and append()
+        location.files.remove(self.file['sl'])
+        # TODO: deletion should cascade automatically
+        self.session.delete(self.file['sl'])
+        self.session.refresh(location)
+        self.assertEqual(1, 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())
+        # test fullpath
+        self.assertEqual('/srv/ftp-master.debian.org/ftp/pool/main/s/sl/sl_3.03-16.dsc', \
+            self.file['sl'].fullpath)
+        # test check_poolfile()
+        self.assertEqual((True, self.file['sl']), \
+            check_poolfile('main/s/sl/sl_3.03-16.dsc', 0, '', \
+                location.location_id, self.session))
+        self.assertEqual((False, None), \
+            check_poolfile('foobar', 0, '', location.location_id, self.session))
+        self.assertEqual((False, self.file['sl']), \
+            check_poolfile('main/s/sl/sl_3.03-16.dsc', 42, '', \
+                location.location_id, self.session))
+        self.assertEqual((False, self.file['sl']), \
+            check_poolfile('main/s/sl/sl_3.03-16.dsc', 0, 'deadbeef', \
+                location.location_id, self.session))
+        # test get_poolfile_like_name()
+        self.assertEqual([self.file['sl']], \
+            get_poolfile_like_name('sl_3.03-16.dsc', self.session))
+        self.assertEqual([], get_poolfile_like_name('foobar', self.session))
+
+    def setup_maintainers(self):
+        'create some Maintainer objects'
+
+        self.maintainer = {}
+        self.maintainer['maintainer'] = Maintainer(name = 'Mr. Maintainer')
+        self.maintainer['uploader'] = Maintainer(name = 'Mrs. Uploader')
+        self.maintainer['lazyguy'] = Maintainer(name = 'Lazy Guy')
+        self.session.add_all(self.maintainer.values())
+
+    def setup_sources(self):
+        'create a DBSource object; but it cannot be stored in the DB yet'
+
+        self.setup_maintainers()
+        self.setup_poolfiles()
+        self.source = DBSource(source = 'hello', version = '2.2-2', \
+            maintainer = self.maintainer['maintainer'], \
+            changedby = self.maintainer['uploader'], \
+            poolfile = self.file['hello'], install_date = self.now())
+
+    def test_maintainers(self):
+        '''
+        tests relation between Maintainer and DBSource
+
+        TODO: add relations to changes_pending_source
+        '''
+
+        self.setup_sources()
+        self.session.flush()
+        maintainer = self.maintainer['maintainer']
+        self.assertEqual(maintainer,
+            self.session.query(Maintainer).get(maintainer.maintainer_id))
+        uploader = self.maintainer['uploader']
+        self.assertEqual(uploader,
+            self.session.query(Maintainer).get(uploader.maintainer_id))
+        lazyguy = self.maintainer['lazyguy']
+        self.assertEqual(lazyguy,
+            self.session.query(Maintainer).get(lazyguy.maintainer_id))
+        self.assertEqual(maintainer.maintains_sources, [self.source])
+        self.assertEqual(maintainer.changed_sources, [])
+        self.assertEqual(uploader.maintains_sources, [])
+        self.assertEqual(uploader.changed_sources, [self.source])
+        self.assertEqual(lazyguy.maintains_sources, [])
+        self.assertEqual(lazyguy.changed_sources, [])
+
+    def test_sources(self):
+        'test relation between DBSource and PoolFile'
+
+        self.setup_sources()
+        poolfile_hello = self.session.query(DBSource)[0].poolfile
+        self.assertEqual(self.file['hello'], poolfile_hello)
+        self.assertEqual(self.source, poolfile_hello.source)
+        poolfile_sl = self.session.query(PoolFile). \
+            filter(PoolFile.filename.like('%/sl/%'))[0]
+        self.assertEqual(None, poolfile_sl.source)
+
+
 if __name__ == '__main__':
     unittest.main()