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)
Exemple #2
0
 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)
Exemple #3
0
 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 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)
Exemple #5
0
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()
Exemple #6
0
    def testSettingInvalidPreferredPeriod(self):
        """
        Scenario: You try to assign a preferred period whose length is 0.

        Expected result: You should get an error.
        """
        timeline = FileTimeline(self.valid_file)
        now = datetime.datetime.now()
        zero_tp = TimePeriod(PyTimeType(), now, now)
        vp = ViewProperties()
        vp.displayed_period = zero_tp
        self.assertRaises(TimelineIOError, timeline.save_view_properties, vp)
Exemple #7
0
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 _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)
Exemple #9
0
 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)