summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimotej Lazar <timotej.lazar@araneo.org>2015-08-27 18:08:26 +0200
committerTimotej Lazar <timotej.lazar@araneo.org>2015-08-27 18:15:05 +0200
commit965bbb88e9727d4376f950239fc06a9fc6c1a723 (patch)
tree7c73a7ba5cd40fcbd82f289f8e8a7a30eaaceeea
parentb00e270965ee4bbfb570a00c921396424192a69e (diff)
Add test service to wsgi_server
This calls the test function defined in common.py for the given problem.
-rw-r--r--server/prolog_session.py41
-rw-r--r--wsgi_server.py16
2 files changed, 38 insertions, 19 deletions
diff --git a/server/prolog_session.py b/server/prolog_session.py
index ee8dba3..c1cc8f9 100644
--- a/server/prolog_session.py
+++ b/server/prolog_session.py
@@ -4,6 +4,7 @@ import operator
import threading
import prolog.engine
import db
+from db.models import CodeqUser, Problem
from . import problems
__all__ = ['PrologSession']
@@ -88,32 +89,34 @@ class PrologSession(object):
prolog.engine.stop(self._engine_id)
self._engine_id = None
+ def test(self, user_id, problem_id, program):
+ language, problem_group, problem = Problem.identifier(problem_id)
+ problem_module = problems.load_problem(language, problem_group, problem, 'common')
+
+ solved_problems = [p for p in CodeqUser.solved_problems(user_id, language)
+ if p != (problem_group, problem)]
+ other_solutions = problems.solutions_for_problems(language, solved_problems)
+ code = program + '\n' + other_solutions
+
+ try:
+ n_correct, n_all = problem_module.test(code)
+ return ['Passed {} / {} tests.'.format(n_correct, n_all)]
+ except AttributeError as ex:
+ return ['No test is defined for this problem']
+
def run_for_user(self, user_id, problem_id, program, query):
"""A "shorthand" method to start a Prolog session, load correct solutions of all user's solved
problems and the given program, and ask a query.
"""
- conn = db.get_connection()
- try:
- cur = conn.cursor()
- try:
- cur.execute('select l.id, l.identifier, g.identifier, p.identifier from problem p inner join problem_group g on g.id = p.problem_group_id inner join language l on l.id = p.language_id where p.id = %s', (problem_id,))
- row = cur.fetchone()
- language_id = row[0]
- language = row[1]
- problem_group = row[2]
- problem = row[3]
- cur.execute('select g.identifier, p.identifier from solution s inner join problem p on p.id = s.problem_id inner join problem_group g on g.id = p.problem_group_id where s.codeq_user_id = %s and s.done = True and s.problem_id != %s and p.language_id = %s', (user_id, problem_id, language_id))
- solved_problems = cur.fetchall()
- finally:
- cur.close()
- finally:
- conn.commit()
- db.return_connection(conn)
+ language, problem_group, problem = Problem.identifier(problem_id)
+ problem_module = problems.load_problem(language, problem_group, problem, 'common')
+ solved_problems = [p for p in CodeqUser.solved_problems(user_id, language)
+ if p != (problem_group, problem)]
other_solutions = problems.solutions_for_problems(language, solved_problems)
- problem_module = problems.load_problem(language, problem_group, problem, 'common')
problem_facts = problems.get_facts(language, problem_module) or ''
- code = other_solutions + problem_facts + program
+ code = program + '\n' + other_solutions + '\n' + problem_facts
+
messages, status, have_more = self.run(code)
if status == 'ok':
more_messages, status, have_more = self.query(query)
diff --git a/wsgi_server.py b/wsgi_server.py
index 76489db..1624772 100644
--- a/wsgi_server.py
+++ b/wsgi_server.py
@@ -147,6 +147,19 @@ class Query(CodeqService):
session.update_solution(problem_id, trace, program)
return result
+class Test(CodeqService):
+ def process(self, js, session):
+ problem_id = js.get('problem_id')
+ program = js.get('program')
+ if problem_id is None:
+ return {'code': 1, 'message': 'Problem ID not given'}
+ if program is None:
+ return {'code': 2, 'message': 'No program specified'}
+
+ prolog = session.get_prolog()
+ messages = prolog.test(session.get_uid(), problem_id, program)
+ return {'code': 0, 'message': 'ok', 'terminal': {'messages': messages}}
+
class GetProblem(CodeqService):
def process(self, js, session):
language = js.get('language')
@@ -173,6 +186,9 @@ api.add_route('/activity', activity)
query = Query()
api.add_route('/query', query)
+test = Test()
+api.add_route('/test', test)
+
get_problem = GetProblem()
api.add_route('/get_problem', get_problem)