Showing
2 changed files
with
145 additions
and
0 deletions
This diff could not be displayed because it is too large.
op_robot_tests/tests_files/op_faker.py
0 → 100644
| 1 | +# -*- coding: utf-8 -*- | |
| 2 | +from faker import Faker | |
| 3 | +from faker.providers import BaseProvider | |
| 4 | +from munch import Munch | |
| 5 | +from json import load | |
| 6 | +import os | |
| 7 | + | |
| 8 | + | |
| 9 | +def load_data_from_file(file_name): | |
| 10 | + if not os.path.exists(file_name): | |
| 11 | + file_name = os.path.join(os.path.dirname(__file__), 'data', file_name) | |
| 12 | + with open(file_name) as file_obj: | |
| 13 | + if file_name.endswith(".json"): | |
| 14 | + return Munch.fromDict(load(file_obj)) | |
| 15 | + elif file_name.endswith(".yaml"): | |
| 16 | + return fromYAML(file_obj) | |
| 17 | + | |
| 18 | + | |
| 19 | +class OP_Provider(BaseProvider): | |
| 20 | + __fake_data = load_data_from_file("faker_data.json") | |
| 21 | + word_list = __fake_data.words | |
| 22 | + procuringEntities = __fake_data.procuringEntities | |
| 23 | + addresses = __fake_data.addresses | |
| 24 | + classifications = __fake_data.classifications | |
| 25 | + units = __fake_data.units | |
| 26 | + cpvs = __fake_data.cpvs | |
| 27 | + items_base_data = __fake_data.items_base_data | |
| 28 | + | |
| 29 | + | |
| 30 | + @classmethod | |
| 31 | + def randomize_nb_elements(self, number=10, le=60, ge=140): | |
| 32 | + """ | |
| 33 | + Returns a random value near number. | |
| 34 | + | |
| 35 | + :param number: value to which the result must be near | |
| 36 | + :param le: lower limit of randomizing (percents). Default - 60 | |
| 37 | + :param ge: upper limit of randomizing (percents). Default - 140 | |
| 38 | + :returns: a random int in range [le * number / 100, ge * number / 100] | |
| 39 | + with minimum of 1 | |
| 40 | + """ | |
| 41 | + if le > ge: | |
| 42 | + raise Exception("Lower bound: {} is greater then upper: {}.".format(le, ge)) | |
| 43 | + return int(number * self.random_int(min=le, max=ge) / 100) + 1 | |
| 44 | + | |
| 45 | + | |
| 46 | + @classmethod | |
| 47 | + def word(self): | |
| 48 | + """ | |
| 49 | + :example 'Курка' | |
| 50 | + """ | |
| 51 | + return self.random_element(self.word_list) | |
| 52 | + | |
| 53 | + | |
| 54 | + @classmethod | |
| 55 | + def words(self, nb=3): | |
| 56 | + """ | |
| 57 | + Generate an array of random words | |
| 58 | + :example: array('Надіньте', 'фуражка', 'зелено') | |
| 59 | + :param nb: how many words to return | |
| 60 | + """ | |
| 61 | + return [self.word() for _ in range(0, nb)] | |
| 62 | + | |
| 63 | + | |
| 64 | + @classmethod | |
| 65 | + def sentence(self, nb_words=5, variable_nb_words=True): | |
| 66 | + """ | |
| 67 | + Generate a random sentence | |
| 68 | + :example: 'Курка надіньте пречудовий зелено на.' | |
| 69 | + :param nb_words: how many words the sentence should contain | |
| 70 | + :param variable_nb_words: set to false if you want exactly $nbWords returned, | |
| 71 | + otherwise $nbWords may vary by +/-40% with a minimum of 1 | |
| 72 | + """ | |
| 73 | + if nb_words <= 0: | |
| 74 | + return '' | |
| 75 | + | |
| 76 | + if variable_nb_words: | |
| 77 | + nb_words = self.randomize_nb_elements(number=nb_words) | |
| 78 | + | |
| 79 | + words = self.words(nb_words) | |
| 80 | + words[0] = words[0].title() | |
| 81 | + | |
| 82 | + return " ".join(words) + '.' | |
| 83 | + | |
| 84 | + | |
| 85 | + @classmethod | |
| 86 | + def title(self): | |
| 87 | + return self.sentence(nb_words = 3) | |
| 88 | + | |
| 89 | + | |
| 90 | + @classmethod | |
| 91 | + def description(self): | |
| 92 | + return self.sentence(nb_words = 10) | |
| 93 | + | |
| 94 | + | |
| 95 | + @classmethod | |
| 96 | + def procuringEntity(self): | |
| 97 | + return self.random_element(self.procuringEntities) | |
| 98 | + | |
| 99 | + | |
| 100 | + @classmethod | |
| 101 | + def cpv(self): | |
| 102 | + return self.random_element(self.cpvs) | |
| 103 | + | |
| 104 | + | |
| 105 | + @classmethod | |
| 106 | + def fake_item(self, cpv_group=None): | |
| 107 | + """ | |
| 108 | + Generate a random item for openprocurement tenders | |
| 109 | + | |
| 110 | + :param cpv_group: gives possibility to generate items | |
| 111 | + from one cpv group. Cpv group is three digits in the beginning | |
| 112 | + of each cpv id. | |
| 113 | + """ | |
| 114 | + if cpv_group is None: | |
| 115 | + item_base_data = self.random_element(self.items_base_data) | |
| 116 | + else: | |
| 117 | + cpv_group = str(cpv_group) | |
| 118 | + similar_cpvs = [] | |
| 119 | + for cpv_element in self.cpvs: | |
| 120 | + if cpv_element.startswith(cpv_group): | |
| 121 | + similar_cpvs.append(cpv_element) | |
| 122 | + cpv = self.random_element(similar_cpvs) | |
| 123 | + for entity in self.items_base_data: | |
| 124 | + if entity["cpv_id"] == cpv: | |
| 125 | + item_base_data = entity | |
| 126 | + break | |
| 127 | + | |
| 128 | + # choose appropriate dkpp classification for item_base_data's cpv | |
| 129 | + for entity in self.classifications: | |
| 130 | + if entity["classification"]["id"] == item_base_data["cpv_id"]: | |
| 131 | + classification = entity | |
| 132 | + break | |
| 133 | + | |
| 134 | + address = self.random_element(self.addresses) | |
| 135 | + unit = self.random_element(self.units) | |
| 136 | + item = { | |
| 137 | + "description": item_base_data["description"], | |
| 138 | + "classification": classification["classification"], | |
| 139 | + "additionalClassifications": classification["additionalClassifications"], | |
| 140 | + "deliveryAddress": address["deliveryAddress"], | |
| 141 | + "deliveryLocation": address["deliveryLocation"], | |
| 142 | + "unit": item_base_data["unit"], | |
| 143 | + "quantity": self.randomize_nb_elements(number=item_base_data["quantity"], le=80, ge=120) | |
| 144 | + } | |
| 145 | + return item | ... | ... |
Please
register
or
login
to post a comment