Skip to content

Commit

Permalink
Implement simple pytest-style guide
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiggi committed Apr 2, 2024
1 parent f0f3f70 commit 2e9506d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion disdrodb/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,6 @@ def create_test_config_files(request):
write_yaml(dictionary, test_filepath)

yield

os.remove(test_filepath)
shutil.rmtree(test_dir)
24 changes: 16 additions & 8 deletions disdrodb/tests/test_api/test_api_create_directories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion disdrodb/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions disdrodb/tests/test_issue/test_issue_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
4 changes: 2 additions & 2 deletions disdrodb/tests/test_l0/test_template_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@


@pytest.mark.parametrize(
"test_input, expected",
("test_input", "expected"),
[
("123.456", True),
("123", True),
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions disdrodb/tests/test_metadata/test_metadata_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2e9506d

Please sign in to comment.