class ArticleTest(TestCase):
	"""
	Tests for the Article models methods.
	"""

	def setUp(self):
		test_file = os.path.normpath(os.path.join(BASE_DIR, "fixtures/testfile.pdf"))
		data = {
			'file': File(open(test_file, 'rb')),
			'title': "This is a test file",
		}
		self.article = Article(**data)
		self.article.save()

	def tearDown(self):
		self.article.file.delete()

	def test_generate_thumbnail(self):
		expected = 'Generated Image: testfile_med.png'
		actual = self.article.generate_thumbnail(dryrun=True)
		self.assertEqual(expected, actual)

	def test_generate_all_thumbnails(self):
		extension_list = ['_%s.png' % k for k in IMG_SIZE.keys()]
		response_list = self.article.generate_all_thumbnails(dryrun=True)
		for ext in extension_list:
			message = "Generated Image: testfile%s" % ext
			self.assertTrue(message in response_list, msg=message + " not in %s" % response_list)

	def test_base_filename(self):
		expected = 'testfile'
		actual = self.article.base_filename()
		self.assertEqual(expected, actual)
	def setUp(self):
		test_file = os.path.normpath(os.path.join(BASE_DIR, "fixtures/testfile.pdf"))
		data = {
			'file': File(open(test_file, 'rb')),
			'title': "This is a test file",
		}
		self.article = Article(**data)
		self.article.save()
    def import_document(self, doc):
        '''Import a single :class:`~georgia_lynchings.articles.models.PcAceDocument`.'''

        id = doc.id
        if id is None:
            if self.verbosity > 0:
                print 'Failed to import document %s: no id' % (doc.uri.n3(),)
            return
        
        filename = doc.pdf_filename
        if filename is None:
            if self.verbosity > 1:
                print 'No filename for %s.' % (doc.uri.n3(),)
            filename_bits = {}
        else:
            filename_bits = self.parse_filename(filename)
        
        def _get_field(name, desc):
            val = getattr(doc, name, None)
            if val is None:
                if name in filename_bits:
                    val = filename_bits[name]
                    if self.verbosity > 2:
                        print 'No %s for %s. Inferring from filename.' % (desc, doc.uri.n3())
                else:
                    if self.verbosity > 2:
                        print 'No %s for %s. Importing without that value.' % (desc, doc.uri.n3())
            return val

        newspaper_name = _get_field('newspaper_name', 'newspaper name')
        newspaper_date = _get_field('newspaper_date', 'newspaper date')
        page_number = _get_field('page_number', 'page')

        # Even if we're simulating, make the Article just to verify that we
        # can. Simulate should stop at the last possible moment to catch as
        # many errors as possible. In particular, this should verify that
        # the file is readable.
        try: # Can't use get_or_create because that autosaves on creation.
            article = Article.objects.get(identifier=doc.uri)
            created = False
        except Article.DoesNotExist:
            article = Article()
            created = True

        input_file = None
        try:
            if filename and not article.file:
                try:
                    filepath = os.path.join(self.article_path, filename)
                    input_file = open(filepath)
                    article.file = File(input_file)
                except IOError:
                    if self.verbosity > 0:
                        print 'Failed to import document %s: failed to read file "%s"' % \
                                (doc.uri.n3(), filepath)
                    return

            if created:
                article.identifier = doc.id
                if newspaper_name:
                    article.publisher = newspaper_name
                if newspaper_date:
                    article.date = newspaper_date
                if page_number:
                    article.coverage = page_number

            if not self.simulate:
                article.save()

        finally:
            if input_file is not None:
                input_file.close()

        if self.verbosity > 1:
            print 'Import: %s %s,%s,%s,%s,%s' % (
                    '(simulated)' if self.simulate
                                  else ('Article %d := ' % (article.id),),
                    id,
                    repr(filename) if filename else '',
                    repr(unicode(newspaper_name)) if newspaper_name else '',
                    newspaper_date or '',
                    page_number or '',
                )
	def setUp(self):
		self.client = Client()
		for num in range(20):
			article = Article(title="Test Article %s" % num, identifier="%s" % num)
			article.save()