def testDeleteExistingCategory(self): # Add two categories to the db self.db.save_category(self.c1) self.db.save_category(self.c2) # Make category 1 hidden vp = ViewProperties() vp.set_category_visible(self.c1, False) self.db.save_view_properties(vp) # Assert both categories in db categories = self.db.get_categories() self.assertEquals(len(categories), 2) self.assertTrue(self.c1 in categories) self.assertTrue(self.c2 in categories) # Remove first (by category) self.db.delete_category(self.c1) categories = self.db.get_categories() self.assertEquals(len(categories), 1) self.assertTrue(self.c2 in categories) self.assertFalse(self.c1.has_id()) self.assertFalse(self.c1 in self.db.hidden_categories) # Remove second (by id) self.db.delete_category(self.c2.id) categories = self.db.get_categories() self.assertEquals(len(categories), 0) self.assertFalse(self.c2.has_id()) # Check events self.assertEqual(self.db_listener.call_count, 4) # 2 save, 2 delete # Assert virtual _save method called: 2 save category, 1 save view # properties, 2 delete categories self.assertEquals(self.db._save.call_count, 5)
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 testRead090File(self): self.writeContentToTmpFile(CONTENT_090) app = wx.PySimpleApp() # Needed for graphics functions to parse icon db = db_open(self.tmp_path) # Assert converted to xml db self.assertTrue(isinstance(db, XmlTimeline)) # Assert events correctly loaded events = db.get_all_events() self.assertEquals(len(events), 2) for event in events: self.assertTrue(event.has_id()) if event.text == "Tennis": self.assertEquals(event.time_period.start_time, datetime(2010, 5, 2, 9, 0, 0)) self.assertEquals(event.time_period.end_time, datetime(2010, 5, 2, 9, 0, 0)) self.assertEquals(event.category.name, "Private") self.assertEquals(event.get_data("description"), "Med Kristoffer.") self.assertEquals(event.get_data("icon"), None) elif event.text == "Konferens": self.assertEquals(event.time_period.start_time, datetime(2010, 5, 5, 0, 0, 0)) self.assertEquals(event.time_period.end_time, datetime(2010, 5, 8, 0, 0, 0)) self.assertEquals(event.category.name, "Work") self.assertEquals(event.get_data("description"), None) self.assertFalse(event.get_data("icon") is None) else: self.fail("Unknown event.") # Assert that correct view properties are loaded (category visibility # checked later) vp = ViewProperties() db.load_view_properties(vp) self.assertEquals(vp.displayed_period.start_time, datetime(2010, 4, 27, 21, 54, 54)) self.assertEquals(vp.displayed_period.end_time, datetime(2010, 5, 13, 6, 33, 18)) # Assert categories correctly loaded categories = db.get_categories() self.assertEquals(len(categories), 3) for cat in categories: self.assertTrue(cat.has_id()) if cat.name == "Work": self.assertEquals(cat.color, (255, 0, 0)) self.assertTrue(vp.category_visible(cat)) self.assertEquals(cat.parent, None) elif cat.name == "Private": self.assertEquals(cat.color, (0, 0, 255)) self.assertTrue(vp.category_visible(cat)) self.assertEquals(cat.parent, None) elif cat.name == "Hidden": self.assertEquals(cat.color, (0, 0, 0)) self.assertFalse(vp.category_visible(cat)) self.assertEquals(cat.parent, None) else: self.fail("Unknown category.")
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 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)
def testGetSetHiddenCategories(self): # Assert that we cannot include categories not in the db self.assertRaises(ValueError, self.db._set_hidden_categories, [self.c1]) self.db._set_hidden_categories([]) self.db.save_category(self.c1) self.db.save_category(self.c2) self.db._set_hidden_categories([self.c1]) # Assert that the returned list is the same self.assertEquals(self.db._get_hidden_categories(), [self.c1]) # Assert that category visibility information is correctly written to # ViewProperties vp = ViewProperties() self.db.load_view_properties(vp) self.assertFalse(vp.category_visible(self.c1)) self.assertTrue(vp.category_visible(self.c2))
def testRead010File(self): self.writeContentToTmpFile(CONTENT_010) db = db_open(self.tmp_path) # Assert converted to xml db self.assertTrue(isinstance(db, XmlTimeline)) # Assert event correctly loaded events = db.get_all_events() self.assertEquals(len(events), 1) event = events[0] self.assertTrue(event.has_id()) self.assertEquals(event.text, "Event 1") self.assertEquals(event.time_period.start_time, datetime(2009, 11, 4, 22, 52, 0)) self.assertEquals(event.time_period.end_time, datetime(2009, 11, 11, 22, 52, 0)) self.assertEquals(event.category.name, "Category 1") self.assertEquals(event.get_data("description"), None) self.assertEquals(event.get_data("icon"), None) # Assert that correct view properties are loaded (category visibility # checked later) vp = ViewProperties() db.load_view_properties(vp) self.assertEquals(vp.displayed_period.start_time, datetime(2009, 10, 17, 22, 38, 32)) self.assertEquals(vp.displayed_period.end_time, datetime(2009, 12, 2, 16, 22, 4)) # Assert categories correctly loaded categories = db.get_categories() self.assertEquals(len(categories), 3) for cat in categories: self.assertTrue(cat.has_id()) if cat.name == "Category 1": self.assertEquals(cat.color, (188, 129, 224)) self.assertTrue(vp.category_visible(cat)) self.assertEquals(cat.parent, None) elif cat.name == "Category 2": self.assertEquals(cat.color, (255, 165, 0)) self.assertTrue(vp.category_visible(cat)) self.assertEquals(cat.parent, None) elif cat.name == "Category 3": self.assertEquals(cat.color, (173, 216, 230)) self.assertFalse(vp.category_visible(cat)) self.assertEquals(cat.parent, None) else: self.fail("Unknown category.")
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 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 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 _assert_re_read_db_same(self, db): # Assert event correctly loaded events = db.get_all_events() self.assertEquals(len(events), 1) event = events[0] self.assertEquals(event.text, "Event 1") self.assertEquals(event.time_period.start_time, datetime(2010, 3, 3)) self.assertEquals(event.time_period.end_time, datetime(2010, 3, 6)) self.assertEquals(event.category.name, "Category 1") self.assertEquals(event.get_data("description"), u"The <b>first</b> event åäö.") self.assertEquals(event.get_data("alert"), (datetime(2012, 12, 31), "Time to go")) self.assertEquals(event.get_data("icon"), None) # Assert that correct view properties are loaded (category visibility # checked later) vp = ViewProperties() db.load_view_properties(vp) self.assertEquals(vp.displayed_period.start_time, datetime(2010, 3, 1)) self.assertEquals(vp.displayed_period.end_time, datetime(2010, 4, 1)) # Assert categories correctly loaded categories = db.get_categories() self.assertEquals(len(categories), 3) for cat in categories: self.assertTrue(cat.has_id()) if cat.name == "Category 1": self.assertEquals(cat.color, (255, 0, 0)) self.assertEquals(cat.font_color, (0, 0, 255)) self.assertTrue(vp.category_visible(cat)) self.assertEquals(cat.parent, None) elif cat.name == "Category 2": self.assertEquals(cat.color, (0, 255, 0)) self.assertTrue(vp.category_visible(cat)) self.assertEquals(cat.parent.name, "Category 1") elif cat.name == "Category 3": self.assertEquals(cat.color, (0, 0, 255)) self.assertFalse(vp.category_visible(cat)) self.assertEquals(cat.parent.name, "Category 2") else: self.fail("Unknown category.")
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 setUp(self): self.db = MemoryDB() self.view_properties = ViewProperties() self.given_number_of_events_stackable_is(5)
class SceneSpec(unittest.TestCase): def test_has_no_hidden_events_when_all_events_belong_to_visible_categories(self): self.given_displayed_period("1 Jan 2010", "10 Jan 2010") self.given_visible_event_at("5 Jan 2010") self.when_scene_is_created() self.assertEquals(0, self.scene.get_hidden_event_count()) def test_has_hidden_events_for_all_events_belonging_to_hidden_categories(self): self.given_displayed_period("1 Jan 2010", "10 Jan 2010") self.given_visible_event_at("5 Jan 2010") self.given_hidden_event_at("5 Jan 2010") self.when_scene_is_created() self.assertEquals(1, self.scene.get_hidden_event_count()) def test_considers_events_outside_screen_hidden(self): self.given_displayed_period("1 Jan 2010", "10 Jan 2010") self.given_number_of_events_stackable_is(5) for i in range(6): self.given_visible_event_at("5 Jan 2010") self.when_scene_is_created() self.assertEquals(1, self.scene.get_hidden_event_count()) def test_point_events_on_same_date_has_different_y_positions(self): self.given_displayed_period("1 Jan 2010", "10 Jan 2010") self.given_visible_event_at("5 Jan 2010") self.given_visible_event_at("5 Jan 2010") self.when_scene_is_created() self.assertTrue(self.scene.event_data[0][1].Y > self.scene.event_data[1][1].Y) def test_point_events_on_different_dates_has_same_y_positions(self): self.given_displayed_period("1 Jan 2010", "10 Jan 2010") self.given_visible_event_at("2 Jan 2010") self.given_visible_event_at("9 Jan 2010") self.when_scene_is_created() self.assertEqual(self.scene.event_data[0][1].Y, self.scene.event_data[1][1].Y) def test_period_events_with_same_period_has_different_y_positions(self): self.given_displayed_period("1 Jan 2010", "12 Jan 2010") self.given_visible_event_at("2 Jan 2010", "10 Jan 2010") self.given_visible_event_at("2 Jan 2010", "10 Jan 2010") self.when_scene_is_created() self.assertTrue(self.scene.event_data[0][1].Y < self.scene.event_data[1][1].Y) def test_period_events_with_different_periods_has_same_y_positions(self): self.given_displayed_period("1 Jan 2010", "12 Jan 2010") self.given_visible_event_at("2 Jan 2010", "3 Jan 2010") self.given_visible_event_at("8 Jan 2010", "10 Jan 2010") self.when_scene_is_created() self.assertEqual(self.scene.event_data[0][1].Y, self.scene.event_data[1][1].Y) def test_scene_must_be_created_at_last_century(self): self.given_displayed_period("1 Jan 9890", "1 Jan 9990") try: self.when_scene_is_created() self.assertTrue(self.scene != None) except: self.assertTrue(False) def setUp(self): self.db = MemoryDB() self.view_properties = ViewProperties() self.given_number_of_events_stackable_is(5) def get_text_size_fn(self, text): return (len(text), self.event_height) def given_number_of_events_stackable_is(self, number): self.event_height = 10 self.size = (100, 2 * self.event_height * number) self.view_properties.divider_position = 0.5 self.outer_padding = 0 self.inner_padding = 0 self.baseline_padding = 0 def given_displayed_period(self, start, end): self.view_properties.displayed_period = py_period(start, end) def given_visible_event_at(self, start_time, end_time=None): self.given_event_at(start_time, end_time, visible=True) def given_hidden_event_at(self, time): self.given_event_at(time, visible=False) def given_event_at(self, start_time, end_time=None, visible=True): category = Category("category", (0, 0, 0), None, visible) if end_time is None: end_time = start_time event = Event(self.db.get_time_type(), human_time_to_py(start_time), human_time_to_py(end_time), "event-text", category) self.db.save_category(category) self.db.save_event(event) self.view_properties.set_category_visible(category, visible) def when_scene_is_created(self): self.scene = TimelineScene( self.size, self.db, self.view_properties, self.get_text_size_fn, None) self.scene.set_outer_padding(self.outer_padding) self.scene.set_inner_padding(self.inner_padding) self.scene.set_baseline_padding(self.baseline_padding) self.scene.create()