def test_use_db_session(monkeypatch, mock_sessionmaker): config = Config() with monkeypatch.context() as m: m.setattr(azafea.model, 'sessionmaker', mock_sessionmaker) db = azafea.model.Db(config.postgresql) with db as dbsession: assert dbsession.open assert not dbsession.committed assert not dbsession.rolled_back assert not dbsession.open assert dbsession.committed assert not dbsession.rolled_back
def test_fail_in_db_session(monkeypatch, mock_sessionmaker): config = Config() with monkeypatch.context() as m: m.setattr(azafea.model, 'sessionmaker', mock_sessionmaker) db = azafea.model.Db(config.postgresql) with pytest.raises(ValueError) as exc_info: with db as dbsession: raise ValueError('Oh no!') assert 'Oh no!' in str(exc_info.value) assert not dbsession.open assert not dbsession.committed assert dbsession.rolled_back
def test_start(capfd, monkeypatch, mock_sessionmaker): config = Config() setup_logging(verbose=config.main.verbose) with monkeypatch.context() as m: m.setattr(azafea.processor, 'Redis', MockRedis) m.setattr(azafea.model, 'sessionmaker', mock_sessionmaker) proc = azafea.processor.Processor('test-worker', config) # Prevent the processor from running its main loop proc._continue = False proc.start() proc.join() capture = capfd.readouterr() assert '{test-worker} Starting' in capture.out
def test_start(capfd, monkeypatch): config = Config() setup_logging(verbose=config.main.verbose) with monkeypatch.context() as m: m.setattr(azafea.controller, 'Processor', MockProcessor) controller = azafea.controller.Controller(config) controller.start() number = get_cpu_count() assert len(controller._processors) == number capture = capfd.readouterr() assert f'Starting the controller with {number} workers' in capture.out for i in range(1, number + 1): assert f'{{worker-{i}}} Starting' in capture.out
def test_exit_from_already_rolled_back_session(monkeypatch, mock_sessionmaker): config = Config() with monkeypatch.context() as m: m.setattr(azafea.model, 'sessionmaker', mock_sessionmaker) db = azafea.model.Db(config.postgresql) with db as dbsession: assert dbsession.open assert not dbsession.committed assert not dbsession.rolled_back assert dbsession.is_active # We tried to commit something explicitly before exiting the context manager, an error # occured which rolled back the transaction; it is now inactive dbsession.rollback() assert not dbsession.open assert not dbsession.committed assert dbsession.rolled_back
def setup_teardown(self, request): # Create a config file for the test, with a common base and some per-test options _, config_file = tempfile.mkstemp() with open(config_file, 'w') as f: f.write(toml.dumps({ 'main': { 'verbose': True, 'number_of_workers': 2, 'exit_on_empty_queues': True, }, 'postgresql': { 'database': 'azafea-tests', }, 'queues': { request.node.name: { 'handler': self.handler_module, }, } })) self.config_file = config_file self.config = Config.from_file(self.config_file) self.db = Db(self.config.postgresql) self.redis = Redis(host=self.config.redis.host, port=self.config.redis.port, password=self.config.redis.password) # Ensure we start with a clean slate self.ensure_no_queues() self.ensure_no_tables() # Run the test function yield # Ensure we finish with a clean DB self.db.drop_all() self.ensure_no_tables() # Deregister the models, tables and events from SQLAlchemy Base._decl_class_registry.clear() Base.metadata.clear() Base.metadata.dispatch._clear() # Deregister the handler modules so the next tests reimport them completely; not doing so # confuses SQLAlchemy, leading to the tables only being created for the first test. :( modules_to_deregister = [] for queue_config in self.config.queues.values(): handler_root = queue_config.handler.rsplit('.', 1)[0] for module in sys.modules: if module.startswith(handler_root): modules_to_deregister.append(module) for module in modules_to_deregister: sys.modules.pop(module) # Ensure we finish with clean a Redis self.clear_queues() self.ensure_no_queues() # And remove the configuration file os.unlink(self.config_file)
def maker(d: Mapping) -> Config: config_file_path = make_config_file(d) return Config.from_file(str(config_file_path))