def test_order_matters_not_okay_recursive(self): with LogCapture(recursive_check=True) as log: root.error('junk') with ShouldAssert( dedent("""\ same: [] expected: [('root', 'INFO', 'one')] actual: [('root', 'ERROR', 'junk')] While comparing [0]: sequence not as expected: same: ('root',) expected: ('INFO', 'one') actual: ('ERROR', 'junk') While comparing [0][1]: 'INFO' (expected) != 'ERROR' (actual)""" )): log.check_present(('root', 'INFO', 'one'), )
def test_simple_strict(self): log_capture = LogCapture(ensure_checks_above=ERROR) root.error('during') log_capture.uninstall() with ShouldAssert( "Not asserted ERROR log(s): [('root', 'ERROR', 'during')]"): log_capture.ensure_checked()
def test_almost_same_order_matters(self): with LogCapture() as log: root.info('one') root.error('junk') root.warning('two') root.error('junk') with ShouldAssert( dedent("""\ sequence not as expected: same: (('root', 'INFO', 'one'),) expected: (('root', 'WARNING', 'two'), ('root', 'ERROR', 'three')) actual: (('root', 'ERROR', 'junk'), ('root', 'WARNING', 'two'), ('root', 'ERROR', 'junk'))""")): log.check_present( ('root', 'INFO', 'one'), ('root', 'WARNING', 'two'), ('root', 'ERROR', 'three'), )
def test_multiple_identical_expected_order_doesnt_matter_not_ok(self): with LogCapture() as log: root.error('junk') root.info('one') root.warning('two') root.error('junk') root.info('one') with ShouldAssert(dedent("""\ entries not as expected: expected and found: [('root', 'INFO', 'one'), ('root', 'WARNING', 'two'), ('root', 'INFO', 'one')] expected but not found: [('root', 'WARNING', 'two')] other entries: [('root', 'ERROR', 'junk'), ('root', 'ERROR', 'junk')]""")): log.check_present( ('root', 'INFO', 'one'), ('root', 'INFO', 'one'), ('root', 'WARNING', 'two'), ('root', 'WARNING', 'two'), order_matters=False )
def test_order_matters_but_wrong(self): with LogCapture() as log: root.info('one') root.error('j1') root.error('three') root.warning('two') root.error('j2') with ShouldAssert( dedent("""\ ignored: [('root', 'ERROR', 'j1'), ('root', 'ERROR', 'three'), ('root', 'ERROR', 'j2')] same: [('root', 'INFO', 'one'), ('root', 'WARNING', 'two')] expected: [('root', 'ERROR', 'three')] actual: []""")): log.check_present( ('root', 'INFO', 'one'), ('root', 'WARNING', 'two'), ('root', 'ERROR', 'three'), )
def test_no_warn_not_expected(self): with ShouldAssert("sequence not as expected:\n\n" "same:\n[]\n\n" "expected:\n[<C:" + warn_module + ".UserWarning>args: ('foo',)</>]" "\n\nactual:\n[]"): with ShouldWarn(UserWarning('foo')): pass
def test_register_more_specific(self): class_ = namedtuple('Test', 'x') with ShouldAssert('compare class_'): compare(class_(1), class_(2), comparers={ tuple: Mock(return_value='compare tuple'), class_: Mock(return_value='compare class_') })
def test_supply_comparer(self): def compare_dict(x, y, context): self.assertEqual(x, {1: 1}) self.assertEqual(y, {2: 2}) self.assertEqual(context.get_option('foo'), 'bar') return 'not equal' with ShouldAssert('not equal'): compare({1: 1}, {2: 2}, foo='bar', comparers={dict: compare_dict})
def test_simple_strict_not_asserted_by_check_present(self): log_capture = LogCapture(ensure_checks_above=ERROR) root.error('before') root.error('during') log_capture.uninstall() log_capture.check_present(("root", "ERROR", "during")) with ShouldAssert( "Not asserted ERROR log(s): [('root', 'ERROR', 'before')]"): log_capture.ensure_checked()
def test_warn_not_expected(self): with ShouldAssert("sequence not as expected:\n\n" "same:\n[]\n\n" "expected:\n[]\n\n" "actual:\n[UserWarning('foo'" + comma + ")]"): with warnings.catch_warnings(record=True) as backstop: with ShouldNotWarn(): warnings.warn('foo') compare(len(backstop), expected=0)
def test_minimal_bad(self): with ShouldAssert("sequence not as expected:\n\n" "same:\n[]\n\n" "expected:\n" "[<C:" + warn_module + ".DeprecationWarning(failed)>wrong type</>]\n\n" "actual:\n[UserWarning('foo'" + comma + ")]"): with ShouldWarn(DeprecationWarning): warnings.warn('foo')
def test_not_as_expected(self): if PY2: message = ('ValidationError([u"d\'oh"]) (expected) != ' 'ValidationError([u\'nuts\']) (raised)') else: message = ('ValidationError(["d\'oh"]) (expected) != ' 'ValidationError([\'nuts\']) (raised)') with ShouldAssert(message): with ShouldRaise(ValidationError("d'oh")): raise ValidationError("nuts")
def test_order_doesnt_matter_not_okay(self): with LogCapture() as log: root.error('junk') with ShouldAssert( dedent("""\ ignored: [('root', 'ERROR', 'junk')] in expected but not actual: [('root', 'INFO', 'one')]""")): log.check_present(('root', 'INFO', 'one'), order_matters=False)
def test_simple_strict_re_defaulted(self): with Replace('testfixtures.LogCapture.default_ensure_checks_above', ERROR): LogCapture.default_ensure_checks_above = ERROR log_capture = LogCapture() root.error('during') log_capture.uninstall() with ShouldAssert( "Not asserted ERROR log(s): [('root', 'ERROR', 'during')]" ): log_capture.ensure_checked()
def test_simple_strict_re_defaulted(self): old = LogCapture.default_ensure_checks_above try: LogCapture.default_ensure_checks_above = ERROR log_capture = LogCapture() root.error('during') log_capture.uninstall() with ShouldAssert("Not asserted ERROR log(s): [('root', 'ERROR', 'during')]"): log_capture.ensure_checked() finally: LogCapture.default_ensure_checks_above = old
def test_normal_check(self): with LogCapture() as log: getLogger().info('oh hai') with ShouldAssert("sequence not as expected:\n\n" "same:\n" "()\n\n" "expected:\n" "(('root', 'INFO', 'oh noez'),)\n\n" "actual:\n" "(('root', 'INFO', 'oh hai'),)"): log.check(('root', 'INFO', 'oh noez'))
def test_maximal_bad(self): with ShouldAssert( "sequence not as expected:\n\n" "same:\n[]\n\n" "expected:\n[\n" "<C(failed):" + warn_module + ".DeprecationWarning>\n" "attributes differ:\n" "'args': ('bar',) (Comparison) != ('foo',) (actual)\n</C>]\n\n" "actual:\n[DeprecationWarning('foo'" + comma + ")]"): with ShouldWarn(DeprecationWarning('bar')): warnings.warn_explicit('foo', DeprecationWarning, 'bar.py', 42, 'bar_module')
def test_filter_missing(self): if PY3: type_repr = 'builtins.DeprecationWarning' else: type_repr = 'exceptions.DeprecationWarning' with ShouldAssert("sequence not as expected:\n\n" "same:\n[]\n\n" "expected:\n[<C:{}>]\n\n" "actual:\n[]".format(type_repr)): with ShouldWarn(DeprecationWarning, message="This function is deprecated."): warnings.warn("This utility is deprecated.", DeprecationWarning)
def test_not_same(self, d): d.write('something', b'stuff') with ShouldAssert("sequence not as expected:\n" "\n" "same:\n" "()\n" "\n" "expected:\n" "('.svn', 'something')\n" "\n" "actual:\n" "('something',)"): d.compare(['.svn', 'something'])
def test_order_doesnt_matter_not_okay(self): with LogCapture() as log: root.error('junk') with ShouldAssert( dedent("""\ entries not as expected: expected and found: [] expected but not found: [('root', 'INFO', 'one')] other entries: [('root', 'ERROR', 'junk')]""")): log.check_present(('root', 'INFO', 'one'), order_matters=False)
def test_order_matters_not_okay(self): with LogCapture() as log: root.error('junk') with ShouldAssert( dedent("""\ sequence not as expected: same: () expected: (('root', 'INFO', 'one'),) actual: (('root', 'ERROR', 'junk'),)""")): log.check_present(('root', 'INFO', 'one'), )
def test_order_matters_not_okay(self): with LogCapture() as log: root.error('junk') with ShouldAssert( dedent("""\ ignored: [('root', 'ERROR', 'junk')] same: [] expected: [('root', 'INFO', 'one')] actual: []""")): log.check_present(('root', 'INFO', 'one'), )
def test_single_item_not_ok(self): with LogCapture(attributes=['getMessage']) as log: root.info('one') root.error('junk') root.error('three') with ShouldAssert(dedent("""\ sequence not as expected: same: () expected: ('two',) actual: ('one', 'junk', 'three')""")): log.check_present('two')
def test_single_item_not_ok(self): with LogCapture(attributes=['getMessage']) as log: root.info('one') root.error('junk') root.error('three') with ShouldAssert( dedent("""\ ignored: ['one', 'junk', 'three'] same: [] expected: ['two'] actual: []""")): log.check_present('two')
def test_recursive_check(self): with LogCapture(recursive_check=True) as log: getLogger().info('oh hai') with ShouldAssert( "sequence not as expected:\n\n" "same:\n()\n\n" "expected:\n(('root', 'INFO', 'oh noez'),)\n\n" "actual:\n(('root', 'INFO', 'oh hai'),)\n\n" "While comparing [0]: sequence not as expected:\n\n" "same:\n('root', 'INFO')\n\n" "expected:\n" "('oh noez',)\n\n" "actual:\n" "('oh hai',)\n\n" "While comparing [0][2]: 'oh noez' (expected) != 'oh hai' (actual)" ): log.check(('root', 'INFO', 'oh noez'))
def test_almost_same_order_doesnt_matter(self): with LogCapture() as log: root.info('one') root.error('junk') root.error('three') root.error('junk') with ShouldAssert( dedent("""\ ignored: [('root', 'ERROR', 'junk'), ('root', 'ERROR', 'junk')] same: [('root', 'ERROR', 'three'), ('root', 'INFO', 'one')] in expected but not actual: [('root', 'WARNING', 'two')]""")): log.check_present(('root', 'ERROR', 'three'), ('root', 'INFO', 'one'), ('root', 'WARNING', 'two'), order_matters=False)
def test_bad_params(self): # not needed if we didn't have to support Python 2! with ShouldAssert('order_matters is the only keyword parameter'): LogCapture(install=False).check_present(foo='bar')
def test_simple_strict_ctx(self): with ShouldAssert( "Not asserted ERROR log(s): [('root', 'ERROR', 'during')]"): with LogCapture(ensure_checks_above=ERROR): root.error('during')