mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-16 22:34:10 +00:00
templategen tests/coverage
This commit is contained in:
@@ -144,7 +144,7 @@ class Templategen:
|
|||||||
return self.generate_string(content)
|
return self.generate_string(content)
|
||||||
if isinstance(content, dict):
|
if isinstance(content, dict):
|
||||||
return self.generate_dict(content)
|
return self.generate_dict(content)
|
||||||
raise UndefinedError(f'could not template {content}')
|
raise UndefinedException(f'could not template {content}')
|
||||||
|
|
||||||
def add_tmp_vars(self, newvars=None):
|
def add_tmp_vars(self, newvars=None):
|
||||||
"""add vars to the globals, make sure to call restore_vars"""
|
"""add vars to the globals, make sure to call restore_vars"""
|
||||||
|
|||||||
@@ -9,8 +9,108 @@ basic unittest for misc stuff
|
|||||||
# pylint: disable=W0212
|
# pylint: disable=W0212
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
|
from unittest.mock import patch
|
||||||
from dotdrop.profile import Profile
|
from dotdrop.profile import Profile
|
||||||
from dotdrop.linktypes import LinkTypes
|
from dotdrop.linktypes import LinkTypes
|
||||||
|
from dotdrop.templategen import Templategen
|
||||||
|
from dotdrop.exceptions import UndefinedException
|
||||||
|
from jinja2 import TemplateNotFound
|
||||||
|
from tests.helpers import create_random_file, \
|
||||||
|
get_tempdir, clean
|
||||||
|
|
||||||
|
|
||||||
|
class TestTemplateGen(unittest.TestCase):
|
||||||
|
"""test case"""
|
||||||
|
|
||||||
|
def test_misc(self):
|
||||||
|
"""test misc"""
|
||||||
|
tmpl = Templategen()
|
||||||
|
self.assertFalse(tmpl.path_is_template('/abc'))
|
||||||
|
self.assertFalse(tmpl._is_template('/abc'))
|
||||||
|
tmpl._debug_dict('a', 'b')
|
||||||
|
|
||||||
|
def test_lodaer(self):
|
||||||
|
"""test loading template"""
|
||||||
|
tmpl = Templategen()
|
||||||
|
with self.assertRaises(TemplateNotFound):
|
||||||
|
tmpl._template_loader('/abc')
|
||||||
|
|
||||||
|
def test_is_text(self):
|
||||||
|
"""test is_text"""
|
||||||
|
self.assertTrue(Templategen._is_text('empty'))
|
||||||
|
self.assertTrue(Templategen._is_text('json'))
|
||||||
|
self.assertTrue(Templategen._is_text('javascript'))
|
||||||
|
self.assertTrue(Templategen._is_text('ecmascript'))
|
||||||
|
self.assertTrue(Templategen._is_text('text'))
|
||||||
|
self.assertFalse(Templategen._is_text('binary'))
|
||||||
|
|
||||||
|
def test_handle_bin_file(self):
|
||||||
|
"""test handle binary file"""
|
||||||
|
tmpl = Templategen()
|
||||||
|
|
||||||
|
tmpdir = get_tempdir()
|
||||||
|
self.addCleanup(clean, tmpdir)
|
||||||
|
content = b'abc'
|
||||||
|
path, _ = create_random_file(tmpdir, content=content, binary=True)
|
||||||
|
|
||||||
|
cont = tmpl._handle_file(path)
|
||||||
|
self.assertEqual(content, cont)
|
||||||
|
|
||||||
|
def test_filetype(self):
|
||||||
|
"""test using file instead of magic"""
|
||||||
|
oimport = __import__
|
||||||
|
|
||||||
|
def import_mock(name, *args):
|
||||||
|
if name == 'magic':
|
||||||
|
raise ImportError
|
||||||
|
return oimport(name, *args)
|
||||||
|
|
||||||
|
with patch('builtins.__import__',
|
||||||
|
side_effect=import_mock):
|
||||||
|
tmpdir = get_tempdir()
|
||||||
|
self.addCleanup(clean, tmpdir)
|
||||||
|
content = 'abc'
|
||||||
|
path, _ = create_random_file(tmpdir, content=content)
|
||||||
|
|
||||||
|
tmpl = Templategen()
|
||||||
|
self.assertTrue('text' in tmpl._get_filetype(path))
|
||||||
|
|
||||||
|
def test_generate(self):
|
||||||
|
"""test generate"""
|
||||||
|
tmpl = Templategen()
|
||||||
|
self.assertEqual(tmpl.generate('/abc'), '')
|
||||||
|
|
||||||
|
tmpdir = get_tempdir()
|
||||||
|
self.addCleanup(clean, tmpdir)
|
||||||
|
content = '{{@@ non-existing-var @@}}'
|
||||||
|
path, _ = create_random_file(tmpdir, content=content)
|
||||||
|
with self.assertRaises(UndefinedException):
|
||||||
|
tmpl.generate(path)
|
||||||
|
|
||||||
|
fakestring = None
|
||||||
|
self.assertEqual(tmpl.generate_string(fakestring), '')
|
||||||
|
fakestring = '{{@@ non-existing-var @@}}'
|
||||||
|
with self.assertRaises(UndefinedException):
|
||||||
|
tmpl.generate_string(fakestring)
|
||||||
|
|
||||||
|
fakedict = None
|
||||||
|
self.assertEqual(tmpl.generate_dict(fakedict), None)
|
||||||
|
fakedict = {'key': {
|
||||||
|
'subkey', fakestring,
|
||||||
|
}}
|
||||||
|
tmpl.generate_dict(fakedict)
|
||||||
|
|
||||||
|
with self.assertRaises(UndefinedException):
|
||||||
|
tmpl.generate_string_or_dict(2)
|
||||||
|
|
||||||
|
tmpdir2 = get_tempdir()
|
||||||
|
self.addCleanup(clean, tmpdir2)
|
||||||
|
adic = {}
|
||||||
|
path, _ = create_random_file(tmpdir, content='blah')
|
||||||
|
with self.assertRaises(NameError):
|
||||||
|
tmpl._load_path_to_dic(path, adic)
|
||||||
|
|
||||||
|
tmpl._load_funcs_to_dic(None, None)
|
||||||
|
|
||||||
|
|
||||||
class TestLinkTypes(unittest.TestCase):
|
class TestLinkTypes(unittest.TestCase):
|
||||||
|
|||||||
Reference in New Issue
Block a user