psycopg2 测试 test_module 由于语言导致的 AssertionError?

psycopg2 tests test_module AssertionError due to language?

我刚刚在 centos7 上安装了一个新的 postgresql 服务器,我从另一个 centos7 服务器连接到它。我 运行 psycopg2 tests.unittest.main 检查这个模块。一切都很好,除了 :

[myuser@myserver dbatools]$ python3 -c "from psycopg2 import tests; tests.unittest.main(defaultTest='tests.test_suite')" 
[...]
======================================================================
FAIL: test_diagnostics_values (psycopg2.tests.test_module.ExceptionsTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib64/python3.4/site-packages/psycopg2/tests/test_module.py", line 189, in test_diagnostics_values
    self.assertEqual(e.diag.severity, 'ERROR')
AssertionError: 'ERREUR' != 'ERROR'
- ERREUR
?    ^^
+ ERROR
?    ^


----------------------------------------------------------------------
Ran 641 tests in 27.328s

FAILED (failures=1, skipped=62)

起初,两台服务器都有 LANG=fr_FR.UTF-8(Erreur = 法语错误)。 所以我将其更改为 en_US.UTF-8 :

localectl set-locale LANG=en_US.utf8

现在我有:

[myuser@myserver dbatools]$ localectl
   System Locale: LANG=en_US.utf8
       VC Keymap: fr
      X11 Layout: fr
[myuser@myserver dbatools]$ locale
LANG=en_US.utf8
LC_CTYPE="en_US.utf8"
LC_NUMERIC="en_US.utf8"
LC_TIME="en_US.utf8"
LC_COLLATE="en_US.utf8"
LC_MONETARY="en_US.utf8"
LC_MESSAGES="en_US.utf8"
LC_PAPER="en_US.utf8"
LC_NAME="en_US.utf8"
LC_ADDRESS="en_US.utf8"
LC_TELEPHONE="en_US.utf8"
LC_MEASUREMENT="en_US.utf8"
LC_IDENTIFICATION="en_US.utf8"
LC_ALL=en_US.utf8

这里抛出异常 (psycopg2/tests/test_module.py) :

def test_diagnostics_values(self):
    cur = self.conn.cursor()
    try:
        cur.execute("select * from nonexist")
    except psycopg2.Error as exc:
        e = exc

    self.assertEqual(e.diag.sqlstate, '42P01')
    self.assertEqual(e.diag.severity, 'ERROR')

我查看了 github 上的资源以找到 psycopg2.Error 的定义,但找不到。

在 python3 shell :

>>> import locale
>>> locale.getlocale()
('en_US', 'UTF-8')
>>> locale.getdefaultlocale()
('en_US', 'UTF-8')
>>> from psycopg2 import tests, __version__ as psycopg2_version
>>> print(psycopg2_version)
2.7.1 (dt dec pq3 ext lo64)
>>> tests.unittest.main(defaultTest='tests.test_suite')
#Same error

有人遇到过这个错误之王运行 psycopg2 的测试吗?为什么在我将所有内容都更改为 en_US 之后 return 是法语单词?

嗯,实际上确实是一个语言相关的错误,但是在 postgresql 服务器设置中。

来自 http://initd.org/psycopg/docs/module.html#exceptions :

exception psycopg2.Error

diag

A Diagnostics object containing further information about the error.

http://initd.org/psycopg/docs/extensions.html#psycopg2.extensions.Diagnostics 开始:

All the information available from the PQresultErrorField() function are exposed as attributes by the object, e.g. the severity attribute returns the PG_DIAG_SEVERITY code

所以...

[myuser@mypostgresqlserver data]# grep fr_FR /data/postgresql.conf
lc_messages = 'fr_FR.UTF-8'                     # locale for system error message
lc_monetary = 'fr_FR.UTF-8'                     # locale for monetary formatting
lc_numeric = 'fr_FR.UTF-8'                      # locale for number formatting
lc_time = 'fr_FR.UTF-8'                         # locale for time formatting
[myuser@mypostgresqlserver data]# vim /data/postgresql.conf
[myuser@mypostgresqlserver data]# grep lc_messages /data/postgresql.conf
lc_messages = 'en_US.UTF-8'                     # locale for system error message
[myuser@mypostgresqlserver data]# systemctl restart postgresql-9.6.service

然后:

[myuser@myserver dbatools]$ python3 -c "from psycopg2 import tests; tests.unittest.main(defaultTest='tests.test_suite')"
----------------------------------------------------------------------
Ran 641 tests in 24.979s

OK (skipped=62)