Coverage for test/test_functions.py: 100%

131 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-12 17:57 +0000

1import pytest 

2 

3from util.functions import * 

4from util.constants import ROOTS 

5from test.data_for_tests import * 

6 

7 

8class TestFunctions: 

9 

10 def test_get_association(self): 

11 test_obj = get_association(annotations[0], 'upon') 

12 assert test_obj == { 

13 "uuid": "c4eaa100-4bee-46a9-0f65-6525fb69d41e", 

14 "link_name": "upon", 

15 "to_concept": "sed", 

16 "link_value": "nil", 

17 "mime_type": "text/plain" 

18 } 

19 

20 def test_get_association_none(self): 

21 test_obj = get_association(annotations[0], 'test') 

22 assert test_obj == {} 

23 

24 def test_get_associations_list(self): 

25 test_list = get_associations_list(annotations[1], 's2') 

26 assert test_list == [{ 

27 "uuid": "a1c3990e-3566-4832-4d6d-6e4fb25dd41e", 

28 "link_name": "s2", 

29 "to_concept": "mantra", 

30 "link_value": "nil", 

31 "mime_type": "text/plain" 

32 }] 

33 

34 def test_get_associations_list_none(self): 

35 test_list = get_associations_list(annotations[0], 's2') 

36 assert test_list == [] 

37 

38 def test_grain_size(self): 

39 test_size = grain_size(annotations[1]['associations'][1]['to_concept']) 

40 print(test_size) 

41 assert test_size == 13 

42 

43 def test_grain_size_no_match(self): 

44 root_index = grain_size(no_match) 

45 assert root_index == len(ROOTS) 

46 

47 def test_get_date_time(self): 

48 date_time = get_date_and_time(list_data) 

49 assert date_time == datetime(2014, 9, 8, 0, 33, 49) 

50 

51 def test_parse_datetime_micro(self): 

52 date_time = parse_datetime('2014-09-05T14:08:41.492Z') 

53 assert date_time == datetime(2014, 9, 5, 14, 8, 41, 492000) 

54 

55 def test_parse_datetime_no_micro(self): 

56 date_time = parse_datetime('2014-09-05T14:08:41Z') 

57 assert date_time == datetime(2014, 9, 5, 14, 8, 41) 

58 

59 def test_parse_datetime_fail(self): 

60 with pytest.raises(Exception): 

61 parse_datetime('fail') 

62 

63 def test_extract_time_no_micro(self): 

64 date_time = extract_time(annotations[0]) 

65 assert date_time == datetime(2014, 9, 5, 20, 6, 26) 

66 

67 def test_extract_time_round_up(self): 

68 date_time = extract_time(annotations[1]) 

69 assert date_time == datetime(2014, 9, 5, 14, 37, 58) 

70 

71 def test_extract_time_round_down(self): 

72 date_time = extract_time(annotations[2]) 

73 assert date_time == datetime(2014, 9, 20, 14, 13, 23) 

74 

75 def test_extract_uuid(self): 

76 uuid = extract_uuid(annotations[2]) 

77 assert uuid == '080118db-baa2-468a-d06a-144249c1d41e' 

78 

79 def test_add_meters_no_m(self): 

80 accuracy = add_meters('50') 

81 assert accuracy == '50m' 

82 

83 def test_add_meters_m(self): 

84 accuracy = add_meters('50m') 

85 assert accuracy == '50m' 

86 

87 def test_convert_username_to_name(self): 

88 test_name = convert_username_to_name('SarahBingo') 

89 assert test_name == 'Bingo, Sarah' 

90 

91 def test_convert_username_to_name_fail(self): 

92 test_name = convert_username_to_name('Sarahbingo') 

93 assert test_name == 'Sarahbingo' 

94 

95 def test_translate_substrate_code_same(self): 

96 test_translated = translate_substrate_code('pebble') 

97 assert test_translated == 'pebble' 

98 

99 def test_translate_substrate_code_tube(self): 

100 test_translated = translate_substrate_code('tube') 

101 assert test_translated == 'Animal-made tube' 

102 

103 def test_translate_substrate_code_simple(self): 

104 test_translated = translate_substrate_code('bed') 

105 assert test_translated == 'bedrock' 

106 

107 def test_translate_substrate_code_complex1(self): 

108 test_translated = translate_substrate_code('pibed') 

109 assert test_translated == 'pillow lava formation of bedrock' 

110 

111 def test_translate_substrate_code_complex2(self): 

112 test_translated = translate_substrate_code('sedmn') 

113 assert test_translated == 'sediment with manganese crust' 

114 

115 def test_translate_substrate_code_complex3(self): 

116 test_translated = translate_substrate_code('boucre') 

117 assert test_translated == 'boulder crevice' 

118 

119 def test_translate_substrate_code_fail(self): 

120 test_translated = translate_substrate_code('hehehe') 

121 assert test_translated == '' 

122 

123 def test_collapse_id_records(self): 

124 test_dupes_removed = collapse_id_records(sample_report_records) 

125 assert test_dupes_removed == 1 

126 assert sample_report_records == sample_report_records_collapsed 

127 

128 def test_collapse_id_records_none(self): 

129 test_dupes_removed = collapse_id_records(sample_report_records_collapsed) 

130 assert test_dupes_removed == 0 

131 assert sample_report_records_collapsed == sample_report_records_collapsed 

132 

133 def test_find_associated_taxa_host_before(self): 

134 """ Check for a host recorded before associate """ 

135 find_associated_taxa(report_records=sample_records_for_associates, concepts=sample_concepts, 

136 warning_messages=[]) 

137 assert sample_records_for_associates[18][ASSOCIATED_TAXA] == 'Ophiacanthidae' 

138 assert 'associate touching host' in sample_records_for_associates[18][OCCURRENCE_COMMENTS] 

139 

140 def test_find_associated_taxa_host_same_time(self): 

141 """ Check for a host recorded at the same time as the associate """ 

142 find_associated_taxa(report_records=sample_records_for_associates, concepts=sample_concepts, 

143 warning_messages=[]) 

144 assert sample_records_for_associates[14][ASSOCIATED_TAXA] == 'Ophiacanthidae' 

145 assert 'associate touching host' in sample_records_for_associates[14][OCCURRENCE_COMMENTS] 

146 

147 def test_find_associated_taxa_host_future(self): 

148 """ Check for a host recorded after the associate (fail) """ 

149 warnings = [] 

150 find_associated_taxa(report_records=sample_records_for_associates, concepts=sample_concepts, 

151 warning_messages=warnings) 

152 assert sample_records_for_associates[2][ASSOCIATED_TAXA] == NULL_VAL_STRING 

153 print(warnings) 

154 assert 'Upon not found in previous records' in warnings[1][3] 

155 

156 def test_find_associated_taxa_prev_dive(self): 

157 """ Associate not found in current dive, but exists in previous dive (fail) """ 

158 warnings = [] 

159 find_associated_taxa(report_records=sample_records_for_associates, concepts=sample_concepts, 

160 warning_messages=warnings) 

161 assert sample_records_for_associates[1][ASSOCIATED_TAXA] == NULL_VAL_STRING 

162 assert 'Upon not found in previous records' in warnings[0][3] 

163 

164 def test_find_associated_taxa_host_same_concept(self): 

165 """ Check for a record where the host and the associate are the same concept """ 

166 find_associated_taxa(report_records=sample_records_for_associates, concepts=sample_concepts, 

167 warning_messages=[]) 

168 assert sample_records_for_associates[3][ASSOCIATED_TAXA] == 'Narella sp.' 

169 assert 'associate touching host' in sample_records_for_associates[3][OCCURRENCE_COMMENTS] 

170 

171 def test_find_associated_taxa_multiple_associates(self): 

172 """ Check for a record where multiple associates are on the same host """ 

173 find_associated_taxa(report_records=sample_records_for_associates, concepts=sample_concepts, 

174 warning_messages=[]) 

175 assert sample_records_for_associates[7][ASSOCIATED_TAXA] == 'Keratoisididae unbranched | Alternatipathes cf. alternata' 

176 assert 'associate touching host' in sample_records_for_associates[7][OCCURRENCE_COMMENTS] 

177 

178 def test_find_associated_taxa_upon_not_in_concepts(self): 

179 """ Check for an 'upon' that is not in concepts """ 

180 warnings = [] 

181 find_associated_taxa(report_records=sample_records_for_associates, concepts=sample_concepts, 

182 warning_messages=warnings) 

183 assert sample_records_for_associates[20][ASSOCIATED_TAXA] == NULL_VAL_STRING 

184 assert 'My Special Concept' in warnings[2][3] 

185 

186 def test_find_associated_taxa_upon_over_one_min(self): 

187 """ Check for an 'upon' that is reported over one minute after its host """ 

188 warnings = [] 

189 find_associated_taxa(report_records=sample_records_for_associates, concepts=sample_concepts, 

190 warning_messages=warnings) 

191 assert sample_records_for_associates[18][ASSOCIATED_TAXA] == 'Ophiacanthidae' 

192 assert 'greater than 1 minute' in warnings[3][3] 

193 

194 def test_find_associated_taxa_upon_over_five_mins(self): 

195 """ Check for an 'upon' that is reported over five minutes after its host """ 

196 warnings = [] 

197 find_associated_taxa(report_records=sample_records_for_associates, concepts=sample_concepts, 

198 warning_messages=warnings) 

199 assert sample_records_for_associates[18][ASSOCIATED_TAXA] == 'Ophiacanthidae' 

200 assert 'greater than 5 minutes' in warnings[4][3]