Exemplo n.º 1
0
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())
Exemplo n.º 2
0
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())
Exemplo n.º 3
0
    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_run_except(self):
     runner = TestRunner(
         solution='foo = bar',
         tests='>>> foo\n1'
     )
     runner.run()
     self.assertIsNone(runner.results)
     self.assertEqual("name 'bar' is not defined", runner.errors)
     self.assertEqual(False, runner.solved)
Exemplo n.º 5
0
 def test_run_unsolved(self):
     runner = TestRunner(solution='foo = 2', tests='>>> foo\n1')
     runner.run()
     self.assertEqual(1, len(runner.results))
     self.assertEqual(
         {
             'call': 'foo',
             'expected': '1',
             'received': '2',
             'correct': False
         }, runner.results[0])
     self.assertEqual(False, runner.solved)
Exemplo n.º 6
0
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)
Exemplo n.º 7
0
 def test_run_one_line(self):
     runner = TestRunner(solution='foo = 1', tests='>>> foo\n1')
     runner.run()
     self.assertEqual(1, len(runner.results))
     self.assertEqual(
         {
             'call': 'foo',
             'expected': '1',
             'received': '1',
             'correct': True
         }, runner.results[0])
     self.assertEqual(True, runner.solved)
     self.assertEqual('', runner.printed)
Exemplo n.º 8
0
 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'])
Exemplo n.º 9
0
    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_run_unsolved(self):
     runner = TestRunner(
         solution='foo = 2',
         tests='>>> foo\n1'
     )
     runner.run()
     self.assertEqual(1, len(runner.results))
     self.assertEqual(
         {
             'call': 'foo',
             'expected': '1',
             'received': '2',
             'correct': False
         },
         runner.results[0]
     )
     self.assertEqual(False, runner.solved)
    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_run_one_line(self):
     runner = TestRunner(
         solution='foo = 1',
         tests='>>> foo\n1'
     )
     runner.run()
     self.assertEqual(1, len(runner.results))
     self.assertEqual(
         {
             'call': 'foo',
             'expected': '1',
             'received': '1',
             'correct': True
         },
         runner.results[0]
     )
     self.assertEqual(True, runner.solved)
     self.assertEqual('', runner.printed)
 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_json_solved(self):
     runner = TestRunner(
         solution='foo = 1',
         tests='>>> foo\n1'
     )
     runner.run()
     data = json.loads(runner.to_json())
     self.assertEqual(
         {'solved', 'results', 'printed'},
         data.keys()
     )
     self.assertTrue(data['solved'])
     self.assertEqual(
         [{
             'call': 'foo',
             'expected': '1',
             'received': '1',
             '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'])
Exemplo n.º 16
0
 def test_run_except(self):
     runner = TestRunner(solution='foo = bar', tests='>>> foo\n1')
     runner.run()
     self.assertIsNone(runner.results)
     self.assertEqual("name 'bar' is not defined", runner.errors)
     self.assertEqual(False, runner.solved)