Coverage for gws-app/gws/lib/uom/_test.py: 0%

96 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-17 01:37 +0200

1"""Tests for the uom module.""" 

2 

3import gws 

4import gws.lib.uom as uom 

5import gws.test.util as u 

6 

7 

8def test_scale_to_res(): 

9 value = 1000 

10 assert uom.scale_to_res(value) == value * uom.OGC_M_PER_PX 

11 

12 

13def test_res_to_scale(): 

14 value = 1000 

15 assert uom.res_to_scale(value) == int(value / uom.OGC_M_PER_PX) 

16 

17 

18def test_mm_to_px(): 

19 value = 1000 

20 ppi = 32 

21 assert uom.mm_to_px(value, ppi) == (value * ppi) / uom.MM_PER_IN 

22 

23 

24def test_to_px(): 

25 """ 

26 Test general conversion of UomValue to pixels 

27 

28 In it's current state we test the ability to convert 

29 all supported units, not the correctness of these conversions. 

30 

31 #TODO: Create Tests for correctness of conversions according to 

32 # whatever standards there are for a given unit. 

33 """ 

34 # obj_uom = gws.core.types.Uom() 

35 # TODO use this line to test aginst all units, but all units are not implemented yet 

36 # units = [attr for attr in dir(obj_uom) if not callable(getattr(obj_uom, attr)) and not attr.startswith("_")] 

37 # units = [gws.Uom.mm, gws.Uom.px] 

38 value = 1000 

39 ppi = 32 

40 for unit in [name for name in dir(gws.Uom) if not name.startswith('_')]: 

41 match unit: 

42 case gws.Uom.mm: 

43 assert uom.to_px((value, gws.Uom.mm), ppi) == (uom.mm_to_px(value, ppi), gws.Uom.px) 

44 case gws.Uom.px: 

45 assert uom.to_px((value, gws.Uom.px), ppi) == (value, gws.Uom.px) 

46 case _: 

47 assert True 

48 

49 

50def test_size_mm_to_px(): 

51 valuex = 123 

52 valuey = 321 

53 valuexy = (valuex, valuey) 

54 ppi = 32 

55 assert uom.size_mm_to_px(valuexy, ppi) == (uom.mm_to_px(valuex, ppi), uom.mm_to_px(valuey, ppi)) 

56 

57 

58def test_size_to_px(): 

59 valuex = 1 

60 valuey = 2 

61 ppi = 123 

62 for unit in [name for name in dir(gws.Uom) if not name.startswith('_')]: 

63 match unit: 

64 case gws.Uom.mm: 

65 assert uom.size_to_px((valuex, valuey, gws.Uom.mm), ppi) == ( 

66 uom.mm_to_px(valuex, ppi), uom.mm_to_px(valuey, ppi), gws.Uom.px) 

67 case gws.Uom.px: 

68 assert uom.size_to_px((valuex, valuey, gws.Uom.px), ppi) == (valuex, valuey, gws.Uom.px) 

69 case _: 

70 assert True 

71 

72 

73def test_px_to_mm(): 

74 value = 1000 

75 ppi = 32 

76 assert uom.px_to_mm(value, ppi) == (value / ppi) * uom.MM_PER_IN 

77 

78 

79def test_to_mm(): 

80 # TODO similar to test_to_px we could test for all possible units here once their implemented 

81 value = 1000 

82 ppi = 32 

83 for unit in [name for name in dir(gws.Uom) if not name.startswith('_')]: 

84 match unit: 

85 case gws.Uom.px: 

86 assert uom.to_mm((value, gws.Uom.px), ppi) == (uom.px_to_mm(value, ppi), gws.Uom.mm) 

87 case gws.Uom.mm: 

88 assert uom.to_mm((value, gws.Uom.mm), ppi) == (value, gws.Uom.mm) 

89 case _: 

90 assert True 

91 

92 

93def test_size_px_to_mm(): 

94 valuex = 1000 

95 valuey = 2000 

96 size = valuex, valuey 

97 ppi = 32 

98 assert uom.size_px_to_mm(size, ppi) == (uom.px_to_mm(valuex, ppi), uom.px_to_mm(valuey, ppi)) 

99 

100 

101# not sure if this is covered by test_size_mm_to_px() 

102def test_size_to_px(): 

103 """ 

104 Assuming test_mm_to_px() passes, this should be sufficient 

105 """ 

106 valuex = 123 

107 valuey = 321 

108 valuexy = (valuex, valuey) 

109 ppi = 32 

110 assert uom.size_mm_to_px(valuexy, ppi) == (uom.mm_to_px(valuex, ppi), uom.mm_to_px(valuey, ppi)) 

111 

112 

113def test_size_to_mm(): 

114 valuex = 1 

115 valuey = 2 

116 ppi = 123 

117 for unit in [name for name in dir(gws.Uom) if not name.startswith('_')]: 

118 match unit: 

119 case gws.Uom.px: 

120 assert uom.size_to_mm((valuex, valuey, gws.Uom.px), ppi) == ( 

121 uom.px_to_mm(valuex, ppi), uom.px_to_mm(valuey, ppi), gws.Uom.mm) 

122 case gws.Uom.mm: 

123 assert uom.size_to_mm((valuex, valuey, gws.Uom.mm), ppi) == (valuex, valuey, gws.Uom.mm) 

124 case _: 

125 assert True 

126 

127 

128 

129def test_to_str(): 

130 assert uom.to_str((1, gws.Uom.mm)) == '1mm' 

131 assert uom.to_str((1.0, gws.Uom.mm)) == '1mm' 

132 assert uom.to_str((1.1, gws.Uom.m)) == '1.1m' 

133 

134 

135def test_parse(): 

136 inputs = [ 

137 ('1 mm', (1.0, 'mm')), 

138 ('10 km', (10.0, 'km')), 

139 ('-10 deg', (-10.0, 'deg')), 

140 # ('1,2 m', (1.2, 'm')), 

141 ('1.245 inch', (1.245, 'in')), 

142 # ('0.21 us-in',(0.21, 'us-in')) 

143 ] 

144 for test, res in inputs: 

145 assert uom.parse(test) == res 

146 

147 

148def test_parse_errors(): 

149 with u.raises(ValueError, match=f'missing unit:'): 

150 uom.parse(1) 

151 with u.raises(ValueError, match=f'invalid format:'): 

152 uom.parse('foo 1') 

153 with u.raises(ValueError, match=f'invalid unit:'): 

154 uom.parse('1 bar') 

155 

156