Ejemplo n.º 1
0
def test_named_field_groups():
    spec_builder = one_two_three_builder()
    spec_builder.named_field_group(key="groupA", fields=["one", "two"])
    spec_builder.named_field_group(key="groupB", fields=["one", "two", "three"]),
    spec = spec_builder.build()

    provider = key_suppliers.from_spec(spec)
    for _ in range(100):
        field_group, keys = provider.get()
        assert field_group == 'groupA' or field_group == 'groupB'
        assert keys == ['one', 'two'] or keys == ['one', 'two', 'three']
Ejemplo n.º 2
0
def test_weighted_field_groups():
    spec_builder = one_two_three_builder()
    spec_builder.weighted_field_group(weight=0.7, fields=["one", "two"])
    spec_builder.weighted_field_group(weight=0.3, fields=["one", "two", "three"]),
    spec = spec_builder.build()

    provider = key_suppliers.from_spec(spec)
    for _ in range(100):
        field_group, keys = provider.get()
        assert field_group == '0.7' or field_group == '0.3'
        assert keys == ['one', 'two'] or keys == ['one', 'two', 'three']
Ejemplo n.º 3
0
def test_list_of_fields():
    field_groups = [
        ["one"],
        ["one", "two"],
        ["one", "two", "three"]
    ]

    spec_builder = one_two_three_builder()
    spec_builder.add_field_groups(field_groups),
    spec = spec_builder.build()
    provider = key_suppliers.from_spec(spec)

    assert _get_keys(provider) == ['one']
    assert _get_keys(provider) == ['one', 'two']
    assert _get_keys(provider) == ['one', 'two', 'three']
Ejemplo n.º 4
0
def _configure_nested_supplier(spec, loader):
    """ configure the supplier for nested types """
    fields = spec['fields']
    keys = list(fields.keys())
    config = datacraft.utils.load_config(spec, loader)
    count_supplier = datacraft.suppliers.count_supplier(**config)
    if 'field_groups' in spec:
        key_supplier = key_suppliers.from_spec(spec)
    else:
        key_supplier = key_suppliers.from_spec(fields)

    as_list = datacraft.utils.is_affirmative('as_list', config)

    field_supplier_map = {}
    # each non reserved key should have a valid spec as a value
    for key in keys:
        nested_spec = fields[key]
        if 'type' in nested_spec and nested_spec.get('type') == _NESTED_KEY:
            supplier = _configure_nested_supplier(nested_spec, loader)
        else:
            supplier = loader.get_from_spec(nested_spec)
        field_supplier_map[key] = supplier
    return nested_supplier(field_supplier_map, count_supplier, key_supplier,
                           as_list)
Ejemplo n.º 5
0
def test_no_field_groups():
    spec = one_two_builder().build()
    provider = key_suppliers.from_spec(spec)

    assert _get_keys(provider) == ['one', 'two']