Exemple #1
0
def standard_example_usage(example: dict,
                           num: int,
                           pretty: bool = False,
                           no_reformat: bool = False):
    """builds a single usage from given example spec"""
    if no_reformat:
        formatted_spec = example  # type: ignore
    else:
        formatted_spec = datacraft.preprocess_and_format(
            example)  # type: ignore
    datacraft_format = 'json-pretty' if pretty else 'json'
    command = f'datacraft -s spec.json -i {num} --format {datacraft_format} -x -l off'
    if pretty:
        output = json.dumps(datacraft.entries(example,
                                              num,
                                              enforce_schema=True),
                            indent=4)
    else:
        output = json.dumps(
            datacraft.entries(example, num, enforce_schema=True),
            ensure_ascii=datacraft.registries.get_default('format_json_ascii'))
    return f'Example Spec:\n{formatted_spec}\n{command}\n{output}\n'
Exemple #2
0
def get_reverse_string_usage():
    example = {
        "backwards": {
            "type": "reverse_string",
            "ref": "ANIMALS"
        },
        "refs": {
            "ANIMALS": {
                "type": "values",
                "data": ["zebra", "hedgehog", "llama", "flamingo"]
            }
        }
    }
    example_str = json.dumps(example, indent=4)
    command = 'datacraft -s spec.json -i 5 --format json-pretty -x -l off'
    output = json.dumps(datacraft.entries(example, 5, enforce_schema=True),
                        indent=4)
    return '\n'.join([
        "Reverses output of other suppliers", "Example:", example_str,
        "Command:", command, "Output:", output
    ])
Exemple #3
0
def test_entries_2():
    spec = {
        "super_power": {
            "type": "values",
            "config": {
                "sample": True
            },
            "data": {
                "fast reader": 0.5,
                "wordle expert": 0.4,
                "super strength": 0.05,
                "super speed": 0.01,
                "invisibility": 0.01,
                "indestructible": 0.01,
                "laser eyes": 0.01,
                "teleportation": 0.00000000001
            }
        }
    }
    records = datacraft.entries(spec, 5)
    assert len(records) == 5
Exemple #4
0
def test_verify_class_mappings():
    for key, values in _CLASS_MAPPING.items():
        spec = _cc_abbrev_spec(f"cc-{key}", count=5)
        single_item = datacraft.entries(spec, 1)[0]['name']
        for c in single_item:
            assert c in values, f'{c} not expected to be part of {key} type. values: {values}'
Exemple #5
0
def test_invalid_range_specs(spec):
    with pytest.raises(datacraft.SpecException):
        datacraft.entries(spec, 1)
Exemple #6
0
def test_entries_1():
    entries = datacraft.entries({'foo': ['one', 'two']}, 2)
    assert entries == [{'foo': 'one'}, {'foo': 'two'}]