This repository has been archived by the owner on Dec 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
factory.py
77 lines (63 loc) · 2.43 KB
/
factory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class Examples(object):
def __init__(self):
self.factories = {}
def make_factory(self, resource_name, example):
def factory(**params):
if not example:
return {}
try:
obj = example.copy()
obj.update(params)
except AttributeError:
raise ValueError("example failed to parse as JSON:\n\n"
"{}".format(example))
return obj
self.factories[resource_name] = factory
def get_factory(self, resource_name):
return self.factories.get(resource_name)
def build(self, resource_name, **kwargs):
return self.factories[resource_name](**kwargs)
# XXX: unused now, but should maybe use it as a helper for
# defining custom factories
class RandomValueGenerator(object):
def __init__(self, params):
self.params = dict(params)
super(RandomValueGenerator, self).__init__()
@classmethod
def generate_value(cls, params):
if 'example' in params:
return params['example']
else:
generator = cls(params)
return generator()
def _random_string(self):
if 'enum' in self.params:
return random.choice(self.params['enum'])
min_length = self.params.get('minLength', 5)
max_length = self.params.get('maxLength', 20)
value_len = random.randint(min_length, max_length)
letters = [random.choice(ascii_letters)
for i in range(value_len)]
return ''.join(letters)
def _random_number(self):
min_val = self.params.get('minimum', 1)
max_val = self.params.get('maximum', 100) - 1
val = random.randint(min_val, max_val)
return val + random.random()
def _random_integer(self):
min_val = self.params.get('minimum', 1)
max_val = self.params.get('maximum', 100)
return random.randint(min_val, max_val)
def _random_date(self):
from datetime import datetime
return datetime.now().isoformat()
def _random_boolean(self):
return random.choice([True, False])
def _random_array(self):
return [self._random_string()]
def _random_object(self):
return {self._random_string(): self._random_string()}
def __call__(self):
type_ = self.params.get('type', 'string')
func = getattr(self, '_random_{}'.format(type_))
return func()