def email_generator(_): """Generator for email addresses.""" domain = choices_as_string(string.ascii_lowercase, k=5) tld = random.choice(("com", "net", "nl", "de", "co.uk")) name = choices_as_string(list(string.digits + string.ascii_lowercase), k=random.randint(4, 10)) return Result(f"{name}@{domain}.{tld}")
def interval_generator(_): """Generator for https://www.postgresql.org/docs/current/datatype-datetime.html""" # We're generating the year separately. year = f"{random.randint(1, 100)} years" value = _time_const_generator( " %m months %d days %H hours %M minutes %S seconds") return Result(year + value.replace(" 0", " "))
def bit_generator(column): """Generator for https://www.postgresql.org/docs/current/datatype-bit.html""" length = column.max_length if column.data_type == "bit varying": # Length only has an upper bound. length = random.randint(1, length) value = f"B'{random.getrandbits(length):0{length}b}'" return Result(value, use_repr=False)
def array_generator(column): """ Generator for https://www.postgresql.org/docs/current/arrays.html Note: This only offers partial support for arrays due to type inference issues. """ # This is not fully supported because postgresql's arrays # are horrible (no type inference for N-dim arrays). d_type = column.udt_name.lower()[:-2] if d_type == "character": # Problematic, because character arrays are capped at 1 char each. generator = lambda _: Result(get_random_string(1)) else: try: generator = get_generator(d_type) except KeyError: return Result("{}") # Mangle the data-type for the pass-through func. column.data_type = d_type elements = "{" + ", ".join(f"\"{generator(column).raw}\"" for _ in range(random.randint(1, 20))) + "}" return Result(elements)
def first_and_last_name_generator(_): """Generator for full names.""" first = _name_generator() last = _name_generator(is_first=False) return Result(first + " " + last)
def last_name_generator(_): """Generator for randomised last names.""" return Result(_name_generator(is_first=False))
def first_name_generator(_): """Generator for randomised first names.""" return Result(_name_generator(is_first=True))
def numeric_generator(_): """Generator for https://www.postgresql.org/docs/current/datatype-numeric.html""" # Technically NUMERIC supports up to 131072 digits past the decimal point. return Result( f'{random.randint(0, 1_000_000_000):d}.{random.randint(0, 10000000):d}' )
def bigint_generator(_): """Generator for https://www.postgresql.org/docs/current/datatype-numeric.html""" return Result(_generate_integer(-9223372036854775808, 9223372036854775807))
def integer_generator(_): """Generator for https://www.postgresql.org/docs/current/datatype-numeric.html""" return Result(_generate_integer(-2147483648, 2147483647))
def money_generator(_): """Generator for https://www.postgresql.org/docs/current/datatype-money.html""" return Result( _generate_integer(-92233720368547758.08, 92233720368547758.07))
def timestamp_generator(_): """Generator for https://www.postgresql.org/docs/current/datatype-datetime.html""" return Result(_time_const_generator(fmt="%Y-%m-%d %H:%M:%S"))
def bytea_generator(_): """Generator for https://www.postgresql.org/docs/current/datatype-binary.html""" value = fr"'\x{''.join(f'{i:02x}' for i in random.randbytes(10))}'" return Result(value, use_repr=False)
def boolean_generator(_): """Generator for https://www.postgresql.org/docs/current/datatype-boolean.html""" return Result(random.choice(("TRUE", "FALSE")), use_repr=False)
def uuid_generator(_): """Generator for https://www.postgresql.org/docs/current/datatype-uuid.html""" return Result(str(uuid4()))
def smallint_generator(_): """Generator for https://www.postgresql.org/docs/current/datatype-numeric.html""" return Result(_generate_integer(-32768, 32767))
def phone_generator(_): """Generator for phone number strings with plus prefixed country code.""" numbers = choices_as_string(list(range(1, 9)), k=12) country = random.choice(["49", "53", "10", "11", "43"]) return Result(f"+{country} {numbers}")
"""Generator for https://www.postgresql.org/docs/current/datatype-datetime.html""" # We're generating the year separately. year = f"{random.randint(1, 100)} years" value = _time_const_generator( " %m months %d days %H hours %M minutes %S seconds") return Result(year + value.replace(" 0", " ")) def text_generator(column): """Generator for https://www.postgresql.org/docs/current/datatype-character.html""" if length := column.max_length: length = random.randint(1, length) else: length = random.randint(60, 300) return Result(get_random_string(length)) def smallint_generator(_): """Generator for https://www.postgresql.org/docs/current/datatype-numeric.html""" return Result(_generate_integer(-32768, 32767)) def integer_generator(_): """Generator for https://www.postgresql.org/docs/current/datatype-numeric.html""" return Result(_generate_integer(-2147483648, 2147483647)) def bigint_generator(_): """Generator for https://www.postgresql.org/docs/current/datatype-numeric.html""" return Result(_generate_integer(-9223372036854775808, 9223372036854775807))