<s> import pytest <EOL> import logbook <EOL> from logbook . utils import ( <EOL> logged_if_slow , deprecated , forget_deprecation_locations , <EOL> suppressed_deprecations , log_deprecation_message ) <EOL> from time import sleep <EOL> _THRESHOLD = <NUM_LIT:0.1> <EOL> try : <EOL> from unittest . mock import Mock , call <EOL> except ImportError : <EOL> from mock import Mock , call <EOL> def test_logged_if_slow_reached ( test_handler ) : <EOL> with test_handler . applicationbound ( ) : <EOL> with logged_if_slow ( '<STR_LIT>' , threshold = _THRESHOLD ) : <EOL> sleep ( <NUM_LIT:2> * _THRESHOLD ) <EOL> assert len ( test_handler . records ) == <NUM_LIT:1> <EOL> [ record ] = test_handler . records <EOL> assert record . message == '<STR_LIT>' <EOL> def test_logged_if_slow_did_not_reached ( test_handler ) : <EOL> with test_handler . applicationbound ( ) : <EOL> with logged_if_slow ( '<STR_LIT>' , threshold = _THRESHOLD ) : <EOL> sleep ( _THRESHOLD / <NUM_LIT:2> ) <EOL> assert len ( test_handler . records ) == <NUM_LIT:0> <EOL> def test_logged_if_slow_logger ( ) : <EOL> logger = Mock ( ) <EOL> with logged_if_slow ( '<STR_LIT>' , threshold = _THRESHOLD , logger = logger ) : <EOL> sleep ( <NUM_LIT:2> * _THRESHOLD ) <EOL> assert logger . log . call_args == call ( logbook . DEBUG , '<STR_LIT>' ) <EOL> def test_logged_if_slow_level ( test_handler ) : <EOL> with test_handler . applicationbound ( ) : <EOL> with logged_if_slow ( '<STR_LIT>' , threshold = _THRESHOLD , <EOL> level = logbook . WARNING ) : <EOL> sleep ( <NUM_LIT:2> * _THRESHOLD ) <EOL> assert test_handler . records [ <NUM_LIT:0> ] . level == logbook . WARNING <EOL> def test_logged_if_slow_deprecated ( logger , test_handler ) : <EOL> with test_handler . applicationbound ( ) : <EOL> with logged_if_slow ( '<STR_LIT>' , threshold = _THRESHOLD , <EOL> func = logbook . error ) : <EOL> sleep ( <NUM_LIT:2> * _THRESHOLD ) <EOL> assert test_handler . records [ <NUM_LIT:0> ] . level == logbook . ERROR <EOL> assert test_handler . records [ <NUM_LIT:0> ] . message == '<STR_LIT>' <EOL> with pytest . raises ( TypeError ) : <EOL> logged_if_slow ( '<STR_LIT>' , logger = logger , func = logger . error ) <EOL> def test_deprecated_func_called ( capture ) : <EOL> assert deprecated_func ( <NUM_LIT:1> , <NUM_LIT:2> ) == <NUM_LIT:3> <EOL> def test_deprecation_message ( capture ) : <EOL> deprecated_func ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> [ record ] = capture . records <EOL> assert "<STR_LIT>" in record . message <EOL> assert '<STR_LIT>' in record . message <EOL> def test_deprecation_with_message ( capture ) : <EOL> @ deprecated ( "<STR_LIT>" ) <EOL> def func ( a , b ) : <EOL> return a + b <EOL> func ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> [ record ] = capture . records <EOL> assert "<STR_LIT>" in record . message <EOL> assert "<STR_LIT>" in record . message <EOL> def test_no_deprecations ( capture ) : <EOL> @ deprecated ( '<STR_LIT>' ) <EOL> def func ( a , b ) : <EOL> return a + b <EOL> with suppressed_deprecations ( ) : <EOL> assert func ( <NUM_LIT:1> , <NUM_LIT:2> ) == <NUM_LIT:3> <EOL> assert not capture . records <EOL> def _no_decorator ( func ) : <EOL> return func <EOL> @ pytest . mark . parametrize ( '<STR_LIT>' , [ _no_decorator , classmethod ] ) <EOL> def test_class_deprecation ( capture , decorator ) : <EOL> class Bla ( object ) : <EOL> @ deprecated ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def func ( self , a , b ) : <EOL> assert isinstance ( self , Bla ) <EOL> return a + b <EOL> assert Bla ( ) . func ( <NUM_LIT:2> , <NUM_LIT:4> ) == <NUM_LIT:6> <EOL> [ record ] = capture . records <EOL> assert '<STR_LIT>' in record . message <EOL> def test_deprecations_different_sources ( capture ) : <EOL> def f ( ) : <EOL> deprecated_func ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> def g ( ) : <EOL> deprecated_func ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> f ( ) <EOL> g ( ) <EOL> assert len ( capture . records ) == <NUM_LIT:2> <EOL> def test_deprecations_same_sources ( capture ) : <EOL> def f ( ) : <EOL> deprecated_func ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> f ( ) <EOL> f ( ) <EOL> assert len ( capture . records ) == <NUM_LIT:1> <EOL> def test_deprecation_message_different_sources ( capture ) : <EOL> def f ( flag ) : <EOL> if flag : <EOL> log_deprecation_message ( '<STR_LIT>' ) <EOL> else : <EOL> log_deprecation_message ( '<STR_LIT>' ) <EOL> f ( True ) <EOL> f ( False ) <EOL> assert len ( capture . records ) == <NUM_LIT:2> <EOL> def test_deprecation_message_same_sources ( capture ) : <EOL> def f ( flag ) : <EOL> if flag : <EOL> log_deprecation_message ( '<STR_LIT>' ) <EOL> else : <EOL> log_deprecation_message ( '<STR_LIT>' ) <EOL> f ( True ) <EOL> f ( True ) <EOL> assert len ( capture . records ) == <NUM_LIT:1> <EOL> def test_deprecation_message_full_warning ( capture ) : <EOL> def f ( ) : <EOL> log_deprecation_message ( '<STR_LIT>' ) <EOL> f ( ) <EOL> [ record ] = capture . records <EOL> assert record . message == '<STR_LIT>' <EOL> def test_name_doc ( ) : <EOL> @ deprecated <EOL> def some_func ( ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> assert some_func . __name__ == '<STR_LIT>' <EOL> assert '<STR_LIT>' in some_func . __doc__ <EOL> def test_doc_update ( ) : <EOL> @ deprecated ( '<STR_LIT>' ) <EOL> def some_func ( ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> some_func . __doc__ = '<STR_LIT>' <EOL> assert '<STR_LIT>' not in some_func . __doc__ <EOL> assert '<STR_LIT>' in some_func . __doc__ <EOL> assert '<STR_LIT>' in some_func . __doc__ <EOL> def test_deprecatd_docstring ( ) : <EOL> message = "<STR_LIT>" <EOL> @ deprecated ( ) <EOL> def some_func ( ) : <EOL> """<STR_LIT>""" <EOL> @ deprecated ( message ) <EOL> def other_func ( ) : <EOL> """<STR_LIT>""" <EOL> assert "<STR_LIT>" in some_func . __doc__ <EOL> assert "<STR_LIT>" . format ( message ) in other_func . __doc__ <EOL> @ pytest . fixture <EOL> def capture ( request ) : <EOL> handler = logbook . TestHandler ( level = logbook . WARNING ) <EOL> handler . push_application ( ) <EOL> @ request . addfinalizer <EOL> def pop ( ) : <EOL> handler . pop_application ( ) <EOL> return handler <EOL> @ deprecated <EOL> def deprecated_func ( a , b ) : <EOL> return a + b <EOL> @ pytest . fixture ( autouse = True ) <EOL> def forget_locations ( ) : <EOL> forget_deprecation_locations ( ) </s>
