def test_format_overlapping_events(self): time_period1 = TimePeriod(self.time_type, wx.DateTimeFromDMY(1, 7, 2010, 13, 44), wx.DateTimeFromDMY(2, 7, 2010, 13, 30)) time_period2 = TimePeriod(self.time_type, wx.DateTimeFromDMY(1, 7, 2010, 13, 44), wx.DateTimeFromDMY(2, 7, 2010, 13, 30)) delta = time_period2.start_time - time_period1.end_time self.assertEquals("0", self.time_type.format_delta(delta))
def move_period_num_months(period, num): try: delta = num years = abs(delta) / 12 gregorian_start = gregorian.from_time(period.start_time) gregorian_end = gregorian.from_time(period.end_time) if num < 0: years = -years delta = delta - 12 * years if delta < 0: start_month = gregorian_start.month + 12 + delta end_month = gregorian_end.month + 12 + delta if start_month > 12: start_month -= 12 end_month -= 12 if start_month > gregorian_start.month: years -= 1 else: start_month = gregorian_start.month + delta end_month = gregorian_start.month + delta if start_month > 12: start_month -= 12 end_month -= 12 years += 1 start_year = gregorian_start.year + years end_year = gregorian_start.year + years start_time = gregorian_start.replace(year=start_year, month=start_month) end_time = gregorian_end.replace(year=end_year, month=end_month) return TimePeriod(period.time_type, start_time.to_time(), end_time.to_time()) except ValueError: return None
def test_format_period_method(self): time_period = TimePeriod(self.time_type, wx.DateTimeFromDMY(1, 7, 2010, 13, 44), wx.DateTimeFromDMY(2, 7, 2010, 13, 30)) self.assertEquals( u"01 %s 2010 13:44 to 02 %s 2010 13:30" % (_("Aug"), _("Aug")), self.time_type.format_period(time_period))
def testLoadSaveViewProperties(self): # Make sure the database contains categories self.db.save_category(self.c1) self.db.save_category(self.c2) # Set up a view properties object that simulates having selected a # specific period and hidden one category vp = ViewProperties() vp.set_category_visible(self.c1, False) start = datetime(2010, 3, 23) end = datetime(2010, 3, 24) tp = TimePeriod(self.db.get_time_type(), start, end) vp.displayed_period = tp # Save these properties and assert that the database fields are written # correctly self.db.save_view_properties(vp) self.assertEquals(self.db.displayed_period, tp) self.assertEquals(self.db.hidden_categories, [self.c1]) # Load view properties from db simulating that this db was just loaded # into memory and the view is being configured new_vp = ViewProperties() self.db.load_view_properties(new_vp) self.assertFalse(new_vp.category_visible(self.c1)) self.assertTrue(new_vp.category_visible(self.c2)) self.assertEquals(new_vp.displayed_period, self.db.displayed_period) # Assert virtual _save method called: 2 save categories, 1 save view # properties self.assertEquals(self.db._save.call_count, 3)
def test_the_new_event_gets_period_from_move_period_fn(self): new_event = self.db.save_event.call_args[0][0] new_period = new_event.time_period expected_period = TimePeriod(PyTimeType(), datetime.datetime(2010, 8, 1), datetime.datetime(2010, 8, 1)) self.assertEquals(expected_period, new_period)
def choose_strip(self, metrics, config): start_time = 1 end_time = 2 limit = 30 period = TimePeriod(self, start_time, end_time) period_width = metrics.calc_exact_width(period) while period_width == 0: start_time *= 10 end_time *= 10 limit /= 10 period = TimePeriod(self, start_time, end_time) period_width = metrics.calc_exact_width(period) nbr_of_units = metrics.width / period_width size = 1 while nbr_of_units > limit: size *= 10 nbr_of_units /= 10 return (NumStrip(size * 10), NumStrip(size))
def testSaveInvalidDisplayedPeriod(self): # Assign a zero-period as displayed period vp = ViewProperties() start = datetime(2010, 3, 23) end = datetime(2010, 3, 23) tp = TimePeriod(PyTimeType(), start, end) vp.displayed_period = tp # Assert error when trying to save self.assertRaises(TimelineIOError, self.db.save_view_properties, vp)
def __init__(self): self.db = MemoryDB() from timelinelib.time.gregoriantime import GregorianTimeType self.db.time_type = GregorianTimeType() now = gregorian.from_time(self.db.time_type.now()) self.start = self.get_time(now.year, now.month, 1) self.end = self.start + self.get_days_delta(30) self.db._set_displayed_period(TimePeriod(self.db.get_time_type(), self.start, self.end)) self.last_cat = None
def testGetSetDisplayedPeriod(self): tp = TimePeriod(self.db.get_time_type(), datetime(2010, 3, 23), datetime(2010, 3, 24)) self.db._set_displayed_period(tp) # Assert that we get back the same period self.assertEquals(self.db._get_displayed_period(), tp) # Assert that the period is correctly loaded into ViewProperties vp = ViewProperties() self.db.load_view_properties(vp) self.assertEquals(vp.displayed_period, tp)
def _get_period(self, t1, t2): if t1 > t2: start = t2 end = t1 else: start = t1 end = t2 return TimePeriod(self.controller.get_timeline().get_time_type(), self.controller.get_drawer().snap(start), self.controller.get_drawer().snap(end))
def testSaveNewEvent(self): self.db.save_event(self.e1) tp = TimePeriod(self.db.get_time_type(), datetime(2010, 2, 12), datetime(2010, 2, 14)) self.assertTrue(self.e1.has_id()) self.assertEqual(self.db.get_events(tp), [self.e1]) self.assertEqual(self.db.get_all_events(), [self.e1]) self.assertEqual(self.db_listener.call_count, 1) # 1 save # Assert virtual _save method called: 1 save event self.assertEquals(self.db._save.call_count, 1)
def setUp(self): self.app = wx.App() # a stored app is needed to create fonts self.drawer = DefaultDrawingAlgorithm() self.dc = Mock(wx.DC) self.dc.GetSizeTuple.return_value = IMAGE_SIZE self.dc.GetTextExtent.return_value = TEXT_SIZE self.timeline = MemoryDB() self.view_properties = ViewProperties() self.view_properties.displayed_period = TimePeriod( PyTimeType(), datetime.datetime(2010, 1, 1), datetime.datetime(2011, 1, 1))
def copy_db(from_db, to_db): """ Copy all content from one db to another. to_db is assumed to have no categories (conflicting category names are not handled). """ if isinstance(to_db, MemoryDB): to_db.disable_save() # Copy categories (parent attribute fixed later) cat_map = {} for cat in from_db.get_categories(): # name, color, and visible all immutable so safe to copy new_cat = Category(cat.name, cat.color, None, cat.visible) cat_map[cat.name] = new_cat to_db.save_category(new_cat) # Fix parent attribute for cat in from_db.get_categories(): if cat.parent is not None: cat_map[cat.name].parent = cat_map[cat.parent.name] # Copy events for event in from_db.get_all_events(): cat = None if event.category is not None: cat = cat_map[event.category.name] # start_time, end_time, and text all immutable so safe to copy new_event = Event(to_db.get_time_type(), event.time_period.start_time, event.time_period.end_time, event.text, cat) # description immutable so safe to copy if event.get_data("description") is not None: new_event.set_data("description", event.get_data("description")) # icon immutable in practice (since never modified) so safe to copy if event.get_data("icon") is not None: new_event.set_data("icon", event.get_data("icon")) to_db.save_event(new_event) # Copy view properties (ViewProperties is specific to db so we need to copy # like this instead of just using load/save_view_properties in db). from_vp = ViewProperties() from_db.load_view_properties(from_vp) to_vp = ViewProperties() for from_cat in from_db.get_categories(): cat = cat_map[from_cat.name] visible = from_vp.is_category_visible(from_cat) to_vp.set_category_visible(cat, visible) if from_vp.displayed_period is not None: # start_time and end_time immutable so safe to copy start = from_vp.displayed_period.start_time end = from_vp.displayed_period.end_time to_vp.displayed_period = TimePeriod(to_db.get_time_type(), start, end) to_db.save_view_properties(to_vp) # Save if isinstance(to_db, MemoryDB): to_db.enable_save()
def testSaveExistingEvent(self): self.db.save_event(self.e1) id_before = self.e1.id self.e1.text = "new text" self.db.save_event(self.e1) tp = TimePeriod(self.db.get_time_type(), datetime(2010, 2, 12), datetime(2010, 2, 14)) self.assertEqual(id_before, self.e1.id) self.assertEqual(self.db.get_events(tp), [self.e1]) self.assertEqual(self.db.get_all_events(), [self.e1]) self.assertEqual(self.db_listener.call_count, 2) # 1 save # Assert virtual _save method called: 2 save event self.assertEquals(self.db._save.call_count, 2)
def move_period_num_years(period, num): try: delta = num start_year = gregorian.from_time(period.start_time).year end_year = gregorian.from_time(period.end_time).year start_time = gregorian.from_time( period.start_time).replace(year=start_year + delta) end_time = gregorian.from_time(period.end_time).replace(year=end_year + delta) return TimePeriod(period.time_type, start_time.to_time(), end_time.to_time()) except ValueError: return None
def fill(list, strip): """Fill the given list with the given strip.""" try: current_start = strip.start( self._view_properties.displayed_period.start_time) while current_start < self._view_properties.displayed_period.end_time: next_start = strip.increment(current_start) list.append( TimePeriod(self._db.get_time_type(), current_start, next_start)) current_start = next_start except: #Exception occurs when major=century and when we are at the end of the calendar pass
def _create_db(self): db = XmlTimeline(self.tmp_path) # Create categories cat1 = Category("Category 1", (255, 0, 0), (0, 0, 255), True) db.save_category(cat1) cat2 = Category("Category 2", (0, 255, 0), None, True, parent=cat1) db.save_category(cat2) cat3 = Category("Category 3", (0, 0, 255), None, True, parent=cat2) db.save_category(cat3) # Create events ev1 = Event(db.get_time_type(), datetime(2010, 3, 3), datetime(2010, 3, 6), "Event 1", cat1) ev1.set_data("description", u"The <b>first</b> event åäö.") ev1.set_data("alert", (datetime(2012, 12, 31), "Time to go")) db.save_event(ev1) # Create view properties vp = ViewProperties() start = datetime(2010, 3, 1) end = datetime(2010, 4, 1) vp.displayed_period = TimePeriod(db.get_time_type(), start, end) vp.set_category_visible(cat3, False) db.save_view_properties(vp)
def testDeleteExistingEvent(self): tp = TimePeriod(self.db.get_time_type(), datetime(2010, 2, 12), datetime(2010, 2, 15)) self.db.save_event(self.e1) self.db.save_event(self.e2) # Assert both in db self.assertEquals(len(self.db.get_events(tp)), 2) self.assertTrue(self.e1 in self.db.get_events(tp)) self.assertTrue(self.e2 in self.db.get_events(tp)) # Delete first (by event) self.db.delete_event(self.e1) self.assertFalse(self.e1.has_id()) self.assertEquals(len(self.db.get_events(tp)), 1) self.assertTrue(self.e2 in self.db.get_events(tp)) # Delete second (by id) self.db.delete_event(self.e2.id) self.assertFalse(self.e2.has_id()) self.assertEquals(len(self.db.get_events(tp)), 0) # Check events self.assertEqual(self.db_listener.call_count, 4) # 2 save, 2 delete # Assert virtual _save method called: 2 save event, 2 delete event self.assertEquals(self.db._save.call_count, 4)
def choose_strip(self, metrics, config): """ Return a tuple (major_strip, minor_strip) for current time period and window size. """ day_period = TimePeriod(self, timeline.Time(0, 0), timeline.Time(1, 0)) one_day_width = metrics.calc_exact_width(day_period) self.major_strip_is_decade = False if one_day_width > 600: return (StripDay(), StripHour()) elif one_day_width > 45: return (StripWeek(config), StripWeekday()) elif one_day_width > 25: return (StripMonth(), StripDay()) elif one_day_width > 1.5: return (StripYear(), StripMonth()) elif one_day_width > 0.12: self.major_strip_is_decade = True return (StripDecade(), StripYear()) elif one_day_width > 0.012: return (StripCentury(), StripDecade()) else: return (StripCentury(), StripCentury())
def test_zoom_out_adds_1_10th_on_each_side(self): time_period = TimePeriod(ATimeType(), ATime(10), ATime(20)) self.assertEquals(time_period.zoom(-1), TimePeriod(ATimeType(), ATime(9), ATime(21)))
def test_move_delta_moves_the_period_that_delta(self): time_period = TimePeriod(ATimeType(), ATime(10), ATime(20)) self.assertEquals(time_period.move_delta(ADelta(-10)), TimePeriod(ATimeType(), ATime(0), ATime(10)))
def test_move_moves_1_10th_backward(self): time_period = TimePeriod(ATimeType(), ATime(20), ATime(30)) self.assertEquals(time_period.move(-1), TimePeriod(ATimeType(), ATime(19), ATime(29)))
def test_zoom_in_removes_1_10th_on_each_side(self): time_period = TimePeriod(ATimeType(), ATime(10), ATime(20)) self.assertEquals(time_period.zoom(1), TimePeriod(ATimeType(), ATime(11), ATime(19)))
def test_move_moves_1_10th_forward(self): time_period = TimePeriod(ATimeType(), ATime(0), ATime(10)) self.assertEquals(time_period.move(1), TimePeriod(ATimeType(), ATime(1), ATime(11)))
def test_formats_period_using_time_type(self): time_period = TimePeriod(ATimeType(), ATime(5), ATime(9)) self.assertEquals("5 to 9", time_period.get_label())
def test_center_after_upper_limit_should_make_period_end_there(self): tp = TimePeriod(ATimeType(), ATime(10), ATime(14)) self.assertEquals(tp.center(ATime(200)), TimePeriod(ATimeType(), ATime(96), ATime(100)))
def test_center_before_lower_limit_should_make_period_start_there(self): tp = TimePeriod(ATimeType(), ATime(10), ATime(14)) self.assertEquals(tp.center(ATime(-5)), TimePeriod(ATimeType(), ATime(0), ATime(4)))
def setUp(self): self.period = TimePeriod(NumTimeType(), 1, 2)
def test_move_period_adds_given_number_of_delta(self): new_period = move_period(self.period, 6) self.assertEquals(TimePeriod(NumTimeType(), 7, 8), new_period)
def test_center_should_center_period_around_time(self): tp = TimePeriod(ATimeType(), ATime(0), ATime(4)) self.assertEquals(tp.center(ATime(5)), TimePeriod(ATimeType(), ATime(3), ATime(7)))