def test_combine_lists(): s1 = suppliers.values({'data': ['a', 'b', 'c']}) s2 = suppliers.values({'data': [1, 2, 3, 4, 5]}) s3 = suppliers.values({'data': ['foo', 'bar', 'baz', 'bin', 'oof']}) combo = suppliers.combine([s1, s2, s3]) assert combo.next(0) == 'a1foo' assert combo.next(1) == 'b2bar' assert combo.next(2) == 'c3baz'
def test_count_param_valid(): spec = {'foo?count=2': ['A', 'B', 'C', 'D']} updated = preprocess_spec(spec) supplier = suppliers.values(updated['foo']) first = supplier.next(0) assert type(first) == list assert ['A', 'B'] == first
def _get_most_common_keys(spec, iterations, num_keys_to_collect): supplier = suppliers.values(spec) data = [supplier.next(i) for i in range(iterations)] counter = Counter(data) most_common_keys = [ item[0] for item in counter.most_common(num_keys_to_collect) ] return most_common_keys
def test_buffered_supplier_size_one(): """ Tests simple case but with a buffer of size one """ wrapped = suppliers.values( builder.values(['a', 'b', 'c', 'd', 'e', 'f', 'g'], sample=True)) buffered = datacraft.supplier.common.BufferedValueSupplier(wrapped, buffer_size=1) for i in range(10): value = buffered.next(i) assert value == buffered.next(i)
def test_buffered_supplier_one_behind(): """ Tests simple case that multiple calls to the same index will return the same result """ wrapped = suppliers.values( builder.values(['a', 'b', 'c', 'd', 'e', 'f', 'g'], sample=True)) buffered = datacraft.supplier.common.BufferedValueSupplier(wrapped, buffer_size=3) for i in range(10): value = buffered.next(i) assert value == buffered.next(i) assert value == buffered.next(i)
def test_weighted_ref_missing_key(): ref_weights = { 'foo': 0.5, 'bar': 0.4, 'baz': 0.1, } spec = builder.single_field('field', builder.weighted_ref(ref_weights)) \ .add_ref('foo', ['foo']) \ .add_ref('bar', 'bar') \ .add_ref('baz', {'baz': 0.999}) \ .build() key_supplier = suppliers.values(['foo', 'bar', 'baz', 'notvalid']) values_map = { key: suppliers.values(value) for key, value in spec['refs'].items() } supplier = weighted_ref_supplier(key_supplier, values_map) with pytest.raises(SupplierException) as exception: [supplier.next(i) for i in range(0, 10)] assert "Unknown Key 'notvalid' for Weighted Reference" in str( exception.value)
def test_buffered_supplier_more_than_one_behind(): """ Tests that we will the buffered gets filled in an expected way """ wrapped = suppliers.values( builder.values(['a', 'b', 'c', 'd', 'e', 'f', 'g'], sample=True)) buffered = datacraft.supplier.common.BufferedValueSupplier(wrapped, buffer_size=4) vals = [buffered.next(i) for i in range(5)] # buffer only holds 4 items so should only be able to reach back to third element assert buffered.next(4) == vals[-1] assert buffered.next(3) == vals[-2] assert buffered.next(2) == vals[-3] assert buffered.next(1) == vals[-4] with pytest.raises(ValueError): buffered.next(0)
def _test_invalid_spec(spec, key): updated = preprocess_spec(spec) with pytest.raises(SpecException): suppliers.values(updated[key])
def test_count_param_invalid(): # the word two is not a valid count spec = {'foo?count=two': ['A', 'B', 'C', 'D']} updated = preprocess_spec(spec) with pytest.raises(ValueError): suppliers.values(updated['foo'])