Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import pytest

from birdnet.acoustic.inference.configs import FilteringConfig

SUPPORTED_FMIN = 0
SUPPORTED_FMAX = 15000


def test_valid_frequencies() -> None:
assert FilteringConfig.validate_bandpass_frequencies(
100, 200, SUPPORTED_FMIN, SUPPORTED_FMAX
) == (100, 200)


def test_full_range_is_valid() -> None:
assert FilteringConfig.validate_bandpass_frequencies(
SUPPORTED_FMIN, SUPPORTED_FMAX, SUPPORTED_FMIN, SUPPORTED_FMAX
) == (SUPPORTED_FMIN, SUPPORTED_FMAX)


def test_fmin_none_raises_error() -> None:
with pytest.raises(
ValueError, match=r"bandpass minimum frequence must be specified"
):
FilteringConfig.validate_bandpass_frequencies(
None, 200, SUPPORTED_FMIN, SUPPORTED_FMAX
)


def test_fmax_none_raises_error() -> None:
with pytest.raises(
ValueError, match=r"bandpass maximum frequence must be specified"
):
FilteringConfig.validate_bandpass_frequencies(
100, None, SUPPORTED_FMIN, SUPPORTED_FMAX
)


def test_non_integer_raises_error() -> None:
with pytest.raises(TypeError, match=r"bandpass frequencies must be integers"):
FilteringConfig.validate_bandpass_frequencies(
1.5, 200, SUPPORTED_FMIN, SUPPORTED_FMAX # type: ignore
)


def test_fmin_not_smaller_than_fmax_raises_error() -> None:
with pytest.raises(ValueError, match=r"bandpass frequencies must be in the range"):
FilteringConfig.validate_bandpass_frequencies(
200, 100, SUPPORTED_FMIN, SUPPORTED_FMAX
)


def test_out_of_supported_range_raises_error() -> None:
with pytest.raises(ValueError, match=r"bandpass frequencies must be in the range"):
FilteringConfig.validate_bandpass_frequencies(
100, SUPPORTED_FMAX + 1, SUPPORTED_FMIN, SUPPORTED_FMAX
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import numpy as np
import pytest

from birdnet.acoustic.inference.configs import InferenceConfig


def test_single_tuple_is_wrapped() -> None:
audio = np.zeros(3, dtype=np.float32)
result = InferenceConfig.validate_input_audio((audio, 48000))
assert len(result) == 1
np.testing.assert_array_equal(result[0][0], audio)
assert result[0][1] == 48000


def test_list_of_tuples_is_valid() -> None:
audio_a = np.zeros(3, dtype=np.float32)
audio_b = np.ones(2, dtype=np.int16)
result = InferenceConfig.validate_input_audio([(audio_a, 48000), (audio_b, 32000)])
assert len(result) == 2


def test_non_iterable_raises_error() -> None:
with pytest.raises(ValueError, match=r"Unsupported input type: <class 'int'>"):
InferenceConfig.validate_input_audio(123)


def test_element_not_a_tuple_raises_error() -> None:
with pytest.raises(ValueError, match=r"Unsupported input type"):
InferenceConfig.validate_input_audio([123])


def test_tuple_wrong_length_raises_error() -> None:
audio = np.zeros(3, dtype=np.float32)
with pytest.raises(
ValueError, match=r"Input audio tuple must have exactly two elements"
):
InferenceConfig.validate_input_audio([(audio, 48000, "extra")])


def test_first_element_not_ndarray_raises_error() -> None:
with pytest.raises(
ValueError, match=r"First element of input audio tuple must be a numpy ndarray"
):
InferenceConfig.validate_input_audio([("not an array", 48000)])


def test_sample_rate_not_int_raises_error() -> None:
audio = np.zeros(3, dtype=np.float32)
with pytest.raises(
ValueError,
match=r"Second element of input audio tuple must be an integer sample rate",
):
InferenceConfig.validate_input_audio([(audio, 48000.0)])


def test_sample_rate_not_positive_raises_error() -> None:
audio = np.zeros(3, dtype=np.float32)
with pytest.raises(
ValueError, match=r"Sample rate must be a positive integer, got 0."
):
InferenceConfig.validate_input_audio([(audio, 0)])


def test_wrong_dtype_raises_error() -> None:
audio = np.array(["a", "b"])
with pytest.raises(
ValueError, match=r"Audio array must have an integer or floating-point dtype"
):
InferenceConfig.validate_input_audio([(audio, 48000)])
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest

from birdnet.acoustic.inference.configs import OutputConfig


@pytest.mark.parametrize("value", ["minimal", "progress", "benchmark", None])
def test_valid_values(value: str | None) -> None:
assert OutputConfig.validate_show_stats(value) == value


def test_unknown_value_raises_error() -> None:
with pytest.raises(
ValueError,
match=r"show stats must be one of 'minimal', 'progress' or 'benchmark'",
):
OutputConfig.validate_show_stats("verbose") # type: ignore


def test_empty_string_raises_error() -> None:
with pytest.raises(
ValueError,
match=r"show stats must be one of 'minimal', 'progress' or 'benchmark'",
):
OutputConfig.validate_show_stats("") # type: ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import pytest

from birdnet.acoustic.inference.configs import PredictionConfig

MODEL_SPECIES = ["species_a", "species_b", "species_c"]


def test_valid_thresholds() -> None:
thresholds = {"species_a": 0.5, "species_b": 0.1}
assert (
PredictionConfig.validate_custom_confidence_thresholds(thresholds, MODEL_SPECIES)
== thresholds
)


def test_empty_dict_is_valid() -> None:
assert PredictionConfig.validate_custom_confidence_thresholds({}, MODEL_SPECIES) == {}


def test_integer_value_is_valid() -> None:
thresholds = {"species_a": 1}
assert (
PredictionConfig.validate_custom_confidence_thresholds(thresholds, MODEL_SPECIES)
== thresholds
)


def test_non_dict_raises_error() -> None:
with pytest.raises(
TypeError, match=r"custom confidence thresholds must be a dictionary"
):
PredictionConfig.validate_custom_confidence_thresholds(
[("species_a", 0.5)], MODEL_SPECIES # type: ignore
)


def test_non_string_key_raises_error() -> None:
with pytest.raises(
TypeError, match=r"custom confidence threshold keys must be strings"
):
PredictionConfig.validate_custom_confidence_thresholds({1: 0.5}, MODEL_SPECIES)


def test_unknown_species_raises_error() -> None:
with pytest.raises(
ValueError, match=r"species 'unknown' is not available in the model"
):
PredictionConfig.validate_custom_confidence_thresholds(
{"unknown": 0.5}, MODEL_SPECIES
)


def test_non_number_value_raises_error() -> None:
with pytest.raises(
TypeError, match=r"custom confidence threshold values must be numbers"
):
PredictionConfig.validate_custom_confidence_thresholds(
{"species_a": "high"}, MODEL_SPECIES
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from pathlib import Path

import pytest

from birdnet.acoustic.inference.configs import PredictionConfig

MODEL_SPECIES = ["species_a", "species_b", "species_c"]


def test_list_is_valid() -> None:
result = PredictionConfig.validate_custom_species_list(
["species_a", "species_b"], MODEL_SPECIES
)
assert result == {"species_a", "species_b"}


def test_set_is_valid() -> None:
result = PredictionConfig.validate_custom_species_list(
{"species_a"}, MODEL_SPECIES
)
assert result == {"species_a"}


def test_reads_from_file_path(tmp_path: Path) -> None:
species_file = tmp_path / "species.txt"
species_file.write_text("species_a\nspecies_c\n", encoding="utf-8")

result = PredictionConfig.validate_custom_species_list(species_file, MODEL_SPECIES)
assert result == {"species_a", "species_c"}


def test_reads_from_string_path(tmp_path: Path) -> None:
species_file = tmp_path / "species.txt"
species_file.write_text("species_b\n", encoding="utf-8")

result = PredictionConfig.validate_custom_species_list(
str(species_file), MODEL_SPECIES
)
assert result == {"species_b"}


def test_unknown_species_raises_error() -> None:
with pytest.raises(
ValueError, match=r"species 'unknown' is not available in the model"
):
PredictionConfig.validate_custom_species_list(["unknown"], MODEL_SPECIES)


def test_non_collection_raises_error() -> None:
with pytest.raises(TypeError, match=r"custom species list must be a str, path"):
PredictionConfig.validate_custom_species_list(123, MODEL_SPECIES) # type: ignore


def test_non_string_element_raises_error() -> None:
with pytest.raises(TypeError, match=r"custom species list must contain strings"):
PredictionConfig.validate_custom_species_list([1, 2], MODEL_SPECIES) # type: ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest

from birdnet.acoustic.inference.configs import PredictionConfig


def test_float_is_valid() -> None:
assert PredictionConfig.validate_default_confidence_threshold(0.5) == 0.5


def test_integer_is_valid() -> None:
assert PredictionConfig.validate_default_confidence_threshold(1) == 1

Comment thread
Josef-Haupt marked this conversation as resolved.
Outdated

def test_negative_is_returned_as_is() -> None:
# The validator only enforces the type, not the range.
assert PredictionConfig.validate_default_confidence_threshold(-0.5) == -0.5


def test_string_raises_error() -> None:
with pytest.raises(
TypeError, match=r"default confidence threshold must be a number"
):
PredictionConfig.validate_default_confidence_threshold("high") # type: ignore


def test_none_raises_error() -> None:
with pytest.raises(
TypeError, match=r"default confidence threshold must be a number"
):
PredictionConfig.validate_default_confidence_threshold(None) # type: ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest

from birdnet.acoustic.inference.configs import PredictionConfig


def test_one_is_valid() -> None:
assert PredictionConfig.validate_sigmoid_sensitivity(1.0) == 1.0


def test_lower_bound_is_valid() -> None:
assert PredictionConfig.validate_sigmoid_sensitivity(0.5) == 0.5


def test_upper_bound_is_valid() -> None:
assert PredictionConfig.validate_sigmoid_sensitivity(1.5) == 1.5


def test_too_small_raises_error() -> None:
with pytest.raises(
ValueError, match=r"sigmoid sensitivity must be in the range \[0.5, 1.5\]"
):
PredictionConfig.validate_sigmoid_sensitivity(0.1)


def test_too_large_raises_error() -> None:
with pytest.raises(
ValueError, match=r"sigmoid sensitivity must be in the range \[0.5, 1.5\]"
):
PredictionConfig.validate_sigmoid_sensitivity(2.0)


def test_non_number_raises_error() -> None:
with pytest.raises(TypeError, match=r"sigmoid sensitivity must be a number"):
PredictionConfig.validate_sigmoid_sensitivity("high") # type: ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest

from birdnet.acoustic.inference.configs import PredictionConfig


def test_valid_value() -> None:
assert PredictionConfig.validate_top_k(3, max_value=5) == 3


def test_lower_bound_is_valid() -> None:
assert PredictionConfig.validate_top_k(1, max_value=5) == 1


def test_upper_bound_is_valid() -> None:
assert PredictionConfig.validate_top_k(5, max_value=5) == 5


def test_zero_raises_error() -> None:
with pytest.raises(ValueError, match=r"top k must be in the range \[1, 5\]"):
PredictionConfig.validate_top_k(0, max_value=5)


def test_above_max_raises_error() -> None:
with pytest.raises(ValueError, match=r"top k must be in the range \[1, 5\]"):
PredictionConfig.validate_top_k(6, max_value=5)


def test_non_integer_raises_error() -> None:
with pytest.raises(TypeError, match=r"top k must be an integer"):
PredictionConfig.validate_top_k(1.5, max_value=5) # type: ignore
Loading
Loading