Commit 5d0ac94a by E. Kolpakov

Added tests for html-containing items and zones

parent 2e53fa53
...@@ -72,54 +72,61 @@ def test_studio_submit(): ...@@ -72,54 +72,61 @@ def test_studio_submit():
assert_equals(block.weight, 5) assert_equals(block.weight, 5)
assert_equals(block.data, {'foo': 1}) assert_equals(block.data, {'foo': 1})
def test_ajax():
def do_ajax(orig_file, get_file):
assert_equals.__self__.maxDiff = None assert_equals.__self__.maxDiff = None
block = make_block() block = make_block()
with open('tests/test_data.json') as f: with open(orig_file) as f:
block.data = json.load(f) block.data = json.load(f)
with open('tests/test_get_data.json') as f: initial_data = block.data
zoneA = initial_data["items"][0]["zone"]
zoneB = initial_data["items"][1]["zone"]
feedback_finish = initial_data["feedback"]["finish"]
get_item_feedback = lambda item, type: initial_data["items"][item]["feedback"][type]
with open(get_file) as f:
get_data = json.loads(block.handle('get_data', Mock()).body) get_data = json.loads(block.handle('get_data', Mock()).body)
assert_equals(json.load(f), get_data) assert_equals(json.load(f), get_data)
# Wrong with feedback # Wrong with feedback
data = json.dumps({"val":0,"zone":"Zone B","top":"31px","left":"216px"}) data = json.dumps({"val":0,"zone":zoneB,"top":"31px","left":"216px"})
res = json.loads(block.handle('do_attempt', make_request(data)).body) res = json.loads(block.handle('do_attempt', make_request(data)).body)
assert_equals(res, { assert_equals(res, {
"final_feedback": None, "final_feedback": None,
"finished": False, "finished": False,
"correct": False, "correct": False,
"feedback": "No A" "feedback": get_item_feedback(0, "incorrect")
}) })
with open('tests/test_get_data.json') as f: with open(get_file) as f:
get_data = json.loads(block.handle('get_data', Mock()).body) get_data = json.loads(block.handle('get_data', Mock()).body)
assert_equals(json.load(f), get_data) assert_equals(json.load(f), get_data)
# Wrong without feedback # Wrong without feedback
data = json.dumps({"val":2,"zone":"Zone B","top":"42px","left":"100px"}) data = json.dumps({"val":2,"zone":zoneB,"top":"42px","left":"100px"})
res = json.loads(block.handle('do_attempt', make_request(data)).body) res = json.loads(block.handle('do_attempt', make_request(data)).body)
assert_equals(res, { assert_equals(res, {
"final_feedback": None, "final_feedback": None,
"finished": False, "finished": False,
"correct": False, "correct": False,
"feedback": "" "feedback": get_item_feedback(2, "incorrect")
}) })
with open('tests/test_get_data.json') as f: with open(get_file) as f:
get_data = json.loads(block.handle('get_data', Mock()).body) get_data = json.loads(block.handle('get_data', Mock()).body)
assert_equals(json.load(f), get_data) assert_equals(json.load(f), get_data)
# Correct # Correct
data = json.dumps({"val":0,"zone":"Zone A","top":"11px","left":"111px"}) data = json.dumps({"val":0,"zone":zoneA,"top":"11px","left":"111px"})
res = json.loads(block.handle('do_attempt', make_request(data)).body) res = json.loads(block.handle('do_attempt', make_request(data)).body)
assert_equals(res, { assert_equals(res, {
"final_feedback": None, "final_feedback": None,
"finished": False, "finished": False,
"correct": True, "correct": True,
"feedback": "Yes A" "feedback": get_item_feedback(0, "correct")
}) })
with open('tests/test_get_data.json') as f: with open(get_file) as f:
expected = json.load(f) expected = json.load(f)
expected["state"] = { expected["state"] = {
"items": { "items": {
...@@ -131,15 +138,15 @@ def test_ajax(): ...@@ -131,15 +138,15 @@ def test_ajax():
assert_equals(expected, get_data) assert_equals(expected, get_data)
# Final # Final
data = json.dumps({"val":1,"zone":"Zone B","top":"22px","left":"222px"}) data = json.dumps({"val":1,"zone":zoneB,"top":"22px","left":"222px"})
res = json.loads(block.handle('do_attempt', make_request(data)).body) res = json.loads(block.handle('do_attempt', make_request(data)).body)
assert_equals(res, { assert_equals(res, {
"final_feedback": "Final Feed", "final_feedback": feedback_finish,
"finished": True, "finished": True,
"correct": True, "correct": True,
"feedback": "Yes B" "feedback": get_item_feedback(1, "correct")
}) })
with open('tests/test_get_data.json') as f: with open(get_file) as f:
expected = json.load(f) expected = json.load(f)
expected["state"] = { expected["state"] = {
"items": { "items": {
...@@ -148,10 +155,17 @@ def test_ajax(): ...@@ -148,10 +155,17 @@ def test_ajax():
}, },
"finished": True "finished": True
} }
expected["feedback"]["finish"] = "Final Feed" expected["feedback"]["finish"] = feedback_finish
get_data = json.loads(block.handle('get_data', Mock()).body) get_data = json.loads(block.handle('get_data', Mock()).body)
assert_equals(expected, get_data) assert_equals(expected, get_data)
def test_ajax():
do_ajax('tests/test_data.json', 'tests/test_get_data.json')
def test_html_ajax():
do_ajax('tests/test_html_data.json', 'tests/test_get_html_data.json')
def test_ajax_solve_and_reset(): def test_ajax_solve_and_reset():
block = make_block() block = make_block()
......
{
"zones": [
{
"index": 1,
"width": 200,
"title": "Zone <i>A</i>",
"height": 100,
"y": "200",
"x": "100",
"id": "zone-1"
},
{
"index": 2,
"width": 200,
"title": "Zone <b>B</b>",
"height": 100,
"y": 0,
"x": 0,
"id": "zone-2"
}
],
"items": [
{
"displayName": "<b>A</b>",
"backgroundImage": "",
"id": 0,
"size": {
"width": "190px",
"height": "auto"
}
},
{
"displayName": "<i>B</i>",
"backgroundImage": "",
"id": 1,
"size": {
"width": "190px",
"height": "auto"
}
},
{
"displayName": "X",
"backgroundImage": "",
"id": 2,
"size": {
"width": "100px",
"height": "100px"
}
},
{
"displayName": "",
"backgroundImage": "http://i1.kym-cdn.com/entries/icons/square/000/006/151/tumblr_lltzgnHi5F1qzib3wo1_400.jpg",
"id": 3,
"size": {
"width": "190px",
"height": "auto"
}
}
],
"state": {
"items": {},
"finished": false
},
"feedback": {
"start": "Intro Feed"
},
"targetImg": "http://i0.kym-cdn.com/photos/images/newsfeed/000/030/404/1260585284155.png"
}
{
"zones": [
{
"index": 1,
"width": 200,
"title": "Zone <i>A</i>",
"height": 100,
"y": "200",
"x": "100",
"id": "zone-1"
},
{
"index": 2,
"width": 200,
"title": "Zone <b>B</b>",
"height": 100,
"y": 0,
"x": 0,
"id": "zone-2"
}
],
"items": [
{
"displayName": "<b>A</b>",
"feedback": {
"incorrect": "No <b>A</b>",
"correct": "Yes <b>A</b>"
},
"zone": "Zone <i>A</i>",
"backgroundImage": "",
"id": 0,
"size": {
"width": "190px",
"height": "auto"
}
},
{
"displayName": "<i>B</i>",
"feedback": {
"incorrect": "No <i>B</i>",
"correct": "Yes <i>B</i>"
},
"zone": "Zone <b>B</b>",
"backgroundImage": "",
"id": 1,
"size": {
"width": "190px",
"height": "auto"
}
},
{
"displayName": "X",
"feedback": {
"incorrect": "",
"correct": ""
},
"zone": "none",
"backgroundImage": "",
"id": 2,
"size": {
"width": "100px",
"height": "100px"
}
},
{
"displayName": "",
"feedback": {
"incorrect": "",
"correct": ""
},
"zone": "none",
"backgroundImage": "http://i1.kym-cdn.com/entries/icons/square/000/006/151/tumblr_lltzgnHi5F1qzib3wo1_400.jpg",
"id": 3,
"size": {
"width": "190px",
"height": "auto"
}
}
],
"state": {
"items": {},
"finished": true
},
"feedback": {
"start": "Intro Feed",
"finish": "Final <b>Feed</b>"
},
"targetImg": "http://i0.kym-cdn.com/photos/images/newsfeed/000/030/404/1260585284155.png"
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment