2026-10-22 16:18:37 +08:00
|
|
|
"""Verify import deletion through real local APIs and remove all successful-run fixtures."""
|
|
|
|
|
import concurrent.futures
|
|
|
|
|
import csv
|
|
|
|
|
import io
|
|
|
|
|
import json
|
|
|
|
|
import urllib.request
|
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
|
|
from cleanup_verification import cleanup
|
2026-10-23 09:03:27 +08:00
|
|
|
from verify_scan_only import ScanOnlyVerifier, ROOT
|
2026-10-22 16:18:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def upload(v, rows, files):
|
|
|
|
|
filename = v.prefix + '-' + uuid.uuid4().hex[:6] + '.csv'
|
|
|
|
|
output = io.StringIO(newline='')
|
|
|
|
|
writer = csv.DictWriter(output, fieldnames=list(rows[0]))
|
|
|
|
|
writer.writeheader(); writer.writerows(rows)
|
|
|
|
|
boundary = '----AMFreeScan' + uuid.uuid4().hex
|
|
|
|
|
raw = (f'--{boundary}\r\nContent-Disposition: form-data; name="templateVersion"\r\n\r\nv2.0\r\n'
|
|
|
|
|
f'--{boundary}\r\nContent-Disposition: form-data; name="file"; filename="{filename}"\r\n'
|
|
|
|
|
'Content-Type: text/csv\r\n\r\n').encode() + output.getvalue().encode('utf-8-sig') + f'\r\n--{boundary}--\r\n'.encode()
|
|
|
|
|
request = urllib.request.Request(v.client.base_url + '/admin-api/lab/task/import/upload', data=raw,
|
|
|
|
|
headers={'tenant-id':'1', 'Authorization':'Bearer ' + v.client._token,
|
|
|
|
|
'Content-Type':'multipart/form-data; boundary=' + boundary}, method='POST')
|
|
|
|
|
with urllib.request.urlopen(request, timeout=20) as response:
|
|
|
|
|
result = json.load(response)
|
|
|
|
|
assert result['code'] == 0, result.get('msg')
|
|
|
|
|
files.extend(v.native.db.select('SELECT id,name FROM infra_file WHERE name=%s AND deleted=0', (filename,)))
|
|
|
|
|
return result['data']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def rows_of(v, batch):
|
|
|
|
|
return v.api('GET', f"/lab/task/import/{batch['id']}/rows/page", query={'pageNo':1,'pageSize':100})['list']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove_rows(v, batch, ids, rejected=False):
|
|
|
|
|
return v.api('DELETE', f"/lab/task/import/{batch['id']}/rows", {'rowIds':ids}, rejected=rejected)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run():
|
|
|
|
|
v = ScanOnlyVerifier(); files = []; passed = False
|
2026-10-23 09:03:27 +08:00
|
|
|
checks = []
|
|
|
|
|
print('Regression prefix: '+v.prefix,flush=True)
|
|
|
|
|
def check(name):
|
|
|
|
|
checks.append(dict(name=name,passed=True))
|
|
|
|
|
print('PASS '+name,flush=True)
|
2026-10-22 16:18:37 +08:00
|
|
|
try:
|
|
|
|
|
case = v.create_case('导入删除验证')
|
|
|
|
|
invalid = upload(v, [v.row('invalid', '', 1)], files)
|
|
|
|
|
assert invalid['errorRows'] == 1
|
|
|
|
|
v.api('DELETE', f"/lab/task/import/{invalid['id']}")
|
|
|
|
|
v.api('GET', f"/lab/task/import/{invalid['id']}", rejected=True)
|
|
|
|
|
v.api('POST', f"/lab/task/import/{invalid['id']}/commit", {'importValidOnly':False}, rejected=True)
|
2026-10-23 09:03:27 +08:00
|
|
|
check('delete uncommitted batch prevents later commit')
|
2026-10-22 16:18:37 +08:00
|
|
|
|
|
|
|
|
mixed = upload(v, [v.row('mixed-bad','',1),v.row('mixed-good',case['caseId'],1)], files)
|
|
|
|
|
bad, good = rows_of(v,mixed)
|
|
|
|
|
remove_rows(v,mixed,[good['id']],rejected=True)
|
|
|
|
|
remove_rows(v,mixed,[bad['id'],good['id']],rejected=True)
|
|
|
|
|
remove_rows(v,mixed,[bad['id']])
|
|
|
|
|
summary = v.api('GET', f"/lab/task/import/{mixed['id']}")
|
|
|
|
|
assert summary['totalRows']==1 and summary['errorRows']==0 and summary['validRows']==1
|
|
|
|
|
assert v.api('POST',f"/lab/task/import/{mixed['id']}/commit",{'importValidOnly':False})['generatedCount']==1
|
|
|
|
|
v.api('DELETE',f"/lab/task/import/{mixed['id']}",rejected=True)
|
2026-10-23 09:03:27 +08:00
|
|
|
check('row deletion refreshes counts and permits committing valid remainder; committed batch protected')
|
2026-10-22 16:18:37 +08:00
|
|
|
|
|
|
|
|
committed = upload(v,[v.row('committed-bad','',1),v.row('committed-good',case['caseId'],1)],files)
|
|
|
|
|
bad,good=rows_of(v,committed)
|
|
|
|
|
assert v.api('POST',f"/lab/task/import/{committed['id']}/commit",{'importValidOnly':True})['generatedCount']==1
|
|
|
|
|
kept_task=rows_of(v,committed)[1]['generatedTaskId']
|
|
|
|
|
foreign=upload(v,[v.row('foreign-bad','',1)],files)
|
|
|
|
|
foreign_id=rows_of(v,foreign)[0]['id']
|
|
|
|
|
remove_rows(v,committed,[bad['id'],foreign_id],rejected=True)
|
|
|
|
|
assert len(rows_of(v,committed))==2
|
|
|
|
|
remove_rows(v,committed,[bad['id']])
|
|
|
|
|
assert rows_of(v,committed)[0]['generatedTaskId']==kept_task
|
|
|
|
|
assert v.api('GET',f"/lab/task/import/{committed['id']}")['status']=='COMMITTED'
|
|
|
|
|
remove_rows(v,foreign,[foreign_id])
|
|
|
|
|
assert v.api('GET',f"/lab/task/import/{foreign['id']}")['totalRows']==0
|
|
|
|
|
v.api('POST',f"/lab/task/import/{foreign['id']}/commit",{'importValidOnly':True},rejected=True)
|
|
|
|
|
v.api('DELETE',f"/lab/task/import/{foreign['id']}")
|
2026-10-23 09:03:27 +08:00
|
|
|
check('cross-batch requests rejected atomically; committed tasks preserved; empty batches cannot commit')
|
2026-10-22 16:18:37 +08:00
|
|
|
|
|
|
|
|
duplicate=upload(v,[v.row('same-number','',1),v.row('same-number',case['caseId'],1)],files)
|
|
|
|
|
first,second=rows_of(v,duplicate)
|
|
|
|
|
assert first['validationStatus']=='ERROR' and second['validationStatus']=='DUPLICATE'
|
|
|
|
|
remove_rows(v,duplicate,[first['id']])
|
|
|
|
|
promoted=rows_of(v,duplicate)[0]
|
|
|
|
|
assert promoted['validationStatus']=='VALID' and promoted['rowNo']==second['rowNo']
|
2026-10-23 09:03:27 +08:00
|
|
|
check('remaining file duplicate is revalidated without renumbering')
|
2026-10-22 16:18:37 +08:00
|
|
|
|
|
|
|
|
for n in range(2):
|
|
|
|
|
race=upload(v,[v.row('race-'+str(n),case['caseId'],1)],files)
|
|
|
|
|
base=f"/lab/task/import/{race['id']}"
|
|
|
|
|
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as pool:
|
|
|
|
|
commit=pool.submit(v.client.request,'POST','/admin-api'+base+'/commit',{'importValidOnly':False},None,None)
|
|
|
|
|
delete=pool.submit(v.client.request,'DELETE','/admin-api'+base,None,None,None)
|
|
|
|
|
outcomes=[commit.result(),delete.result()]
|
|
|
|
|
assert sum(r['code']==0 for r in outcomes)==1, outcomes
|
|
|
|
|
tasks=v.native.db.select('SELECT id FROM lab_test_task WHERE source_batch_id=%s AND deleted=0',(race['id'],))
|
|
|
|
|
assert len(tasks)==(1 if outcomes[0]['code']==0 else 0)
|
2026-10-23 09:03:27 +08:00
|
|
|
check('concurrent commit/delete has one winner and no orphan task')
|
2026-10-22 16:18:37 +08:00
|
|
|
passed=True
|
|
|
|
|
finally:
|
|
|
|
|
try:
|
|
|
|
|
if passed:
|
|
|
|
|
result=cleanup([v.prefix],apply=True)
|
|
|
|
|
for file in files:
|
|
|
|
|
assert file['name'].startswith(v.prefix)
|
|
|
|
|
if v.native.db.select('SELECT id FROM infra_file WHERE id=%s AND deleted=0',(file['id'],)):
|
|
|
|
|
v.client.request('DELETE','/admin-api/infra/file/delete',query={'id':file['id']})
|
|
|
|
|
assert not cleanup([v.prefix])['counts']
|
2026-10-23 09:03:27 +08:00
|
|
|
(ROOT/'.local/import-deletion-verification.json').write_text(json.dumps(dict(prefix=v.prefix,passed=True,checks=checks,cleanup=result,remaining={}),ensure_ascii=False,indent=2),encoding='utf-8')
|
2026-10-22 16:18:37 +08:00
|
|
|
print('PASS all current-run fixtures and files cleaned',flush=True)
|
|
|
|
|
finally:
|
|
|
|
|
if v.native._db: v.native.db.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__=='__main__':
|
|
|
|
|
run()
|