def test_cache(self): """Ensure that _FactoryWrapper tries to import only once.""" orig_date = datetime.date w = declarations._FactoryWrapper('datetime.date') self.assertEqual(None, w.factory) factory_class = w.get() self.assertEqual(orig_date, factory_class) try: # Modify original value datetime.date = None # Repeat import factory_class = w.get() self.assertEqual(orig_date, factory_class) finally: # IMPORTANT: restore attribute. datetime.date = orig_date
def test_lazyness(self): f = declarations._FactoryWrapper('factory.declarations.Sequence') self.assertEqual(None, f.factory) factory_class = f.get() self.assertEqual(declarations.Sequence, factory_class)
def test_path(self): w = declarations._FactoryWrapper('datetime.date') self.assertEqual(datetime.date, w.get())
def test_class(self): w = declarations._FactoryWrapper(datetime.date) self.assertEqual(datetime.date, w.get())
def test_invalid_path(self): with self.assertRaises(ValueError): declarations._FactoryWrapper('UnqualifiedSymbol') with self.assertRaises(ValueError): declarations._FactoryWrapper(42)