~gtg-user/gtg/urgency-color-api

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Getting Things Gnome! - a personal organizer for the GNOME desktop
# Copyright (c) 2008-2009 - Lionel Dricot & Bertrand Rousseau
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see <http://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------------

'''
Tests for the various Date classes
'''

import unittest
from datetime import date, timedelta

from GTG import _
from GTG.tools.dates import Date


def next_month(aday, day=None):
    """ Increase month, change 2012-02-13 into 2012-03-13.
    If day is set, replace day in month as well

    @returns: updated date """
    if day is None:
        day = aday.day

    if aday.month == 12:
        return aday.replace(day=day, month=1, year=aday.year + 1)
    else:
        return aday.replace(day=day, month=aday.month + 1)


class TestDates(unittest.TestCase): # pylint: disable-msg=R0904
    """ Tests for the various Date classes """

    def test_parse_dates(self):
        """ Parse common numeric date """
        self.assertEqual(str(Date.parse("1985-03-29")), "1985-03-29")
        self.assertEqual(str(Date.parse("19850329")), "1985-03-29")
        self.assertEqual(str(Date.parse("1985/03/29")), "1985-03-29")

        today = date.today()
        parse_string = "%02d%02d" % (today.month, today.day)
        self.assertEqual(Date.parse(parse_string), today)

    def test_parse_fuzzy_dates(self):
        """ Parse fuzzy dates like now, soon, later, someday """
        self.assertEqual(Date.parse("now"), Date.now())
        self.assertEqual(Date.parse("soon"), Date.soon())
        self.assertEqual(Date.parse("later"), Date.someday())
        self.assertEqual(Date.parse("someday"), Date.someday())
        self.assertEqual(Date.parse(""), Date.no_date())

    def test_parse_local_fuzzy_dates(self):
        """ Parse fuzzy dates in their localized version """
        self.assertEqual(Date.parse(_("now")), Date.now())
        self.assertEqual(Date.parse(_("soon")), Date.soon())
        self.assertEqual(Date.parse(_("later")), Date.someday())
        self.assertEqual(Date.parse(_("someday")), Date.someday())
        self.assertEqual(Date.parse(""), Date.no_date())

    def test_parse_fuzzy_dates_str(self):
        """ Print fuzzy dates in localized version """
        self.assertEqual(str(Date.parse("now")), _("now"))
        self.assertEqual(str(Date.parse("soon")), _("soon"))
        self.assertEqual(str(Date.parse("later")), _("someday"))
        self.assertEqual(str(Date.parse("someday")), _("someday"))
        self.assertEqual(str(Date.parse("")), "")

    def test_parse_week_days(self):
        """ Parse name of week days and don't care about case-sensitivity """
        weekday = date.today().weekday()
        for i, day in enumerate(['Monday', 'Tuesday', 'Wednesday',
            'Thursday', 'Friday', 'Saturday', 'Sunday']):
            if i <= weekday:
                expected = date.today() + timedelta(7+i-weekday)
            else:
                expected = date.today() + timedelta(i-weekday)

            self.assertEqual(Date.parse(day), expected)
            self.assertEqual(Date.parse(day.lower()), expected)
            self.assertEqual(Date.parse(day.upper()), expected)

            # Test localized version
            day = _(day)
            self.assertEqual(Date.parse(day), expected)
            self.assertEqual(Date.parse(day.lower()), expected)
            self.assertEqual(Date.parse(day.upper()), expected)

    def test_missing_year_this_year(self):
        """ Parsing %m%d have to find correct date:
        we enter a day this year """
        aday = date.today()
        aday = aday.replace(month=aday.month+1, day=1)
        parse_string = "%02d%02d" % (aday.month, aday.day)
        self.assertEqual(Date.parse(parse_string), aday)

    def test_missing_year_next_year(self):
        """ Parsing %m%d have to find correct date:
        we enter a day the next year """
        aday = date.today()
        if aday.day == 1 and aday.month == 1:
            # not possible to add a day next year
            return

        aday = aday.replace(year=aday.year+1, month=1, day=1)
        self.assertEqual(Date.parse("0101"), aday)

    def test_on_certain_day(self):
        """ Parse due:3 as 3rd day this month or next month
        if it is already more or already 3rd day """
        for i in range(28):
            i += 1
            aday = date.today()
            if i <= aday.day:
                aday = next_month(aday, i)
            else:
                aday = aday.replace(day=i)

            self.assertEqual(Date.parse(str(i)), aday)

    def test_prevent_regression(self):
        """ A day represented in GTG Date must be still the same """
        aday = date.today()
        self.assertEqual(Date(aday), aday)


def test_suite():
    """ Return unittests """
    return unittest.TestLoader().loadTestsFromTestCase(TestDates)