async def test_value_write_bind_with_empty(): write = SQLValuesToWrite({'$num': 123}) view: PeeweeView = await make_mocked_view_instance(app, ATestView, 'POST', '/api/list/1') write.bind(view, None, None) assert len(write) == 0
async def set(self): """ 更新数据接口 查询规则参考 https://fy0.github.io/slim/#/quickstart/query_and_modify 赋值规则参考 https://fy0.github.io/slim/#/quickstart/query_and_modify?id=修改新建 """ self.current_interface = InnerInterfaceName.SET with ErrorCatchContext(self): info = SQLQueryInfo(self.params, self) await self._call_handle(self.before_query, info) records, count = await self._sql.select_page(info, size=self.bulk_num()) if records: # 确保 before_update 时得到list records = list(records) values = SQLValuesToWrite(await self.post_data()) values.bind(self, A.WRITE, records) await self._call_handle(self.before_update, values, records) # 如果 before_update 之后,不再有values,那么抛出invalid_postdata if len(values) == 0: raise InvalidPostData("No value to set for table: %s" % self.table_name) if logger.isEnabledFor(logging.DEBUG): logger.debug('update record(s): %s' % values) # 注:此处returning为true是因为后续要检查数据的权限,和前端要求无关 new_records = await self._sql.update(records, values, returning=True) await self.check_records_permission(None, new_records) await self._call_handle(self.after_update, values, records, new_records) if await self.is_returning(): self.finish(RETCODE.SUCCESS, new_records) else: self.finish(RETCODE.SUCCESS, len(new_records)) else: self.finish(RETCODE.NOT_FOUND)
async def test_value_write_invalid(): write = SQLValuesToWrite({'num1': 123, 'str1': {}}) view: PeeweeView = await make_mocked_view_instance(app, ATestView, 'POST', '/api/list/1') with pytest.raises(InvalidPostData) as e: write.bind(view, None, None)
async def test_value_write_normal2(): write = SQLValuesToWrite({'num1': 123, 'str1': 456}) view: PeeweeView = await make_mocked_view_instance(app, ATestView, 'POST', '/api/list/1') write.bind(view, None, None) assert write['str1'] == '456'
async def test_value_write_normal(): write = SQLValuesToWrite({'num1': 123, 'str1': 'whatever'}) view: PeeweeView = await make_mocked_view_instance(app, ATestView, 'POST', '/api/list/1') write.bind(view, None, None)