Esempio n. 1
0
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}")
Esempio n. 2
0
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", " "))
Esempio n. 3
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)
Esempio n. 4
0
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)
Esempio n. 5
0
def first_and_last_name_generator(_):
    """Generator for full names."""
    first = _name_generator()
    last = _name_generator(is_first=False)
    return Result(first + " " + last)
Esempio n. 6
0
def last_name_generator(_):
    """Generator for randomised last names."""
    return Result(_name_generator(is_first=False))
Esempio n. 7
0
def first_name_generator(_):
    """Generator for randomised first names."""
    return Result(_name_generator(is_first=True))
Esempio n. 8
0
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}'
    )
Esempio n. 9
0
def bigint_generator(_):
    """Generator for https://www.postgresql.org/docs/current/datatype-numeric.html"""
    return Result(_generate_integer(-9223372036854775808, 9223372036854775807))
Esempio n. 10
0
def integer_generator(_):
    """Generator for https://www.postgresql.org/docs/current/datatype-numeric.html"""
    return Result(_generate_integer(-2147483648, 2147483647))
Esempio n. 11
0
def money_generator(_):
    """Generator for https://www.postgresql.org/docs/current/datatype-money.html"""
    return Result(
        _generate_integer(-92233720368547758.08, 92233720368547758.07))
Esempio n. 12
0
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"))
Esempio n. 13
0
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)
Esempio n. 14
0
def boolean_generator(_):
    """Generator for https://www.postgresql.org/docs/current/datatype-boolean.html"""
    return Result(random.choice(("TRUE", "FALSE")), use_repr=False)
Esempio n. 15
0
def uuid_generator(_):
    """Generator for https://www.postgresql.org/docs/current/datatype-uuid.html"""
    return Result(str(uuid4()))
Esempio n. 16
0
def smallint_generator(_):
    """Generator for https://www.postgresql.org/docs/current/datatype-numeric.html"""
    return Result(_generate_integer(-32768, 32767))
Esempio n. 17
0
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}")
Esempio n. 18
0
    """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))