Beispiel #1
0
    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)
Beispiel #2
0
    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)
Beispiel #3
0
class App:
    def __init__(self, CONSTANTS):
        for name in CONSTANTS:
            setattr(self, name, CONSTANTS[name])
        self.meetup = MeetupRSS(self.MEETUP_GROUP_NAME)
        self.git = GitClient(self.REPO_PATH, self.REPO_AUTHOR_NAME,
                             self.REPO_AUTHOR_EMAIL)
        self.writer = FileWriter(self.EVENT_POSTS_DIR)
        self.db = FileDB(self.EVENT_POSTS_DIR)

    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 sync_git(self):
        if self.git.status:
            self.git.stage_all()
            self.git.commit()
            try:
                self.git.push()
                return True
            except Exception as e:
                self.git.remove_head_commit()
                self.git.pull()
                raise e
        else:
            return False

    def poll_and_update(self):
        events = self.meetup.events

        if events:
            self.git.reset_hard()
            self.git.pull()

            for event in events:
                event = self.meetup.parse_event(event)
                self.sync_event_file(event)

            self.sync_git()
Beispiel #4
0
class FileWriterTest(SiteBotTestCase):
    def setUp(self):
        super(FileWriterTest, self).setUp()
        self.test_dir = tempfile.mkdtemp()
        self.subject = FileWriter(self.test_dir)

    def tearDown(self):
        super(FileWriterTest, self).tearDown()
        shutil.rmtree(self.test_dir)

    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')

    def test_split_filename(self):
        self.assertEqual(
            FileWriter.split_filename('2017-01-30-another-event.md'),
            (date(2017, 1, 30), 'another-event'))

    def test_base_path(self):
        self.assertEqual(self.subject.base_path, self.test_dir)

    def test_write(self):
        event = FakeEvent()
        expected_file = '2017-01-01-the-event.md'
        self.subject.write(event, expected_file)
        written = open(path.join(self.test_dir, expected_file))
        self.assertEqual(written.read(), 'event_text')
        written.close()

    def test_delete(self):
        doomed_file_name = '2017-01-01-doomed.md'
        with open(path.join(self.test_dir, doomed_file_name), 'w') as f:
            f.write('Doomed')

        self.assertTrue(path.exists(path.join(self.test_dir,
                                              doomed_file_name)))
        self.subject.delete(doomed_file_name)
        self.assertFalse(
            path.exists(path.join(self.test_dir, doomed_file_name)))

    def test_delete_nonexistant_file(self):
        doomed_file_name = '2017-01-01-doomed-ghost.md'
        self.assertFalse(
            path.exists(path.join(self.test_dir, doomed_file_name)))
        try:
            self.subject.delete(doomed_file_name)
        except:
            self.fail('attempting to delete a nonexistant file should not ' +
                      'raise an exception')