From d1c3c3b3f66a3b98c83bc88efc8d286895d9df79 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Sun, 16 Jan 2011 17:22:37 +0100
Subject: [PATCH] query: change (debug-mode) field validation errors

Currently, the single assert just checks that the entire row is
consistent (true/false), and dumps the row and field definitions as an
accompanying message. This makes it very hard to understand what failed.

This patch changes this validation to show descriptive messages, which
makes it much faster in diagnosing invalid result.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 lib/query.py | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/lib/query.py b/lib/query.py
index 937b4d3f1..7caa9c5a7 100644
--- a/lib/query.py
+++ b/lib/query.py
@@ -207,10 +207,8 @@ class Query:
 
     # Verify result
     if __debug__:
-      for (idx, row) in enumerate(result):
-        assert _VerifyResultRow(self._fields, row), \
-               ("Inconsistent result for fields %s in row %s: %r" %
-                (GetAllFields(self._fields), idx, row))
+      for row in result:
+        _VerifyResultRow(self._fields, row)
 
     return result
 
@@ -256,11 +254,17 @@ def _VerifyResultRow(fields, row):
   @param row: Row data
 
   """
-  return (len(row) == len(fields) and
-          compat.all((status == QRFS_NORMAL and _VERIFY_FN[fdef.kind](value)) or
-                     # Value for an abnormal status must be None
-                     (status != QRFS_NORMAL and value is None)
-                     for ((status, value), (fdef, _, _)) in zip(row, fields)))
+  assert len(row) == len(fields)
+  errs = []
+  for ((status, value), (fdef, _, _)) in zip(row, fields):
+    if status == QRFS_NORMAL:
+      if not _VERIFY_FN[fdef.kind](value):
+        errs.append("normal field %s fails validation (value is %s)" %
+                    (fdef.name, value))
+    elif value is not None:
+      errs.append("abnormal field %s has a non-None value" % fdef.name)
+  assert not errs, ("Failed validation: %s in row %s" %
+                    (utils.CommaJoin(errors), row))
 
 
 def _PrepareFieldList(fields):
-- 
GitLab