fix(dashboard-testing): canonicalize reflected row field names
This commit is contained in:
@@ -8,6 +8,7 @@ from uuid import UUID
|
||||
|
||||
from sqlalchemy import MetaData, and_, create_engine, inspect, select, text
|
||||
from sqlalchemy.pool import NullPool
|
||||
from sqlalchemy.sql.elements import quoted_name
|
||||
|
||||
from dashboard_testing_lab_reset_archive import digest, encode, require
|
||||
|
||||
@@ -135,6 +136,27 @@ def locked_database(url, scope, expected_identity):
|
||||
# #endregion Tooling.LabReset.Database.Transaction
|
||||
|
||||
|
||||
# #region Tooling.LabReset.Database.RowFields [C:3] [TYPE Function]
|
||||
# @PRE Exact reflected columns and only plain str/SQLAlchemy quoted_name keys; no duplicate/foreign/missing field.
|
||||
# @POST Plain identifier keys preserve every native value by identity; nested values and recursive codec remain unchanged.
|
||||
def row_fields(row, table):
|
||||
identifiers = (str, quoted_name)
|
||||
names = [column.name for column in table.columns]
|
||||
require(all(type(name) in identifiers for name in names), "LAB_RESET_ROW_FIELD_CONFLICT")
|
||||
expected = [str(name) for name in names]
|
||||
items = list(row.items())
|
||||
require(len(set(expected)) == len(expected) == len(items), "LAB_RESET_ROW_FIELD_CONFLICT")
|
||||
result = {}
|
||||
for key, value in items:
|
||||
require(type(key) in identifiers, "LAB_RESET_ROW_FIELD_CONFLICT")
|
||||
name = str(key)
|
||||
require(name in expected and name not in result, "LAB_RESET_ROW_FIELD_CONFLICT")
|
||||
result[name] = value
|
||||
require(set(result) == set(expected), "LAB_RESET_ROW_FIELD_CONFLICT")
|
||||
return result
|
||||
# #endregion Tooling.LabReset.Database.RowFields
|
||||
|
||||
|
||||
# #region Tooling.LabReset.Database.Reflection [C:3] [TYPE Function]
|
||||
def reflect(connection, scope):
|
||||
metadata = MetaData(schema="public")
|
||||
@@ -144,7 +166,7 @@ def reflect(connection, scope):
|
||||
keys = {name: [column.name for column in table.primary_key] for name, table in tables.items()}
|
||||
require(all(keys.values()), "LAB_RESET_PRIMARY_KEY_REQUIRED")
|
||||
fks = {name: inspector.get_foreign_keys(name, schema="public") for name in tables}
|
||||
rows = {name: [dict(row) for row in connection.execute(select(table)).mappings()]
|
||||
rows = {name: [row_fields(row, table) for row in connection.execute(select(table)).mappings()]
|
||||
for name, table in tables.items()}
|
||||
return tables, keys, fks, rows
|
||||
# #endregion Tooling.LabReset.Database.Reflection
|
||||
@@ -301,7 +323,7 @@ def verify_deleted(connection, scope, before, state):
|
||||
table_names(connection, scope)
|
||||
no_foreign_sessions(connection)
|
||||
for name, table in tables.items():
|
||||
actual = [dict(row) for row in connection.execute(select(table)).mappings()]
|
||||
actual = [row_fields(row, table) for row in connection.execute(select(table)).mappings()]
|
||||
expected = [row for row in original[name] if node(name, row, keys[name]) not in selected]
|
||||
require(digest(sorted(map(encode, actual), key=digest)) == digest(sorted(map(encode, expected), key=digest)),
|
||||
"LAB_RESET_POST_DELETE_STATE_CONFLICT")
|
||||
|
||||
Reference in New Issue
Block a user