|
| 1 | +import io |
| 2 | + |
| 3 | +import tempfile |
| 4 | +from pathlib import Path |
| 5 | +import shutil |
| 6 | + |
| 7 | +from django.test import SimpleTestCase |
| 8 | +from django.core.management import call_command |
| 9 | + |
| 10 | + |
| 11 | +class RenderPatternsTests(SimpleTestCase): |
| 12 | + """Tests of the render_pattern command’s output, based on the test project’s templates""" |
| 13 | + |
| 14 | + def test_displays_patterns(self): |
| 15 | + stdout = io.StringIO() |
| 16 | + call_command('render_patterns', dry_run=True, stdout=stdout) |
| 17 | + stdout = stdout.getvalue() |
| 18 | + self.assertIn("""patterns/atoms/tags_test_atom/tags_test_atom.html |
| 19 | +patterns/atoms/test_atom/test_atom.html |
| 20 | +""", stdout) |
| 21 | + |
| 22 | + def test_verbose_output(self): |
| 23 | + stdout = io.StringIO() |
| 24 | + call_command('render_patterns', dry_run=True, stdout=stdout, verbosity=2) |
| 25 | + stdout = stdout.getvalue() |
| 26 | + self.assertIn("""Target directory: dpl-rendered-patterns |
| 27 | +Group: atoms |
| 28 | +Group: tags_test_atom |
| 29 | +Pattern: tags_test_atom.html |
| 30 | +patterns/atoms/tags_test_atom/tags_test_atom.html |
| 31 | +
|
| 32 | +
|
| 33 | +SANDWICH |
| 34 | +SANDNoneWICH |
| 35 | +SAND0WICH |
| 36 | +""", stdout) |
| 37 | + |
| 38 | + def test_quiet_output(self): |
| 39 | + stdout = io.StringIO() |
| 40 | + call_command('render_patterns', dry_run=True, stdout=stdout, verbosity=0) |
| 41 | + stdout = stdout.getvalue() |
| 42 | + self.assertEqual(stdout, '') |
| 43 | + |
| 44 | + def test_shows_output_folder(self): |
| 45 | + stdout = io.StringIO() |
| 46 | + temp = tempfile.gettempdir() |
| 47 | + call_command('render_patterns', dry_run=True, stdout=stdout, output=temp, verbosity=2) |
| 48 | + stdout = stdout.getvalue() |
| 49 | + self.assertIn(temp, stdout) |
| 50 | + |
| 51 | + |
| 52 | +class RenderPatternsFileSystemTests(SimpleTestCase): |
| 53 | + """Tests of the render_pattern command’s file system changes, based on the test project’s templates""" |
| 54 | + |
| 55 | + def setUp(self): |
| 56 | + self.output = tempfile.mkdtemp() |
| 57 | + |
| 58 | + def tearDown(self): |
| 59 | + shutil.rmtree(self.output) |
| 60 | + |
| 61 | + def test_uses_output(self): |
| 62 | + stdout = io.StringIO() |
| 63 | + modification_time_before = Path(self.output).stat().st_mtime |
| 64 | + call_command('render_patterns', dry_run=False, stdout=stdout, output=self.output) |
| 65 | + self.assertNotEqual(Path(self.output).stat().st_mtime, modification_time_before) |
| 66 | + stdout = stdout.getvalue() |
| 67 | + |
| 68 | + def test_uses_subfolders(self): |
| 69 | + stdout = io.StringIO() |
| 70 | + call_command('render_patterns', dry_run=False, stdout=stdout, output=self.output) |
| 71 | + subfolders = Path(self.output).iterdir() |
| 72 | + self.assertIn('atoms', [p.name for p in subfolders]) |
| 73 | + |
| 74 | + def test_outputs_html(self): |
| 75 | + stdout = io.StringIO() |
| 76 | + call_command('render_patterns', dry_run=False, stdout=stdout, output=self.output) |
| 77 | + html_files = Path(self.output).glob('**/*.html') |
| 78 | + self.assertIn('test_atom.html', [p.name for p in html_files]) |
0 commit comments