Exemplo n.º 1
0
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)
Exemplo n.º 2
0
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
Exemplo n.º 3
0
def _configure_ref_supplier(field_spec: dict, loader: datacraft.Loader):
    """ configures supplier for ref type """
    key = None
    if 'data' in field_spec:
        key = field_spec.get('data')
    if 'ref' in field_spec:
        key = field_spec.get('ref')
    if key is None:
        raise datacraft.SpecException('No key found for spec: ' +
                                      json.dumps(field_spec))
    return loader.get(key)
Exemplo n.º 4
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)
Exemplo n.º 5
0
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
Exemplo n.º 6
0
def build_suppliers_map(field_spec: dict, loader: datacraft.Loader) -> dict:
    """uses refs or fields to build a map for those suppliers"""
    if 'refs' not in field_spec and 'fields' not in field_spec:
        raise datacraft.SpecException(
            f'Must define one of fields or refs. {json.dumps(field_spec)}')
    if 'refs' in field_spec and 'fields' in field_spec:
        raise datacraft.SpecException(
            f'Must define only one of fields or refs. {json.dumps(field_spec)}'
        )
    mappings = _get_mappings(field_spec, 'refs')
    mappings.update(_get_mappings(field_spec, 'fields'))
    if len(mappings) < 1:
        raise datacraft.SpecException(
            f'fields or refs empty: {json.dumps(field_spec)}')
    suppliers_map = {}
    for field_or_ref, alias in mappings.items():
        supplier = loader.get(field_or_ref)
        suppliers_map[alias] = supplier
    return suppliers_map