Generate pairs of date-time and isolated date-time

This commit is contained in:
Sebastian Böckelmann 2024-04-17 17:29:24 +02:00
parent 805d72a593
commit 90e08dad2e
4 changed files with 60 additions and 25 deletions

View File

@ -1,10 +1,10 @@
CREATE TABLE `test` (
`id` bigint(20) NOT NULL,
`test_value` int(11) NOT NULL,
`test_string` varchar(255) DEFAULT NULL,
`local_date` date DEFAULT NULL,
`starting_time` time DEFAULT NULL,
`ending_time` time DEFAULT NULL,
`starting_date_time` datetime(6) DEFAULT NULL,
`ending_date_time` datetime(6) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci

View File

@ -17,14 +17,14 @@ max_int = 2147483647
min_date = datetime.date(2023, 1, 1)
max_date = datetime.date(2024, 12, 31)
min_time = datetime.time(0,0,0)
min_time = datetime.time(0, 0, 0)
max_time = datetime.time(23, 59, 59)
def generate_random_insert(sql_table: SQLTable):
columnValues = []
for column in sql_table.columns:
columnValues.append(generate_random_column(column, columnValues))
columnValues.append(generate_random_column(column, columnValues, True))
sql_insert_command = "INSERT INTO " + sql_table.table_name + " VALUES (${i});"
for columnValue in columnValues:
@ -37,7 +37,7 @@ def generate_random_insert(sql_table: SQLTable):
return sql_insert_command
def generate_random_column(sql_column: SQLColumn, generated_values: List[ColumnValue]):
def generate_random_column(sql_column: SQLColumn, generated_values: List[ColumnValue], fixed_date_time: bool = False):
if sql_column.columnType == SQLColumnType.BIGINT:
return ColumnValue(random.randint(min_bigint, max_bigint), ValueType.INT, sql_column)
elif sql_column.columnType == SQLColumnType.INT:
@ -47,41 +47,73 @@ def generate_random_column(sql_column: SQLColumn, generated_values: List[ColumnV
lorem_text = lorem.sentence()
return ColumnValue(lorem_text[:text_legnth], ValueType.STRING, sql_column)
elif sql_column.columnType == SQLColumnType.DATE:
if "end" in sql_column.columnName or "max" in sql_column.columnName:
if "end" in sql_column.columnName or "max" in sql_column.columnName and sql_column.columnType == SQLColumnType.DATE:
for value in generated_values:
if "start" in value.sqlColumn.columnName or "min" in sql_column.columnName:
if ("start" in value.sqlColumn.columnName or "min" in sql_column.columnName) and sql_column.columnType == SQLColumnType.DATE:
startingDate = datetime.datetime.strptime(value.value, "%Y-%m-%d").date()
return generate_random_date(startingDate, max_date, sql_column)
elif "start" in sql_column.columnName or "min" in sql_column.columnName:
return ColumnValue(str(generate_random_date(startingDate, max_date)), ValueType.STRING, sql_column)
elif "start" in sql_column.columnName or "min" in sql_column.columnName and sql_column.columnType == SQLColumnType.DATE:
for value in generated_values:
if "end" in value.sqlColumn.columnName or "max" in sql_column.columnName:
if ("end" in value.sqlColumn.columnName or "max" in sql_column.columnName) and sql_column.columnType == SQLColumnType.DATE:
endingDate = datetime.datetime.strptime(value.value, "%Y-%m-%d").date()
return generate_random_date(min_date, endingDate, sql_column)
return generate_random_date(min_date, max_date, sql_column)
return ColumnValue(str(generate_random_date(min_date, endingDate)), ValueType.STRING, sql_column)
return ColumnValue(str(generate_random_date(min_date, max_date)), ValueType.STRING, sql_column)
elif sql_column.columnType == SQLColumnType.TIME:
if "end" in sql_column.columnName or "max" in sql_column.columnName:
if "end" in sql_column.columnName or "max" in sql_column.columnName and sql_column.columnType == SQLColumnType.TIME:
for value in generated_values:
if "start" in value.sqlColumn.columnName or "min" in sql_column.columnName:
if ("start" in value.sqlColumn.columnName or "min" in sql_column.columnName) and sql_column.columnType == SQLColumnType.TIME:
startingTime = datetime.datetime.strptime(value.value, "%H:%M:%S").time()
return generate_random_time(startingTime, max_time, sql_column)
elif "start" in sql_column.columnName or "min" in sql_column.columnName:
return ColumnValue(str(generate_random_time(startingTime, max_time)), ValueType.STRING, sql_column)
elif "start" in sql_column.columnName or "min" in sql_column.columnName and sql_column.columnType == SQLColumnType.TIME:
for value in generated_values:
if "end" in value.sqlColumn.columnName or "max" in sql_column.columnName:
if ("end" in value.sqlColumn.columnName or "max" in sql_column.columnName) and value.sqlColumn.columnType == SQLColumnType.TIME:
endingTime = datetime.datetime.strptime(value.value, "%H:%M:%S").time()
return generate_random_time(min_time, endingTime, sql_column)
return generate_random_time(min_time, max_time, sql_column)
return ColumnValue(str(generate_random_time(min_time, endingTime)), ValueType.STRING, sql_column)
return ColumnValue(str(generate_random_time(min_time, max_time)), ValueType.STRING, sql_column)
elif sql_column.columnType == SQLColumnType.DATETIME:
if "end" in sql_column.columnName or "max" in sql_column.columnName and sql_column.columnType == SQLColumnType.DATETIME:
for value in generated_values:
if ("start" in value.sqlColumn.columnName or "min" in sql_column.columnName) and value.sqlColumn.columnType == SQLColumnType.DATETIME:
startingDate = datetime.datetime.strptime(value.value, "%Y-%m-%d %H:%M:%S").date()
startingTime = datetime.datetime.strptime(value.value, "%Y-%m-%d %H:%M:%S").time()
if fixed_date_time:
return ColumnValue(str(generate_random_date_time_fixed_date(startingDate, startingTime, max_time)), ValueType.STRING, sql_column)
else:
return ColumnValue(str(generate_random_date_time(startingDate, startingTime, max_date, max_time)), ValueType.STRING, sql_column)
elif "start" in sql_column.columnName or "min" in sql_column.columnName and sql_column.columnType == SQLColumnType.DATETIME:
for value in generated_values:
if ("end" in value.sqlColumn.columnName or "max" in sql_column.columnName) and value.sqlColumn == SQLColumnType.DATETIME:
endingDate = datetime.datetime.strptime(value.value, "%Y-%m-%d %H:%M:%S").date()
endingTime = datetime.datetime.strptime(value.value, "%Y-%m-%d %H:%M:%S").time()
if fixed_date_time:
return ColumnValue(str(generate_random_date_time_fixed_date(endingDate, min_time, endingTime)), ValueType.STRING, sql_column)
else:
return ColumnValue(str(generate_random_date_time(min_date, min_time, endingDate, endingTime)), ValueType.STRING, sql_column)
return ColumnValue(str(generate_random_date_time(min_date, min_time, max_date, max_time)), ValueType.STRING, sql_column)
def generate_random_date(start_date, end_date, sql_column):
def generate_random_date(start_date, end_date):
delta = end_date - start_date
random_days = random.randrange(delta.days + 1)
random_date = start_date + datetime.timedelta(days=random_days)
return ColumnValue(str(random_date), ValueType.STRING, sql_column)
return start_date + datetime.timedelta(days=random_days)
def generate_random_time(start_time, end_time, sql_column):
def generate_random_time(start_time, end_time):
hours = random.randint(start_time.hour, end_time.hour)
minutes = random.randint(start_time.minute, end_time.minute)
seconds = random.randint(start_time.second, end_time.second)
random_time = datetime.time(hours, minutes, seconds)
return ColumnValue(str(random_time), ValueType.STRING, sql_column)
return datetime.time(hours, minutes, seconds)
def generate_random_date_time(start_date, start_time, end_date, end_time):
random_date = generate_random_date(start_date, end_date)
if random_date == start_date:
random_time = generate_random_time(start_time, end_time)
else:
random_time = generate_random_time(min_time, max_time)
return datetime.datetime.combine(random_date, random_time)
def generate_random_date_time_fixed_date(fixed_date, start_time, end_time):
random_time = generate_random_time(start_time, end_time)
return datetime.datetime.combine(fixed_date, random_time)

View File

@ -7,6 +7,7 @@ class SQLColumnType(Enum):
VARCHAR = 'varchar(255)'
DATE = 'date'
TIME = 'time'
DATETIME = 'datetime'
class SQLColumn:

View File

@ -74,6 +74,8 @@ def get_column_type(column_definition: str):
return SQLColumn.SQLColumnType.DATE
elif column_definition_characteristic == 'time':
return SQLColumn.SQLColumnType.TIME
elif column_definition_characteristic == 'datetime(6)':
return SQLColumn.SQLColumnType.DATETIME
def get_column_name(column_definition: str):