Commit 5de8bcd6 by Andy Armstrong

Split AcidBlock into leaf and parent variants

Introduced a new AcidParentBlock, and refactored AcidBlock so that it doesn't have children.
parent 73de65fe
from .acid import AcidBlock
from .acid import AcidBlock, AcidParentBlock
......@@ -56,7 +56,7 @@ class AcidBlock(XBlock):
"""
A testing block that checks the behavior of the container.
"""
has_children = True
has_children = False
SUCCESS_CLASS = 'fa fa-check-square-o fa-lg pass'
FAILURE_CLASS = 'fa fa-times fa-lg fail'
......@@ -151,40 +151,18 @@ class AcidBlock(XBlock):
except Exception:
logging.warning('Unable to use scope in acid test', exc_info=True)
# Save the changes to our fields so that they are visible to our children
self.save()
children = [
self.runtime.get_block(child_id)
for child_id in self.children
]
rendered_children = [
self.runtime.render_child(child, view_name, context=context)
for child in children
]
child_values = {
child.name: child.parent_value
for child in children
if child.scope_ids.block_type == 'acid' and child.name is not None
}
frag = Fragment(self.render_template(
'html/acid.html.mako',
error_class=self.ERROR_CLASS,
success_class=self.SUCCESS_CLASS,
failure_class=self.FAILURE_CLASS,
unknown_class=self.UNKNOWN_CLASS,
acid_child_values=child_values,
acid_child_count=len([child for child in children if child.scope_ids.block_type == 'acid']),
rendered_children=(fragment.content for fragment in rendered_children),
storage_tests=scope_test_contexts,
local_resource_url=self.runtime.local_resource_url(self, 'public/test_data.json'),
))
frag.add_frags_resources(rendered_children)
frag.add_javascript(self.resource_string("static/js/jquery.ajaxq-0.0.1.js"))
frag.add_javascript(self.resource_string('static/js/acid_update_status.js'))
frag.add_javascript(self.resource_string('static/js/acid.js'))
frag.add_css(self.resource_string("static/css/acid.css"))
frag.add_css_url('//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css')
......@@ -253,13 +231,78 @@ class AcidBlock(XBlock):
def workbench_scenarios():
"""A canned scenario for display in the workbench."""
return [
("XBlock Acid test",
("XBlock Acid single block test",
"""\
<vertical_demo>
<acid name='test'>
</acid>
</vertical_demo>
""")
]
@generate_fields
class AcidParentBlock(AcidBlock):
"""
A testing block that checks the behavior of the container for a parent XBlock.
"""
has_children = True
def fallback_view(self, view_name, context=None): # pylint: disable=W0613
"""
This view is used by the Acid XBlock to test various features of
the runtime it is contained in
"""
acid_fragment = super(AcidParentBlock, self).fallback_view(view_name, context=context)
# Save the changes to our fields so that they are visible to our children
self.save()
children = [
self.runtime.get_block(child_id)
for child_id in self.children
]
rendered_children = [
self.runtime.render_child(child, view_name, context=context)
for child in children
]
child_values = {
child.name: child.parent_value
for child in children
if child.scope_ids.block_type == 'acid' and child.name is not None
}
frag = Fragment(self.render_template(
'html/acid_parent.html.mako',
error_class=self.ERROR_CLASS,
success_class=self.SUCCESS_CLASS,
failure_class=self.FAILURE_CLASS,
unknown_class=self.UNKNOWN_CLASS,
acid_html=acid_fragment.content,
acid_child_values=child_values,
acid_child_count=len([child for child in children if child.scope_ids.block_type == 'acid']),
rendered_children=(fragment.content for fragment in rendered_children),
local_resource_url=self.runtime.local_resource_url(self, 'public/test_data.json'),
))
frag.add_frag_resources(acid_fragment)
frag.add_frags_resources(rendered_children)
frag.add_javascript(self.resource_string('static/js/acid_parent.js'))
frag.initialize_js('AcidParentBlock')
return frag
@staticmethod
def workbench_scenarios():
"""A canned scenario for display in the workbench."""
return [
("XBlock Acid Parent test",
"""\
<vertical_demo>
<acid name='parent'>
<acid_parent name='parent'>
<acid name='left-child'/>
<acid name='right-child'/>
</acid>
</acid_parent>
</vertical_demo>
""")
]
......@@ -5,29 +5,14 @@
data-failure-class="${failure_class}"
data-error-class="${error_class}"
data-unknown-class="${unknown_class}"
data-acid-child-count="${acid_child_count}"
data-local-resource-url="${local_resource_url}"
>
<script class="acid-child-values" type="application/json">
${json.dumps(acid_child_values)}
</script>
<p>Acid XBlock</p>
<h3>Acid XBlock</h3>
<p>JS init function run:
<span class="js-init-run">
<i class="${unknown_class}"></i>
</span>
</p>
<p>Acid Child counts match:
<span class="child-counts-match">
<i class="${unknown_class}"></i>
</span>
</p>
<p>Acid Child values match:
<span class="child-values-match">
<i class="${unknown_class}"></i>
</span>
</p>
<p>Resource Url Test:
<span class="local-resource-test">
<i class="${unknown_class}"></i>
......@@ -71,9 +56,4 @@
</tr>
% endfor
</table>
<div class='acid-children'>
% for child in rendered_children:
${child}
% endfor
</div>
</div>
<%! import json %>
<div class="acid-parent-block"
data-success-class="${success_class}"
data-failure-class="${failure_class}"
data-error-class="${error_class}"
data-unknown-class="${unknown_class}"
data-acid-child-count="${acid_child_count}"
data-local-resource-url="${local_resource_url}"
>
<script class="acid-child-values" type="application/json">
${json.dumps(acid_child_values)}
</script>
${acid_html}
<hr/>
<h3>Acid Children</h3>
<p>Acid Child counts match:
<span class="child-counts-match">
<i class="${unknown_class}"></i>
</span>
</p>
<p>Acid Child values match:
<span class="child-values-match">
<i class="${unknown_class}"></i>
</span>
</p>
<div class='acid-children'>
% for child in rendered_children:
${child}
% endfor
</div>
</div>
......@@ -6,23 +6,8 @@ function AcidBlock(runtime, element) {
}
function mark(result, selector, subelem, msg) {
subelem = subelem || element;
msg = msg || "";
var elems = $(selector, subelem || element).not($('.acid-children *', element))
if (elems.length == 1) {
symbol = $("<i/>", {
'class': acidData(result + '-class')
});
if (msg) {
symbol = symbol.after(": " + msg)
}
symbol.appendTo(elems.empty());
} else {
$("<i/>", {
'class': acidData('error-class')
}).after("ASSERTION FAILURE: Can only mark single elements").appendTo(elems.empty())
console.log(elems);
}
acid_update_status(selector, subelem, element, msg,
acidData(result + '-class'), acidData('error-class'));
}
function resourceTests() {
......@@ -31,7 +16,7 @@ function AcidBlock(runtime, element) {
mark('failure', '.local-resource-test', element, 'Unable to load local resource');
})
.done(function(data) {
if (data.test_data == 'success') {
if (data.test_data === 'success') {
mark('success', '.local-resource-test');
} else {
mark('failure', '.local-resource-test', element, 'Data mismatch');
......@@ -39,25 +24,6 @@ function AcidBlock(runtime, element) {
});
}
function childTests() {
if (acidData('acid-child-count') == runtime.children(element).length) {
mark('success', '.child-counts-match');
}
var childValues = JSON.parse($('.acid-child-values', element).html());
$.each(childValues, function(name, value) {
var child_value = runtime.childMap(element, name).parentValue;
if (child_value != value) {
mark(
'failure', '.child-values-match', element,
'Child ' + name + ' had value ' + child_value + ' but expected ' + value
);
return;
}
});
mark('success', '.child-values-match');
}
function scopeTests() {
$('.scope-storage-test', element).each(function() {
var $this = $(this);
......@@ -67,7 +33,7 @@ function AcidBlock(runtime, element) {
url: $this.data('handler-url'),
success: function (ret) {
mark('success', '.server-storage-test-returned', $this);
if (ret.status == "ok") {
if (ret.status === "ok") {
mark('success', '.server-storage-test-succeeded', $this);
$.ajaxq("acid-queue", {
......@@ -77,7 +43,7 @@ function AcidBlock(runtime, element) {
success: function (ret) {
mark('success', '.client-storage-test-returned', $this);
if (ret.status == "ok") {
if (ret.status === "ok") {
mark('success', '.client-storage-test-succeeded', $this);
} else {
mark('failure', '.client-storage-test-succeeded', $this, ret.message);
......@@ -94,7 +60,6 @@ function AcidBlock(runtime, element) {
mark('success', '.js-init-run');
childTests();
resourceTests();
scopeTests();
......
/* Javascript for the Acid XBlock. */
function AcidParentBlock(runtime, element) {
function acidData(key) {
return $('.acid-parent-block', element).data(key);
}
function mark(result, selector, subelem, msg) {
acid_update_status(selector, subelem, element, msg,
acidData(result + '-class'), acidData('error-class'));
}
function childTests() {
if (acidData('acid-child-count') === runtime.children(element).length) {
mark('success', '.child-counts-match');
}
var childValues = JSON.parse($('.acid-child-values', element).html());
$.each(childValues, function(name, value) {
var child_value = runtime.childMap(element, name).parentValue;
if (child_value !== value) {
mark(
'failure', '.child-values-match', element,
'Child ' + name + ' had value ' + child_value + ' but expected ' + value
);
return;
}
});
mark('success', '.child-values-match');
}
AcidBlock(runtime, element);
childTests();
return {parentValue: acidData('parent-value')};
}
function acid_update_status(selector, element, block, msg, successClass, errorClass) {
var elems, symbol;
msg = msg || "";
elems = $(selector, element || block).not($('.acid-children *', block));
if (elems.length === 1) {
symbol = $("<i/>", {
'class': successClass
});
if (msg) {
symbol = symbol.after(": " + msg);
}
symbol.appendTo(elems.empty());
} else {
$("<i/>", {
'class': errorClass
}).after("ASSERTION FAILURE: Can only mark single elements").appendTo(elems.empty());
console.log(elems);
}
}
......@@ -29,6 +29,7 @@ setup(
entry_points={
'xblock.v1': [
'acid = acid:AcidBlock',
'acid_parent = acid:AcidParentBlock',
],
},
package_data=package_data("acid", ["static", "public"]),
......
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