"Fossies" - the Fresh Open Source Software Archive

Member "buildbot-2.3.1/buildbot/test/unit/test_db_dbconfig.py" (23 May 2019, 4057 Bytes) of package /linux/misc/buildbot-2.3.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "test_db_dbconfig.py": 1.8.0_vs_2.0.0.

    1 # This file is part of Buildbot.  Buildbot is free software: you can
    2 # redistribute it and/or modify it under the terms of the GNU General Public
    3 # License as published by the Free Software Foundation, version 2.
    4 #
    5 # This program is distributed in the hope that it will be useful, but WITHOUT
    6 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    7 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    8 # details.
    9 #
   10 # You should have received a copy of the GNU General Public License along with
   11 # this program; if not, write to the Free Software Foundation, Inc., 51
   12 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   13 #
   14 # Copyright Buildbot Team Members
   15 
   16 from twisted.internet import defer
   17 from twisted.internet import threads
   18 from twisted.trial import unittest
   19 
   20 from buildbot.db import dbconfig
   21 from buildbot.test.util import db
   22 
   23 
   24 class TestDbConfig(db.RealDatabaseMixin, unittest.TestCase):
   25 
   26     @defer.inlineCallbacks
   27     def setUp(self):
   28         # as we will open the db twice, we can't use in memory sqlite
   29         yield self.setUpRealDatabase(table_names=['objects', 'object_state'], sqlite_memory=False)
   30         yield threads.deferToThread(self.createDbConfig)
   31 
   32     def createDbConfig(self):
   33         self.dbConfig = dbconfig.DbConfig(
   34             {"db_url": self.db_url}, self.basedir)
   35 
   36     def tearDown(self):
   37         return self.tearDownRealDatabase()
   38 
   39     def test_basic(self):
   40         def thd():
   41             workersInDB = ['foo', 'bar']
   42             self.dbConfig.set("workers", workersInDB)
   43             workers = self.dbConfig.get("workers")
   44             self.assertEqual(workers, workersInDB)
   45 
   46         return threads.deferToThread(thd)
   47 
   48     def test_default(self):
   49         def thd():
   50             workers = self.dbConfig.get("workers", "default")
   51             self.assertEqual(workers, "default")
   52 
   53         return threads.deferToThread(thd)
   54 
   55     def test_error(self):
   56         def thd():
   57             with self.assertRaises(KeyError):
   58                 self.dbConfig.get("workers")
   59 
   60         return threads.deferToThread(thd)
   61 
   62     # supports the 3 different ways to declare db_url in the master.cfg
   63     def test_init1(self):
   64         obj = dbconfig.DbConfig({"db_url": self.db_url}, self.basedir)
   65         self.assertEqual(obj.db_url, self.db_url)
   66 
   67     def test_init2(self):
   68         obj = dbconfig.DbConfig({"db": {"db_url": self.db_url}}, self.basedir)
   69         self.assertEqual(obj.db_url, self.db_url)
   70 
   71     def test_init3(self):
   72         obj = dbconfig.DbConfig({}, self.basedir)
   73         self.assertEqual(obj.db_url, "sqlite:///state.sqlite")
   74 
   75 
   76 class TestDbConfigNotInitialized(db.RealDatabaseMixin, unittest.TestCase):
   77 
   78     @defer.inlineCallbacks
   79     def setUp(self):
   80         # as we will open the db twice, we can't use in memory sqlite
   81         yield self.setUpRealDatabase(table_names=[], sqlite_memory=False)
   82 
   83     def createDbConfig(self, db_url=None):
   84         return dbconfig.DbConfig({"db_url": db_url or self.db_url}, self.basedir)
   85 
   86     def test_default(self):
   87         def thd():
   88             db = self.createDbConfig()
   89             self.assertEqual("foo", db.get("workers", "foo"))
   90 
   91         return threads.deferToThread(thd)
   92 
   93     def test_error(self):
   94         def thd():
   95             db = self.createDbConfig()
   96             with self.assertRaises(KeyError):
   97                 db.get("workers")
   98 
   99         return threads.deferToThread(thd)
  100 
  101     def test_bad_url(self):
  102         def thd():
  103             db = self.createDbConfig("garbage://")
  104             with self.assertRaises(KeyError):
  105                 db.get("workers")
  106 
  107         return threads.deferToThread(thd)
  108 
  109     def test_bad_url2(self):
  110         def thd():
  111             db = self.createDbConfig("trash")
  112             with self.assertRaises(KeyError):
  113                 db.get("workers")
  114 
  115         return threads.deferToThread(thd)
  116 
  117     def test_bad_url3(self):
  118         def thd():
  119             db = self.createDbConfig("sqlite://bad")
  120             with self.assertRaises(KeyError):
  121                 db.get("workers")
  122 
  123         return threads.deferToThread(thd)