def test_cached_read(self): """ Test method that creates a copy with file read already performed """ file_path = os.path.dirname(os.path.realpath(__file__)) file_path = os.path.join(file_path, 'person_body_template.json') # Read file to compare file_content = None with open(file_path, 'r') as f: file_content = f.read() handler = ContentHandler() handler.setup(file_path, is_file=True) # Check it read the file correctly cached_handler = handler.create_noread_version() self.assertEqual(file_content, handler.get_content()) self.assertFalse(cached_handler.is_file) # Check with templating handler.is_template_content = True cached_handler = handler.create_noread_version() self.assertEqual(file_content, handler.get_content()) self.assertEqual(handler.is_template_content, cached_handler.is_template_content) self.assertFalse(cached_handler.is_file) # Check dynamic paths don't try to template handler.is_template_path = True cached_handler = handler.create_noread_version() self.assertTrue(handler is cached_handler)
def test_content_templating(self): """ Test content and templating of it """ handler = ContentHandler() body = '$variable value' templated_body = 'bar value' context = Context() context.bind_variable('variable', 'bar') # No templating handler.setup(body, is_template_content=False) self.assertEqual(body, handler.get_content()) self.assertEqual(body, handler.get_content(context)) self.assertRaises(TypeError, handler.setup, []) # Templating handler.setup(body, is_template_content=True) self.assertEqual(body, handler.get_content()) self.assertEqual(templated_body, handler.get_content(context))
def test_content_file_template(self): """ Test file read and templating of read files in this directory """ variables = {'id': 1, 'login': '******'} context = Context() file_path = os.path.dirname(os.path.realpath(__file__)) file_path = os.path.join(file_path, 'person_body_template.json') file_content = None with open(file_path, 'r') as f: file_content = f.read() # Test basic read handler = ContentHandler() handler.setup(file_path, is_file=True) self.assertEqual(file_content, handler.get_content()) # Test templating of read content handler.setup(file_path, is_file=True, is_template_content=True) self.assertEqual(file_content, handler.get_content()) self.assertEqual(file_content, handler.get_content( context)) # No substitution substituted = string.Template(file_content).safe_substitute(variables) context.bind_variables(variables) self.assertEqual(substituted, handler.get_content(context)) # Test path templating templated_file_path = '$filepath' context.bind_variable('filepath', file_path) handler.setup(file_path, is_file=True, is_template_path=True) self.assertEqual(file_content, handler.get_content(context)) # Test double templating with files handler.setup(file_path, is_file=True, is_template_path=True, is_template_content=True) self.assertEqual(substituted, handler.get_content(context=context))
def test_unicode_templating(self): """ A couple combination of templating using Unicode data """ handler = ContentHandler() context = Context() # ASCII body, unicode data body = '$variable value' context.bind_variable('variable', u'😽') handler.setup(body, is_template_content=True) self.assertEqual(body, handler.get_content()) self.assertEqual(u'😽 value', handler.get_content(context)) # Unicode body, ASCII data context.bind_variable('variable', u'string') body = u'$variable 😽 value' handler.setup(body, is_template_content=True) self.assertEqual(u'$variable 😽 value', handler.get_content()) self.assertEqual(u'string 😽 value', handler.get_content(context)) # All the Unicodes, all the times! context.bind_variable('variable', u'😽') body = u'$variable 😽 value' handler.setup(body, is_template_content=True) self.assertEqual(u'😽 😽 value', handler.get_content(context))