def get_bloom(**overrides): ''' Helper function to easily get a bloom for testing. ''' kwargs = copy(BLOOM_DEFAULTS) kwargs.update(overrides) return TimingBloomFilter(**kwargs)
def get_bloom(temp_path, disable_optimizations=False): # Create a bloom bloom = TimingBloomFilter( capacity=1000, decay_time=86400, data_path=temp_path, disable_optimizations=disable_optimizations, ) # Add a bunch of keys for i in range(100): bloom.add(str(i)) # Check that the bloom is working as expected assert bloom.contains('1') assert bloom.contains('50') assert not bloom.contains('101') return bloom
def test_init_with_bloom_data(exists_mock, load_mock): # Setup test data and mocks exists_mock.return_value = True load_mock.return_value = sentinel.data capacity = 1000 error = 0.002 decay_time = 86400 data_path = '/does/not/exist' # Call init bloom = TimingBloomFilter( capacity=capacity, error=error, decay_time=decay_time, data_path=data_path, ) # Check that the bloom is setup as expected assert_bloom_values( bloom, { 'capacity': capacity, 'error': error, 'data_path': data_path, 'id': None, 'num_bytes': 12935, 'num_hashes': 9, 'ring_size': 15, 'dN': 7, 'seconds_per_tick': 12342.857142857143, '_optimize': True, 'data': sentinel.data, }) test_dp, test_mf, test_bf = bloom._get_paths(None) assert test_dp == '/does/not/exist' assert test_mf == '/does/not/exist/meta.json' assert test_bf == '/does/not/exist/bloom.npy' exists_mock.assert_called_once_with(test_bf) load_mock.assert_called_once_with(test_bf)
def test_init_no_bloom_data(): # Setup test data capacity = 1000 error = 0.002 decay_time = 86400 data_path = '/does/not/exist' id = 5 # Call init and get back a bloom bloom = TimingBloomFilter( capacity=capacity, error=error, decay_time=decay_time, data_path=data_path, id=id, ) # Make sure the bloom is setup as expected assert_bloom_values( bloom, { 'capacity': capacity, 'error': error, 'data_path': data_path, 'id': id, 'num_bytes': 12935, 'num_hashes': 9, 'ring_size': 15, 'dN': 7, 'seconds_per_tick': 12342.857142857143, '_optimize': True, }) test_dp, test_mf, test_bf = bloom._get_paths(None) assert test_dp == '/does/not/exist' assert test_mf == '/does/not/exist/meta.json' assert test_bf == '/does/not/exist/bloom.npy' assert_empty_bloom(bloom)