def test_log_stderr(self): Utils.prepare_test(connect_logger=True) with Utils.nostderr() as err: Logger.error("FOO_CAT", "Log message") self.assertIn("FOO_CAT", err.buffer) self.assertIn("Log message", err.buffer)
def test_read_from_disk_missing_dir(self): Logger.LOG_LEVEL = Logger.DEBUG with Utils.nostderr() as stderr: Config.contest_path = "/not/existing/path" with self.assertRaises(UnprocessableEntity): ContestManager.read_from_disk() self.assertIn("Contest not found", stderr.buffer)
def test_evaluate_output_failed(self, check_mock): ContestManager.tasks["poldo"] = {"checker": "/gen"} Logger.LOG_LEVEL = self.log_backup with self.assertRaises(NotImplementedError) as ex: with Utils.nostderr() as stderr: ContestManager.evaluate_output("poldo", "/input", "/output") self.assertIn("Error while evaluating output", stderr.buffer) self.assertEqual("ops ;)", ex.exception.args[0])
def test_run(self, spawn, init): Logger.set_log_level("INFO") with patch.object(spawn(), "join") as join: with Utils.nostderr() as stderr: self.server.run() init.assert_called_once_with() self.assertTrue(spawn.called) join.assert_called_once_with() self.assertIn("SERVER_STATUS", stderr.buffer) self.assertIn("Server started", stderr.buffer)
def test_submit_db_broken(self, g_i_mock, g_f_s_mock): Utils.start_contest() self._insert_data() self.handler.generate_input(token='token', task='poldo', _ip='1.1.1.1') Database.c.execute( "INSERT INTO outputs (id, input, path, size, result) " "VALUES ('outputid', 'inputid', '/output', 42," "'{\"score\":0.5,\"feedback\":{\"a\":1},\"validation\":{" "\"b\":2}}')") Database.c.execute("INSERT INTO sources (id, input, path, size) " "VALUES ('sourceid', 'inputid', '/source', 42)") with patch("terry.database.Database.get_input", return_value=None): with Utils.nostderr() as stderr: with self.assertRaises(BadRequest) as ex: self.handler.submit(output_id='outputid', source_id='sourceid', _ip='1.1.1.1') self.assertIn("WRONG_INPUT", ex.exception.response.data.decode()) self.assertIn("The provided input in invalid", ex.exception.response.data.decode())
def test_submit_db_broken(self, g_i_mock, g_f_s_mock): Utils.start_contest() self._insert_data() self.handler.generate_input(token="token", task="poldo", _ip="1.1.1.1") Database.c.execute( "INSERT INTO outputs (id, input, path, size, result) " "VALUES ('outputid', 'inputid', '/output', 42," '\'{"score":0.5,"feedback":{"a":1},"validation":{' '"b":2}}\')') Database.c.execute("INSERT INTO sources (id, input, path, size) " "VALUES ('sourceid', 'inputid', '/source', 42)") with patch("terry.database.Database.get_input", return_value=None): with Utils.nostderr() as stderr: with self.assertRaises(BadRequest) as ex: self.handler.submit(output_id="outputid", source_id="sourceid", _ip="1.1.1.1") self.assertIn("WRONG_INPUT", ex.exception.response.data.decode()) self.assertIn("The provided input in invalid", ex.exception.response.data.decode())
def test_run_port_in_use(self, mock): with self.assertRaises(SystemExit) as ex: with Utils.nostderr() as stderr: self.server.run() self.assertEqual(1, ex.exception.code) self.assertIn("PORT_ALREADY_IN_USE", stderr.buffer)
def test_call_with_error(self, mock): with Utils.nostderr() as stderr: res = self.server({"REQUEST_METHOD": "GET"}, lambda *args: 42) # this is a garbage way to check the output of the method... self.assertEqual(500, res._callbacks[1].__self__._status_code) self.assertIn("UNCAUGHT_EXCEPTION", stderr.buffer)
def test_file_not_found(self): with self.assertRaises(SystemExit): with Utils.nostderr(): Config.set_config_file("/this/file/doesnt/exist")