def setUp(self): B3TestCase.setUp(self) logging.getLogger('output').setLevel(logging.DEBUG) with logging_disabled(): self.console.startup() self.conf = CfgConfigParser() self.p = ChatloggerPlugin(self.console, self.conf) ## prepare the mysql test database db = MySQLdb.connect(host=MYSQL_TEST_HOST, user=MYSQL_TEST_USER, passwd=MYSQL_TEST_PASSWORD) db.query("DROP DATABASE IF EXISTS %s" % MYSQL_TEST_DB) db.query("CREATE DATABASE %s CHARACTER SET utf8;" % MYSQL_TEST_DB) self.console.storage = getStorage( 'mysql://%s:%s@%s/%s' % (MYSQL_TEST_USER, MYSQL_TEST_PASSWORD, MYSQL_TEST_HOST, MYSQL_TEST_DB), self.console) self.console.storage.executeSql("@b3/sql/b3.sql") self.console.storage.executeSql(CHATLOGGER_SQL_FILE) when(self.console.config).get('b3', 'time_zone').thenReturn('GMT') self.p.setup_fileLogger = Mock() self.conf.loadFromString( dedent(""" [general] save_to_database: Yes save_to_file: no [file] logfile: @conf/chat.log rotation_rate: D [purge] max_age: 0 hour: 0 min: 0 """)) with logging_disabled(): self.p.onLoadConfig() self.p.onStartup() self.joe = FakeClient(self.console, name="Joe", guid="joe_guid", team=TEAM_RED) self.simon = FakeClient(self.console, name="Simon", guid="simon_guid", team=TEAM_BLUE) self.joe.connects(1) self.simon.connects(3) self.assertEqual(0, self.count_chatlog_lines()) self.assertEqual(0, self.count_cmdlog_lines())
def run(self): """ Run the DB update """ clearscreen() print(""" _\|/_ (o o) {:>32} +----oOO---OOo----------------------------------+ | | | UPDATING B3 DATABASE | | | +-----------------------------------------------+ """.format('B3 : %s' % b3.__version__)) input("press any key to start the update...") def _update_database(storage, update_version): """ Update a B3 database. :param storage: the initialized storage module :param update_version: the update version """ if B3version(b3.__version__) >= update_version: sql = b3.getAbsolutePath('@b3/sql/%s/b3-update-%s.sql' % (storage.protocol, update_version)) if os.path.isfile(sql): try: print('>>> updating database to version %s' % update_version) sleep(.5) storage.queryFromFile(sql) except Exception as err: print( 'WARNING: could not update database properly: %s' % err) sleep(3) dsn = self.config.get('b3', 'database') dsndict = splitDSN(dsn) database = getStorage(dsn, dsndict, StubParser()) _update_database(database, '1.3.0') _update_database(database, '1.6.0') _update_database(database, '1.7.0') _update_database(database, '1.8.1') _update_database(database, '1.9.0') _update_database(database, '1.10.0') console_exit('B3 database update completed!')
def test_sqlite(self): storage = getStorage('sqlite://:memory:', splitDSN('sqlite://:memory:'), Mock()) self.assertIsInstance(storage, SqliteStorage)
def test_postgresql(self): storage = getStorage('postgresql://*****:*****@localhost/b3', splitDSN('postgresql://*****:*****@localhost/b3'), Mock()) self.assertIsInstance(storage, PostgresqlStorage)
class DBUpdate(object): """ Console database update procedure. """ def __init__(self, config=None): """ Object constructor. :param config: The B3 configuration file path """ if config: # use the specified configuration file config = b3.getAbsolutePath(config, True) if not os.path.isfile(config): console_exit('ERROR: configuration file not found (%s).\n' 'Please visit %s to create one.' % (config, B3_CONFIG_GENERATOR)) else: # search a configuration file for p in ('b3.%s', 'conf/b3.%s', 'b3/conf/b3.%s', os.path.join(HOMEDIR, 'b3.%s'), os.path.join(HOMEDIR, 'conf', 'b3.%s'), os.path.join(HOMEDIR, 'b3', 'conf', 'b3.%s'), '@b3/conf/b3.%s'): for e in ('ini', 'cfg', 'xml'): path = b3.getAbsolutePath(p % e, True) if os.path.isfile(path): print "Using configuration file: %s" % path config = path sleep(3) break if not config: console_exit('ERROR: could not find any valid configuration file.\n' 'Please visit %s to create one.' % B3_CONFIG_GENERATOR) try: self.config = b3.config.MainConfig(b3.config.load(config)) if self.config.analyze(): raise b3.config.ConfigFileNotValid except b3.config.ConfigFileNotValid: console_exit('ERROR: configuration file not valid (%s).\n' 'Please visit %s to generate a new one.' % (config, B3_CONFIG_GENERATOR)) def run(self): """ Run the DB update """ clearscreen() print """ _\|/_ (o o) {:>32} +----oOO---OOo----------------------------------+ | | | UPDATING B3 DATABASE | | | +-----------------------------------------------+ """.format('B3 : %s' % b3.__version__) raw_input("press any key to start the update...") def _update_database(storage, update_version): """ Update a B3 database. :param storage: the initialized storage module :param update_version: the update version """ if B3version(b3.__version__) >= update_version: sql = b3.getAbsolutePath('@b3/sql/%s/b3-update-%s.sql' % (storage.protocol, update_version)) if os.path.isfile(sql): try: print '>>> updating database to version %s' % update_version sleep(.5) storage.queryFromFile(sql) except Exception, err: print 'WARNING: could not update database properly: %s' % err sleep(3) dsn = self.config.get('b3', 'database') dsndict = splitDSN(dsn) database = getStorage(dsn, dsndict, StubParser()) _update_database(database, '1.3.0') _update_database(database, '1.6.0') _update_database(database, '1.7.0') _update_database(database, '1.8.1') _update_database(database, '1.9.0') _update_database(database, '1.10.0') console_exit('B3 database update completed!')
def test_empty(self, mock_Storage): getStorage('') mock_Storage.assert_called_once()
def test_Database(self, mock_DatabaseStorage): getStorage('database') mock_DatabaseStorage.assert_called_once()