Passed
Pull Request — master (#39)
by Paolo
03:19
created

TestAttributes.test_format_attribute()   A

Complexity

Conditions 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 25
rs 9.6
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 format_attribute, get_admin_emails, image_timedelta
15
16
17
class TestImageTimedelta(TestCase):
18
    """A class to test common.helpers.image_timedelta functions"""
19
20
    def test_years(self):
21
        t1 = parse_date("2019-03-27")
22
        t2 = parse_date("2018-03-27")
23
24
        years, units = image_timedelta(t1, t2)
25
26
        self.assertEqual(years, 1)
27
        self.assertEqual(units, constants.YEARS)
28
29
        with self.assertLogs('common.helpers', level="WARNING") as cm:
30
            # assert date inversion (returns None)
31
            years, units = image_timedelta(t2, t1)
32
33
        self.assertEqual(years, None)
34
        self.assertEqual(units, constants.YEARS)
35
        self.assertEqual(len(cm.output), 1)
36
        self.assertIn("t2>t1", cm.output[0])
37
38
    def test_months(self):
39
        t1 = parse_date("2019-03-27")
40
        t2 = parse_date("2019-01-27")
41
42
        months, units = image_timedelta(t1, t2)
43
44
        self.assertEqual(months, 2)
45
        self.assertEqual(units, constants.MONTHS)
46
47
    def test_days(self):
48
        t1 = parse_date("2019-03-27")
49
        t2 = parse_date("2019-03-20")
50
51
        days, units = image_timedelta(t1, t2)
52
53
        self.assertEqual(days, 7)
54
        self.assertEqual(units, constants.DAYS)
55
56 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...
57
        t1 = parse_date("2019-03-20")
58
        t2 = parse_date("1900-01-01")
59
60
        with self.assertLogs('common.helpers', level="WARNING") as cm:
61
            years, units = image_timedelta(t1, t2)
62
63
        self.assertIsNone(years)
64
        self.assertEqual(units, constants.YEARS)
65
        self.assertEqual(len(cm.output), 1)
66
        self.assertIn("Ignoring one date", cm.output[0])
67
68 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...
69
        t1 = None
70
        t2 = parse_date("1900-01-01")
71
72
        with self.assertLogs('common.helpers', level="WARNING") as cm:
73
            years, units = image_timedelta(t1, t2)
74
75
        self.assertIsNone(years)
76
        self.assertEqual(units, constants.YEARS)
77
        self.assertEqual(len(cm.output), 1)
78
        self.assertIn("One date is NULL", cm.output[0])
79
80
81
class TestAttributes(TestCase):
82
83
    def test_format_attribute(self):
84
        reference = [{
85
            "value": "54.20944444444445",
86
            "units": "Decimal degrees"
87
        }]
88
89
        test = format_attribute(
90
            value="54.20944444444445",
91
            units="Decimal degrees")
92
93
        self.assertEqual(reference, test, msg="testing units")
94
95
        # another test
96
        reference = [{
97
            "value": "organism",
98
            "terms": [{
99
                "url": "%s/OBI_0100026" % (constants.OBO_URL)
100
            }]
101
        }]
102
103
        test = format_attribute(
104
            value="organism",
105
            terms="OBI_0100026")
106
107
        self.assertEqual(reference, test, msg="testing terms")
108
109
    def test_null(self):
110
        test = format_attribute(value=None)
111
112
        self.assertIsNone(test)
113
114
115
class TestAdminEmails(TestCase):
116
117
    def test_admin_emails(self):
118
        # calling objects
119
        emails = get_admin_emails()
120
121
        for email in emails:
122
            self.assertIsNone(validate_email(email))
123