Source

djangosassy / tests.py

import glob
import os
import time

from django import test

import djangosassy.utils


class UtilsTest(test.TestCase):
    css_root = os.path.join(
        os.path.join(djangosassy.utils.DJANGOSASSY_PATH, 'static'),
        'styles'
    )

    scss_root = djangosassy.utils.SCSS_ROOT

    directories = {
        "app_root": djangosassy.utils.DJANGOSASSY_PATH,
        "scss_dir": djangosassy.utils.SCSS_ROOT,
        "static_dir": djangosassy.utils.STATIC_DIR
    }

    def setUp(self):
        self.scss_file = os.path.join(self.scss_root, 'test.scss')
        self.css_file = os.path.join(self.css_root, 'test.css')

        with file(self.scss_file, 'w') as scss_file:
            scss_file.write(' ')

        with file(self.css_file, 'w') as css_file:
            css_file.write(' ')

    def tearDown(self):
        os.remove(self.scss_file)
        os.remove(self.css_file)

        for directory in self.directories.values():
            try:
                os.rmdir(directory)
            except OSError:
                pass

    def test_compile_style_old(self):
        original_modified_time = os.stat(self.css_file).st_mtime

        file_name = djangosassy.utils.compile_style(
            self.scss_file,
            self.css_root
        )
        full_path = os.path.join(self.css_root, file_name)

        new_modified_time = os.stat(full_path).st_mtime

        self.assertEqual(original_modified_time, new_modified_time)

    def test_compile_style_new(self):
        time.sleep(.5)
        os.utime(self.scss_file, None)

        original_modified_time = os.stat(self.css_file).st_mtime

        file_name = djangosassy.utils.compile_style(
            self.scss_file,
            self.css_root
        )
        full_path = os.path.join(self.css_root, file_name)

        new_modified_time = os.stat(full_path).st_mtime

        self.assertNotEqual(original_modified_time, new_modified_time)

    def test_compile_style_does_not_exist(self):
        with self.assertRaises(djangosassy.utils.ScssFileNotFound):
            djangosassy.utils.compile_style('stuff', self.css_root)

    def test_compile_styles(self):
        file_names = glob.glob(os.path.join(self.scss_root, '*.scss'))

        self.assertEqual(
            len(file_names),
            len(
                djangosassy.utils.compile_styles(
                    self.scss_root,
                    self.css_root
                )
            )
        )

    def test_create_directories(self):
        djangosassy.utils.create_directories(self.directories)

        self.assertTrue(
            os.path.isdir(directory)
            for directory in self.directories.values()
        )

    def test_create_directories_junk_scss(self):
        self.assertFalse(djangosassy.utils.create_directories({}))

    def test_build_paths_count(self):
        module_path = os.path.realpath(os.path.dirname(djangosassy.__file__))

        directories = djangosassy.utils.build_paths(module_path)

        self.assertTrue(len(directories) == 4)

    def test_build_paths_exist(self):
        module_path = os.path.realpath(os.path.dirname(djangosassy.__file__))

        directories = djangosassy.utils.build_paths(module_path)

        self.assertTrue(os.path.isdir(directory) for directory in directories)