def test_function_add(_func): src = ( 'def run():\n' ' pass' ) key = sha256(src.encode('utf-8')).hexdigest() assert UserFunction.add(key, src) assert UserFunction.select(key) == src
def test_function_add_key_mismatch(_func): src = ( 'def run():\n' ' pass' ) with pytest.raises(ValueError) as err: assert UserFunction.add('42', src) assert UserFunction.select() == {} assert 'mismatched' in str(err)
def set_join(self, payload): ''' Setup or change the join function. If ``payload['prev']`` is not the same as current running in ESM process, we will reject this request. ''' if self.esm_worker.join_func['src'] != payload['prev']: self.send_error(payload, reason='`prev` field mismatch') return elif payload['new'] is None: self.send_error(payload, reason='`new` can not be null') return func = UserFunction.select(payload['new']) if not func: self.send_error(payload, reason='New function not found') self.esm_worker.join_func = {'src': func} self.esm_worker.start() return self.res({ 'op': 'set_join', 'prev': payload['prev'], 'new': payload['new'], 'state': 'ok', })
def success(payload): ''' :return: the response of ``add_link`` api call. This handle return *ok* if the ``Link`` record create successfully. The sqlite in-memory will be executed in ``serializable`` ioslation level. If the writting transaction of ``Link`` is failed, we will return a *error* state as response. ''' mode = self.get_mode(payload) try: Link.add(payload['da_id'], payload[mode], mode, topic) except ValueError as err: payload['state'] = 'error' payload['reason'] = 'Link already exists' # update esm worker config and (re)start func = UserFunction.select(payload['func']) if payload.get('func') else None if mode == 'idf': conf = self.esm_worker.idf_confs.copy() conf.append({'src': func, 'topic': topic}) self.esm_worker.idf_confs = conf elif mode == 'odf': conf = self.esm_worker.odf_confs.copy() conf.append({'src': func, 'topic': topic}) self.esm_worker.odf_confs = conf self.esm_worker.start() return payload
def test_rm_funcs_single(graph, _func): func_src = 'def f(): pass' func_sig = ( '3c70e0681767dfd372b5fd92795a22c070937db46671cac5140bbbc5ffa552b3') graph.add_funcs({ 'op': 'add_funcs', 'codes': [func_src], 'digests': [func_sig], }) assert UserFunction.select() == {func_sig: func_src} graph.rm_funcs({ 'op': 'rm_funcs', 'codes': [func_src], 'digests': [func_sig], }) assert UserFunction.select() == {}
def test_rm_funcs_multi(graph, _func): foo_src = 'def foo(): pass' bar_src = 'def bar(): pass' foo_sig = ( 'edd9f8855bc0387c75b6cd52ba6cadbaf706d0cecade3445832374978ee01dcd') bar_sig = ( 'a7b41e562e909c378b102162c118bf83cfa30fc06908436b8ba3cc99aaf42575') graph.add_funcs({ 'op': 'add_funcs', 'codes': [foo_src, bar_src], 'digests': [foo_sig, bar_sig], }) assert UserFunction.select() == {foo_sig: foo_src, bar_sig: bar_src} graph.rm_funcs({ 'op': 'rm_funcs', 'digests': [foo_sig], }) assert UserFunction.select() == {bar_sig: bar_src}
def add_link(self, payload): ''' This add link command will send a ``CONNECT`` control message to device application. We will check the device feature lock as first. Then, record the link to ``Link`` for future lookup. ''' da_id = UUID(payload['da_id']) mode = self.get_mode(payload.keys()) feature = payload[mode] if Link.select(payload['da_id'], feature, mode): return self.send_error(payload, reason='Link already exists') elif payload.get('func') and not UserFunction.select(payload['func']): return self.send_error( payload, reason='Function unknown. Please add it first.') return self._add_link(da_id, mode, feature, payload)
def _func(): assert UserFunction.clear() assert UserFunction.select() == {} yield assert UserFunction.clear() assert UserFunction.select() == {}
def test_function_rm_unknown(_func): assert UserFunction.rm('magic') is False