class ReportModelTest(unittest.TestCase): def setUp(self) -> None: self.inj = Injector() self.db = sqlDatabase self.db.connect(argv[1], int(argv[2]), argv[3], argv[4], argv[5]) self.inj.provide(SQLDatabase, self.db) def insert_data(self): r = Room(room_id='test') rm = RoomModelImpl(self.inj) rid = rm.insert(r.room_id, r.app_key) sleep(1) em = EventModelImpl(self.inj) mm = MetricsModelImpl(self.inj) sm = StatisticModelImpl(self.inj) em.insert_connect_event(rid) sleep(1) em.insert_start_state_control_event(rid, 'high') sleep(0.5) mm.insert(rid, 'high', 24.0) sleep(0.5) sm.insert(rid, 2.0, 10.0) sleep(0.5) mm.insert(rid, 'high', 23.0) sleep(0.5) sm.insert(rid, 2.0, 10.0) sm.insert(rid, 0.2, 1.0) em.insert_stop_state_control_event(rid) em.insert_disconnect_event(rid)
def build_service(self, inj: Injector = None): inj = inj or self.injector inj.build(ConnectionService, ConnectionServiceImpl) inj.build(StartStateControlService, StartStateControlServiceImpl) inj.build(StopStateControlService, StopStateControlServiceImpl) inj.build(MetricsService, MetricsServiceImpl) inj.build(GenerateStatisticService, GenerateStatisticServiceImpl) inj.build(DisConnectionService, DisConnectionServiceImpl) inj.build(AdminGenerateReportService, AdminGenerateReportServiceImpl) inj.build(AdminGetConnectedSlavesService, AdminGetConnectedSlavesServiceImpl) inj.build(AdminGetConnectedSlaveService, AdminGetConnectedSlaveServiceImpl) inj.build(AdminGetServerStatusService, AdminGetServerStatusServiceImpl) inj.build(AdminGetSlaveStatisticsService, AdminGetSlaveStatisticsServiceImpl) inj.provide((AdminGetSlaveStatisticsService, 'V2'), AdminGetSlaveStatisticsServiceImplV2(inj)) inj.build(AdminSetCurrentTemperatureService, AdminSetCurrentTemperatureServiceImpl) inj.build(AdminSetModeService, AdminSetModeServiceImpl) inj.build(AdminLoginService, AdminLoginServiceImpl) inj.build(AdminBootMasterService, AdminBootMasterServiceImpl) inj.build(AdminShutdownMasterService, AdminShutdownMasterServiceImpl) inj.build(AdminBootMasterDaemonService, AdminBootMasterDaemonServiceImpl) inj.build(AdminShutdownMasterDaemonService, AdminShutdownMasterDaemonServiceImpl) inj.build(AdminSetMetricDelayService, AdminSetMetricDelayServiceImpl) inj.build(AdminSetUpdateDelayService, AdminSetUpdateDelayServiceImpl) inj.build(AdminGetRoomCountService, AdminGetRoomCountServiceImpl) return inj
class BasicSqlite3Test(unittest.TestCase): def setUp(self) -> None: self.db = SQLite3(memory=True) self.injector = Injector() self.injector.provide(SQLDatabase, self.db) self.model = None def tearDown(self) -> None: del self.db def assert_create_table(self): self.assertTrue(self.model.create(), self.db.last_error_lazy)
def build_and_run_console(inj: Injector): inj.provide(MainConsole, MainConsoleImpl()) inj.build(BootConsole, BootConsoleImpl) inj.build(MetricConsole, MetricConsoleImpl) inj.build(ReportConsole, ReportConsoleImpl) inj.build(SetMetricFrequencyConsole, SetMetricFrequencyConsoleImpl) inj.build(SetModeConsole, SetModeConsoleImpl) inj.build(SetStatisticFrequencyConsole, SetStatisticFrequencyConsoleImpl) inj.build(SetTemperatureConsole, SetTemperatureConsoleImpl) inj.build(ShutdownConsole, ShutdownConsoleImpl) inj.build(StatusConsole, StatusConsoleImpl) console = inj.require(MainConsole) console.start()
class MetricModelTest(unittest.TestCase): def setUp(self) -> None: self.inj = Injector() self.inj.provide(SQLDatabase, BaseSQLDatabaseImpl()) self.db = self.inj.require(SQLDatabase) self.db.connect(argv[1], int(argv[2]), argv[3], argv[4], argv[5]) def insert_data(self): r = Room(room_id='metric_test') rm = RoomModelImpl(self.inj) rid = rm.insert(r.room_id, r.app_key) r2 = Room(room_id='metric_test2') r2id = rm.insert(r2.room_id, r2.app_key) sleep(1) em = EventModelImpl(self.inj) mm = MetricsModelImpl(self.inj) sm = StatisticModelImpl(self.inj) em.insert_connect_event(rid) sleep(1) em.insert_start_state_control_event(rid, 'high') sleep(0.5) mm.insert(rid, 'high', 24.0) sleep(0.5) sm.insert(rid, 2.0, 10.0) em.insert_start_state_control_event(r2id, 'low') sleep(0.5) mm.insert(rid, 'high', 23.0) sm.insert(r2id, 1.0, 5.0) sleep(0.5) sm.insert(rid, 2.0, 10.0) mm.insert(r2id, 'low', 24.0) sm.insert(rid, 0.2, 1.0) em.insert_stop_state_control_event(rid) sleep(0.5) mm.insert(r2id, 'low', 23.0) sleep(0.5) em.insert_disconnect_event(rid) sm.insert(r2id, 1.0, 5.0) sleep(0.5) mm.insert(r2id, 'low', 22.0) sm.insert(r2id, 0.5, 2.5) em.insert_stop_state_control_event(r2id) sleep(1) em.insert_disconnect_event(r2id)
def inject_external_dependency(inj: Injector): # 无依赖接口 inj.provide(Serializer, JSONSerializer()) # system接口 inj.provide(SystemEntropyProvider, SystemEntropyProviderImpl()) # 日志 logger = std_logging.StdLoggerImpl() logger.logger.addHandler(std_logging.StreamHandler()) inj.provide(Logger, logger) inj.provide(Flask, Flask(APPName)) inj.build(OptionProvider, StdArgParser) inj.build(ConfigurationProvider, FileConfigurationProvider) inj.build(RouteController, FlaskRouteController) inj.build(JWT, PyJWTImpl) return inj
def inject_global_vars(inj: Injector): inj.provide(APPVersion, 'v0.1.0') inj.provide(APPDescription, 'center air conditioner daemon base on flask') inj.provide(APPName, 'center-air-conditioner-daemon') return inj
def build_external_dependency(self, inj: Injector): # 无依赖接口 inj.provide(Serializer, JSONSerializer()) inj.build(RouteController, FlaskRouteController) # system接口 inj.provide(SystemEntropyProvider, SystemEntropyProviderImpl()) # 日志 self.logger = std_logging.StdLoggerImpl() self.logger.logger.addHandler(std_logging.StreamHandler()) inj.provide(Logger, self.logger) # 配置 inj.build(OptionProvider, StdArgParser) inj.build(ConfigurationProvider, FileConfigurationProvider) # 数据库 if self.cfg.use_test_database: self.db_conn = SQLite3(memory=True) else: self.db_conn = BaseSQLDatabaseImpl(inj) inj.provide(SQLDatabase, self.db_conn) inj.build(UUIDGenerator, SystemEntropyUUIDGeneratorImpl) inj.build(PasswordVerifier, BCryptPasswordVerifier) inj.build(JWT, PyJWTImpl) inj.provide(Flask, Flask(APPName)) self.connection_pool = SafeMemoryConnectionPoolImpl(inj) inj.provide(ConnectionPool, self.connection_pool) # todo: should provide parameters later # inj.build(Dispatcher, PriQueueDispatcher()) inj.provide(Dispatcher, QueueDispatcherWithThreadPool()) self.websocket_conn = functional_flask_socket_io_connection_impl(inj) inj.provide(WebsocketConn, self.websocket_conn) inj.build(MasterFanPipe, MasterFanPipeImpl) inj.build(MasterAirCond, MasterAirCondImpl) return inj
def build_global_vars(self, inj: Injector): inj.provide(APPVersion, 'v0.8.5') inj.provide(APPDescription, 'center air conditioner base on flask') inj.provide(APPName, 'center-air-conditioner-server') return inj