152 lines
6.8 KiB
Python
152 lines
6.8 KiB
Python
"""Run production comparisons against the actual PON titleblock capture."""
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
import test_accuracy_vbs_guards as guards
|
|
import test_native_snapshot_reader as native
|
|
|
|
|
|
FIXTURE = Path(__file__).with_name('fixtures') / 'pon_titleblock_timestamp.json'
|
|
|
|
|
|
def encoded(text):
|
|
return text.replace('%', '%0025').replace('\t', '%0009').replace('\r', '%000D').replace('\n', '%000A')
|
|
|
|
|
|
def pair(state):
|
|
uid = state.split('\t')[1]
|
|
return f'U\t{uid}\t\t\t\t\t\r\nG\t{uid}\t{encoded(state)}\r\n'
|
|
|
|
|
|
def change_field(state, name, index, value):
|
|
rows = state.split('\r\n')
|
|
for i, row in enumerate(rows):
|
|
fields = row.split('\t')
|
|
if len(fields) > index and fields[:1] == ['A'] and fields[2] == name:
|
|
fields[index] = value
|
|
rows[i] = '\t'.join(fields)
|
|
return '\r\n'.join(rows)
|
|
|
|
|
|
def states(fault, fixture_name):
|
|
before, after = json.loads(FIXTURE.with_name(fixture_name).read_text(encoding='ascii'))['states']
|
|
uid = before.split('\t')[1]
|
|
if fault == 'style':
|
|
after = change_field(after, '@DATETIME', 7, '99')
|
|
elif fault == 'geometry':
|
|
after = after.replace('\t0\t0\t0\t1\tNO_PINS', '\t1\t0\t0\t1\tNO_PINS')
|
|
elif fault == 'author':
|
|
if '\tAUTHOR\t' in after:
|
|
after = change_field(after, 'AUTHOR', 3, 'another author')
|
|
else:
|
|
after += f'A\t{uid}\tAUTHOR\tanother author\t\t0\t0\t0\t0\t0\t0\t1\r\n'
|
|
elif fault == 'sheet':
|
|
after = change_field(after, '@SHEET', 3, '9')
|
|
elif fault == 'missing_time':
|
|
after = '\r\n'.join(row for row in after.split('\r\n') if '@DATETIME' not in row)
|
|
elif fault == 'duplicate_time':
|
|
before += next(row for row in before.split('\r\n') if '@DATETIME' in row) + '\r\n'
|
|
after += next(row for row in after.split('\r\n') if '@DATETIME' in row) + '\r\n'
|
|
elif fault in {'electrical', 'electrical_lower'}:
|
|
name = 'part number' if fault == 'electrical_lower' else 'Part Number'
|
|
row = f'A\t{uid}\t{name}\tPN\tPN\t0\t0\t0\t0\t0\t0\t1\r\n'
|
|
before += row
|
|
after += row
|
|
elif fault == 'refdes':
|
|
before, after = [s.replace(uid+'\t\t0', uid+'\tR1\t0') for s in (before, after)]
|
|
elif fault == 'pins':
|
|
before, after = [s.replace('NO_PINS', 'pin1|GND') for s in (before, after)]
|
|
elif fault == 'missing_marker':
|
|
before, after = [s.replace('@SHEET\t', 'Other\t') for s in (before, after)]
|
|
elif fault == 'lower_markers':
|
|
before, after = [s.replace('@DATETIME', '@datetime').replace('@NAME', '@name').replace(
|
|
'@PATH', '@path').replace('@SHEETTOTAL', '@sheettotal').replace('@SHEET', '@sheet') for s in (before, after)]
|
|
return before, after
|
|
|
|
|
|
FAULTS = ['', 'style', 'geometry', 'author', 'sheet', 'missing_time',
|
|
'duplicate_time', 'electrical', 'electrical_lower', 'refdes', 'pins', 'missing_marker', 'lower_markers']
|
|
|
|
|
|
@pytest.mark.parametrize('fault', FAULTS)
|
|
@pytest.mark.parametrize('comparison', ['baseline', 'native', 'fallback'])
|
|
@pytest.mark.parametrize('fixture_name', ['pon_titleblock_timestamp.json', 'pon_titleblock_without_path.json'])
|
|
def test_only_automatic_titleblock_time_is_ignored(tmp_path, fault, comparison, fixture_name):
|
|
before, after = states(fault, fixture_name)
|
|
uid = before.split('\t')[1]
|
|
expected, actual = tmp_path / 'before.tsv', tmp_path / 'after.tsv'
|
|
# A rows precede C in full-sheet state; annotations must be identified by UID.
|
|
if comparison == 'baseline':
|
|
before, after = ['S\tfull_sheet_v1\r\n' + ''.join(row + '\r\n' for row in sorted(s.splitlines()))
|
|
for s in (before, after)]
|
|
elif comparison == 'native':
|
|
before, after = pair(before), pair(after)
|
|
expected.write_bytes(before.encode('ascii'))
|
|
actual.write_bytes(after.encode('ascii'))
|
|
common = ['QPReadUtf8', 'QPDecode', 'QPEncode', '?QPComparableState']
|
|
setup = '''
|
|
Dim Expected,Actual,Failure
|
|
Expected=QPReadUtf8(''' + native.quote(expected) + ''')
|
|
Actual=QPReadUtf8(''' + native.quote(actual) + ''')
|
|
'''
|
|
if comparison == 'baseline':
|
|
body = setup + '''
|
|
On Error Resume Next
|
|
QPRequireExpectedState Expected,Actual
|
|
Failure=Err.Number
|
|
'''
|
|
output = guards.run_vbs(tmp_path, common + ['QPRequireExpectedState'],
|
|
body + 'WScript.Echo CStr(Failure<>0)', guards.LOG_STUB)
|
|
elif comparison == 'native':
|
|
body = setup + '''
|
|
Dim Records,Components,Identities,FullStates,Rows,ActualRecords,Lines,Fields
|
|
Set Records=CreateObject("Scripting.Dictionary")
|
|
Set Components=CreateObject("Scripting.Dictionary")
|
|
Set Identities=CreateObject("Scripting.Dictionary")
|
|
Set FullStates=CreateObject("Scripting.Dictionary")
|
|
Set Rows=CreateObject("Scripting.Dictionary")
|
|
Set ActualRecords=CreateObject("Scripting.Dictionary")
|
|
Lines=Split(Expected,vbCrLf):Fields=Split(Lines(1),vbTab)
|
|
Identities.Add "$4I918",Lines(0):FullStates.Add "$4I918",QPDecode(Fields(2))
|
|
Records.Add "components",Components:Records.Add "untouched",Identities:Records.Add "untouched_full",FullStates
|
|
Rows.Add "$4I918",Actual:ActualRecords.Add "rows",Rows:ActualRecords.Add "total",1
|
|
On Error Resume Next
|
|
QPCompareNativeUntouched Records,ActualRecords
|
|
Failure=Err.Number
|
|
WScript.Echo CStr(Failure<>0)
|
|
'''
|
|
output = native.run(tmp_path, body.replace('$4I918', uid))
|
|
else:
|
|
# Exercise the COM fallback using the same real full-state capture.
|
|
body = setup + '''
|
|
Dim Records,Components,Identities,FullStates,Comp
|
|
Set QP_App=New FakeApp
|
|
Set Comp=New FullComponent:Comp.UID="$4I918":Comp.Refdes=""
|
|
QP_App.ActiveView.Components.Add Comp,True
|
|
Set Records=CreateObject("Scripting.Dictionary")
|
|
Set Components=CreateObject("Scripting.Dictionary")
|
|
Set Identities=CreateObject("Scripting.Dictionary")
|
|
Set FullStates=CreateObject("Scripting.Dictionary")
|
|
Identities.Add "$4I918","U" & vbTab & "$4I918" & vbTab & vbTab & vbTab & vbTab & vbTab
|
|
FullStates.Add "$4I918",Expected
|
|
Records.Add "components",Components:Records.Add "untouched",Identities:Records.Add "untouched_full",FullStates
|
|
Records.Add "untouched_full_version","1"
|
|
On Error Resume Next
|
|
QPVerifyUntouched Records
|
|
Failure=Err.Number
|
|
WScript.Echo CStr(Failure<>0)
|
|
'''
|
|
names = [n for n in guards.UNTOUCHED_ROUTINES if n != 'QPUntouchedComponent'] + ['?QPComparableState']
|
|
prelude = guards.LOG_STUB + guards.FULL_FAKE + '''
|
|
Function QPUntouchedComponent(ByRef Comp)
|
|
QPUntouchedComponent="U" & vbTab & "$4I918" & vbTab & vbTab & vbTab & vbTab & vbTab & vbCrLf & _
|
|
"G" & vbTab & "$4I918" & vbTab & QPEncode(Actual) & vbCrLf
|
|
End Function
|
|
'''
|
|
output = guards.run_vbs(tmp_path, names, body.replace('$4I918', uid), prelude.replace('$4I918', uid))
|
|
assert output == ('False' if fault in {'', 'lower_markers'} else 'True')
|
|
assert expected.read_bytes() == before.encode('ascii')
|
|
assert actual.read_bytes() == after.encode('ascii')
|