def test_folder_handler(): with temp_value_setting.TempValueSetter( (cute_profile.profile_handling, 'threading'), dummy_threading): with temp_file_tools.create_temp_folder( suffix='_python_toolbox_testing') as temp_folder: f = cute_profile.profile_ready(profile_handler=temp_folder)(func) f(1, 2) assert len(list(temp_folder.iterdir())) == 0 f(1, 2) assert len(list(temp_folder.iterdir())) == 0 f.profiling_on = True f(1, 2) assert len(list(temp_folder.iterdir())) == 1 f(1, 2) assert len(list(temp_folder.iterdir())) == 1 time.sleep(0.01) # To make for a different filename. f.profiling_on = True f(1, 2) assert len(list(temp_folder.iterdir())) == 2 f(1, 2) assert len(list(temp_folder.iterdir())) == 2
def test_time_to_keep(): counting_func.i = 0 # Resetting so we could refer to hard numbers # without worrying whether other tests made `i` higher. f = cache(time_to_keep={'days': 356})(counting_func) print(f('zero')) assert f('zero') == 0 # Just to get rid of zero assert f('a') == 1 assert f('b') == 2 assert f('c') == 3 assert f('b') == 2 start_datetime = datetime_module.datetime.now() fixed_time = start_datetime def _mock_now(): return fixed_time with temp_value_setting.TempValueSetter((caching.decorators, '_get_now'), _mock_now): assert list(map(f, 'abc')) == [1, 2, 3] fixed_time += datetime_module.timedelta(days=100) assert list(map(f, 'abc')) == [1, 2, 3] assert list(map(f, 'def')) == [4, 5, 6] fixed_time += datetime_module.timedelta(days=100) assert list(map(f, 'abc')) == [1, 2, 3] assert list(map(f, 'def')) == [4, 5, 6] fixed_time += datetime_module.timedelta(days=100) assert list(map(f, 'abc')) == [1, 2, 3] assert list(map(f, 'def')) == [4, 5, 6] fixed_time += datetime_module.timedelta(days=100) assert list(map(f, 'abc')) == [7, 8, 9] assert list(map(f, 'def')) == [4, 5, 6] fixed_time += datetime_module.timedelta(days=100) assert list(map(f, 'abc')) == [7, 8, 9] assert list(map(f, 'def')) == [10, 11, 12] assert f(a='d') == f(a='d', b=2) == f('d') == 10 fixed_time += datetime_module.timedelta(days=1000) assert list(map(f, 'abcdef')) == [13, 14, 15, 16, 17, 18] assert f(a='d', b='meow') == 19
def manage_context(self): with temp_value_setting.TempValueSetter( (nifty_collections.bagging, '_count_elements'), nifty_collections.bagging._count_elements_slow): yield self