78 lines
2.8 KiB
Python
78 lines
2.8 KiB
Python
import unittest
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from common.vo import PageModel
|
|
from module_business.dao.master_dao import MasterDao
|
|
from module_business.dao.operation_dao import OperationDao
|
|
from module_business.dao.report_dao import ReportDao
|
|
from module_business.entity.do.master_do import BizStrain
|
|
from module_business.entity.do.operation_do import BizInboundOrder
|
|
from module_business.entity.vo.master_vo import StrainPageQueryModel
|
|
from module_business.entity.vo.operation_vo import InboundPageQueryModel
|
|
from module_business.entity.vo.report_vo import FillingReportQueryModel
|
|
from utils.page_util import PageUtil
|
|
|
|
|
|
class TestPageUtil(unittest.IsolatedAsyncioTestCase):
|
|
async def test_paginate_excludes_internal_test_batch_field_from_serialized_rows(self) -> None:
|
|
record = BizStrain(
|
|
strain_id=1,
|
|
strain_code='S-1',
|
|
strain_name='菌种',
|
|
status='0',
|
|
test_batch_id=None,
|
|
)
|
|
count_result = MagicMock()
|
|
count_result.scalar.return_value = 1
|
|
query_result = MagicMock()
|
|
query_result.__iter__.return_value = iter([(record,)])
|
|
db = MagicMock(spec=AsyncSession)
|
|
db.execute = AsyncMock(side_effect=[count_result, query_result])
|
|
|
|
page = await PageUtil.paginate(
|
|
db,
|
|
select(BizStrain),
|
|
page_num=1,
|
|
page_size=10,
|
|
is_page=True,
|
|
exclude_fields={'test_batch_id'},
|
|
)
|
|
|
|
self.assertEqual('S-1', page.rows[0]['strainCode'])
|
|
self.assertNotIn('testBatchId', page.rows[0])
|
|
|
|
async def test_business_paginators_hide_internal_test_batch_field(self) -> None:
|
|
db = MagicMock(spec=AsyncSession)
|
|
page = PageModel(rows=[], pageNum=1, pageSize=10, total=0, hasNext=False)
|
|
cases = (
|
|
(
|
|
MasterDao._paginate,
|
|
(db, select(BizStrain), StrainPageQueryModel(), True),
|
|
),
|
|
(
|
|
OperationDao._paginate,
|
|
(db, select(BizInboundOrder), InboundPageQueryModel(), True),
|
|
),
|
|
(
|
|
ReportDao._paginate,
|
|
(db, select(BizInboundOrder), FillingReportQueryModel()),
|
|
),
|
|
)
|
|
|
|
for paginator, args in cases:
|
|
with (
|
|
self.subTest(paginator=paginator.__qualname__),
|
|
patch.object(
|
|
PageUtil,
|
|
'paginate',
|
|
new=AsyncMock(return_value=page),
|
|
) as paginate,
|
|
):
|
|
result = await paginator(*args)
|
|
|
|
self.assertIs(page, result)
|
|
self.assertEqual({'test_batch_id'}, paginate.await_args.kwargs['exclude_fields'])
|