Coverage for test / test_functions.py: 100%

38 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-23 05:22 +0000

1from datetime import datetime 

2 

3import pytest 

4 

5from application.util.functions import extract_recorded_datetime, format_annotator, flatten_taxa_tree, get_association, parse_datetime 

6from test.data.vars_responses import ex_23060001 

7from test.data.worms_responses import clownfish_tree 

8 

9 

10class TestFunctions: 

11 def test_parse_datetime_micro(self): 

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

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

14 

15 def test_parse_datetime_no_micro(self): 

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

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

18 

19 def test_parse_datetime_fail(self): 

20 with pytest.raises(Exception): 

21 parse_datetime('fail') 

22 

23 def test_extract_recorded_datetime_no_micro(self): 

24 date_time = extract_recorded_datetime(ex_23060001['annotations'][0]) 

25 assert date_time == datetime(2023, 8, 24, 18, 36, 14) 

26 

27 def test_extract_recorded_datetime_round_up(self): 

28 date_time = extract_recorded_datetime(ex_23060001['annotations'][1]) 

29 assert date_time == datetime(2023, 8, 24, 21, 28, 26) 

30 

31 def test_extract_recorded_datetime_round_down(self): 

32 date_time = extract_recorded_datetime(ex_23060001['annotations'][2]) 

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

34 

35 def test_extract_recorded_datetime_fail(self): 

36 assert extract_recorded_datetime({}) is None 

37 

38 def test_get_association(self): 

39 test_obj = get_association(ex_23060001['annotations'][1], 'upon') 

40 assert test_obj == { 

41 'uuid': 'c4eaa100-upon', 

42 'link_name': 'upon', 

43 'to_concept': 'sed', 

44 'link_value': 'nil', 

45 'mime_type': 'text/plain' 

46 } 

47 

48 def test_get_association_none(self): 

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

50 assert test_obj == {} 

51 

52 def test_format_annotator_normal(self): 

53 assert format_annotator(ex_23060001['annotations'][0]['observer']) == 'Nikki Cunanan' 

54 

55 def test_format_annotator_harold(self): 

56 assert format_annotator('hcarlson') == 'Harold Carlson' 

57 

58 def test_flatten_taxa_tree(self): 

59 assert flatten_taxa_tree(clownfish_tree, {}) == { 

60 'superdomain': 'Biota', 

61 'kingdom': 'Animalia', 

62 'phylum': 'Chordata', 

63 'subphylum': 'Vertebrata', 

64 'infraphylum': 'Gnathostomata', 

65 'parvphylum': 'Osteichthyes', 

66 'gigaclass': 'Actinopterygii', 

67 'superclass': 'Actinopteri', 

68 'class': 'Teleostei', 

69 'order': 'Ovalentaria incertae sedis', 

70 'family': 'Pomacentridae', 

71 'subfamily': 'Amphiprioninae', 

72 }