|
| 1 | +"""Unit tests for the interactive preview and conflict resolution.""" |
| 2 | + |
| 3 | +import uuid |
| 4 | + |
| 5 | +from osw.tools.user_sync.interactive import ( |
| 6 | + Prompter, |
| 7 | + nonempty_fields, |
| 8 | + render_preview, |
| 9 | + resolve_plan, |
| 10 | +) |
| 11 | +from osw.tools.user_sync.mapping import ProposedUser |
| 12 | +from osw.tools.user_sync.reconcile import ( |
| 13 | + CONFLICT, |
| 14 | + GAP_FILL, |
| 15 | + NEW, |
| 16 | + UNCHANGED, |
| 17 | + FieldDiff, |
| 18 | + ReconcilePlan, |
| 19 | + UserChange, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +class FakeIO: |
| 24 | + def __init__(self, answers): |
| 25 | + self.answers = list(answers) |
| 26 | + self.outputs = [] |
| 27 | + |
| 28 | + def input(self, prompt=""): |
| 29 | + return self.answers.pop(0) |
| 30 | + |
| 31 | + def output(self, text=""): |
| 32 | + self.outputs.append(text) |
| 33 | + |
| 34 | + |
| 35 | +def _proposed(username="u", **kw): |
| 36 | + base = dict( |
| 37 | + uuid=uuid.uuid4(), |
| 38 | + username=username, |
| 39 | + first_name="F", |
| 40 | + surname="S", |
| 41 | + label="F S", |
| 42 | + orcid=None, |
| 43 | + emails=[], |
| 44 | + websites=[], |
| 45 | + organizations=[], |
| 46 | + ) |
| 47 | + base.update(kw) |
| 48 | + return ProposedUser(**base) |
| 49 | + |
| 50 | + |
| 51 | +def _change(category, username="u", diffs=None, placeholder=False): |
| 52 | + proposed = _proposed(username=username) |
| 53 | + proposed.placeholder_name = placeholder |
| 54 | + return UserChange( |
| 55 | + proposed=proposed, |
| 56 | + existing=None if category == NEW else object(), |
| 57 | + category=category, |
| 58 | + diffs=diffs or [], |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +def _prompter(answers): |
| 63 | + io = FakeIO(answers) |
| 64 | + return Prompter(io.input, io.output), io |
| 65 | + |
| 66 | + |
| 67 | +def test_render_preview(): |
| 68 | + plan = ReconcilePlan(changes=[_change(NEW, "alice", placeholder=True)]) |
| 69 | + lines = render_preview(plan) |
| 70 | + assert lines[0].startswith("Users: 1") |
| 71 | + assert "new=1" in lines[0] |
| 72 | + assert "+ alice new" in lines[1] |
| 73 | + assert "[placeholder name]" in lines[1] |
| 74 | + |
| 75 | + |
| 76 | +def test_nonempty_fields(): |
| 77 | + proposed = _proposed(orcid="https://orcid.org/x", emails=["e@x.org"]) |
| 78 | + assert nonempty_fields(proposed) == { |
| 79 | + "label", |
| 80 | + "first_name", |
| 81 | + "surname", |
| 82 | + "orcid", |
| 83 | + "emails", |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | +def _mixed_plan(): |
| 88 | + return ReconcilePlan( |
| 89 | + changes=[ |
| 90 | + _change(NEW, "new1"), |
| 91 | + _change(GAP_FILL, "gap1", [FieldDiff("emails", set(), {"e"}, GAP_FILL)]), |
| 92 | + _change(CONFLICT, "con1", [FieldDiff("surname", "Old", "New", CONFLICT)]), |
| 93 | + _change( |
| 94 | + CONFLICT, |
| 95 | + "mix1", |
| 96 | + [ |
| 97 | + FieldDiff("emails", set(), {"e"}, GAP_FILL), |
| 98 | + FieldDiff("surname", "Old", "New", CONFLICT), |
| 99 | + ], |
| 100 | + ), |
| 101 | + _change(UNCHANGED, "same1"), |
| 102 | + ] |
| 103 | + ) |
| 104 | + |
| 105 | + |
| 106 | +def test_resolve_keep_existing(): |
| 107 | + prompter, _ = _prompter(["k", "y"]) |
| 108 | + res = resolve_plan(_mixed_plan(), prompter) |
| 109 | + assert [r.change.proposed.username for r in res.creates()] == ["new1"] |
| 110 | + updates = {r.change.proposed.username: r.apply_fields for r in res.updates()} |
| 111 | + assert updates == {"gap1": {"emails"}, "mix1": {"emails"}} |
| 112 | + skipped = {r.change.proposed.username for r in res.resolved if r.action == "skip"} |
| 113 | + assert skipped == {"con1", "same1"} |
| 114 | + assert res.accepted is True |
| 115 | + |
| 116 | + |
| 117 | +def test_resolve_apply_all(): |
| 118 | + prompter, _ = _prompter(["a", "y"]) |
| 119 | + res = resolve_plan(_mixed_plan(), prompter) |
| 120 | + updates = {r.change.proposed.username: r.apply_fields for r in res.updates()} |
| 121 | + assert updates["con1"] == {"surname"} |
| 122 | + assert updates["mix1"] == {"emails", "surname"} |
| 123 | + |
| 124 | + |
| 125 | +def test_resolve_abort(): |
| 126 | + prompter, _ = _prompter(["x"]) |
| 127 | + res = resolve_plan(_mixed_plan(), prompter) |
| 128 | + assert res.resolved == [] |
| 129 | + assert res.accepted is False |
| 130 | + |
| 131 | + |
| 132 | +def test_resolve_dry_run_makes_no_writes_and_no_prompts(): |
| 133 | + prompter, io = _prompter([]) # no answers available |
| 134 | + res = resolve_plan(_mixed_plan(), prompter, dry_run=True) |
| 135 | + assert res.accepted is False |
| 136 | + assert io.answers == [] # nothing was asked |
| 137 | + |
| 138 | + |
| 139 | +def test_resolve_assume_yes_keeps_conflicts(): |
| 140 | + prompter, io = _prompter([]) |
| 141 | + res = resolve_plan(_mixed_plan(), prompter, assume_yes=True) |
| 142 | + assert res.accepted is True |
| 143 | + updates = {r.change.proposed.username: r.apply_fields for r in res.updates()} |
| 144 | + assert updates == {"gap1": {"emails"}, "mix1": {"emails"}} |
| 145 | + assert io.answers == [] |
| 146 | + |
| 147 | + |
| 148 | +def test_resolve_review_field_by_field(): |
| 149 | + plan = ReconcilePlan( |
| 150 | + changes=[ |
| 151 | + _change( |
| 152 | + CONFLICT, |
| 153 | + "con1", |
| 154 | + [ |
| 155 | + FieldDiff("surname", "Old", "New", CONFLICT), |
| 156 | + FieldDiff("label", "L1", "L2", CONFLICT), |
| 157 | + ], |
| 158 | + ) |
| 159 | + ] |
| 160 | + ) |
| 161 | + prompter, _ = _prompter(["r", "f", "n", "k", "y"]) |
| 162 | + res = resolve_plan(plan, prompter) |
| 163 | + assert res.updates()[0].apply_fields == {"surname"} |
| 164 | + assert res.accepted is True |
| 165 | + |
| 166 | + |
| 167 | +def test_resolve_review_take_all_for_user(): |
| 168 | + plan = ReconcilePlan( |
| 169 | + changes=[ |
| 170 | + _change( |
| 171 | + CONFLICT, |
| 172 | + "con1", |
| 173 | + [ |
| 174 | + FieldDiff("surname", "Old", "New", CONFLICT), |
| 175 | + FieldDiff("label", "L1", "L2", CONFLICT), |
| 176 | + ], |
| 177 | + ) |
| 178 | + ] |
| 179 | + ) |
| 180 | + prompter, _ = _prompter(["r", "a", "y"]) |
| 181 | + res = resolve_plan(plan, prompter) |
| 182 | + assert res.updates()[0].apply_fields == {"surname", "label"} |
| 183 | + |
| 184 | + |
| 185 | +def test_final_confirm_no(): |
| 186 | + plan = ReconcilePlan(changes=[_change(NEW, "new1")]) |
| 187 | + prompter, _ = _prompter(["n"]) |
| 188 | + res = resolve_plan(plan, prompter) |
| 189 | + assert res.accepted is False |
| 190 | + assert res.creates()[0].change.proposed.username == "new1" |
| 191 | + |
| 192 | + |
| 193 | +def test_prompter_reprompts_on_invalid(): |
| 194 | + prompter, io = _prompter(["z", "k"]) |
| 195 | + assert prompter.ask("q", ("k", "n")) == "k" |
| 196 | + assert any("Please choose one of" in o for o in io.outputs) |
0 commit comments