Files
dxdesigner-part-replacement/tests/v1_1/test_backend_targets.py
T

181 lines
7.1 KiB
Python

from pathlib import Path
import pytest
from dx_productivity.trial_task_center.errors import TaskCenterError
from dx_productivity.trial_task_center.project_tasks import create_project_tasks
from dx_productivity.trial_task_center.store import TaskStore
from dx_productivity.trial_task_center.target_plan import ComponentTarget, TargetPlanRow, compile_target_rules
def target(uid: str, refdes: str) -> ComponentTarget:
return ComponentTarget(Path('board.prj'), 'MAIN', '1', uid, refdes, 'OLD', 'V0')
def test_directory_creates_one_task_per_project_and_deduplicates_active_paths(tmp_path):
root = tmp_path / 'projects'
for folder in ('left', 'right'):
path = root / folder / 'same.prj'
path.parent.mkdir(parents=True)
path.write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n', encoding='ascii')
store = TaskStore(tmp_path / 'tasks.db')
created = create_project_tasks(store, root)
again = create_project_tasks(store, root)
assert len(created) == 2
assert again == ()
assert {task.name for task in created} == {'left/same.prj', 'right/same.prj'}
assert all(len(store.list_projects(task.task_id)) == 1 for task in created)
store.close()
reopened = TaskStore(tmp_path / 'tasks.db')
assert len(reopened.list_tasks()) == 2
def test_directory_creation_rolls_back_on_second_project_failure(tmp_path, monkeypatch):
root = tmp_path / 'projects'
root.mkdir()
for name in ('a.prj', 'b.prj'):
(root / name).write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n', encoding='ascii')
store = TaskStore(tmp_path / 'tasks.db')
original = store.sync_projects
calls = 0
def fail_second(*args, **kwargs):
nonlocal calls
calls += 1
if calls == 2:
raise TaskCenterError('test_failure', 'injected failure')
return original(*args, **kwargs)
monkeypatch.setattr(store, 'sync_projects', fail_second)
with pytest.raises(TaskCenterError):
create_project_tasks(store, root)
assert store.list_tasks() == ()
def test_completed_project_can_be_created_as_new_task(tmp_path):
root = tmp_path / 'projects'
root.mkdir()
(root / 'a.prj').write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n', encoding='ascii')
store = TaskStore(tmp_path / 'tasks.db')
first = create_project_tasks(store, root)[0]
from dx_productivity.trial_task_center.models import TaskStatus
store.set_task_status(first.task_id, TaskStatus.READY)
store.set_task_status(first.task_id, TaskStatus.RUNNING)
store.set_task_status(first.task_id, TaskStatus.COMPLETED)
second = create_project_tasks(store, root)
assert len(second) == 1
assert second[0].name == 'a.prj (2)'
assert second[0].primary_project_id != first.primary_project_id
def test_target_rules_preserve_distinct_subsets_of_same_old_part():
rows = (
TargetPlanRow(target('u1', 'R1'), True, 'NEW-A'),
TargetPlanRow(target('u2', 'R2'), False, ''),
TargetPlanRow(target('u3', 'R3'), True, 'NEW-B', True, 'VALUE', False),
)
rules = compile_target_rules(rows)
assert len(rules) == 2
assert {(rule.new_part_number, tuple(item.uid for item in rule.targets)) for rule in rules} == {
('NEW-A', ('u1',)), ('NEW-B', ('u3',))
}
@pytest.mark.parametrize('new_part', ['', 'OLD'])
def test_enabled_target_requires_valid_replacement(new_part):
with pytest.raises(TaskCenterError):
compile_target_rules((TargetPlanRow(target('u1', 'R1'), True, new_part),))
def test_empty_target_group_rejected():
from dx_productivity.trial_task_center.target_plan import TargetRule
with pytest.raises(TaskCenterError):
TargetRule('OLD', 'NEW', ())
def test_plan_edit_is_atomic_and_survives_reopen(tmp_path):
store = TaskStore(tmp_path / 'tasks.db')
task = store.create_task('board')
path = tmp_path / 'board.prj'
store.sync_projects(task.task_id, tmp_path, (path,))
base = ComponentTarget(path, 'MAIN', '1', 'u1', 'R1', 'OLD', 'V0')
store.record_target_inventory(task.task_id, (base,))
saved = store.replace_target_plan(task.task_id, (TargetPlanRow(base, True, 'NEW', True, 'V1', False),))
assert saved[0].new_part_number == 'NEW'
with pytest.raises(TaskCenterError):
forged = ComponentTarget(path, 'MAIN', '1', 'u1', 'R1', 'WRONG', 'V0')
store.replace_target_plan(task.task_id, (TargetPlanRow(forged, True, 'OTHER'),))
assert store.list_target_plan(task.task_id) == saved
store.close()
reopened = TaskStore(tmp_path / 'tasks.db')
assert reopened.list_target_plan(task.task_id) == saved
def test_plan_rejects_duplicate_uid_and_chain(tmp_path):
store = TaskStore(tmp_path / 'tasks.db')
task = store.create_task('board')
path = tmp_path / 'board.prj'
store.sync_projects(task.task_id, tmp_path, (path,))
a = ComponentTarget(path, 'MAIN', '1', 'u1', 'R1', 'OLD', '')
b = ComponentTarget(path, 'MAIN', '1', 'u2', 'R2', 'NEW', '')
store.record_target_inventory(task.task_id, (a, b))
with pytest.raises(TaskCenterError):
store.replace_target_plan(task.task_id, (TargetPlanRow(a), TargetPlanRow(a)))
with pytest.raises(TaskCenterError):
store.replace_target_plan(task.task_id, (TargetPlanRow(a, True, 'NEW'), TargetPlanRow(b, True, 'NEXT')))
def test_forged_initial_plan_cannot_create_inventory(tmp_path):
store = TaskStore(tmp_path / 'tasks.db')
task = store.create_task('board')
path = tmp_path / 'board.prj'
store.sync_projects(task.task_id, tmp_path, (path,))
forged = ComponentTarget(path, 'MAIN', '1', 'forged', 'R1', 'OLD', '')
with pytest.raises(TaskCenterError):
store.replace_target_plan(task.task_id, (TargetPlanRow(forged, True, 'NEW'),))
assert store.get_task(task.task_id).target_plan_mode is False
def test_purge_deletes_target_plan_with_its_task(tmp_path):
store = TaskStore(tmp_path / 'tasks.db')
task = store.create_task('board')
path = tmp_path / 'board.prj'
store.sync_projects(task.task_id, tmp_path, (path,))
store.record_target_inventory(task.task_id, (ComponentTarget(path, 'MAIN', '1', 'u1', 'R1', 'OLD', ''),))
store.soft_delete_task(task.task_id)
assert store.purge_task(task.task_id) == task.task_id
assert store.list_tasks() == ()
def test_schema_five_migrates_without_changing_historic_task_and_rules(tmp_path):
import sqlite3
path = tmp_path / 'tasks.db'
store = TaskStore(path)
task = store.create_task('historic')
rule = store.add_rule(task.task_id, 'OLD', 'NEW')
store.close()
connection = sqlite3.connect(path)
connection.execute('DROP TABLE target_plan')
for column in ('target_plan_mode', 'primary_project_id', 'group_position', 'group_id'):
connection.execute(f'ALTER TABLE tasks DROP COLUMN {column}')
connection.execute("UPDATE metadata SET value = '5' WHERE key = 'schema_version'")
connection.commit()
connection.close()
migrated = TaskStore(path)
assert migrated.get_task(task.task_id).name == 'historic'
assert migrated.get_task(task.task_id).target_plan_mode is False
assert migrated.list_rules(task.task_id) == (rule,)