Skip to content

Commit

Permalink
Improving the process when the tests are restoring the configuration …
Browse files Browse the repository at this point in the history
…file
  • Loading branch information
XavierCLL committed Dec 15, 2024
1 parent 786ae94 commit a520d8c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 58 deletions.
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import os
import pytest
from pathlib import Path

from qgis.testing import start_app

from AcATaMa import classFactory
from AcATaMa.core import config
from AcATaMa.gui.acatama_dockwidget import AcATaMaDockWidget

pytest_plugins = ('pytest_qgis',)
Expand All @@ -15,6 +17,7 @@
# when running in a docker container, we use the start_app provided by qgis rather
# than that of pytest-qgis. pytest-qgis does not clean up the application properly
# and results in a seg-fault
print("RUNNING IN DOCKER CONTAINER")
start_app()


Expand All @@ -29,6 +32,13 @@ def unwrapper(func):
yield unwrapper


@pytest.fixture
def restore_config_file(monkeypatch):
def _load_config_file(input_yml_path):
config.restore(input_yml_path)
return _load_config_file


@pytest.fixture
def plugin(pytestconfig, qgis_iface, qgis_parent, qgis_new_project):
"""
Expand Down
26 changes: 10 additions & 16 deletions tests/test_analysis.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest

from AcATaMa.core import config
from AcATaMa.core.analysis import AccuracyAssessmentWindow
from AcATaMa.gui import accuracy_assessment_results

Expand All @@ -9,11 +8,10 @@ def clean_raw_html(html):
return html.replace('\n', '').replace('\t', '').replace(' ', '')


def test_accuracy_assessment_simple_html(plugin, unwrap):
def test_accuracy_assessment_simple_html(plugin, restore_config_file):
# restore
input_yml_path = pytest.tests_data_dir.parent.parent / "examples" / "Simple exercise - 2019-2020 change - Tinigua.yml"
config_restore = unwrap(config.restore)
config_restore(input_yml_path)
restore_config_file(input_yml_path)

# get accuracy assessment results in html
accuracy_assessment = AccuracyAssessmentWindow()
Expand All @@ -31,11 +29,10 @@ def test_accuracy_assessment_simple_html(plugin, unwrap):
assert clean_raw_html(result_html_computed) == clean_raw_html(result_html_in_file.read())


def test_accuracy_assessment_simple_post_stratify_html(plugin, unwrap):
def test_accuracy_assessment_simple_post_stratify_html(plugin, restore_config_file):
# restore
input_yml_path = pytest.tests_data_dir.parent.parent / "examples" / "Simple post-stratify exercise - 2019-2020 change - Tinigua.yml"
config_restore = unwrap(config.restore)
config_restore(input_yml_path)
restore_config_file(input_yml_path)

# get accuracy assessment results in html
accuracy_assessment = AccuracyAssessmentWindow()
Expand All @@ -53,11 +50,10 @@ def test_accuracy_assessment_simple_post_stratify_html(plugin, unwrap):
assert clean_raw_html(result_html_computed) == clean_raw_html(result_html_in_file.read())


def test_accuracy_assessment_systematic_html(plugin, unwrap):
def test_accuracy_assessment_systematic_html(plugin, restore_config_file):
# restore
input_yml_path = pytest.tests_data_dir.parent.parent / "examples" / "Systematic exercise - 2019-2020 change - Tinigua.yml"
config_restore = unwrap(config.restore)
config_restore(input_yml_path)
restore_config_file(input_yml_path)

# get accuracy assessment results in html
accuracy_assessment = AccuracyAssessmentWindow()
Expand All @@ -75,11 +71,10 @@ def test_accuracy_assessment_systematic_html(plugin, unwrap):
assert clean_raw_html(result_html_computed) == clean_raw_html(result_html_in_file.read())


def test_accuracy_assessment_systematic_post_stratify_html(plugin, unwrap):
def test_accuracy_assessment_systematic_post_stratify_html(plugin, restore_config_file):
# restore
input_yml_path = pytest.tests_data_dir.parent.parent / "examples" / "Systematic post-stratify exercise - 2019-2020 change - Tinigua.yml"
config_restore = unwrap(config.restore)
config_restore(input_yml_path)
restore_config_file(input_yml_path)

# get accuracy assessment results in html
accuracy_assessment = AccuracyAssessmentWindow()
Expand All @@ -97,11 +92,10 @@ def test_accuracy_assessment_systematic_post_stratify_html(plugin, unwrap):
assert clean_raw_html(result_html_computed) == clean_raw_html(result_html_in_file.read())


def test_accuracy_assessment_stratified_html(plugin, unwrap):
def test_accuracy_assessment_stratified_html(plugin, restore_config_file):
# restore
input_yml_path = pytest.tests_data_dir.parent.parent / "examples" / "Stratified exercise - 2019-2020 change - Tinigua.yml"
config_restore = unwrap(config.restore)
config_restore(input_yml_path)
restore_config_file(input_yml_path)

# get accuracy assessment results in html
accuracy_assessment = AccuracyAssessmentWindow()
Expand Down
12 changes: 5 additions & 7 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
from AcATaMa.core import config


def compare_config_files(unwrap, tmpdir, input_yml_path):
def compare_config_files(restore_config_file, tmpdir, input_yml_path):
# restore
config_restore = unwrap(config.restore)
config_restore(input_yml_path)
restore_config_file(input_yml_path)
input_yml_file = open(input_yml_path, 'r')
# save
output_yml_file = tmpdir.join('acatama_config.yml')
config_save = unwrap(config.save)
config_save(output_yml_file.strpath)
config.save(output_yml_file.strpath)

assert output_yml_file.read() == input_yml_file.read()


def disabled_test_restore_and_save_config_file(plugin, unwrap, tmpdir):
def disabled_test_restore_and_save_config_file(restore_config_file, tmpdir):
yml_files = [pytest.tests_data_dir.parent.parent / "examples" / "Simple exercise - 2019-2020 change - Tinigua.yml"]

for yml_file in yml_files:
compare_config_files(unwrap, tmpdir, yml_file)
compare_config_files(restore_config_file, tmpdir, yml_file)
23 changes: 9 additions & 14 deletions tests/test_pixel_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@
import pytest
from osgeo import gdal

from AcATaMa.core import config
from AcATaMa.core.map import auto_symbology_classification_render
from AcATaMa.utils.others_utils import get_pixel_count_by_pixel_values, get_pixel_count_by_pixel_values_sequential, \
get_pixel_count_by_pixel_values_parallel, get_nodata_format
from AcATaMa.utils.qgis_utils import load_layer


def test_pixel_count_without_nodata_sequential(plugin, unwrap):
def test_pixel_count_without_nodata_sequential(plugin, restore_config_file):
# restore
input_yml_path = pytest.tests_data_dir / "test_pixel_count_acatama.yml"
config_restore = unwrap(config.restore)
config_restore(input_yml_path)
restore_config_file(input_yml_path)

pixel_count = get_pixel_count_by_pixel_values_sequential(plugin.dockwidget.QCBox_ThematicMap.currentLayer(),
band=2, nodata=None)
Expand All @@ -22,11 +20,10 @@ def test_pixel_count_without_nodata_sequential(plugin, unwrap):
44: 79, 45: 5, 46: 468, 47: 57, 49: 3, 51: 1, 52: 15, 53: 24}


def test_pixel_count_without_nodata_parallel(plugin, unwrap):
def test_pixel_count_without_nodata_parallel(plugin, restore_config_file):
# restore
input_yml_path = pytest.tests_data_dir / "test_pixel_count_acatama.yml"
config_restore = unwrap(config.restore)
config_restore(input_yml_path)
restore_config_file(input_yml_path)

pixel_count = get_pixel_count_by_pixel_values_parallel(plugin.dockwidget.QCBox_ThematicMap.currentLayer(),
band=2, nodata=None)
Expand All @@ -35,11 +32,10 @@ def test_pixel_count_without_nodata_parallel(plugin, unwrap):
44: 79, 45: 5, 46: 468, 47: 57, 49: 3, 51: 1, 52: 15, 53: 24}


def test_pixel_count_with_nodata_sequential(plugin, unwrap):
def test_pixel_count_with_nodata_sequential(plugin, restore_config_file):
# restore
input_yml_path = pytest.tests_data_dir / "test_pixel_count_acatama_nodata.yml"
config_restore = unwrap(config.restore)
config_restore(input_yml_path)
restore_config_file(input_yml_path)
sampling_design = plugin.dockwidget.sampling_design_window

pixel_count = get_pixel_count_by_pixel_values_sequential(
Expand All @@ -51,11 +47,10 @@ def test_pixel_count_with_nodata_sequential(plugin, unwrap):
assert pixel_count == {1: 10423, 2: 418, 5: 8822}


def test_pixel_count_with_nodata_parallel(plugin, unwrap):
def test_pixel_count_with_nodata_parallel(plugin, restore_config_file):
# restore
input_yml_path = pytest.tests_data_dir / "test_pixel_count_acatama_nodata.yml"
config_restore = unwrap(config.restore)
config_restore(input_yml_path)
restore_config_file(input_yml_path)
sampling_design = plugin.dockwidget.sampling_design_window

pixel_count = get_pixel_count_by_pixel_values_parallel(
Expand All @@ -67,7 +62,7 @@ def test_pixel_count_with_nodata_parallel(plugin, unwrap):
assert pixel_count == {1: 10423, 2: 418, 5: 8822}


def test_pixel_count_and_auto_symbology(plugin, unwrap, tmpdir):
def test_pixel_count_and_auto_symbology(plugin, tmpdir):
# copy and unset the nodata
layer_unsetnodata = tmpdir.join('test_layer_with_nodata.tif')
gdal.Translate(str(layer_unsetnodata), str(pytest.tests_data_dir / "test_layer_with_nodata.tif"))
Expand Down
13 changes: 5 additions & 8 deletions tests/test_sample_size.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import pytest

from AcATaMa.core import config


def test_get_n_calculates_correct_samples_simple(plugin, unwrap):
def test_get_n_calculates_correct_samples_simple(plugin, restore_config_file):
# restore
input_yml_path = pytest.tests_data_dir / "test_sample_size.yml"
config_restore = unwrap(config.restore)
config_restore(input_yml_path)
restore_config_file(input_yml_path)

sampling_design = plugin.dockwidget.sampling_design_window

# set values
sampling_design.determine_number_samples_dialog_SimpRS.get_n()

assert sampling_design.determine_number_samples_dialog_SimpRS.NumberOfSamples.text() == "306"

def test_point_spacing_calculates_correct_samples_systematic(plugin, unwrap, monkeypatch):
def test_point_spacing_calculates_correct_samples_systematic(plugin, restore_config_file, monkeypatch):
# restore
input_yml_path = pytest.tests_data_dir / "test_sample_size.yml"
config_restore = unwrap(config.restore)
config_restore(input_yml_path)
restore_config_file(input_yml_path)
sampling_design = plugin.dockwidget.sampling_design_window

determine_number_samples_dialog = sampling_design.determine_number_samples_dialog_SystS
Expand Down
21 changes: 8 additions & 13 deletions tests/test_sampling_design.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
import pytest
from shapely.geometry import shape

from AcATaMa.core import config
from AcATaMa.core.map import Map
from AcATaMa.core.sampling_design import Sampling
from AcATaMa.utils.others_utils import get_nodata_format


def test_simple_post_stratified_random_sampling(plugin, unwrap, tmpdir):
def test_simple_post_stratified_random_sampling(plugin, restore_config_file, tmpdir):
# restore
input_yml_path = pytest.tests_data_dir / "test_sampling.yml"
config_restore = unwrap(config.restore)
config_restore(input_yml_path)
restore_config_file(input_yml_path)
sampling_design = plugin.dockwidget.sampling_design_window

# load simple post stratify random sampling config
Expand Down Expand Up @@ -52,11 +50,10 @@ def test_simple_post_stratified_random_sampling(plugin, unwrap, tmpdir):
assert shape(s['geometry']).equals(shape(t['geometry']))


def test_stratified_random_sampling(plugin, unwrap, tmpdir):
def test_stratified_random_sampling(plugin, restore_config_file, tmpdir):
# restore
input_yml_path = pytest.tests_data_dir / "test_sampling.yml"
config_restore = unwrap(config.restore)
config_restore(input_yml_path)
restore_config_file(input_yml_path)
sampling_design = plugin.dockwidget.sampling_design_window

# load stratify random sampling area based proportion config
Expand Down Expand Up @@ -109,11 +106,10 @@ def test_stratified_random_sampling(plugin, unwrap, tmpdir):
assert shape(s['geometry']).equals(shape(t['geometry']))


def test_systematic_post_stratified_random_sampling(plugin, unwrap, tmpdir):
def test_systematic_post_stratified_random_sampling(plugin, restore_config_file, tmpdir):
# restore
input_yml_path = pytest.tests_data_dir / "test_sampling.yml"
config_restore = unwrap(config.restore)
config_restore(input_yml_path)
restore_config_file(input_yml_path)
sampling_design = plugin.dockwidget.sampling_design_window

# load simple post stratify random sampling config
Expand Down Expand Up @@ -157,12 +153,11 @@ def test_systematic_post_stratified_random_sampling(plugin, unwrap, tmpdir):
assert shape(s['geometry']).equals(shape(t['geometry']))


def test_systematic_post_stratified_with_initial_inset_random(plugin, unwrap, tmpdir):
def test_systematic_post_stratified_with_initial_inset_random(plugin, restore_config_file, tmpdir):
import random
# restore
input_yml_path = pytest.tests_data_dir / "test_sampling.yml"
config_restore = unwrap(config.restore)
config_restore(input_yml_path)
restore_config_file(input_yml_path)
sampling_design = plugin.dockwidget.sampling_design_window

# load simple post stratify random sampling config
Expand Down

0 comments on commit a520d8c

Please sign in to comment.