1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
""" |
4
|
|
|
Created on Wed Mar 27 14:35:36 2019 |
5
|
|
|
|
6
|
|
|
@author: Paolo Cozzi <[email protected]> |
7
|
|
|
""" |
8
|
|
|
|
9
|
|
|
from django.test import TestCase |
10
|
|
|
from django.utils.dateparse import parse_date |
11
|
|
|
from django.core import mail |
12
|
|
|
from django.core.validators import validate_email |
13
|
|
|
|
14
|
|
|
from .. import constants |
15
|
|
|
from ..helpers import ( |
16
|
|
|
format_attribute, get_admin_emails, image_timedelta, parse_image_timedelta, |
17
|
|
|
uid2biosample, send_mail_to_admins) |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
class TestImageTimedelta(TestCase): |
21
|
|
|
"""A class to test common.helpers.image_timedelta functions""" |
22
|
|
|
|
23
|
|
|
def test_years(self): |
24
|
|
|
t1 = parse_date("2019-03-27") |
25
|
|
|
t2 = parse_date("2018-03-27") |
26
|
|
|
|
27
|
|
|
years, units = image_timedelta(t1, t2) |
28
|
|
|
|
29
|
|
|
self.assertEqual(years, 1) |
30
|
|
|
self.assertEqual(units, constants.YEARS) |
31
|
|
|
|
32
|
|
|
with self.assertLogs('common.helpers', level="WARNING") as cm: |
33
|
|
|
# assert date inversion (returns None) |
34
|
|
|
years, units = image_timedelta(t2, t1) |
35
|
|
|
|
36
|
|
|
self.assertEqual(years, None) |
37
|
|
|
self.assertEqual(units, constants.YEARS) |
38
|
|
|
self.assertEqual(len(cm.output), 1) |
39
|
|
|
self.assertIn("t2>t1", cm.output[0]) |
40
|
|
|
|
41
|
|
|
def test_months(self): |
42
|
|
|
t1 = parse_date("2019-03-27") |
43
|
|
|
t2 = parse_date("2019-01-27") |
44
|
|
|
|
45
|
|
|
months, units = image_timedelta(t1, t2) |
46
|
|
|
|
47
|
|
|
self.assertEqual(months, 2) |
48
|
|
|
self.assertEqual(units, constants.MONTHS) |
49
|
|
|
|
50
|
|
|
def test_days(self): |
51
|
|
|
t1 = parse_date("2019-03-27") |
52
|
|
|
t2 = parse_date("2019-03-20") |
53
|
|
|
|
54
|
|
|
days, units = image_timedelta(t1, t2) |
55
|
|
|
|
56
|
|
|
self.assertEqual(days, 7) |
57
|
|
|
self.assertEqual(units, constants.DAYS) |
58
|
|
|
|
59
|
|
View Code Duplication |
def test_unknown_date(self): |
|
|
|
|
60
|
|
|
t1 = parse_date("2019-03-20") |
61
|
|
|
t2 = parse_date("1900-01-01") |
62
|
|
|
|
63
|
|
|
with self.assertLogs('common.helpers', level="WARNING") as cm: |
64
|
|
|
years, units = image_timedelta(t1, t2) |
65
|
|
|
|
66
|
|
|
self.assertIsNone(years) |
67
|
|
|
self.assertEqual(units, constants.YEARS) |
68
|
|
|
self.assertEqual(len(cm.output), 1) |
69
|
|
|
self.assertIn("Ignoring one date", cm.output[0]) |
70
|
|
|
|
71
|
|
View Code Duplication |
def test_null_date(self): |
|
|
|
|
72
|
|
|
t1 = None |
73
|
|
|
t2 = parse_date("1900-01-01") |
74
|
|
|
|
75
|
|
|
with self.assertLogs('common.helpers', level="WARNING") as cm: |
76
|
|
|
years, units = image_timedelta(t1, t2) |
77
|
|
|
|
78
|
|
|
self.assertIsNone(years) |
79
|
|
|
self.assertEqual(units, constants.YEARS) |
80
|
|
|
self.assertEqual(len(cm.output), 1) |
81
|
|
|
self.assertIn("One date is NULL", cm.output[0]) |
82
|
|
|
|
83
|
|
|
def test_parse_image_timedelta(self): |
84
|
|
|
interval = "7 days" |
85
|
|
|
reference = (7, 2) # days in common constants |
86
|
|
|
|
87
|
|
|
test = parse_image_timedelta(interval) |
88
|
|
|
self.assertEqual(reference, test) |
89
|
|
|
|
90
|
|
|
interval = "1 year" |
91
|
|
|
reference = (1, 5) # years in common constants |
92
|
|
|
|
93
|
|
|
test = parse_image_timedelta(interval) |
94
|
|
|
self.assertEqual(reference, test) |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
class TestAttributes(TestCase): |
98
|
|
|
|
99
|
|
|
def test_format_attribute(self): |
100
|
|
|
reference = [{ |
101
|
|
|
"value": "54.20944444444445", |
102
|
|
|
"units": "Decimal degrees" |
103
|
|
|
}] |
104
|
|
|
|
105
|
|
|
test = format_attribute( |
106
|
|
|
value="54.20944444444445", |
107
|
|
|
units="Decimal degrees") |
108
|
|
|
|
109
|
|
|
self.assertEqual(reference, test, msg="testing units") |
110
|
|
|
|
111
|
|
|
# another test |
112
|
|
|
reference = [{ |
113
|
|
|
"value": "organism", |
114
|
|
|
"terms": [{ |
115
|
|
|
"url": "%s/OBI_0100026" % (constants.OBO_URL) |
116
|
|
|
}] |
117
|
|
|
}] |
118
|
|
|
|
119
|
|
|
test = format_attribute( |
120
|
|
|
value="organism", |
121
|
|
|
terms="OBI_0100026") |
122
|
|
|
|
123
|
|
|
self.assertEqual(reference, test, msg="testing terms") |
124
|
|
|
|
125
|
|
|
def test_null(self): |
126
|
|
|
test = format_attribute(value=None) |
127
|
|
|
|
128
|
|
|
self.assertIsNone(test) |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
class TestAdminEmails(TestCase): |
132
|
|
|
|
133
|
|
|
def setUp(self): |
134
|
|
|
# tracking admin emails |
135
|
|
|
self.admin_emails = get_admin_emails() |
136
|
|
|
|
137
|
|
|
def test_admin_emails(self): |
138
|
|
|
"""Test admin emails are valid""" |
139
|
|
|
|
140
|
|
|
for email in self.admin_emails: |
141
|
|
|
self.assertIsNone(validate_email(email)) |
142
|
|
|
|
143
|
|
|
def test_send_mail_to_admins(self): |
144
|
|
|
"""Test mails sent to admin""" |
145
|
|
|
|
146
|
|
|
# sending mails |
147
|
|
|
send_mail_to_admins("subject", "body") |
148
|
|
|
|
149
|
|
|
# checking messages |
150
|
|
|
email = mail.outbox[0] |
151
|
|
|
self.assertEqual(email.subject, "subject") |
152
|
|
|
self.assertEqual(email.body, "body") |
153
|
|
|
self.assertEqual(email.to, self.admin_emails) |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
class TestUid2Biosample(TestCase): |
157
|
|
|
|
158
|
|
|
def test_uid2biosample(self): |
159
|
|
|
test = uid2biosample('Sample storage') |
160
|
|
|
self.assertEqual(test, 'storage') |
161
|
|
|
|
162
|
|
|
test = uid2biosample('Sample storage processing') |
163
|
|
|
self.assertEqual(test, 'storage_processing') |
164
|
|
|
|
165
|
|
|
test = uid2biosample('Sampling to preparation interval') |
166
|
|
|
self.assertEqual(test, 'preparation_interval_units') |
167
|
|
|
|
168
|
|
|
test = uid2biosample('Specimen collection protocol') |
169
|
|
|
self.assertEqual(test, 'protocol') |
170
|
|
|
|
171
|
|
|
test = uid2biosample('meow bark') |
172
|
|
|
self.assertEqual(test, 'meow_bark') |
173
|
|
|
|