GispoCoding / pytest-qgis

A pytest plugin for testing QGIS python plugins
GNU General Public License v2.0
29 stars 8 forks source link

CRS Issue in the tests #56

Closed JanCaha closed 3 months ago

JanCaha commented 8 months ago

I run into an interesting bug:

I have a very simple test:

from pathlib import Path
from qgis.core import QgsRasterLayer

def test_crs():
    raster_path = Path(__file__).parent.parent / "raster.tif"
    layer = QgsRasterLayer(raster_path.as_posix())
    assert layer.crs().toWkt()
    print(layer.crs())
    assert layer.crs().isValid()

Which is supposed to print layer crs and test that the crs is valid. However if I run the test with pytest-qgis using:

pytest tests/test_crs.py -v -s

The print outputs <QgsCoordinateReferenceSystem: invalid> and the last assertion fails.

Running the test using (which disables the pytest-qgis plugin):

pytest tests/test_crs.py -p no:qgis -v -s

Works as expected, the print is <QgsCoordinateReferenceSystem: EPSG:8353> and the assertion passed.

This happens with any CRS in the data that I have. If I try to get CRS using osgeo.gdal it works both times, so it seems more like the issue is in QGIS and not in PROJ setup. I did my best to get to the bottom of it, but it seems that PROJ is loaded correctly, pointing to the correct PROJ database file. I have like no idea what is going on.

I am using Ubuntu based distro and latest version of QGIS 3.34.1. The issue is not appearing only on my machine but also in Github Actions (you can see it here but it is a little bit more complex, but caused by the same issue).

I used some previous version (1.3.x - cannot remeber which one exactly) without any issues, but the upgraded seems cause this, even though I tried to reinstall the older version and it seemed to prevail (but I might messed up the enviroment a bit so I am not 100% sure).

Any ideas what might be going on? I am happy to assist in any way I can, but I right now, I am out of ideas. Thanks a lot :)

JanCaha commented 8 months ago

BTW: it seems that this line https://github.com/GispoCoding/pytest-qgis/blob/af7f77aa962175263dcbcbbdce31afeeb6e842e8/tests/conftest.py#L98 was added specifically to handle this issue, the question is, why is it even happening?

Joonalai commented 8 months ago

Thank you for the exceptionally good bug report! :) When I added the line, I actually thought that it was some bug in my nightly QGIS version and didn't put too much thought to it. I will investigate it now and see where it comes from.

Joonalai commented 8 months ago

I managed to track down the reason. By adding line app = QgsApplication([], GUIenabled=False) to your example test, it fails even when run with pytest tests/test_crs.py -p no:qgis -v -s.

from pathlib import Path

from qgis.core import QgsApplication, QgsRasterLayer

def test_crs():
    app = QgsApplication([], GUIenabled=False)  # or GuiEnabled=True
    raster_path = Path(Path(__file__).parent.parent, "raster.tif")
    layer = QgsRasterLayer(raster_path.as_posix())
    assert layer.crs().toWkt()
    print(layer.crs())
    assert layer.crs().isValid()

It definitely has something to do with newest QGIS versions. I will try to see if there is some workaround. Otherwise fix might have to be done on QGIS itself.

Joonalai commented 8 months ago

It seems that the bug was introduced in QGIS 3.34.

I was wondering why this went unnoticed with QGIS's own tests and I noticed that the problem does not necessarily occur with Unittests framework. python -m unittest tests/test_crs.py works just fine but pytest tests/test_crs.py -p no:qgis doesn't with the following tests (also shown in actions run in link above).

https://github.com/GispoCoding/pytest-qgis/blob/06fbc617ee15f812e3bad48bb08bfe678aae7d58/tests/test_crs.py#L9

JanCaha commented 8 months ago

Hmm interesting. This will be quite an issue to track down properly. The code works if runned without testing framework and indeed with Unittest. Funny thing is that not everything is broken ... you can still create CRS using EPSG:XXXX notation. It seems to be related to WKT somehow.

I am wondering what pytest could messed up...

Joonalai commented 8 months ago

Pytest itself did not cause this since it worked just fine with QGIS 3.32 for instance but somehow they run tests differently.

I will try to find the cause in QGIS but in the meanwhile you could use lower QGIS version in your Github actions for the tests. I recommend using container, take a look at pickLayer workflow for example. I think in QGIS the plugin should work just fine also with newest QGIS.

JanCaha commented 8 months ago

It is not really an issue for me, I was just thinking about converting some old tests from Unittest to Pytest but in first test I came across this problem....so I guess it can wait ;-) But I was kinda interested about the error, because I though that I was doing something wrong or that the issue was caused by the pytest-qgis, would not guess that it is QGIS problem.

Joonalai commented 4 months ago

Good news! Even though the problem was caused by QGIS, it was possible to circle around it in pytest-qgis :smile:

JanCaha commented 4 months ago

Great! Any idea when the updated version could be released? This is pretty critical for practical use...

Joonalai commented 4 months ago

I believe the new version will be released later this week, probably on Friday.

JanCaha commented 4 months ago

Ok, thanks a lot. I have been unable to test some my plugins for some time due to this ;)

If you ever need second pair of eyes or some other help with the package feel free to ping me. Happy to help if I could :)

Joonalai commented 4 months ago

Oh, I am sorry it has taken this much time to fix this. I actually had tried to find the reason behind this in QGIS source code but that turned out to be more time consuming than I thought.

If you ever need second pair of eyes or some other help with the package feel free to ping me. Happy to help if I could :)

Thank you for your offer! Any kind of contributions are always more than welcome :) Feel free to write more issues and PRs if you encounter any issues or have any enhancement proposals for the project.

JanCaha commented 3 months ago

@Joonalai Any chance for release? ;-)

Joonalai commented 3 months ago

The release will be made soon, the PR is now reviewed and ready for merge.