def test_precompiler_cache_issue750(self, mock_cache): # emulate memcached and return string mock_cache.side_effect = (lambda key: str("body { color:#990; }")) command = '%s %s -f {infile} -o {outfile}' % (sys.executable, self.test_precompiler) compiler = CachedCompilerFilter(command=command, **self.cached_precompiler_args) self.assertEqual("body { color:#990; }", compiler.input()) self.assertEqual(type(compiler.input()), type(smart_text("body { color:#990; }")))
def test_precompiler_caches_empty_files(self): command = '%s %s -f {infile} -o {outfile}' % (sys.executable, self.test_precompiler) compiler = CachedCompilerFilter(command=command, **self.cached_precompiler_args) self.assertEqual("body { color:#990; }", compiler.input()) cache.set(compiler.get_cache_key(), "") compiler = CachedCompilerFilter(command=command, **self.cached_precompiler_args) self.assertEqual("", compiler.input())
def test_precompiler_not_cacheable(self): command = '%s %s -f {infile} -o {outfile}' % (sys.executable, self.test_precompiler) self.cached_precompiler_args['mimetype'] = 'text/different' compiler = CachedCompilerFilter(command=command, **self.cached_precompiler_args) self.assertEqual("body { color:#990; }", compiler.input()) self.assertIsNotNone(compiler.infile) # Not cached compiler = CachedCompilerFilter(command=command, **self.cached_precompiler_args) self.assertEqual("body { color:#990; }", compiler.input()) self.assertIsNotNone(compiler.infile) # Not cached
def test_precompiler_cache(self): command = '%s %s -f {infile} -o {outfile}' % (sys.executable, self.test_precompiler) compiler = CachedCompilerFilter(command=command, **self.cached_precompiler_args) self.assertEqual("body { color:#990; }", compiler.input()) # We tell whether the precompiler actually ran by inspecting compiler.infile. If not None, the compiler had to # write the input out to the file for the external command. If None, it was in the cache and thus skipped. self.assertIsNotNone(compiler.infile) # Not cached compiler = CachedCompilerFilter(command=command, **self.cached_precompiler_args) self.assertEqual("body { color:#990; }", compiler.input()) self.assertIsNone(compiler.infile) # Cached self.cached_precompiler_args['content'] += ' ' # Invalidate cache by slightly changing content compiler = CachedCompilerFilter(command=command, **self.cached_precompiler_args) self.assertEqual("body { color:#990; }", compiler.input()) self.assertIsNotNone(compiler.infile) # Not cached