예제 #1
0
 def test_read_empty(self):
     """FileResource.read() empty file returns empty string."""
     with temporary_directory() as template_dir:
         path = join(template_dir, 'dummy')
         open(path, 'w')
         resource = resources.FileResource(path=path)
         self.assertEqual(resource.read(), u'')
예제 #2
0
 def test_exists_file(self):
     """FileResource.exists is True if path points a file."""
     with temporary_directory() as template_dir:
         path = join(template_dir, 'dummy')
         open(path, 'w')
         self.assertTrue(isfile(path))  # Check initial status.
         resource = resources.FileResource(path=path)
         self.assertTrue(resource.exists is True)
예제 #3
0
 def test_exists_dir(self):
     """FileResource.exists is False if path points a directory."""
     with temporary_directory() as template_dir:
         path = join(template_dir, 'dummy')
         mkdir(path)
         self.assertTrue(isdir(path))  # Check initial status.
         resource = resources.FileResource(path=path)
         self.assertTrue(resource.exists is False)
예제 #4
0
 def test_read_utf8(self):
     """FileResource.read() decodes UTF-8 files."""
     with temporary_directory() as template_dir:
         path = join(template_dir, 'dummy')
         content = u'Thé ou café ?'
         open(path, 'w').write(content.encode('utf8'))
         resource = resources.FileResource(path=path)
         self.assertEqual(resource.read(), content)
예제 #5
0
 def test_render_error(self):
     """FileResource.render() raises ``TemplateError`` in case of fail."""
     with temporary_directory() as template_dir:
         path = join(template_dir, 'dummy')
         template = u'Tea or coffee'
         context = {'a': 1, 'b': 2}
         engine = MockEngine(fail=TemplateError('This is an error message'))
         open(path, 'w').write(template.encode('utf8'))
         resource = resources.FileResource(path=path, engine=engine)
         self.assertRaises(TemplateError, resource.render, context)
예제 #6
0
    def get_resource(self, request):
        """Return the resource matching request.

        Return value is a :py:class:`FileResource` or :py:class`DirResource`.

        """
        path = self.get_resource_path(request)
        engine = self.get_engine(request)
        filename_engine = self.get_filename_engine(request)
        if isdir(path):
            resource = resources.DirResource(path=path,
                                             engine=engine,
                                             filename_engine=filename_engine)
        else:
            resource = resources.FileResource(path=path,
                                              engine=engine,
                                              filename_engine=filename_engine)
        return resource
예제 #7
0
    def test_render(self):
        """FileResource.render() generates rendered template against context.

        It returns an iteratable or generator.

        """
        with temporary_directory() as template_dir:
            path = join(template_dir, 'dummy')
            template = u'Tea or coffee'
            context = {'a': 1, 'b': 2}
            engine = MockEngine(u'this is render result')
            open(path, 'w').write(template.encode('utf8'))
            resource = resources.FileResource(path=path, engine=engine)
            result = resource.render(context)
            result = ''.join(result)
            self.assertEqual(result, u'this is render result')
            self.assertEqual(engine.args, (template, context))
            self.assertEqual(engine.kwargs, {})
예제 #8
0
 def test_exists_false(self):
     """FileResource.exists is False if file doesn't exist at path."""
     path = join('i', 'do', 'not', 'exist')
     self.assertFalse(exists(path))  # Just in case.
     resource = resources.FileResource(path=path)
     self.assertTrue(resource.exists is False)
예제 #9
0
 def test_content_type(self):
     """FileResource.content_type is 'text/plain'."""
     resource = resources.FileResource()
     self.assertEqual(resource.content_type, 'text/plain')