sql type with multiple test cases not providing error output

sql type with multiple test cases not providing error output

de către Aleksi Postari-
Număr de răspunsuri: 2

Hi,

I am using the SQL question type, and so far, things have been running smoothly. However, now I realized that after adding a second test case to a question, the error output is no longer visible to the student.

After adding a second test case for a SQL question type, I am just getting this back if there is any kind of error executing the code (like a typo or anything else in the answer):

Error in Question

Expected 2 test results, got 1. Perhaps excessive output or error in question?

My test cases are just simply:

Case 1: SELECT * FROM customerMeals; 
Case 2: PRAGMA table_info([customerMeals]);


And below is my customization template (pretty much taken from the standard SQL question type, in which just some MySQL -> sqlite conversions will be done):

import subprocess, os, shutil, sys

# Use Twig to get students answer and the columnwidth template parameter

student_answer = """{{ STUDENT_ANSWER | e('py') }}""".rstrip()

column_widths = []

{% for width in QUESTION.parameters.columnwidths %}

column_widths.append({{width}})

{% endfor %}

def ireplace(old, new, text):

    """ 

    Replace case insensitive text

    """

    try:

        index_l = text.lower().index(old.lower())

        return text[:index_l] + new + text[index_l + len(old):] 

    except:

        return text

import re

student_answer = ireplace("NOW()", "(datetime('now', 'localtime'))", student_answer)

student_answer = ireplace("INT AUTO_INCREMENT NOT NULL", "INTEGER PRIMARY KEY", student_answer)

student_answer = ireplace("INT NOT NULL AUTO_INCREMENT", "INTEGER PRIMARY KEY", student_answer)

student_answer = ireplace("INT AUTO_INCREMENT", "INTEGER PRIMARY KEY", student_answer)

student_answer = re.sub(r'^\s+PRIMARY.*\n?', '', student_answer, flags=re.MULTILINE|re.IGNORECASE)

student_answer = re.sub(r',(?=\n\);)', '', student_answer)


column_widths = []

{% for width in QUESTION.parameters.columnwidths %}

column_widths.append({{width}})

{% endfor %}

if not student_answer.endswith(';'):

    student_answer = student_answer + ';'

db_files = [fname for fname in os.listdir() if fname.endswith('.db')]

if len(db_files) == 0:

    raise Exception("No DB files found!")

elif len(db_files) == 1:

    db_working = db_files[0][:-3]  # Strip .db extension

else:

    raise Exception("Multiple DB files not implemented yet, sorry!")


SEPARATOR = "#<ab@17943918#@>#"

controls = [".mode column", ".headers on"]

if column_widths: # Add column width specifiers if given

    controls.append(".width " + ' '.join(str(width) for width in column_widths))

prelude = '\n'.join(controls) + '\n'


{% for TEST in TESTCASES %}

shutil.copyfile(db_files[0], db_working)  # Copy clean writeable db file

testcode = """{{ TEST.testcode | e('py') }}"""

extra = """{{ TEST.extra | e('py') }}"""

code_to_run = '\n'.join([prelude, extra, student_answer, testcode])

with open('commands', 'w') as sqlite_commands:

    sqlite_commands.write(code_to_run)


with open('commands') as cmd_input:

    text_input = cmd_input.read()

    try:

        output = subprocess.check_output(['sqlite3', db_working], input=text_input,

                 universal_newlines=True)

        print(output)

    except Exception as e:

        raise Exception("sqlite3 error: " + str(e))


{% if not loop.last %}

print(SEPARATOR)

{% endif %}

{% endfor %}


Any ideas?

Ca răspuns la Aleksi Postari

Re: sql type with multiple test cases not providing error output

de către Richard Lobb-
The error message means that the output from the program, when split with the SEPARATOR, didn't yield two test results.

The SEPARATOR in your template (which is an arbitrary random-looking string I came up with about 10 years ago) must match the value in the Test splitter question field (under Template controls). Check that the value in that field is
|#<ab@17943918#@>#\n|ms
See attached screenshot.
Atașament Screenshot from 2022-10-04 15-05-45.png
Ca răspuns la Richard Lobb

Re: sql type with multiple test cases not providing error output

de către Aleksi Postari-
I guess this fixed the problem. Thanks!