|
| 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()) |
0 commit comments