def _builder_nested(outer_key, inner_key, inner_spec): """ First part builds { innerkey: { some: spec } } Second part wraps first in { outerkey: { type: nested, fields: { innerkey: { some: spec } } } } """ inner = builder.single_field(inner_key, inner_spec).build() return builder.single_field(outer_key, builder.nested(inner)).build()
def test_range_wrap_around(): data = [1, 3] spec = builder.single_field("field:range", data).build() supplier = Loader(spec).get('field') vals = [supplier.next(i) for i in range(4)] assert vals == [1, 2, 3, 1]
def test_config_ref_for_values(): """ verifies that the values ref inherits the config from the config_ref """ spec = builder.single_field("name?config_ref=quoteit", ["bob", "joe", "ann", "sue"]) \ .add_ref("quoteit", builder.config_ref(quote="\"")) \ .build() supplier = Loader(spec).get('name') assert supplier.next(0) == '"bob"'
def test_values_list_order(): data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] spec = builder.single_field('field', data).build() supplier = Loader(spec).get('field') values = [supplier.next(i) for i in range(10)] assert values == data
def test_unicode_range_single_range_as_hex(): field_spec = builder.unicode_range(data=[0x3040, 0x309f], count=5) spec = builder.single_field("text", field_spec).build() supplier = Loader(spec).get('text') first = supplier.next(0) for c in first: assert 0x3040 <= ord(c) <= 0x309f
def test_unicode_no_data_element(): spec = builder.single_field("field", builder.unicode_range(data=None)).build() spec['field'].pop('data') with pytest.raises(SpecException): Loader(spec).get("field")
def test_uuid_valid_schema(key, spec): # for coverage spec = builder.single_field(key, spec).build() loader = Loader(spec, enforce_schema=True) supplier = loader.get('foo') value1 = supplier.next(0) assert UUID_REGEX.match(value1)
def test_unicode_multiple_ranges(): data = [['0x0590', '0x05ff'], ['0x3040', '0x309f']] field_spec = builder.unicode_range(data=data, min=3, max=7) spec = builder.single_field("text", field_spec).build() supplier = Loader(spec).get('text') first = supplier.next(0) assert 3 <= len(first) <= 7 for c in first: assert 0x0590 <= ord(c) <= 0x05ff or 0x3040 <= ord(c) <= 0x309f
def test_nested_range_lists_mixed_types_and_step_cast(): data = [ [0.5, 2.5, 0.5], [20.01234, 30.56789] ] spec = builder.single_field("field:range?cast=str&precision=2", data).build() supplier = Loader(spec).get('field') assert supplier.next(0) == '0.5' assert supplier.next(1) == '20.01'
def test_values_count_as_list(): data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] spec = builder.single_field('field', builder.values(data, count=[2, 3])).build() supplier = Loader(spec).get('field') first = supplier.next(0) assert isinstance(first, list) and len(first) == 2 second = supplier.next(1) assert isinstance(second, list) and len(second) == 3
def test_uuid_spec(): spec = builder.single_field("foo:uuid", {}).build() loader = Loader(spec) supplier = loader.get('foo') value1 = supplier.next(0) assert UUID_REGEX.match(value1) value2 = supplier.next(1) assert UUID_REGEX.match(value2) assert value1 != value2
def test_unicode_range_single_range_as_hex_strings(): field_spec = builder.unicode_range(data=[0x3040, 0x309f], mean=5, stddev=2, min=2, max=7) spec = builder.single_field("text", field_spec).build() supplier = Loader(spec).get('text') first = supplier.next(0) assert 2 <= len(first) <= 7 for c in first: assert 0x3040 <= ord(c) <= 0x309f
def test_nested_range_lists_simple(): data = [ [0, 10], [20, 30] ] spec = builder.single_field("field:range", data).build() supplier = Loader(spec).get('field') first = supplier.next(0) assert 0 <= first <= 10 second = supplier.next(1) assert 20 <= second <= 30
def test_nested_range_lists_mixed_types_and_step(): data = [ [0, 10, 2], [20.0, 30.0] ] spec = builder.single_field("field:range", data).build() supplier = Loader(spec).get('field') first = supplier.next(0) assert first % 2 == 0 assert 0 <= first <= 10 second = supplier.next(1) assert 20.0 <= second <= 30.0
def test_buffered_supplier_from_spec(): """ Tests interpreting specs for buffering """ values_spec = builder.values(['a', 'b', 'c', 'd', 'e', 'f', 'g'], sample=True, buffer_size="20") assert suppliers._is_buffered(**values_spec['config']) data_spec = builder.single_field('field', values_spec).build() loader = Loader(data_spec) supplier = loader.get('field') for i in range(10): value = supplier.next(i) assert value == supplier.next(i)
def test_weighed_ref_count_as_list(): ref_weights = { 'one': 0.5, 'two': 0.4, 'tre': 0.1, } spec = builder.single_field('field', builder.weighted_ref(ref_weights, count=3)) \ .add_ref('one', 'uno') \ .add_ref('two', 'dos') \ .add_ref('tre', 'tres') \ .build() loader = Loader(spec) supplier = loader.get('field') first = supplier.next(0) assert isinstance(first, list) assert len(first) == 3
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_invalid_when_ref_and_data_specified(): spec = builder.single_field( "field?mean=2", builder.select_list_subset(data=["one", "two", "three", "four"], ref="REF")).build() _test_invalid_select_list_spec(spec)
def test_invalid_when_ref_not_defined(): spec = builder.single_field("field:select_list_subset?mean=2", { "ref": "REF" }).build() _test_invalid_select_list_spec(spec)
def test_unicode_data_is_not_list(): spec = builder.single_field( "field", builder.unicode_range(data="0x3040,0x309f")).build() with pytest.raises(SpecException): Loader(spec).get("field")