Coverage for gws-app/gws/base/legend/_test.py: 100%

49 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-16 23:09 +0200

1"""Tests for the legend module.""" 

2 

3import gws 

4import gws.test.util as u 

5import gws.base.legend as legend 

6import gws.lib.image as image 

7import unittest.mock as mock 

8 

9 

10def test_output_to_bytes(tmp_path): 

11 img = image.from_size((50, 50), color=(255, 0, 0, 255)) 

12 img.to_path(str(tmp_path / 'img.png')) 

13 lro = gws.LegendRenderOutput( 

14 html=f"<img src='{str(tmp_path / 'img.png')}' alt='image'>", 

15 image=img, 

16 image_path=str(tmp_path / 'img.png'), 

17 size=gws.Size((50.0, 50.5)), 

18 mime="image/jpeg" 

19 ) 

20 assert legend.output_to_bytes(lro) == img.to_bytes() 

21 

22 

23def test_output_to_bytes_none(tmp_path): 

24 lro = gws.LegendRenderOutput( 

25 html=f"<img src='{str(tmp_path / 'img.png')}' alt='image'>", 

26 image=None, 

27 image_path=None, 

28 size=gws.Size((50.0, 50.5)), 

29 mime="image/jpeg" 

30 ) 

31 assert not legend.output_to_bytes(lro) 

32 

33 

34def test_output_to_image(tmp_path): 

35 img = image.from_size((50, 50), color=(255, 0, 0, 255)) 

36 img.to_path(str(tmp_path / 'img.png')) 

37 lro = gws.LegendRenderOutput( 

38 html=f"<img src='{str(tmp_path / 'img.png')}' alt='image'>", 

39 image=img, 

40 image_path=str(tmp_path / 'img.png'), 

41 size=gws.Size((50.0, 50.5)), 

42 mime="image/jpeg" 

43 ) 

44 assert legend.output_to_image(lro).compare_to(img) == 0 

45 

46 

47def test_output_to_image_from_path(tmp_path): 

48 img = image.from_size((50, 50), color=(255, 0, 0, 255)) 

49 img.to_path(str(tmp_path / 'img.png')) 

50 lro = gws.LegendRenderOutput( 

51 html=f"<img src='{str(tmp_path / 'img.png')}' alt='image'>", 

52 image=None, 

53 image_path=str(tmp_path / 'img.png'), 

54 size=gws.Size((50.0, 50.5)), 

55 mime="image/jpeg" 

56 ) 

57 assert legend.output_to_image(lro).compare_to(img) == 0 

58 

59 

60def test_output_to_image_none(tmp_path): 

61 lro = gws.LegendRenderOutput( 

62 html=f"<img src='{str(tmp_path / 'img.png')}' alt='image'>", 

63 image=None, 

64 image_path=None, 

65 size=gws.Size((50.0, 50.5)), 

66 mime="image/jpeg" 

67 ) 

68 assert not legend.output_to_image(lro) 

69 

70 

71# used mock to avoid randomized naming of the image path 

72def test_output_to_image_path(tmp_path): 

73 with mock.patch('gws.u.ephemeral_path', return_value=str(tmp_path / 'test_legend.png')): 

74 img = image.from_size((50, 50), color=(255, 0, 0, 255)) 

75 img.to_path(str(tmp_path / 'img.png')) 

76 lro = gws.LegendRenderOutput( 

77 html=f"<img src='{str(tmp_path / 'img.png')}' alt='image'>", 

78 image=img, 

79 image_path=str(tmp_path / 'img.png'), 

80 size=gws.Size((50.0, 50.5)), 

81 mime="image/jpeg" 

82 ) 

83 assert legend.output_to_image_path(lro) == str(tmp_path / 'test_legend.png') 

84 assert image.from_path(legend.output_to_image_path(lro)).compare_to(img) == 0 

85 

86 

87def test_combine_outputs(tmp_path): 

88 img = image.from_size((5, 10), color=(255, 255, 255, 255)) 

89 red = image.from_size((5, 5), color=(255, 0, 0, 255)) 

90 blue = image.from_size((5, 5), color=(0, 0, 255, 255)) 

91 img.paste(red, where=(0, 0)) 

92 img.paste(blue, where=(0, 5)) 

93 img.to_path(str(tmp_path / 'combined.png')) 

94 

95 red.to_path(str(tmp_path / 'red.png')) 

96 blue.to_path(str(tmp_path / 'blue.png')) 

97 

98 lro_1 = gws.LegendRenderOutput( 

99 html=f"<img src='{str(tmp_path / 'red.png')}' alt='red'>", 

100 image=red, 

101 image_path=str(tmp_path / 'red.png'), 

102 size=gws.Size((5.0, 5.0)), 

103 mime="image/jpeg" 

104 ) 

105 

106 lro_2 = gws.LegendRenderOutput( 

107 html=f"<img src='{str(tmp_path / 'blue.png')}' alt='blue'>", 

108 image=blue, 

109 image_path=str(tmp_path / 'blue.png'), 

110 size=gws.Size((5.0, 5.0)), 

111 mime="image/jpeg" 

112 ) 

113 assert legend.combine_outputs([lro_1, lro_2]).image.compare_to(img) == 0 

114 

115 

116def test_combine_outputs_none(tmp_path): 

117 lro_1 = gws.LegendRenderOutput( 

118 html=f"<img src='{str(tmp_path / 'red.png')}' alt='red'>", 

119 image=None, 

120 image_path=None, 

121 size=gws.Size((5.0, 5.0)), 

122 mime="image/jpeg" 

123 ) 

124 

125 lro_2 = gws.LegendRenderOutput( 

126 html=f"<img src='{str(tmp_path / 'blue.png')}' alt='blue'>", 

127 image=None, 

128 image_path=None, 

129 size=gws.Size((5.0, 5.0)), 

130 mime="image/jpeg" 

131 ) 

132 assert not legend.combine_outputs([lro_1, lro_2])