Passed
Push — master ( f41365...23c626 )
by Amresh
01:22
created

fastest.fuzzers.rest_fuzzer.fuzz   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 27
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
B schema_to_object_builder() 0 14 6
A build_one_of_type() 0 14 5
1
import random
2
from fastest.fuzzers.primitive_fuzzer.fuzz import random_integers, random_ascii_chars, random_float
3
4
5
def build_one_of_type(item_type, p=1.0):
6
    empty_values_for_types = {
7
        "str": random_ascii_chars,
8
        "int": random_integers,
9
        "float": random_float,
10
        "list": lambda: [],
11
        "dict": lambda: {},
12
        "null": lambda: None
13
    }
14
    basic_types = ['str', 'int', 'float', 'list', 'dict', 'null']
15
    mutation = random.random()
16
    return empty_values_for_types[item_type] \
17
        if p > mutation \
18
        else empty_values_for_types[random.choice(basic_types)]
19
20
21
def schema_to_object_builder(schema, p=1.0):
22
    mutation = random.random()
23
    root_object = build_one_of_type(schema['type'])()
24
25
    if isinstance(root_object, list):
26
        if p > mutation:
27
            root_object.append(schema_to_object_builder(schema['inner'], p=p))
28
29
    elif isinstance(root_object, dict):
30
        for prop in schema['inner']:
31
            if p > mutation:
32
                root_object[prop['name']] = schema_to_object_builder(prop, p=p)
33
34
    return root_object
35