def test_sync_event_file_changed_name(self): # Build an event and establish that it has already been observed and # written event = self.fake_event() mock_created = datetime.date.today() - datetime.timedelta(days=7) mock_old_filename = FileWriter.create_filename(mock_created, event['title']) mock_file_info = { 'title': event['title'], 'filename': mock_old_filename } prep_writer = FileWriter(self.test_posts_dir) prep_writer.write(MeetupEvent(event), mock_old_filename) # Modify event name event['title'] = 'A new name' expected_transformed_event = MeetupEvent(event) mock_new_filename = FileWriter.create_filename(mock_created, event['title']) # Verify that # 1. writer.delete is called to delete to old file # 2. writer.write is going to write content that includes the updated # name # 3. writer.write is going to write to a new file using the new name with mock.patch.object(self.subject.writer, 'write') as mock_write,\ mock.patch.object(self.subject.writer, 'delete') as mock_delete,\ mock.patch.object(self.subject.db, 'find_event') as\ mock_find_event: mock_find_event.return_value = mock_file_info self.subject.sync_event_file(event) mock_delete.assert_called_with(mock_old_filename) self.assertEqual(str(mock_write.call_args[0][0]), str(expected_transformed_event)) self.assertEqual(mock_write.call_args[0][1], mock_new_filename)
def test_sync_event_file_excerpt_update(self): # Build an event and establish that it has already been observed and # written event = self.fake_event() mock_created = datetime.date.today() - datetime.timedelta(days=7) mock_filename = FileWriter.create_filename(mock_created, event['title']) mock_file_info = {'title': event['title'], 'filename': mock_filename} prep_writer = FileWriter(self.test_posts_dir) prep_writer.write(MeetupEvent(event), mock_filename) # Modify event content that will become excerpt event['excerpt'] = "<p>This is new</p>" + event['excerpt'] expected_transformed_event = MeetupEvent(event) # Verify that # 1. writer.write is going to write content that includes the updated # excerpt # 2. writer.write is going to write to the already existing file with mock.patch.object(self.subject.writer, 'write') as mock_write,\ mock.patch.object(self.subject.db, 'find_event') as\ mock_find_event: mock_find_event.return_value = mock_file_info self.subject.sync_event_file(event) self.assertEqual(str(mock_write.call_args[0][0]), str(expected_transformed_event)) self.assertEqual(mock_write.call_args[0][1], mock_filename)
def sync_event_file(self, event): renamed = False file_info = self.db.find_event(event['id']) if file_info: if file_info['title'] != event['title']: renamed = True (file_date, title) = FileWriter.split_filename( file_info['filename']) filename = FileWriter.create_filename(file_date, event['title']) else: filename = file_info['filename'] else: filename = FileWriter.create_filename(event['time'], event['title']) if renamed: self.writer.delete(file_info['filename']) self.writer.write(MeetupEvent(event), filename)
def test_create_filename(self): file_date = date(2017, 1, 20) file_title = 'A Test' self.assertEqual(FileWriter.create_filename(file_date, file_title), '2017-01-20-a-test.md')