def generate_record(self): adoption = None if self.nested_count or chance.boolean(likelihood=70): immunizations = [] for i in range(0, self.nested_count or random.randint(0, 4)): immunizations.append({ 'type': chance.pickone( ['FIV', 'Panleukopenia', 'Rabies', 'Feline Leukemia']), 'date_administered': chance.date(minyear=2012).isoformat() }) adoption = { 'adopted_on': chance.date(minyear=2012).isoformat(), 'was_foster': chance.boolean(), 'immunizations': immunizations } return { 'id': self.id, 'name': fake.first_name(), 'pattern': chance.pickone(['Tabby', 'Tuxedo', 'Calico', 'Tortoiseshell']), 'age': random.randint(1, 15), 'adoption': adoption }
def generate_record(self): value_null = None value_integer = random.randint(-314159265359, 314159265359) value_integer_as_number = float( random.randint(-314159265359, 314159265359)) value_number = random.uniform(-314159265359, 314159265359) value_boolean = chance.boolean() value_date_time_string = chance.date(minyear=2012).isoformat() value_array = [] for i in range(random.randint(0, 1000)): value_array.append(random.randint(-314, 314)) value_object = { 'i': random.randint(-314159265359, 314159265359), 'n': random.uniform(-314159265359, 314159265359), 'b': chance.boolean() } return { 'every_type': chance.pickone([ value_null, value_integer, value_integer_as_number, value_number, value_boolean, value_date_time_string, value_array, value_object ]), 'number_which_only_comes_as_integer': value_integer }
def __init__(self, n, starting_id): FakeStream.__init__(self, n) self.starting_id = starting_id self.changing_literal_type = chance.pickone( ['integer', 'number', 'boolean', 'string', 'date-time']) type_def = {'type': self.changing_literal_type} if self.changing_literal_type == 'date-time': type_def = {'type': 'string', 'format': 'date-time'} print('TypeChangeStream chose:', type_def, 'id starting at:', self.id) self.schema = { 'type': 'SCHEMA', 'stream': 'root', 'schema': { 'additionalProperties': False, 'properties': { 'id': { 'type': 'integer' }, 'changing_literal_type': type_def } }, 'key_properties': ['id'] }
def run(process_id, input_run_time, mongodb_ip_address, mongodb_port, queue): # Setup collection = connect_to_test_collection(mongodb_ip_address, mongodb_port, process_id) first_name_array = initialise_data(100) # Get all scenario functions test_scenarios = dir(scenarios)[9:] number_of_operations = {} initial_time = time.time() while True: chosen_scenario = chance.pickone(test_scenarios) # Increment count of number of times a scenario has been called if chosen_scenario not in number_of_operations: number_of_operations[chosen_scenario] = 1 else: number_of_operations[chosen_scenario] += 1 data = generate_data(first_name_array) scenario = getattr(scenarios, chosen_scenario) scenario(collection, data) # Calculate run time current_time = time.time() total_time = current_time - initial_time if (total_time > input_run_time): break cleanup_database(collection) # Store results queue.put(number_of_operations)
def random_array_schema(): length_of_path = random.randint(1, 50) path = [] schema = { 'type': json_schema.ARRAY, 'items': { 'type': chance.pickone([ json_schema.BOOLEAN, json_schema.INTEGER, json_schema.NUMBER, json_schema.STRING ]) } } for _ in range(0, length_of_path): field = chance.string(pool='', length=0) schema = { 'type': json_schema.ARRAY, 'items': { 'type': json_schema.OBJECT, 'properties': { field: schema } } } path.append(field) schema = {'type': json_schema.OBJECT, 'properties': {'root': schema}} path.append('root') return {'schema': schema, 'path': path[::-1]}
def fake_conjunctive_text(n): t = fake.text() for i in range(0, n): t = '{}, {} {}'.format( t[:-1], chance.pickone(['and', 'or', 'for', 'nor', 'but', 'yet', 'so']), fake.text()) return t
def generate_record(self): record = CatStream.generate_record(self) if chance.boolean(likelihood=50): record['adoption'] = ['invalid', 'adoption'] elif chance.boolean(likelihood=50): record['age'] = 'very invalid age' elif record['adoption'] and chance.boolean(likelihood=50): record['adoption']['immunizations'] = { 'type': chance.pickone(['a', 'b', 'c']), 'date_administered': ['clearly', 'not', 'a', 'date'] } else: record['name'] = 22 / 7 return record
def generate_data(input_names): # Mandatory data output_data = {} output_data["first_name"] = chance.pickone(input_names) output_data["last_name"] = chance.last() output_data["age"] = chance.age() output_data["email"] = chance.email() # Optional data optional_data = ["phone", "twitter", "country"] number_of_optional_data = random.randint(0, len(optional_data)) for _ in range(number_of_optional_data): # Picks a random data to input random_index = random.randint(0, len(optional_data) - 1) random_choice = optional_data.pop(random_index) random_method = getattr(chance, random_choice) output_data[random_choice] = random_method() return output_data
def generate_record(self): value = None if self.changing_literal_type == 'integer': value = random.randint(-314159265359, 314159265359) elif self.changing_literal_type == 'number': value = chance.pickone([ random.uniform(-314159265359, 314159265359), float(random.randint(-314159265359, 314159265359)), random.randint(-314159265359, 314159265359) ]) elif self.changing_literal_type == 'boolean': value = chance.boolean() elif self.changing_literal_type == 'string': value = chance.date(minyear=2012).isoformat() elif self.changing_literal_type == 'date-time': value = chance.date(minyear=2012).isoformat() else: raise Exception('Unknown changing_literal_type: `{}`'.format( self.changing_literal_type)) return { 'id': self.id + self.starting_id, 'changing_literal_type': value, }