|
| 1 | +from django.core.exceptions import ImproperlyConfigured |
| 2 | +from mock import patch, MagicMock |
| 3 | +from static_precompiler.compilers import CoffeeScript |
| 4 | +from static_precompiler.exceptions import UnsupportedFile |
| 5 | +from static_precompiler.utils import get_compilers, compile_static, compile_static_lazy |
| 6 | +import unittest |
| 7 | + |
| 8 | + |
| 9 | +class UtilsTestCase(unittest.TestCase): |
| 10 | + |
| 11 | + def test_get_compilers(self): |
| 12 | + with patch("static_precompiler.utils.COMPILERS", ["invalid_classpath"]): |
| 13 | + self.assertRaises(ImproperlyConfigured, get_compilers) |
| 14 | + |
| 15 | + with patch("static_precompiler.utils.COMPILERS", ["non_existing_module.ClassName"]): |
| 16 | + self.assertRaises(ImproperlyConfigured, get_compilers) |
| 17 | + |
| 18 | + with patch("static_precompiler.utils.COMPILERS", ["static_precompiler.NonExistingClass"]): |
| 19 | + self.assertRaises(ImproperlyConfigured, get_compilers) |
| 20 | + |
| 21 | + with patch("static_precompiler.utils.COMPILERS", ["static_precompiler.compilers.CoffeeScript"]): |
| 22 | + compilers = get_compilers() |
| 23 | + self.assertEqual(len(compilers), 1) |
| 24 | + self.assertTrue(isinstance(compilers[0], CoffeeScript)) |
| 25 | + |
| 26 | + def test_compile_static(self): |
| 27 | + mocked_coffeescript_compiler = MagicMock() |
| 28 | + mocked_coffeescript_compiler.is_supported.side_effect = lambda source_path: source_path.endswith(".coffee") |
| 29 | + mocked_coffeescript_compiler.compile.return_value = "compiled coffeescript" |
| 30 | + mocked_less_compiler = MagicMock() |
| 31 | + mocked_less_compiler.is_supported.side_effect = lambda source_path: source_path.endswith(".less") |
| 32 | + mocked_less_compiler.compile.return_value = "compiled less" |
| 33 | + |
| 34 | + with patch("static_precompiler.utils.get_compilers") as mocked_get_compilers: |
| 35 | + mocked_get_compilers.return_value = [ |
| 36 | + mocked_coffeescript_compiler, |
| 37 | + mocked_less_compiler |
| 38 | + ] |
| 39 | + self.assertEquals( |
| 40 | + compile_static("test.coffee"), |
| 41 | + "compiled coffeescript" |
| 42 | + ) |
| 43 | + self.assertEquals( |
| 44 | + compile_static("test.less"), |
| 45 | + "compiled less" |
| 46 | + ) |
| 47 | + self.assertRaises( |
| 48 | + UnsupportedFile, |
| 49 | + lambda: compile_static("test.sass") |
| 50 | + ) |
| 51 | + |
| 52 | + def test_compile_static_lazy(self): |
| 53 | + mocked_compiler = MagicMock() |
| 54 | + mocked_compiler.compile_lazy.return_value = "compiled" |
| 55 | + |
| 56 | + with patch("static_precompiler.utils.get_compilers") as mocked_get_compilers: |
| 57 | + mocked_get_compilers.return_value = [ |
| 58 | + mocked_compiler, |
| 59 | + ] |
| 60 | + self.assertEquals( |
| 61 | + compile_static_lazy("source"), |
| 62 | + "compiled" |
| 63 | + ) |
| 64 | + mocked_compiler.compile_lazy.assert_called_with("source") |
| 65 | + |
| 66 | + |
| 67 | +def suite(): |
| 68 | + loader = unittest.TestLoader() |
| 69 | + test_suite = unittest.TestSuite() |
| 70 | + test_suite.addTest(loader.loadTestsFromTestCase(UtilsTestCase)) |
| 71 | + return test_suite |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == '__main__': |
| 75 | + unittest.TextTestRunner(verbosity=2).run(suite()) |
0 commit comments