Python 3.13: fix tests

Following a recent change in Python[1], mock_open now invokes close()
when used as a context manager. This causes some of our tests to fail.
This commit rewrites part of these tests to be less dependant on
internal Python details.

[1] https://github.com/python/cpython/pull/26902

Change-Id: Iad9c0f0bf7f34d5cf209ec10863b28ddde281d7e
Signed-off-by: Cyril Roelandt <cyril@redhat.com>
This commit is contained in:
Cyril Roelandt
2024-07-22 15:13:03 +02:00
parent 6c0f16e709
commit d46a9bff00

View File

@@ -786,15 +786,15 @@ class ShellCacheSchemaTest(testutils.TestCase):
client = self.shell._get_versioned_client('2', args)
self.shell._cache_schemas(args, client, home_dir=self.cache_dir)
self.assertEqual(12, open.mock_calls.__len__())
self.assertEqual(len(self.cache_files), len(open.call_args_list))
self.assertEqual(mock.call(self.cache_files[0], 'w'),
open.mock_calls[0])
open.call_args_list[0])
self.assertEqual(mock.call(self.cache_files[1], 'w'),
open.mock_calls[4])
actual = json.loads(open.mock_calls[2][1][0])
self.assertEqual(schema_odict, actual)
actual = json.loads(open.mock_calls[6][1][0])
self.assertEqual(schema_odict, actual)
open.call_args_list[1])
actual = json.loads(open().write.call_args_list[0][0][0])
self.assertEqual(schema_odict, OrderedDict(actual))
actual = json.loads(open().write.call_args_list[1][0][0])
self.assertEqual(schema_odict, OrderedDict(actual))
@mock.patch('builtins.open', new=mock.mock_open(), create=True)
@mock.patch('os.path.exists', side_effect=[True, False, False, False])
@@ -809,15 +809,15 @@ class ShellCacheSchemaTest(testutils.TestCase):
client = self.shell._get_versioned_client('2', args)
self.shell._cache_schemas(args, client, home_dir=self.cache_dir)
self.assertEqual(12, open.mock_calls.__len__())
self.assertEqual(len(self.cache_files), len(open.call_args_list))
self.assertEqual(mock.call(self.cache_files[0], 'w'),
open.mock_calls[0])
open.call_args_list[0])
self.assertEqual(mock.call(self.cache_files[1], 'w'),
open.mock_calls[4])
actual = json.loads(open.mock_calls[2][1][0])
self.assertEqual(schema_odict, actual)
actual = json.loads(open.mock_calls[6][1][0])
self.assertEqual(schema_odict, actual)
open.call_args_list[1])
actual = json.loads(open().write.call_args_list[0][0][0])
self.assertEqual(schema_odict, OrderedDict(actual))
actual = json.loads(open().write.call_args_list[1][0][0])
self.assertEqual(schema_odict, OrderedDict(actual))
@mock.patch('builtins.open', new=mock.mock_open(), create=True)
@mock.patch('os.path.exists', return_value=True)