'Tests that previous revisions are not deleted
Tests that only the most recent revision is deleted when
"replace" is checked.'

def test_replace(self):

    url = reverse(u'wiki:attachments_index', kwargs={u'path': u''})
    data = u'This    is    a    plain    text    file'
    filestream = self._createTxtFilestream(data)
    self.c.post(url, {u'description': u'My    file', u'file': filestream, u'save': u'1'})
    attachment = self.article.shared_plugins_set.all()[0].attachment
    self.assertEqual(attachment.attachmentrevision_set.count(), 1)
    url = reverse(u'wiki:attachments_replace', kwargs={u'attachment_id': attachment.id, u'article_id': self.article.id})
    replacement_data = (data + u'    And    this    is    my    edit')
    replacement_filestream = self._createTxtFilestream(replacement_data)
    self.c.post(url, {u'description': u'Replacement    upload', u'file': replacement_filestream})
    attachment = self.article.shared_plugins_set.all()[0].attachment
    self.assertEqual(attachment.attachmentrevision_set.count(), 2)
    self.assertEqual(attachment.original_filename, u'test.txt')
    self.assertEqual(attachment.current_revision.file.file.read(), replacement_data.encode(u'utf-8'))
    first_replacement = attachment.current_revision
    replacement_data2 = (data + u'    And    this    is    a    different    edit')
    replacement_filestream2 = self._createTxtFilestream(replacement_data2)
    self.c.post(url, {u'description': u'Replacement    upload', u'file': replacement_filestream2, u'replace': u'on'})
    attachment = self.article.shared_plugins_set.all()[0].attachment
    self.assertEqual(attachment.attachmentrevision_set.count(), 2)
    self.assertEqual(attachment.current_revision.file.file.read(), replacement_data2.encode(u'utf-8'))
    self.assertNotIn(first_replacement, attachment.attachmentrevision_set.all())
