[gdal-dev] Intermittent failure with Python mocks

Jon Morris Jon.Morris at jbarisk.com
Fri Apr 14 00:47:42 PDT 2023


Yes it does help. I’m still getting to grips with patching and see it as a last resort if you can’t set the test up a better way. I’ll skip the failing tests for now and see if I can rewrite them.

Though I guess the question now is why does it actually work most of the time? 😊

Thanks


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.
From: Sean Gillies <sean.gillies at gmail.com>
Sent: Thursday, April 13, 2023 3:14 PM
To: Jon Morris <Jon.Morris at jbarisk.com>
Cc: gdal dev <gdal-dev at lists.osgeo.org>
Subject: Re: [gdal-dev] Intermittent failure with Python mocks

CAUTION: This email originated from outside of JBA and contains one or more links and one or more attachments. DO NOT click links or open attachments unless you recognise the sender's email address and are absolutely certain that the content is safe.
See the Phishing page on IMS on SP for more information about how to spot and report suspicious messages.
Hi Jon,

The number one gotcha with patching is documented here https://docs.python.org/3/library/unittest.mock.html#where-to-patch. In a nutshell, you have to patch your own module. I see you're patching gdal in your tests. Patching gdal from your test doesn't do anything, it's too late.

Hope this helps!

On Thu, Apr 13, 2023, 7:53 AM Jon Morris <Jon.Morris at jbarisk.com<mailto:Jon.Morris at jbarisk.com>> wrote:
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<mailto:Jon.Morris at jbarisk.com>
d:
+44 (0)1756 587229
t:
+44 (0)1756 799919<tel:+44%20(0)1756%20799919>
www.jbarisk.com<http://www.jbarisk.com/>
[cid:image978682.png at F0F5C55D.6B2FD5D3]
[Facebook]<https://www.facebook.com/TheFloodPeople>
[LinkedIn]<https://www.linkedin.com/company/jba-risk-management/>
[Twitter]<https://twitter.com/JBARisk>
[YouTube]<https://www.youtube.com/channel/UC0iatom2jYbW96voW0rlpCw>



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.
_______________________________________________
gdal-dev mailing list
gdal-dev at lists.osgeo.org<mailto:gdal-dev at lists.osgeo.org>
https://lists.osgeo.org/mailman/listinfo/gdal-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230414/280f4c14/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image057709.png
Type: image/png
Size: 9321 bytes
Desc: image057709.png
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230414/280f4c14/attachment-0005.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image904684.png
Type: image/png
Size: 651 bytes
Desc: image904684.png
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230414/280f4c14/attachment-0006.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image271628.png
Type: image/png
Size: 715 bytes
Desc: image271628.png
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230414/280f4c14/attachment-0007.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image256656.png
Type: image/png
Size: 966 bytes
Desc: image256656.png
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230414/280f4c14/attachment-0008.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image951714.png
Type: image/png
Size: 784 bytes
Desc: image951714.png
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230414/280f4c14/attachment-0009.png>


More information about the gdal-dev mailing list