removes duplication in tests

This commit is contained in:
Roman Konoval 2024-09-11 17:12:41 +02:00 committed by Daniele Varrazzo
parent 282360dd04
commit cba6d39be0

View File

@ -26,6 +26,7 @@
import os import os
import unittest import unittest
from collections import deque from collections import deque
from functools import partial
import psycopg2 import psycopg2
from psycopg2 import extensions from psycopg2 import extensions
@ -129,69 +130,51 @@ conn.close()
self.assertEqual(pid, self.conn.notifies[0][0]) self.assertEqual(pid, self.conn.notifies[0][0])
self.assertEqual('foo', self.conn.notifies[0][1]) self.assertEqual('foo', self.conn.notifies[0][1])
@slow def _test_notifies_received_on_operation(self, operation, execute_query=True):
@skip_if_windows
def test_notifies_received_on_commit(self):
self.listen('foo') self.listen('foo')
self.conn.commit() self.conn.commit()
self.conn.cursor().execute('select 1;') if execute_query:
self.conn.cursor().execute('select 1;')
pid = int(self.notify('foo').communicate()[0]) pid = int(self.notify('foo').communicate()[0])
self.assertEqual(0, len(self.conn.notifies)) self.assertEqual(0, len(self.conn.notifies))
self.conn.commit() operation()
self.assertEqual(1, len(self.conn.notifies)) self.assertEqual(1, len(self.conn.notifies))
self.assertEqual(pid, self.conn.notifies[0][0]) self.assertEqual(pid, self.conn.notifies[0][0])
self.assertEqual('foo', self.conn.notifies[0][1]) self.assertEqual('foo', self.conn.notifies[0][1])
@slow
@skip_if_windows
def test_notifies_received_on_commit(self):
self._test_notifies_received_on_operation(self.conn.commit)
@slow @slow
@skip_if_windows @skip_if_windows
def test_notifies_received_on_rollback(self): def test_notifies_received_on_rollback(self):
self.listen('foo') self._test_notifies_received_on_operation(self.conn.rollback)
self.conn.commit()
self.conn.cursor().execute('select 1;')
pid = int(self.notify('foo').communicate()[0])
self.assertEqual(0, len(self.conn.notifies))
self.conn.rollback()
self.assertEqual(1, len(self.conn.notifies))
self.assertEqual(pid, self.conn.notifies[0][0])
self.assertEqual('foo', self.conn.notifies[0][1])
@slow @slow
@skip_if_windows @skip_if_windows
def test_notifies_received_on_reset(self): def test_notifies_received_on_reset(self):
self.listen('foo') self._test_notifies_received_on_operation(self.conn.reset, execute_query=False)
self.conn.commit()
pid = int(self.notify('foo').communicate()[0])
self.assertEqual(0, len(self.conn.notifies))
self.conn.reset()
self.assertEqual(1, len(self.conn.notifies))
self.assertEqual(pid, self.conn.notifies[0][0])
self.assertEqual('foo', self.conn.notifies[0][1])
@slow @slow
@skip_if_windows @skip_if_windows
def test_notifies_received_on_set_session(self): def test_notifies_received_on_set_session(self):
self.listen('foo') self._test_notifies_received_on_operation(
self.conn.commit() partial(self.conn.set_session, autocommit=True, readonly=True),
pid = int(self.notify('foo').communicate()[0]) execute_query=False,
self.assertEqual(0, len(self.conn.notifies)) )
self.conn.set_session(autocommit=True, readonly=True)
self.assertEqual(1, len(self.conn.notifies))
self.assertEqual(pid, self.conn.notifies[0][0])
self.assertEqual('foo', self.conn.notifies[0][1])
@slow @slow
@skip_if_windows @skip_if_windows
def test_notifies_received_on_set_client_encoding(self): def test_notifies_received_on_set_client_encoding(self):
self.listen('foo') self._test_notifies_received_on_operation(
self.conn.commit() partial(
pid = int(self.notify('foo').communicate()[0]) self.conn.set_client_encoding,
self.assertEqual(0, len(self.conn.notifies)) 'LATIN1' if self.conn.encoding != 'LATIN1' else 'UTF8'
self.conn.set_client_encoding( ),
'LATIN1' if self.conn.encoding != 'LATIN1' else 'UTF8' execute_query=False,
) )
self.assertEqual(1, len(self.conn.notifies))
self.assertEqual(pid, self.conn.notifies[0][0])
self.assertEqual('foo', self.conn.notifies[0][1])
@slow @slow
def test_notify_object(self): def test_notify_object(self):