[gdal-dev] Intermittent failure with Python mocks

Jon Morris Jon.Morris at jbarisk.com
Thu Apr 13 06:52:42 PDT 2023


I'm getting intermittent fails with our Python unit tests and I wondered if anyone has seen the same issue. I think it's a race condition where a mock patch is either not applied at all or maybe applied too slowly so the patched function still has its original return value. It might be a pytest issue, but I've now seen it with two different GDAL methods so I'm just curious if anyone has any idea what may be going on.

The first example I reported on Stack Overflow (https://stackoverflow.com/q/75185263/3182496) and looks something like this:

        mock_feat_defn = mocker.patch.object(ogr.FeatureDefn, 'GetFieldCount')
        mock_feat_defn.side_effect = RuntimeError("Some other error message")

        with pytest.raises(RuntimeError, match="Some other error message"):
            get_from_layer_schema(test_lyr, get_names=True, get_types=False)

The second example is this:

        with mock.patch('osgeo.gdal.Driver.Delete', side_effect=None):
            with self.assertRaisesRegex(DatasetError, "Could not delete"):
                delete_path(shp_path)

In both cases we're mocking a GDAL method call to check one of our functions correctly raises an error, but sometimes it works and sometimes it doesn't. If it's not a GDAL issue, no problem, I will keep looking elsewhere for a solution.

Jon

e: Jon.Morris at jbarisk.com
d:+44 (0)1756 587229
t: +44 (0)1756 799919
www.jbarisk.com
All JBA Risk Management's email messages contain confidential information and are intended only for the individual(s) named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail.
Please notify the sender immediately by email if you have received this email by mistake and delete this email from your system. 
JBA Risk Management Limited is registered in England, company number 07732946, 1 Broughton Park, Old Lane North, Broughton, Skipton, North Yorkshire, BD23 3FD, England.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230413/6aefdccd/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image978682.png
Type: image/png
Size: 9321 bytes
Desc: image978682.png
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230413/6aefdccd/attachment-0005.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image945321.png
Type: image/png
Size: 651 bytes
Desc: image945321.png
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230413/6aefdccd/attachment-0006.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image904217.png
Type: image/png
Size: 715 bytes
Desc: image904217.png
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230413/6aefdccd/attachment-0007.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image622653.png
Type: image/png
Size: 966 bytes
Desc: image622653.png
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230413/6aefdccd/attachment-0008.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image221773.png
Type: image/png
Size: 784 bytes
Desc: image221773.png
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230413/6aefdccd/attachment-0009.png>


More information about the gdal-dev mailing list