class TestBundleClass(unittest.TestCase): def setUp(self): self.bundle = Bundle(load=False) def tearDown(self): self.bundle = None def test_init(self): self.assertIn("sdk_dir", self.bundle.__dict__) self.assertIn("bundle_dir", self.bundle.__dict__) self.assertIn("profile", self.bundle.__dict__) # force load equals to false for testing bundle = Bundle(bundle_dir="/123/", sdk_dir="/456/", load=False) self.assertEqual(bundle.bundle_dir, "/123/") self.assertEqual(bundle.sdk_dir, "/456/") # catching BundleConfigError when input bundle path is wrong with self.assertRaises(BundleConfigError): bundle = Bundle(bundle_dir="/YOU CANT FIND ME/") bundle = Bundle(bundle_dir=bundle_dir) self.assertEqual(bundle.bundle_dir, bundle_dir) json_path = os.path.normpath( os.path.realpath(__file__) + './../../mockup/bundle/bundle.json') with open(json_path) as f: self.assertDictEqual(bundle.profile, json.load(f)) def test_load_json(self): with self.assertRaises(BundleConfigError): self.bundle.load_json()
class TestBundleClass(unittest.TestCase): def setUp(self): self.bundle = Bundle(load=False) def tearDown(self): self.bundle = None def test_init(self): self.assertIn("sdk_dir", self.bundle.__dict__) self.assertIn("bundle_dir", self.bundle.__dict__) self.assertIn("profile", self.bundle.__dict__) # force load equals to false for testing bundle = Bundle(bundle_dir="/123/", sdk_dir="/456/", load=False) self.assertEqual(bundle.bundle_dir, "/123/") self.assertEqual(bundle.sdk_dir, "/456/") # catching BundleConfigError when input bundle path is wrong with self.assertRaises(BundleConfigError): bundle = Bundle(bundle_dir="/YOU CANT FIND ME/") bundle = Bundle(bundle_dir=bundle_dir) self.assertEqual(bundle.bundle_dir, bundle_dir) json_path = os.path.normpath(os.path.realpath(__file__) + './../../mockup/bundle/bundle.json') with open(json_path) as f: self.assertDictEqual(bundle.profile, json.load(f)) def test_load_json(self): with self.assertRaises(BundleConfigError): self.bundle.load_json()
def test_boot_all(self, boot): bundle_path = dirpath + "/mock_bundles/bundle_1" boot.return_value = BundleMeta (None, Event().set(), None, Mock(is_ready=Event().set())) self.sanjikeeper.boot_all(bundles={bundle_path: Bundle(bundle_path)}, bundle_sequence=[], connection_class=Mockup)
def __init__(self, *args, **kwargs): # Setup default options bundle = kwargs.get("bundle", None) connection = kwargs.get("connection", None) stop_event = kwargs.get("stop_event", Event()) if connection is None: raise ValueError("Connection is required.") # Model-related bundle_dir = os.path.dirname(inspect.getfile(self.__class__)) if bundle is None: bundle = Bundle(bundle_dir=bundle_dir) self.bundle = bundle self.stop_event = stop_event self.reg_thread = None self.reg_delay = lambda: random() * 5 # Router-related (Dispatch) self.router = Router() self.dispatch_thread_count = 3 self.thread_list = [] # Response-related (Resolve) self._session = Session() self.resolve_thread_count = 1 # Message Bus self._conn = connection self.is_ready = Event() self.req_queue = Queue() self.res_queue = Queue() # Setup callbacks self._conn.set_on_message(self.on_sanji_message) self._conn.set_on_connect(self.on_connect) self._conn.set_on_publish(self.on_publish) # Publisher self.publish = Publish(self._conn, self._session) # Register signal to call stop() (only mainthread could do this) if threading.current_thread().__class__.__name__ == '_MainThread': signal.signal(signal.SIGINT, self.exit) # Auto-register routes methods = inspect.getmembers(self, predicate=inspect.ismethod) self._register_routes(methods) # Custom init function if hasattr(self, 'init') and \ hasattr(self.init, '__call__'): _logger.debug("Custom init start") self.init(*args, **kwargs) _logger.debug("Custom init finish")
def test_boot(self, Thread): Thread.return_value = Mock() options = { "bundle": Bundle(dirpath + "/mock_bundles/bundle_1"), "bundle_dir": os.path.normpath(dirpath + "/mock_bundles/bundle_1"), "connection": Mockup(), "stop_event": Event() } meta = self.sanjikeeper.boot(**options) self.assertIsInstance(meta, BundleMeta)
def test_route(self): bundle = Bundle(bundle_dir=bundle_dir) test_model = TestModel(connection=ConnectionMockup(), bundle=bundle) routes = test_model.router.routes self.assertIn("/model/test/:id", routes) self.assertEqual(4, len(routes["/model/test/:id"].handlers)) self.assertIn("/model", routes) self.assertEqual(1, len(routes["/model"].handlers)) self.assertIn("/model/:id", routes) self.assertEqual(3, len(routes["/model/:id"].handlers))
def test_route(self): bundle = Bundle(bundle_dir=bundle_dir) test_model = TestModel(connection=Mockup(), bundle=bundle) routes = test_model.router.routes self.assertIn("/model/test/:id", routes) self.assertEqual(4, len(routes["/model/test/:id"].handlers)) self.assertIn("/model", routes) self.assertEqual(1, len(routes["/model"].handlers)) self.assertIn("/model/:id", routes) self.assertEqual(3, len(routes["/model/:id"].handlers)) # while test=True don't warp function def resp(test): self.assertTrue(test) test_model.mockup_handler(True, resp, test=True)
def test_init(self): with self.assertRaises(BundleConfigError): TestModel(connection=Mockup()) with self.assertRaises(ValueError): TestModel() event = Event() bundle = Bundle(bundle_dir=bundle_dir) tm = TestModel(bundle=bundle, connection=Mockup(), stop_event=event, test_variable=True) # bypassing test_variable to init() self.assertTrue(tm.test_variable)
def test_init(self): with self.assertRaises(BundleConfigError): TestModel(connection=ConnectionMockup()) with self.assertRaises(ValueError): TestModel() event = Event() bundle = Bundle(bundle_dir=bundle_dir) tm = TestModel(bundle=bundle, connection=ConnectionMockup(), stop_event=event) thread = Thread(target=tm.start) thread.daemon = True thread.start() thread.join(0.2) event.set()
def __init__(self, bundle=None, connection=None, stop_event=Event()): if connection is None: raise ValueError("Connection is required.") # Model-related bundle_dir = os.path.dirname(inspect.getfile(self.__class__)) if bundle is None: bundle = Bundle(bundle_dir=bundle_dir) self.bundle = bundle self.stop_event = stop_event # Router-related (Dispatch) self.router = Router() self.dispatch_thread_count = 5 self.dispatch_thread_list = [] # Response-related (Resolve) self._session = Session() self.resolve_thread_count = 1 self.resolve_thread_list = [] # Message Bus self._conn = connection self.is_ready = Event() self.req_queue = Queue() self.res_queue = Queue() # Setup callbacks self._conn.set_on_connect(self.on_connect) self._conn.set_on_message(self.on_message) self._conn.set_on_connect(self.on_connect) self._conn.set_on_publish(self.on_publish) # Publisher self.publish = Publish(self._conn, self._session) # Register signal to call stop() signal.signal(signal.SIGINT, self.exit) # Auto-register routes methods = inspect.getmembers(self, predicate=inspect.ismethod) self._register_routes(methods) # Custom init function logger.debug("Custom init start") self.init() logger.debug("Custom init finish")
def setUp(self): self.bundle = Bundle(load=False)
def setUp(self): self.bundle = Bundle(load=False)
def setUp(self, Thread): self.bundle = Bundle(bundle_dir=bundle_dir) self.test_model = TestModel(connection=Mockup(), bundle=self.bundle)