altered constraint tests to create bad update statements. Tests caught a bug where I was copying the wrong values from the registers

This commit is contained in:
pedrocarlo 2025-04-29 22:48:57 -03:00
parent cf7f60b8f5
commit 3aaf4206b7
3 changed files with 58 additions and 13 deletions

View file

@ -230,12 +230,24 @@ class Table(BaseModel):
return f"INSERT INTO {self.name} VALUES ({vals});"
# These statements should always cause a constraint error as there is no where clause here
def generate_update(self) -> str:
vals = [
f"{col.name} = {col.col_type.generate(fake)}"
for col in self.columns
if col.primary_key
]
vals = ", ".join(vals)
return f"UPDATE {self.name} SET {vals};"
class ConstraintTest(BaseModel):
table: Table
db_path: str = "testing/constraint.db"
insert_stmts: list[str]
insert_errors: list[str]
update_errors: list[str]
def run(
self,
@ -258,6 +270,14 @@ class ConstraintTest(BaseModel):
str(len(self.insert_stmts)),
)
for update_stmt in self.update_errors:
limbo.run_test_fn(
update_stmt,
lambda val: "Runtime error: UNIQUE constraint failed" in val,
)
# TODO: When we implement rollbacks, have a test here to assure the values did not change
def validate_with_expected(result: str, expected: str):
return (expected in result, expected)
@ -281,8 +301,18 @@ def generate_test(col_amount: int, primary_keys: int) -> ConstraintTest:
table = Table(columns=cols, name=fake.word())
insert_stmts = [table.generate_insert() for _ in range(col_amount)]
update_errors = []
if len(insert_stmts) > 1:
update_errors = [
table.generate_update() for _ in table.columns if col.primary_key
]
return ConstraintTest(
table=table, insert_stmts=insert_stmts, insert_errors=insert_stmts
table=table,
insert_stmts=insert_stmts,
insert_errors=insert_stmts,
update_errors=update_errors,
)
@ -296,8 +326,15 @@ def custom_test_1() -> ConstraintTest:
"INSERT INTO users VALUES (1, 'alice');",
"INSERT INTO users VALUES (2, 'bob');",
]
update_stmts = [
"UPDATE users SET id = 3;",
"UPDATE users SET id = 2, username = 'bob' WHERE id == 1;",
]
return ConstraintTest(
table=table, insert_stmts=insert_stmts, insert_errors=insert_stmts
table=table,
insert_stmts=insert_stmts,
insert_errors=insert_stmts,
update_errors=update_stmts,
)