Passed
Pull Request — master (#41)
by Paolo
07:36
created

TestUid2Biosample.test_uid2biosample()   A

Complexity

Conditions 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 12
rs 9.95
c 0
b 0
f 0
cc 1
nop 1
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.validators import validate_email
12
13
from .. import constants
14
from ..helpers import (
15
    format_attribute, get_admin_emails, image_timedelta, parse_image_timedelta,
16
    uid2biosample)
17
18
19
class TestImageTimedelta(TestCase):
20
    """A class to test common.helpers.image_timedelta functions"""
21
22
    def test_years(self):
23
        t1 = parse_date("2019-03-27")
24
        t2 = parse_date("2018-03-27")
25
26
        years, units = image_timedelta(t1, t2)
27
28
        self.assertEqual(years, 1)
29
        self.assertEqual(units, constants.YEARS)
30
31
        with self.assertLogs('common.helpers', level="WARNING") as cm:
32
            # assert date inversion (returns None)
33
            years, units = image_timedelta(t2, t1)
34
35
        self.assertEqual(years, None)
36
        self.assertEqual(units, constants.YEARS)
37
        self.assertEqual(len(cm.output), 1)
38
        self.assertIn("t2>t1", cm.output[0])
39
40
    def test_months(self):
41
        t1 = parse_date("2019-03-27")
42
        t2 = parse_date("2019-01-27")
43
44
        months, units = image_timedelta(t1, t2)
45
46
        self.assertEqual(months, 2)
47
        self.assertEqual(units, constants.MONTHS)
48
49
    def test_days(self):
50
        t1 = parse_date("2019-03-27")
51
        t2 = parse_date("2019-03-20")
52
53
        days, units = image_timedelta(t1, t2)
54
55
        self.assertEqual(days, 7)
56
        self.assertEqual(units, constants.DAYS)
57
58 View Code Duplication
    def test_unknown_date(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
59
        t1 = parse_date("2019-03-20")
60
        t2 = parse_date("1900-01-01")
61
62
        with self.assertLogs('common.helpers', level="WARNING") as cm:
63
            years, units = image_timedelta(t1, t2)
64
65
        self.assertIsNone(years)
66
        self.assertEqual(units, constants.YEARS)
67
        self.assertEqual(len(cm.output), 1)
68
        self.assertIn("Ignoring one date", cm.output[0])
69
70 View Code Duplication
    def test_null_date(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
71
        t1 = None
72
        t2 = parse_date("1900-01-01")
73
74
        with self.assertLogs('common.helpers', level="WARNING") as cm:
75
            years, units = image_timedelta(t1, t2)
76
77
        self.assertIsNone(years)
78
        self.assertEqual(units, constants.YEARS)
79
        self.assertEqual(len(cm.output), 1)
80
        self.assertIn("One date is NULL", cm.output[0])
81
82
    def test_parse_image_timedelta(self):
83
        interval = "7 days"
84
        reference = (7, 2)  # days in common constants
85
86
        test = parse_image_timedelta(interval)
87
        self.assertEqual(reference, test)
88
89
        interval = "1 year"
90
        reference = (1, 5)  # years in common constants
91
92
        test = parse_image_timedelta(interval)
93
        self.assertEqual(reference, test)
94
95
96
class TestAttributes(TestCase):
97
98
    def test_format_attribute(self):
99
        reference = [{
100
            "value": "54.20944444444445",
101
            "units": "Decimal degrees"
102
        }]
103
104
        test = format_attribute(
105
            value="54.20944444444445",
106
            units="Decimal degrees")
107
108
        self.assertEqual(reference, test, msg="testing units")
109
110
        # another test
111
        reference = [{
112
            "value": "organism",
113
            "terms": [{
114
                "url": "%s/OBI_0100026" % (constants.OBO_URL)
115
            }]
116
        }]
117
118
        test = format_attribute(
119
            value="organism",
120
            terms="OBI_0100026")
121
122
        self.assertEqual(reference, test, msg="testing terms")
123
124
    def test_null(self):
125
        test = format_attribute(value=None)
126
127
        self.assertIsNone(test)
128
129
130
class TestAdminEmails(TestCase):
131
132
    def test_admin_emails(self):
133
        # calling objects
134
        emails = get_admin_emails()
135
136
        for email in emails:
137
            self.assertIsNone(validate_email(email))
138
139
140
class TestUid2Biosample(TestCase):
141
142
    def test_uid2biosample(self):
143
        test = uid2biosample('Sample storage')
144
        self.assertEqual(test, 'storage')
145
146
        test = uid2biosample('Sample storage processing')
147
        self.assertEqual(test, 'storage_processing')
148
149
        test = uid2biosample('Sampling to preparation interval')
150
        self.assertEqual(test, 'preparation_interval_units')
151
152
        test = uid2biosample('meow bark')
153
        self.assertEqual(test, 'meow_bark')
154