def test_registry_load_working_set(self): # do note these mocking sets are for the registry; actual # filenames is not overridden (uses pkg_resources directly) working_set = mocks.WorkingSet( {'nunja.mold': [ 'nunja.testing.molds = nunja.testing:mold', ]}, dist=Distribution(project_name='nunjatesting', version='0.0')) with pretty_logging(logger='nunja', stream=mocks.StringIO()) as stream: registry = MoldRegistry('nunja.mold', _working_set=working_set) records = registry.get_records_for_package('nunjatesting') keys = [ 'nunja.testing.molds/include_by_name/index', 'nunja.testing.molds/include_by_value/index', 'nunja.testing.molds/itemlist/index', 'nunja.testing.molds/noinit/index', 'nunja.testing.molds/problem/index', 'text!nunja.testing.molds/basic/template.nja', 'text!nunja.testing.molds/include_by_name/empty.nja', 'text!nunja.testing.molds/include_by_name/template.nja', 'text!nunja.testing.molds/include_by_value/template.nja', 'text!nunja.testing.molds/itemlist/template.nja', 'text!nunja.testing.molds/noinit/template.nja', 'text!nunja.testing.molds/problem/template.nja', ] self.assertEqual(sorted(records.keys()), keys) self.assertIn('7 templates', stream.getvalue()) self.assertIn('5 scripts', stream.getvalue()) self.assertIn('generated 6 molds', stream.getvalue()) # select directly by mold_id through get_record self.assertEqual( sorted(registry.get_record('nunja.testing.molds/basic').keys()), ['text!nunja.testing.molds/basic/template.nja'], ) self.assertEqual( sorted(registry.get_record('nunja.testing.molds/itemlist').keys()), [ 'nunja.testing.molds/itemlist/index', 'text!nunja.testing.molds/itemlist/template.nja', ], ) self.assertEqual( sorted( registry.get_record( 'nunja.testing.molds/include_by_name').keys()), [ 'nunja.testing.molds/include_by_name/index', 'text!nunja.testing.molds/include_by_name/empty.nja', 'text!nunja.testing.molds/include_by_name/template.nja', ], )
def test_registry_graceful_fail(self): working_set = mocks.WorkingSet({ 'calmjs.registry': [ 'failure = calmjs.testing.no_such_module:NoClass', ] }) registry = calmjs.registry.Registry('calmjs.registry', _working_set=working_set) # This should not be registered or available self.assertIsNone(registry.get_record('calmjs.module')) self.assertIsNone(registry.get_record('failure'))
def test_registry_graceful_fail_bad_constructor(self): working_set = mocks.WorkingSet({'calmjs.registry': [ 'failure = calmjs.testing.module3.module:NotRegistry', ]}) registry = calmjs.registry.Registry( 'calmjs.registry', _working_set=working_set) # This should not be registered or available self.assertIsNone(registry.get_record('calmjs.module')) with pretty_logging(stream=mocks.StringIO()) as stream: self.assertIsNone(registry.get_record('failure')) # exact error message differs between Python versions. self.assertIn('TypeError: __init__() ', stream.getvalue())
def test_registry_fresh_from_entrypoint(self): working_set = mocks.WorkingSet({'calmjs.registry': [ 'custom = calmjs.testing.module3.module:CustomModuleRegistry', ]}) registry = calmjs.registry.Registry( 'calmjs.registry', _working_set=working_set) self.assertEqual(len(registry.records), 0) registry.get_record('custom') self.assertEqual(len(registry.records), 1) from calmjs.testing.module3.module import CustomModuleRegistry self.assertTrue(isinstance( registry.get_record('custom'), CustomModuleRegistry))
def test_registry_graceful_fail(self): working_set = mocks.WorkingSet({'calmjs.registry': [ 'failure = calmjs.testing.no_such_module:NoClass', ]}) registry = calmjs.registry.Registry( 'calmjs.registry', _working_set=working_set) with pretty_logging(stream=mocks.StringIO()) as stream: self.assertIsNone(registry.get_record('calmjs.module')) self.assertIn("'calmjs.module' does not resolve", stream.getvalue()) with pretty_logging(stream=mocks.StringIO()) as stream: self.assertIsNone(registry.get_record('failure')) self.assertIn("ImportError 'failure", stream.getvalue())
def test_registry_graceful_fail_bad_constructor(self): working_set = mocks.WorkingSet({ 'calmjs.registry': [ 'failure = calmjs.testing.module3.module:NotRegistry', ] }) registry = calmjs.registry.Registry('calmjs.registry', _working_set=working_set) # This should not be registered or available self.assertIsNone(registry.get_record('calmjs.module')) with pretty_logging(stream=mocks.StringIO()) as stream: self.assertIsNone(registry.get_record('failure')) # exact error message differs between Python versions. self.assertIn('TypeError: __init__() ', stream.getvalue())
def test_registry_fresh_from_entrypoint(self): working_set = mocks.WorkingSet({ 'calmjs.registry': [ 'custom = calmjs.testing.module3.module:CustomModuleRegistry', ] }) registry = calmjs.registry.Registry('calmjs.registry', _working_set=working_set) self.assertEqual(len(registry.records), 0) registry.get_record('custom') self.assertEqual(len(registry.records), 1) from calmjs.testing.module3.module import CustomModuleRegistry self.assertTrue( isinstance(registry.get_record('custom'), CustomModuleRegistry))
def test_registry_graceful_fail(self): working_set = mocks.WorkingSet({ 'calmjs.registry': [ 'failure = calmjs.testing.no_such_module:NoClass', ] }) registry = calmjs.registry.Registry('calmjs.registry', _working_set=working_set) with pretty_logging(stream=mocks.StringIO()) as stream: self.assertIsNone(registry.get_record('calmjs.module')) self.assertIn("'calmjs.module' does not resolve", stream.getvalue()) with pretty_logging(stream=mocks.StringIO()) as stream: self.assertIsNone(registry.get_record('failure')) self.assertIn("ImportError 'failure", stream.getvalue())
def test_registry_load_working_set(self): # do note these mocking sets are for the registry; actual # filenames is not overridden (uses pkg_resources directly) working_set = mocks.WorkingSet( {'nunja.tmpl': [ 'nunja.testing.templates = nunja.testing:mold', ]}, dist=Distribution(project_name='nunjatesting', version='0.0')) with pretty_logging(logger='nunja', stream=mocks.StringIO()) as stream: registry = JinjaTemplateRegistry('nunja.tmpl', _working_set=working_set) self.assertIn('7 templates', stream.getvalue()) self.assertNotIn('scripts', stream.getvalue()) # to prevent the export of names into the calmjs toolchain, the # standard record retrieval provides nothing. self.assertEqual({}, registry.get_records_for_package('nunjatesting')) self.assertEqual(registry.get_record('nunja.testing.templates/basic'), {}) # records are available via an alternative method. self.assertEqual([ 'nunja.testing.templates/basic/template.nja', 'nunja.testing.templates/include_by_name/empty.nja', 'nunja.testing.templates/include_by_name/template.nja', 'nunja.testing.templates/include_by_value/template.nja', 'nunja.testing.templates/itemlist/template.nja', 'nunja.testing.templates/noinit/template.nja', 'nunja.testing.templates/problem/template.nja', ], sorted(registry.templates.keys()))