From 2e9506d73093593bc8a065907017037e30492473 Mon Sep 17 00:00:00 2001 From: ghiggi Date: Tue, 2 Apr 2024 20:54:14 +0200 Subject: [PATCH] Implement simple pytest-style guide --- disdrodb/tests/conftest.py | 2 +- .../test_api/test_api_create_directories.py | 24 ++++++++++++------- disdrodb/tests/test_config.py | 2 +- .../tests/test_issue/test_issue_checks.py | 6 ++--- disdrodb/tests/test_l0/test_template_tools.py | 4 ++-- .../test_metadata/test_metadata_checks.py | 16 ++++++------- 6 files changed, 31 insertions(+), 23 deletions(-) diff --git a/disdrodb/tests/conftest.py b/disdrodb/tests/conftest.py index 2cb5fef0..e6b20419 100644 --- a/disdrodb/tests/conftest.py +++ b/disdrodb/tests/conftest.py @@ -182,6 +182,6 @@ def create_test_config_files(request): write_yaml(dictionary, test_filepath) yield - + os.remove(test_filepath) shutil.rmtree(test_dir) diff --git a/disdrodb/tests/test_api/test_api_create_directories.py b/disdrodb/tests/test_api/test_api_create_directories.py index 4eacae64..b7dd8544 100644 --- a/disdrodb/tests/test_api/test_api_create_directories.py +++ b/disdrodb/tests/test_api/test_api_create_directories.py @@ -148,10 +148,13 @@ def test_create_l0_directory_structure(tmp_path, mocker, product): ) # Test product, metadata and station directories have been created - assert os.path.exists(dst_station_dir) and os.path.isdir(dst_station_dir) - assert os.path.exists(dst_metadata_dir) and os.path.isdir(dst_metadata_dir) + assert os.path.exists(dst_station_dir) + assert os.path.isdir(dst_station_dir) + assert os.path.exists(dst_metadata_dir) + assert os.path.isdir(dst_metadata_dir) # Test it copied the metadata from RAW - assert os.path.exists(dst_metadata_filepath) and os.path.isfile(dst_metadata_filepath) + assert os.path.exists(dst_metadata_filepath) + assert os.path.isfile(dst_metadata_filepath) os.remove(dst_metadata_filepath) # Test raise error if already data in L0A (if force=False) @@ -182,9 +185,12 @@ def test_create_l0_directory_structure(tmp_path, mocker, product): station_name=station_name, ) assert not os.path.exists(product_filepath) - assert os.path.exists(dst_station_dir) and os.path.isdir(dst_station_dir) - assert os.path.exists(dst_metadata_dir) and os.path.isdir(dst_metadata_dir) - assert os.path.exists(dst_metadata_filepath) and os.path.isfile(dst_metadata_filepath) + assert os.path.exists(dst_station_dir) + assert os.path.isdir(dst_station_dir) + assert os.path.exists(dst_metadata_dir) + assert os.path.isdir(dst_metadata_dir) + assert os.path.exists(dst_metadata_filepath) + assert os.path.isfile(dst_metadata_filepath) def test_create_directory_structure(tmp_path, mocker): @@ -253,7 +259,8 @@ def test_create_directory_structure(tmp_path, mocker): # Test product directory has been created dst_station_dir = os.path.join(processed_dir, dst_product) - assert os.path.exists(dst_station_dir) and os.path.isdir(dst_station_dir) + assert os.path.exists(dst_station_dir) + assert os.path.isdir(dst_station_dir) # Test raise error if already data in dst_product (if force=False) dst_product_file_filepath = create_fake_raw_data_file( @@ -281,7 +288,8 @@ def test_create_directory_structure(tmp_path, mocker): station_name=station_name, ) assert not os.path.exists(dst_product_file_filepath) - assert os.path.exists(dst_station_dir) and os.path.isdir(dst_station_dir) + assert os.path.exists(dst_station_dir) + assert os.path.isdir(dst_station_dir) # Test raise error if bad station_name with pytest.raises(ValueError): diff --git a/disdrodb/tests/test_config.py b/disdrodb/tests/test_config.py index 59b9b768..5328c6bd 100644 --- a/disdrodb/tests/test_config.py +++ b/disdrodb/tests/test_config.py @@ -95,7 +95,7 @@ def test_get_base_dir(): assert get_base_dir() == "another_test_dir/DISDRODB" -@pytest.mark.parametrize("sandbox,expected_token", [(False, "my_zenodo_token"), (True, "my_sandbox_zenodo_token")]) +@pytest.mark.parametrize(("sandbox", "expected_token"), [(False, "my_zenodo_token"), (True, "my_sandbox_zenodo_token")]) def test_get_zenodo_token(sandbox, expected_token): import disdrodb from disdrodb.configs import get_zenodo_token diff --git a/disdrodb/tests/test_issue/test_issue_checks.py b/disdrodb/tests/test_issue/test_issue_checks.py index 0ed1ca99..c9beb94f 100644 --- a/disdrodb/tests/test_issue/test_issue_checks.py +++ b/disdrodb/tests/test_issue/test_issue_checks.py @@ -90,18 +90,18 @@ def test_check_timesteps(): check_timesteps(123) # Test invalid datetime input + timesteps = np.array(["2022-01-01", "2022-01-02"], dtype="datetime64[D]") with pytest.raises(ValueError): - timesteps = np.array(["2022-01-01", "2022-01-02"], dtype="datetime64[D]") check_timesteps(timesteps) # Test invalid list of string (wrong temporal resolution) + timesteps = ["2022-01-01 01:00", "2022-01-01 02:00"] with pytest.raises(ValueError): - timesteps = ["2022-01-01 01:00", "2022-01-01 02:00"] check_timesteps(timesteps) # Test invalid list of string (wrong time format) + timesteps = ["2022-15-01 01:00:00", "2022-15-01 02:00:00"] with pytest.raises(ValueError): - timesteps = ["2022-15-01 01:00:00", "2022-15-01 02:00:00"] check_timesteps(timesteps) diff --git a/disdrodb/tests/test_l0/test_template_tools.py b/disdrodb/tests/test_l0/test_template_tools.py index 1a9a6b56..b10d032f 100644 --- a/disdrodb/tests/test_l0/test_template_tools.py +++ b/disdrodb/tests/test_l0/test_template_tools.py @@ -46,7 +46,7 @@ @pytest.mark.parametrize( - "test_input, expected", + ("test_input", "expected"), [ ("123.456", True), ("123", True), @@ -208,7 +208,7 @@ def test_print_all_columns(self, capfd): assert "Column 0 ( A ):" in out assert "Column 1 ( B ):" in out - @pytest.mark.parametrize("column_indices", ([0, 1], slice(0, 2))) + @pytest.mark.parametrize("column_indices", [[0, 1], slice(0, 2)]) def test_print_specific_columns(self, capfd, column_indices): df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]}) print_df_summary_stats(df, column_indices=column_indices) diff --git a/disdrodb/tests/test_metadata/test_metadata_checks.py b/disdrodb/tests/test_metadata/test_metadata_checks.py index 6a3cb399..e5c02400 100644 --- a/disdrodb/tests/test_metadata/test_metadata_checks.py +++ b/disdrodb/tests/test_metadata/test_metadata_checks.py @@ -44,43 +44,43 @@ def test_check_metadata_geolocation(): # Test missing longitude and latitude + metadata = {"platform_type": "fixed"} with pytest.raises(ValueError): - metadata = {"platform_type": "fixed"} check_metadata_geolocation(metadata) # Test non-numeric longitude + metadata = {"longitude": "not_a_number", "latitude": 20, "platform_type": "fixed"} with pytest.raises(TypeError): - metadata = {"longitude": "not_a_number", "latitude": 20, "platform_type": "fixed"} check_metadata_geolocation(metadata) # Test non-numeric latitude + metadata = {"longitude": 10, "latitude": "not_a_number", "platform_type": "fixed"} with pytest.raises(TypeError): - metadata = {"longitude": 10, "latitude": "not_a_number", "platform_type": "fixed"} check_metadata_geolocation(metadata) # Test mobile platform with wrong coordinates + metadata = {"longitude": 10, "latitude": 20, "platform_type": "mobile"} with pytest.raises(ValueError): - metadata = {"longitude": 10, "latitude": 20, "platform_type": "mobile"} check_metadata_geolocation(metadata) # Test fixed platform with missing latitude + metadata = {"longitude": 10, "latitude": -9999, "platform_type": "fixed"} with pytest.raises(ValueError): - metadata = {"longitude": 10, "latitude": -9999, "platform_type": "fixed"} check_metadata_geolocation(metadata) # Test fixed platform with missing longitude + metadata = {"longitude": -9999, "latitude": 20, "platform_type": "fixed"} with pytest.raises(ValueError): - metadata = {"longitude": -9999, "latitude": 20, "platform_type": "fixed"} check_metadata_geolocation(metadata) # Test invalid longitude value + metadata = {"longitude": 200, "latitude": 20, "platform_type": "fixed"} with pytest.raises(ValueError): - metadata = {"longitude": 200, "latitude": 20, "platform_type": "fixed"} check_metadata_geolocation(metadata) # Test invalid latitude value + metadata = {"longitude": 10, "latitude": -100, "platform_type": "fixed"} with pytest.raises(ValueError): - metadata = {"longitude": 10, "latitude": -100, "platform_type": "fixed"} check_metadata_geolocation(metadata) # Test valid metadata