Added all supported properties to the Diagnostic object

This commit is contained in:
Daniele Varrazzo 2013-03-17 23:31:53 +00:00
parent 9e79112e25
commit 42b063b562
2 changed files with 55 additions and 1 deletions

View File

@ -82,6 +82,24 @@ exit:
/* object calculated member list */ /* object calculated member list */
static struct PyGetSetDef diagnosticsObject_getsets[] = { static struct PyGetSetDef diagnosticsObject_getsets[] = {
{ "severity", (getter)psyco_diagnostics_get_field, NULL,
NULL, (void*) PG_DIAG_SEVERITY },
{ "sqlstate", (getter)psyco_diagnostics_get_field, NULL,
NULL, (void*) PG_DIAG_SQLSTATE },
{ "message_primary", (getter)psyco_diagnostics_get_field, NULL,
NULL, (void*) PG_DIAG_MESSAGE_PRIMARY },
{ "message_detail", (getter)psyco_diagnostics_get_field, NULL,
NULL, (void*) PG_DIAG_MESSAGE_DETAIL },
{ "message_hint", (getter)psyco_diagnostics_get_field, NULL,
NULL, (void*) PG_DIAG_MESSAGE_HINT },
{ "statement_position", (getter)psyco_diagnostics_get_field, NULL,
NULL, (void*) PG_DIAG_STATEMENT_POSITION },
{ "internal_position", (getter)psyco_diagnostics_get_field, NULL,
NULL, (void*) PG_DIAG_INTERNAL_POSITION },
{ "internal_query", (getter)psyco_diagnostics_get_field, NULL,
NULL, (void*) PG_DIAG_INTERNAL_QUERY },
{ "context", (getter)psyco_diagnostics_get_field, NULL,
NULL, (void*) PG_DIAG_CONTEXT },
{ "schema_name", (getter)psyco_diagnostics_get_field, NULL, { "schema_name", (getter)psyco_diagnostics_get_field, NULL,
NULL, (void*) PG_DIAG_SCHEMA_NAME }, NULL, (void*) PG_DIAG_SCHEMA_NAME },
{ "table_name", (getter)psyco_diagnostics_get_field, NULL, { "table_name", (getter)psyco_diagnostics_get_field, NULL,
@ -92,6 +110,12 @@ static struct PyGetSetDef diagnosticsObject_getsets[] = {
NULL, (void*) PG_DIAG_DATATYPE_NAME }, NULL, (void*) PG_DIAG_DATATYPE_NAME },
{ "constraint_name", (getter)psyco_diagnostics_get_field, NULL, { "constraint_name", (getter)psyco_diagnostics_get_field, NULL,
NULL, (void*) PG_DIAG_CONSTRAINT_NAME }, NULL, (void*) PG_DIAG_CONSTRAINT_NAME },
{ "source_file", (getter)psyco_diagnostics_get_field, NULL,
NULL, (void*) PG_DIAG_SOURCE_FILE },
{ "source_line", (getter)psyco_diagnostics_get_field, NULL,
NULL, (void*) PG_DIAG_SOURCE_LINE },
{ "source_function", (getter)psyco_diagnostics_get_field, NULL,
NULL, (void*) PG_DIAG_SOURCE_FUNCTION },
{NULL} {NULL}
}; };

View File

@ -155,8 +155,38 @@ class ExceptionsTestCase(unittest.TestCase):
self.assert_(e.pgerror) self.assert_(e.pgerror)
self.assert_(e.cursor is cur) self.assert_(e.cursor is cur)
def test_diagnostics_attributes(self):
cur = self.conn.cursor()
try:
cur.execute("select * from nonexist")
except psycopg2.Error, exc:
e = exc
diag = e.diag
self.assert_(isinstance(diag, psycopg2.extensions.Diagnostics))
for attr in [
'column_name', 'constraint_name', 'context', 'datatype_name',
'internal_position', 'internal_query', 'message_detail',
'message_hint', 'message_primary', 'schema_name', 'severity',
'source_file', 'source_function', 'source_line', 'sqlstate',
'statement_position', 'table_name', ]:
v = getattr(diag, attr)
if v is not None:
self.assert_(isinstance(v, str))
def test_diagnostics_values(self):
cur = self.conn.cursor()
try:
cur.execute("select * from nonexist")
except psycopg2.Error, exc:
e = exc
self.assertEqual(e.diag.sqlstate, '42P01')
self.assertEqual(e.diag.severity, 'ERROR')
self.assertEqual(e.diag.statement_position, '15')
@skip_before_postgres(9, 3) @skip_before_postgres(9, 3)
def test_diagnostics(self): def test_9_3_diagnostics(self):
cur = self.conn.cursor() cur = self.conn.cursor()
cur.execute(""" cur.execute("""
create temp table test_exc ( create temp table test_exc (