本文整理汇总了Python中xblock.test.tools.assert_equals函数的典型用法代码示例。如果您正苦于以下问题:Python assert_equals函数的具体用法?Python assert_equals怎么用?Python assert_equals使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了assert_equals函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: student_view
def student_view(self, _context):
"""Try out some services."""
# i18n is available, and works.
def assert_equals_unicode(str1, str2):
"""`str1` equals `str2`, and both are Unicode strings."""
assert_equals(str1, str2)
assert isinstance(str1, unicode)
assert isinstance(str2, unicode)
i18n = self.runtime.service(self, "i18n")
assert_equals_unicode(u"Welcome!", i18n.ugettext("Welcome!"))
assert_equals_unicode(u"Plural", i18n.ungettext("Singular", "Plural", 0))
assert_equals_unicode(u"Singular", i18n.ungettext("Singular", "Plural", 1))
assert_equals_unicode(u"Plural", i18n.ungettext("Singular", "Plural", 2))
# secret_service is available.
assert_equals(self.runtime.service(self, "secret_service"), 17)
# no_such_service is not available, and raises an exception, because we
# said we needed it.
with assert_raises(NoSuchServiceError):
self.runtime.service(self, "no_such_service")
# another_not_service is not available, and returns None, because we
# didn't need it, we only wanted it.
assert_is(self.runtime.service(self, "another_not_service"), None)
示例2: test_cached_parent
def test_cached_parent():
class HasParent(XBlock):
pass
runtime = Mock()
block = HasParent(runtime, DictFieldData({}), Mock())
# block has no parent yet, and we don't need to call the runtime to find
# that out.
assert_equals(block.get_parent(), None)
assert not runtime.get_block.called
# Set a parent id for the block. Get the parent. Now we have one, and we
# used runtime.get_block to get it.
block.parent = "some_parent_id"
parent = block.get_parent()
assert_not_equals(parent, None)
assert runtime.get_block.called_with("some_parent_id")
# Get the parent again. It will be the same parent, and we didn't call the
# runtime.
runtime.reset_mock()
parent2 = block.get_parent()
assert parent2 is parent
assert not runtime.get_block.called
示例3: test_cached_parent
def test_cached_parent():
class HasParent(XBlock):
"""
Dummy empty class
"""
pass
runtime = TestRuntime(services={'field-data': DictFieldData({})})
runtime.get_block = Mock()
block = HasParent(runtime, scope_ids=Mock(spec=ScopeIds))
# block has no parent yet, and we don't need to call the runtime to find
# that out.
assert_equals(block.get_parent(), None)
assert not runtime.get_block.called
# Set a parent id for the block. Get the parent. Now we have one, and we
# used runtime.get_block to get it.
block.parent = "some_parent_id"
parent = block.get_parent()
assert_not_equals(parent, None)
assert runtime.get_block.called_with("some_parent_id")
# Get the parent again. It will be the same parent, and we didn't call the
# runtime.
runtime.get_block.reset_mock()
parent2 = block.get_parent()
assert parent2 is parent
assert not runtime.get_block.called
示例4: test_all_scenarios
def test_all_scenarios():
"""Load the home page, get every URL, make a test from it."""
client = Client()
response = client.get("/")
assert response.status_code == 200
html = lxml.html.fromstring(response.content)
a_tags = list(html.xpath('//a'))
for a_tag in a_tags:
yield try_scenario, a_tag.get('href'), a_tag.text
# Load the scenarios from the classes.
scenarios = []
for _, cls in XBlock.load_classes():
if hasattr(cls, "workbench_scenarios"):
for _, xml in cls.workbench_scenarios():
scenarios.append(xml)
# We should have an <a> tag for each scenario.
assert_equals(len(a_tags), len(scenarios))
# We should have at least one scenario with a vertical tag, since we use
# empty verticals as our canary in the coal mine that something has gone
# horribly wrong with loading the scenarios.
assert any("<vertical>" in xml for xml in scenarios)
# Since we are claiming in try_scenario that no vertical is empty, let's
# eliminate the possibility that a scenario has an actual empty vertical.
assert all("<vertical></vertical>" not in xml for xml in scenarios)
assert all("<vertical/>" not in xml for xml in scenarios)
示例5: test_get_mutable_mark_dirty
def test_get_mutable_mark_dirty():
"""
Ensure that accessing a mutable field type does not mark it dirty
if the field has never been set. If the field has been set, ensure
that it is set to dirty.
"""
class MutableTester(XBlock):
"""Test class with mutable fields."""
list_field = List(default=[])
mutable_test = MutableTester(TestRuntime(services={'field-data': DictFieldData({})}), scope_ids=Mock(spec=ScopeIds))
# Test get/set with a default value.
assert_equals(len(mutable_test._dirty_fields), 0)
_test_get = mutable_test.list_field
assert_equals(len(mutable_test._dirty_fields), 1)
mutable_test.list_field = []
assert_equals(len(mutable_test._dirty_fields), 1)
# Now test after having explicitly set the field.
mutable_test.save()
assert_equals(len(mutable_test._dirty_fields), 0)
_test_get = mutable_test.list_field
assert_equals(len(mutable_test._dirty_fields), 1)
示例6: test_set_save_mutate_save
def test_set_save_mutate_save(self):
pointer = self.new_value
self.set(pointer)
self.block.save()
self.mutate(pointer)
self.block.save()
assert_equals(pointer, self.field_data.get(self.block, 'field'))
示例7: test_caching_is_per_instance
def test_caching_is_per_instance():
# Test that values cached for one instance do not appear on another
class FieldTester(ScopedStorageMixin):
"""Toy class for ModelMetaclass and field access testing"""
field_a = List(scope=Scope.settings)
field_data = MagicMock(spec=FieldData)
field_data.get = lambda block, name, default=None: [name] # pylint: disable=C0322
# Same field_data used in different objects should result
# in separately-cached values, so that changing a value
# in one instance doesn't affect values stored in others.
field_tester_a = FieldTester(
runtime=TestRuntime(services={'field-data': field_data}),
scope_ids=MagicMock(spec=ScopeIds)
)
field_tester_b = FieldTester(
runtime=TestRuntime(services={'field-data': field_data}),
scope_ids=MagicMock(spec=ScopeIds)
)
value = field_tester_a.field_a
assert_equals(value, field_tester_a.field_a)
field_tester_a.field_a.append(1)
assert_equals(value, field_tester_a.field_a)
assert_not_equals(value, field_tester_b.field_a)
示例8: test_field_serialization
def test_field_serialization():
# Some Fields can define their own serialization mechanisms.
# This test ensures that we are using them properly.
class CustomField(Field):
"""
Specifiy a custom field that defines its own serialization
"""
def from_json(self, value):
return value['value']
def to_json(self, value):
return {'value': value}
class FieldTester(XBlock):
"""Test XBlock for field serialization testing"""
field = CustomField()
field_data = DictFieldData({
'field': {'value': 4}
})
field_tester = FieldTester(
TestRuntime(services={'field-data': field_data}),
None,
Mock(),
)
assert_equals(4, field_tester.field)
field_tester.field = 5
field_tester.save()
assert_equals({'value': 5}, field_data.get(field_tester, 'field'))
示例9: test_all_scenarios
def test_all_scenarios(self):
"""
Load the home page, examine the scenarios displayed.
"""
client = Client()
response = client.get("/")
assert response.status_code == 200
html = lxml.html.fromstring(response.content)
a_tags = list(html.xpath('//a'))
# Load the loaded_scenarios from the classes.
loaded_scenarios = list(scenarios.get_scenarios().values())
# We should have an <a> tag for each scenario.
assert_equals(len(a_tags), len(loaded_scenarios))
# We should have at least one scenario with a vertical tag, since we use
# empty verticals as our canary in the coal mine that something has gone
# horribly wrong with loading the loaded_scenarios.
assert any("<vertical_demo>" in scen.xml for scen in loaded_scenarios)
# Since we are claiming in try_scenario that no vertical is empty, let's
# eliminate the possibility that a scenario has an actual empty vertical.
assert all("<vertical_demo></vertical_demo>" not in scen.xml for scen in loaded_scenarios)
assert all("<vertical_demo/>" not in scen.xml for scen in loaded_scenarios)
示例10: test_mutable_none_values
def test_mutable_none_values():
# Check that fields with values intentionally set to None
# save properly.
class FieldTester(XBlock):
"""Test XBlock for field access testing"""
field_a = List(scope=Scope.settings)
field_b = List(scope=Scope.settings)
field_c = List(scope=Scope.content, default=None)
field_tester = FieldTester(
TestRuntime(services={'field-data': DictFieldData({'field_a': None})}),
scope_ids=Mock(spec=ScopeIds)
)
# Set fields b & c to None
field_tester.field_b = None
field_tester.field_c = None
# Save our changes
field_tester.save()
# Access the fields without modifying them. Want to call `__get__`, not `__set__`,
# because `__get__` marks only mutable fields as dirty.
_test_get = field_tester.field_a
_test_get = field_tester.field_b
_test_get = field_tester.field_c
# The previous accesses will mark the fields as dirty (via __get__)
assert_equals(len(field_tester._dirty_fields), 3) # pylint: disable=W0212
# However, the fields should not ACTUALLY be marked as fields that need to be saved.
assert_equals(len(field_tester._get_fields_to_save()), 0) # pylint: disable=W0212
示例11: test_children_metaclass
def test_children_metaclass():
class HasChildren(object):
"""Toy class for ChildrenModelMetaclass testing"""
__metaclass__ = ChildrenModelMetaclass
has_children = True
class WithoutChildren(object):
"""Toy class for ChildrenModelMetaclass testing"""
__metaclass__ = ChildrenModelMetaclass
class InheritedChildren(HasChildren):
"""Toy class for ChildrenModelMetaclass testing"""
pass
# `HasChildren` and `WithoutChildren` both obtain the `children` attribute and
# the `has_children` method from the `ChildrenModelMetaclass`. Since this is not
# understood by static analysis, silence this error for the duration of this test.
# pylint: disable=E1101
assert HasChildren.has_children
assert not WithoutChildren.has_children
assert InheritedChildren.has_children
assert hasattr(HasChildren, 'children')
assert not hasattr(WithoutChildren, 'children')
assert hasattr(InheritedChildren, 'children')
assert isinstance(HasChildren.children, List)
assert_equals(Scope.children, HasChildren.children.scope)
assert isinstance(InheritedChildren.children, List)
assert_equals(Scope.children, InheritedChildren.children.scope)
示例12: test_change_mutable_default
def test_change_mutable_default():
"""
Ensure that mutating the default value for a field causes
the changes to be saved, and doesn't corrupt other instances
"""
class MutableTester(XBlock):
"""Test class with mutable fields."""
list_field = List()
mutable_test_a = MutableTester(MagicMock(), DictFieldData({}), Mock())
mutable_test_b = MutableTester(MagicMock(), DictFieldData({}), Mock())
# Saving without changing the default value shouldn't write to _field_data
mutable_test_a.list_field # pylint: disable=W0104
mutable_test_a.save()
with assert_raises(KeyError):
mutable_test_a._field_data.get(mutable_test_a, 'list_field')
mutable_test_a.list_field.append(1)
mutable_test_a.save()
assert_equals([1], mutable_test_a._field_data.get(mutable_test_a, 'list_field'))
with assert_raises(KeyError):
mutable_test_b._field_data.get(mutable_test_b, 'list_field')
示例13: test_caching_is_per_instance
def test_caching_is_per_instance():
# Test that values cached for one instance do not appear on another
class FieldTester(object):
"""Toy class for ModelMetaclass and field access testing"""
__metaclass__ = ModelMetaclass
field_a = List(scope=Scope.settings)
def __init__(self, field_data):
self._field_data = field_data
self._dirty_fields = {}
field_data = MagicMock(spec=FieldData)
field_data.get = lambda block, name, default=None: [name] # pylint: disable=C0322
# Same field_data used in different objects should result
# in separately-cached values, so that changing a value
# in one instance doesn't affect values stored in others.
field_tester_a = FieldTester(field_data)
field_tester_b = FieldTester(field_data)
value = field_tester_a.field_a
assert_equals(value, field_tester_a.field_a)
field_tester_a.field_a.append(1)
assert_equals(value, field_tester_a.field_a)
assert_not_equals(value, field_tester_b.field_a)
示例14: student_view
def student_view(self, _context):
"""Try out some services."""
# i18n is available, and works.
def assert_equals_unicode(str1, str2):
"""`str1` equals `str2`, and both are Unicode strings."""
assert_equals(str1, str2)
assert isinstance(str1, unicode)
assert isinstance(str2, unicode)
i18n = self.runtime.service(self, "i18n")
assert_equals_unicode(u"Welcome!", i18n.ugettext("Welcome!"))
assert_equals_unicode(u"Plural", i18n.ungettext("Singular", "Plural", 0))
assert_equals_unicode(u"Singular", i18n.ungettext("Singular", "Plural", 1))
assert_equals_unicode(u"Plural", i18n.ungettext("Singular", "Plural", 2))
when = datetime(2013, 2, 14, 22, 30, 17)
assert_equals_unicode(u"2013-02-14", i18n.strftime(when, "%Y-%m-%d"))
assert_equals_unicode(u"Feb 14, 2013", i18n.strftime(when, "SHORT_DATE"))
assert_equals_unicode(u"Thursday, February 14, 2013", i18n.strftime(when, "LONG_DATE"))
assert_equals_unicode(u"Feb 14, 2013 at 22:30", i18n.strftime(when, "DATE_TIME"))
assert_equals_unicode(u"10:30:17 PM", i18n.strftime(when, "TIME"))
# secret_service is available.
assert_equals(self.runtime.service(self, "secret_service"), 17)
# no_such_service is not available, and raises an exception, because we
# said we needed it.
with assert_raises_regexp(NoSuchServiceError, "is not available"):
self.runtime.service(self, "no_such_service")
# another_not_service is not available, and returns None, because we
# didn't need it, we only wanted it.
assert_is(self.runtime.service(self, "another_not_service"), None)
示例15: test_change_mutable_default
def test_change_mutable_default():
"""
Ensure that mutating the default value for a field causes
the changes to be saved, and doesn't corrupt other instances
"""
class MutableTester(XBlock):
"""Test class with mutable fields."""
list_field = List()
field_data_a = DictFieldData({})
mutable_test_a = MutableTester(TestRuntime(services={'field-data': field_data_a}), scope_ids=Mock(spec=ScopeIds))
field_data_b = DictFieldData({})
mutable_test_b = MutableTester(TestRuntime(services={'field-data': field_data_b}), scope_ids=Mock(spec=ScopeIds))
# Saving without changing the default value shouldn't write to field_data
mutable_test_a.list_field # pylint: disable=W0104
mutable_test_a.save()
with assert_raises(KeyError):
field_data_a.get(mutable_test_a, 'list_field')
mutable_test_a.list_field.append(1)
mutable_test_a.save()
assert_equals([1], field_data_a.get(mutable_test_a, 'list_field'))
with assert_raises(KeyError):
field_data_b.get(mutable_test_b, 'list_field')