Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit 38830b2

Browse files
author
Brad Miller
committed
Add autograde to sql
1 parent 32f7f0d commit 38830b2

3 files changed

Lines changed: 71 additions & 2 deletions

File tree

runestone/activecode/js/activecode.js

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2431,14 +2431,13 @@ SQLActiveCode.prototype.runProg = function() {
24312431
}
24322432
$(this.output).text("")
24332433
// Run this query
2434-
let query = this.buildProg();
2434+
let query = this.buildProg(false); // false --> Do not include suffix
24352435
try {
24362436
var res = this.db.exec(query);
24372437
} catch(error) {
24382438
result_mess = error.toString();
24392439
$(this.output).text(error);
24402440
$(this.outDiv).show();
2441-
24422441
}
24432442
this.logRunEvent({
24442443
'div_id': this.divid,
@@ -2479,8 +2478,71 @@ SQLActiveCode.prototype.runProg = function() {
24792478
respDiv.appendChild(table)
24802479
$(this.outDiv).show()
24812480

2481+
// Now handle autograding
2482+
if (this.suffix) {
2483+
result = this.autograde(res[0]);
2484+
$(this.output).text(result);
2485+
}
2486+
2487+
}
2488+
2489+
SQLActiveCode.prototype.autograde = function(result_table) {
2490+
tests = this.suffix.split(/\n/);
2491+
this.passed = 0;
2492+
this.failed = 0;
2493+
// Tests should be of the form
2494+
// assert row,col oper value for example
2495+
// assert 4,4 == 3
2496+
result = ""
2497+
tests = tests.filter(function(s) {
2498+
return s.indexOf('assert') > -1
2499+
})
2500+
for (let test of tests) {
2501+
[assert, loc, oper, expected] = test.split(/\s+/);
2502+
[row,col] = loc.split(',');
2503+
result += this.testOneAssert(row, col, oper, expected, result_table);
2504+
result += "\n"
2505+
}
2506+
pct = 100 * this.passed / (this.passed + this.failed);
2507+
pct = pct.toLocaleString(undefined, { maximumFractionDigits: 2});
2508+
result += `You passed ${this.passed} out of ${this.passed+this.failed} tests for ${pct}%`
2509+
this.logBookEvent({event: 'unittest',
2510+
div_id: this.divid,
2511+
course: eBookConfig.course,
2512+
act: `percent:${pct}:passed:${this.passed}:failed:${this.failed}`
2513+
});
2514+
return result;
2515+
}
2516+
2517+
SQLActiveCode.prototype.testOneAssert = function(row, col, oper, expected, result_table) {
2518+
let actual = result_table.values[row][col]
2519+
const operators = {
2520+
"==" : function (operand1, operand2) {
2521+
return operand1 == operand2;
2522+
},
2523+
"!=" : function (operand1, operand2) {
2524+
return operand1 != operand2;
2525+
},
2526+
">" : function (operand1, operand2) {
2527+
return operand1 > operand2;
2528+
},
2529+
"<" : function (operand1, operand2) {
2530+
return operand1 > operand2;
2531+
}
2532+
};
2533+
2534+
res = operators[oper](actual, expected)
2535+
if (res) {
2536+
output = `Pass: ${actual} ${oper} ${expected} in row ${row} column ${result_table.columns[col]}`;
2537+
this.passed++;
2538+
} else {
2539+
output = `Failed ${actual} ${oper} ${expected} in row ${row} column ${result_table.columns[col]}`;
2540+
this.failed++
2541+
}
2542+
return output;
24822543
}
24832544

2545+
24842546
function createTable(tableData) {
24852547
var table = document.createElement('table');
24862548
var head = document.createElement('thead');

runestone/activecode/test/_sources/index.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,4 +1310,9 @@ Support for SQL in the browser ? Yes!
13101310

13111311
select * from test;
13121312

1313+
=====
1314+
assert 1,1 == world
1315+
assert 0,1 == hello
1316+
assert 2,1 == 42
1317+
13131318
.. :dburl: http://dev.runestoneinteractive.org:8080/_static/bikeshare.db

runestone/activecode/test/test_activecode.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ def test_sql_activecode(self):
113113
trlist = res.find_elements_by_tag_name('tr')
114114
self.assertEqual(6, len(trlist))
115115
self.assertTrue("hello" in trlist[1].text)
116+
out = self.driver.find_element_by_id("sql1_stdout")
117+
self.assertTrue('You passed 2 out of 3 tests' in out.text)
116118

117119

118120
def test_readfiles(self):

0 commit comments

Comments
 (0)