def verifiy_get(): req = request.args.get("jsonrequest") if req is None: return response(code=CODE_BAD_REQUEST, errors=ERR_JSONREQUEST_REQUIRED) try: req = base64.b64decode(req).decode("utf8") except Exception as e: logging.debug(e) try: req = json.loads(req) except Exception: return response( code=CODE_BAD_REQUEST, errors=ERR_JSONREQUEST_REQUIRED_BASE64 ) solution = req.get("solution") if not solution: return response( code=CODE_BAD_REQUEST, errors=ERR_SOLUTION_REQUIRED ) runner = TestRunner(solution, req.get("tests")) runner.run() return response(cb=request.args.get("vcallback"), **runner.to_dict())
def verifiy_post(): if request.headers.get("Content-Type").split(';')[0] == JSON_TYPE: req = request.get_json() if not req: return response(code=CODE_BAD_REQUEST, errors="Invalid JSON body.") else: req = request.form.get("jsonrequest") if req is None: return response( code=CODE_BAD_REQUEST, errors=ERR_JSONREQUEST_REQUIRED ) try: req = json.loads(req) except Exception as e: logging.debug(e) return response( code=CODE_BAD_REQUEST, errors=ERR_JSONREQUEST_REQUIRED ) solution = req.get("solution") if not solution: return response( code=CODE_BAD_REQUEST, errors=ERR_SOLUTION_REQUIRED ) runner = TestRunner(solution, req.get("tests")) runner.run() return response(**runner.to_dict())
def test_empty_test_output(self): runner = TestRunner(solution='a=1', tests='>>> a=2') runner.run() data = runner.to_dict() self.assertEqual([{'call': 'a=2'}], data.get('results')) self.assertEqual('', data['printed']) self.assertTrue(data['solved']) self.assertIsNone(data.get('errors'))
def main(): args = sys.argv[-2:] if len(args) == 1: solution, tests = args[0], "" elif len(args) == 2: solution, tests = args else: exit(1) runner = TestRunner(solution, tests) runner.run() json.dump(runner.to_dict(), sys.stdout, indent=2)
def test_list_comprehension(self): runner = TestRunner(solution='def foo(x):\n return x*2', tests='>>> [foo(x) for x in [1,2]]\n [2, 4]') runner.run() data = runner.to_dict() self.assertTrue(data['solved']) self.assertEqual([{ 'call': '[foo(x) for x in [1,2]]', 'expected': '[2, 4]', 'received': '[2, 4]', 'correct': True }], data['results']) self.assertEqual('', data['printed'])
def test_to_dict_solved(self): runner = TestRunner(solution='foo = 1', tests='>>> foo\n1') runner.run() data = runner.to_dict() self.assertEqual({'solved', 'results', 'printed'}, {k for k in data}) self.assertTrue(data['solved']) self.assertEqual([{ 'call': 'foo', 'expected': '1', 'received': '1', 'correct': True }], data['results']) self.assertEqual('', data['printed'])
def test_empty_test_output(self): runner = TestRunner( solution='a=1', tests='>>> a=2' ) runner.run() data = runner.to_dict() self.assertEqual( [{ 'call': 'a=2' }], data.get('results') ) self.assertEqual('', data['printed']) self.assertTrue(data['solved']) self.assertIsNone(data.get('errors'))
def test_list_comprehension(self): runner = TestRunner( solution='def foo(x):\n return x*2', tests='>>> [foo(x) for x in [1,2]]\n [2, 4]' ) runner.run() data = runner.to_dict() self.assertTrue(data['solved']) self.assertEqual( [{ 'call': '[foo(x) for x in [1,2]]', 'expected': '[2, 4]', 'received': '[2, 4]', 'correct': True }], data['results'] ) self.assertEqual('', data['printed'])
def test_to_dict_solved(self): runner = TestRunner( solution='foo = 1', tests='>>> foo\n1' ) runner.run() data = runner.to_dict() self.assertEqual( {'solved', 'results', 'printed'}, {k for k in data} ) self.assertTrue(data['solved']) self.assertEqual( [{ 'call': 'foo', 'expected': '1', 'received': '1', 'correct': True }], data['results'] ) self.assertEqual('', data['printed'])