Skip to content

Commit 5464fa6

Browse files
authored
Merge pull request #59 from SystemsGenetics/add-more-tests
Expand test suite from 92 to 156 tests
2 parents ce678d4 + 9d8fc79 commit 5464fa6

7 files changed

Lines changed: 451 additions & 10 deletions

File tree

Granny/Models/Values/BoolValue.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def __init__(self, name: str, label: str, help: str):
1313
super().__init__(name, label, help)
1414
self.type = bool
1515

16-
def validate(self) -> bool:
16+
def validate(self, value=None) -> bool:
1717
"""
1818
{@inheritdoc}
1919
"""
Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,107 @@
1+
import numpy as np
2+
import pytest
13
from Granny.Analyses.BlushColor import BlushColor
24

35

46
def test_BlushColorInstantiation():
5-
"""Test that BlushColor can be instantiated"""
67
analysis = BlushColor()
78
assert analysis is not None
89
assert analysis.__analysis_name__ == "blush"
910

1011

1112
def test_BlushColorInputImages():
12-
"""Test that BlushColor input_images can be set"""
1313
analysis = BlushColor()
1414
analysis.input_images.setValue("demo/pear_images/full_masked_images")
1515
assert analysis.input_images.getValue() == "demo/pear_images/full_masked_images"
16+
17+
18+
# ---------------------------------------------------------------------------
19+
# Default parameter values
20+
# ---------------------------------------------------------------------------
21+
22+
def test_default_threshold_is_148():
23+
assert BlushColor().threshold.getValue() == 148
24+
25+
26+
def test_default_fruit_threshold_is_140():
27+
assert BlushColor().fruit_threshold.getValue() == 140
28+
29+
30+
def test_default_blush_color():
31+
a = BlushColor()
32+
assert a.blush_color_r.getValue() == 150
33+
assert a.blush_color_g.getValue() == 55
34+
assert a.blush_color_b.getValue() == 50
35+
36+
37+
def test_default_text_position():
38+
a = BlushColor()
39+
assert a.text_x.getValue() == 20
40+
assert a.text_y.getValue() == 50
41+
42+
43+
def test_default_font_scale_is_1():
44+
assert BlushColor().font_scale.getValue() == pytest.approx(1.0)
45+
46+
47+
def test_default_text_thickness_is_3():
48+
assert BlushColor().text_thickness.getValue() == 3
49+
50+
51+
def test_threshold_boundary_values():
52+
a = BlushColor()
53+
a.threshold.setValue(0)
54+
assert a.threshold.getValue() == 0
55+
a.threshold.setValue(255)
56+
assert a.threshold.getValue() == 255
57+
58+
59+
# ---------------------------------------------------------------------------
60+
# _calculateBlush with synthetic images
61+
# ---------------------------------------------------------------------------
62+
63+
def _make_bgr(r, g, b, size=50):
64+
img = np.zeros((size, size, 3), dtype=np.uint8)
65+
img[:, :, 0] = b
66+
img[:, :, 1] = g
67+
img[:, :, 2] = r
68+
return img
69+
70+
71+
def test_calculate_blush_returns_ratio_between_0_and_1():
72+
a = BlushColor()
73+
img = _make_bgr(200, 100, 100)
74+
ratio, _ = a._calculateBlush(img)
75+
assert 0.0 <= ratio <= 1.0
76+
77+
78+
def test_calculate_blush_output_shape_matches_input():
79+
a = BlushColor()
80+
img = _make_bgr(200, 100, 100)
81+
_, result = a._calculateBlush(img)
82+
assert result.shape == img.shape
83+
84+
85+
def test_calculate_blush_threshold_affects_ratio():
86+
img = _make_bgr(200, 150, 100)
87+
low_a = BlushColor()
88+
low_a.threshold.setValue(50)
89+
high_a = BlushColor()
90+
high_a.threshold.setValue(200)
91+
low_ratio, _ = low_a._calculateBlush(img)
92+
high_ratio, _ = high_a._calculateBlush(img)
93+
assert low_ratio != high_ratio
94+
95+
96+
def test_calculate_blush_all_black_image():
97+
a = BlushColor()
98+
img = np.zeros((50, 50, 3), dtype=np.uint8)
99+
ratio, result = a._calculateBlush(img)
100+
assert result.shape == img.shape
101+
102+
103+
def test_calculate_blush_returns_float():
104+
a = BlushColor()
105+
img = _make_bgr(180, 100, 80)
106+
ratio, _ = a._calculateBlush(img)
107+
assert isinstance(ratio, float)
Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,103 @@
1+
import numpy as np
2+
import pytest
13
from Granny.Analyses.PeelColor import PeelColor
24

35

46
def test_PeelColorInstantiation():
5-
"""Test that PeelColor can be instantiated"""
67
analysis = PeelColor()
78
assert analysis is not None
89
assert analysis.__analysis_name__ == "color"
910

1011

1112
def test_PeelColorInputImages():
12-
"""Test that PeelColor input_images can be set"""
1313
analysis = PeelColor()
1414
analysis.input_images.setValue("demo/pear_images/full_masked_images")
1515
assert analysis.input_images.getValue() == "demo/pear_images/full_masked_images"
16+
17+
18+
# ---------------------------------------------------------------------------
19+
# Default parameter values
20+
# ---------------------------------------------------------------------------
21+
22+
def test_default_purple_threshold_is_126():
23+
assert PeelColor().purple_threshold.getValue() == 126
24+
25+
26+
def test_default_lightness_range():
27+
a = PeelColor()
28+
assert a.lightness_min.getValue() == 0
29+
assert a.lightness_max.getValue() == 255
30+
31+
32+
def test_default_green_range():
33+
a = PeelColor()
34+
assert a.green_min.getValue() == 0
35+
assert a.green_max.getValue() == 128
36+
37+
38+
def test_default_yellow_range():
39+
a = PeelColor()
40+
assert a.yellow_min.getValue() == 128
41+
assert a.yellow_max.getValue() == 255
42+
43+
44+
def test_default_normalize_lightness_is_50():
45+
assert PeelColor().normalize_lightness.getValue() == 50
46+
47+
48+
def test_mean_values_length_match():
49+
a = PeelColor()
50+
assert len(a.MEAN_VALUES_A) == len(a.MEAN_VALUES_B) == len(a.SCORE)
51+
52+
53+
def test_line_points_are_2d():
54+
a = PeelColor()
55+
assert a.LINE_POINT_1.shape == (2,)
56+
assert a.LINE_POINT_2.shape == (2,)
57+
58+
59+
# ---------------------------------------------------------------------------
60+
# remove_purple
61+
# ---------------------------------------------------------------------------
62+
63+
def test_remove_purple_output_shape():
64+
a = PeelColor()
65+
img = np.full((50, 50, 3), 128, dtype=np.uint8)
66+
result = a.remove_purple(img)
67+
assert result.shape == img.shape
68+
69+
70+
def test_remove_purple_does_not_modify_original():
71+
a = PeelColor()
72+
img = np.full((50, 50, 3), 100, dtype=np.uint8)
73+
original = img.copy()
74+
a.remove_purple(img)
75+
np.testing.assert_array_equal(img, original)
76+
77+
78+
# ---------------------------------------------------------------------------
79+
# calculate_bin_distance
80+
# ---------------------------------------------------------------------------
81+
82+
def test_calculate_bin_distance_euclidean_returns_valid_bin():
83+
a = PeelColor()
84+
bin_num, dist = a.calculate_bin_distance([-20.0, 70.0], method="Euclidean")
85+
assert 1 <= bin_num <= len(a.SCORE)
86+
87+
88+
def test_calculate_bin_distance_score_method():
89+
a = PeelColor()
90+
bin_num, dist = a.calculate_bin_distance([0.6], method="Score")
91+
assert 1 <= bin_num <= len(a.SCORE)
92+
93+
94+
def test_calculate_bin_distance_x_component():
95+
a = PeelColor()
96+
bin_num, _ = a.calculate_bin_distance([-20.0, 70.0], method="X-component")
97+
assert 1 <= bin_num <= len(a.SCORE)
98+
99+
100+
def test_calculate_bin_distance_y_component():
101+
a = PeelColor()
102+
bin_num, _ = a.calculate_bin_distance([-20.0, 70.0], method="Y-component")
103+
assert 1 <= bin_num <= len(a.SCORE)
Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,66 @@
1+
import pytest
12
from Granny.Analyses.Segmentation import Segmentation
23

4+
35
def test_performAnalysis():
46
analysis = Segmentation()
5-
7+
8+
9+
# ---------------------------------------------------------------------------
10+
# Default parameter values
11+
# ---------------------------------------------------------------------------
12+
13+
def test_default_conf_threshold_is_0_7():
14+
assert Segmentation().conf_threshold.getValue() == pytest.approx(0.7)
15+
16+
17+
def test_default_iou_threshold():
18+
assert Segmentation().iou_threshold.getValue() == pytest.approx(0.45)
19+
20+
21+
def test_default_font_scale():
22+
s = Segmentation()
23+
assert s.font_scale.getValue() == pytest.approx(2.0)
24+
25+
26+
def test_default_text_thickness_is_3():
27+
assert Segmentation().text_thickness.getValue() == 3
28+
29+
30+
def test_default_text_color_is_black():
31+
s = Segmentation()
32+
assert s.text_color_r.getValue() == 0
33+
assert s.text_color_g.getValue() == 0
34+
assert s.text_color_b.getValue() == 0
35+
36+
37+
def test_text_color_boundary_values():
38+
s = Segmentation()
39+
s.text_color_r.setValue(255)
40+
s.text_color_g.setValue(255)
41+
s.text_color_b.setValue(255)
42+
assert s.text_color_r.getValue() == 255
43+
assert s.text_color_g.getValue() == 255
44+
assert s.text_color_b.getValue() == 255
45+
46+
47+
def test_conf_threshold_range():
48+
s = Segmentation()
49+
s.conf_threshold.setValue(0.0)
50+
assert s.conf_threshold.getValue() == pytest.approx(0.0)
51+
s.conf_threshold.setValue(1.0)
52+
assert s.conf_threshold.getValue() == pytest.approx(1.0)
53+
54+
55+
def test_qr_detector_initialized():
56+
from Granny.Utils.QRCodeDetector import QRCodeDetector
57+
s = Segmentation()
58+
assert isinstance(s.qr_detector, QRCodeDetector)
59+
60+
61+
def test_variety_info_initially_none():
62+
assert Segmentation().variety_info is None
63+
64+
65+
def test_analysis_name_is_segmentation():
66+
assert Segmentation().__analysis_name__ == "segmentation"

0 commit comments

Comments
 (0)