Skip to content

Commit 3686a5c

Browse files
committed
Merge pull request #92 from nowsecure/popupcal_tests
Popupcal tests
2 parents a593df4 + c98db68 commit 3686a5c

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed

datagrid_gtk3/tests/ui/__init__.py

Whitespace-only changes.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
"""Popupcal tests."""
2+
3+
import datetime
4+
import unittest
5+
6+
from gi.repository import Gtk
7+
import mock
8+
9+
from datagrid_gtk3.ui.popupcal import DateEntry
10+
11+
12+
class FakeDialog(object):
13+
14+
"""Object mimicing Gtk.Dialog's api."""
15+
16+
def __init__(self, retval, *args):
17+
"""Initialize the FakeDialog object."""
18+
self._retval = retval
19+
self.vbox = Gtk.VBox()
20+
21+
def run(self):
22+
"""Mimic the run method."""
23+
return self._retval
24+
25+
def set_decorated(self, value):
26+
"""Stub set_decorated method."""
27+
pass
28+
29+
def add_button(self, label, response):
30+
"""Mimic fake_button method."""
31+
return Gtk.Button(label)
32+
33+
def destroy(self):
34+
"""Stub destroy method."""
35+
pass
36+
37+
def get_action_area(self):
38+
"""Mimic get_action_area method."""
39+
class FakeActionArea(object):
40+
def reorder_child(self, btn, order):
41+
pass
42+
return FakeActionArea()
43+
44+
45+
class DateEntryTest(unittest.TestCase):
46+
47+
"""Tests for :class:`datagrid_gtk3.ui.popupcal.DateEntry`."""
48+
49+
def setUp(self):
50+
"""Setup widget for testing."""
51+
self._entry = DateEntry(Gtk.Window(), DateEntry.TYPE_START)
52+
53+
def test_set_date(self):
54+
"""set_date should set the text for the widget based on the format."""
55+
self.assertEqual(self._entry.get_property('text'), '')
56+
57+
self._entry.set_date(datetime.datetime(2015, 8, 10))
58+
self.assertEqual(self._entry.get_property('text'), '10-Aug-2015 00:00')
59+
60+
self._entry.set_date(datetime.datetime(2015, 8, 12, 15, 20))
61+
self.assertEqual(self._entry.get_property('text'), '12-Aug-2015 15:20')
62+
63+
self._entry.set_date(None)
64+
self.assertEqual(self._entry.get_property('text'), '')
65+
66+
def test_get_date(self):
67+
"""get_date should return a datetime based on the text on the entry."""
68+
self._entry.set_property('text', '')
69+
self.assertIsNone(self._entry.get_date())
70+
71+
self._entry.set_property('text', '10-Aug-2015 00:00')
72+
self.assertEqual(self._entry.get_date(),
73+
datetime.datetime(2015, 8, 10))
74+
75+
self._entry.set_property('text', '12-Aug-2015 15:20')
76+
self.assertEqual(self._entry.get_date(),
77+
datetime.datetime(2015, 8, 12, 15, 20))
78+
79+
def test_set_text(self):
80+
"""set_text should set the text for the widget based on the format."""
81+
self.assertEqual(self._entry.get_property('text'), '')
82+
83+
self._entry.set_text('2-Jun-2013 06:48')
84+
self.assertEqual(self._entry.get_date(),
85+
datetime.datetime(2013, 6, 2, 6, 48))
86+
self.assertEqual(self._entry.get_property('text'), '2-Jun-2013 06:48')
87+
88+
self._entry.set_text('02/10/2015')
89+
self.assertEqual(self._entry.get_date(),
90+
datetime.datetime(2015, 2, 10))
91+
self.assertEqual(self._entry.get_property('text'), '10-Feb-2015 00:00')
92+
93+
def test_get_text(self):
94+
"""get_text should return a formated date based on the format."""
95+
self.assertEqual(self._entry.get_property('text'), '')
96+
97+
self._entry.set_date(datetime.datetime(2015, 8, 10))
98+
self.assertEqual(self._entry.get_text(), '10-Aug-2015 00:00')
99+
100+
self._entry.set_date(datetime.datetime(2015, 8, 12, 15, 20))
101+
self.assertEqual(self._entry.get_text(), '12-Aug-2015 15:20')
102+
103+
def test_popup_response_ok(self):
104+
"""DatePicker should change the date on entry on response ok."""
105+
self._entry.set_date(datetime.datetime(2015, 1, 2, 10, 20))
106+
107+
with mock.patch('datagrid_gtk3.ui.popupcal.Gtk.Dialog') as dialog:
108+
fake_dialog = FakeDialog(Gtk.ResponseType.OK)
109+
dialog.return_value = fake_dialog
110+
111+
with mock.patch.object(self._entry, 'get_date') as get_date:
112+
# Entry will use the actual date to populate the date picker
113+
get_date.return_value = datetime.datetime(2010, 2, 3)
114+
self._entry._popup_picker()
115+
self.assertEqual(self._entry.get_date(),
116+
datetime.datetime(2010, 2, 3))
117+
118+
def test_popup_response_clear(self):
119+
"""DatePicker should clear the date on entry on response clear."""
120+
self._entry.set_date(datetime.datetime(2015, 1, 2, 10, 20))
121+
122+
with mock.patch('datagrid_gtk3.ui.popupcal.Gtk.Dialog') as dialog:
123+
fake_dialog = FakeDialog(99)
124+
dialog.return_value = fake_dialog
125+
126+
self.assertEqual(self._entry.get_date(),
127+
datetime.datetime(2015, 1, 2, 10, 20))
128+
self._entry._popup_picker()
129+
self.assertIsNone(self._entry.get_date())
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""Date utilities test cases."""
4+
5+
import datetime
6+
import unittest
7+
8+
from datagrid_gtk3.utils.dateutils import (
9+
InvalidDateFormat,
10+
parse_string,
11+
)
12+
13+
14+
class DateUtilsTest(unittest.TestCase):
15+
16+
"""Tests for :mod:`datagrid.utils.dateutils`."""
17+
18+
def test_parse_string_valid(self):
19+
"""Parse string should generate datetime for valid inputs."""
20+
for date_str, date in [
21+
('4/10/2015', datetime.datetime(2015, 4, 10)),
22+
('4/10/2015 04:20', datetime.datetime(2015, 4, 10, 4, 20)),
23+
('2-Jun-2013', datetime.datetime(2013, 6, 2)),
24+
('2-Jun-2013 06:48:15',
25+
datetime.datetime(2013, 6, 2, 6, 48, 15)),
26+
('Tue, 10 Apr 2001 15:51:24',
27+
datetime.datetime(2001, 4, 10, 15, 51, 24))]:
28+
self.assertEqual(parse_string(date_str), date)
29+
30+
def test_parse_string_invalid(self):
31+
"""InvalidDateFormat should be raised for invalid inputs."""
32+
for invalid_str in [
33+
'non-valid-string',
34+
'10/50/2010',
35+
'50/10/2015',
36+
'10/10/2010 25:10']:
37+
with self.assertRaises(InvalidDateFormat):
38+
parse_string(invalid_str)

0 commit comments

Comments
 (0)