117 lines
9.3 KiB
Python
117 lines
9.3 KiB
Python
import pathlib
|
|
import re
|
|
import sys
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
|
|
|
from plan.test_ranking import DECISIONS, README, TODO
|
|
from project_config.settings import DEFAULT_FLOOR
|
|
from failure import Failure
|
|
from plan import ranking, todo_table
|
|
|
|
|
|
class Blockers(unittest.TestCase):
|
|
def ranked(self, todo):
|
|
return ranking.rank(todo, README, DECISIONS, "", DEFAULT_FLOOR)
|
|
|
|
def test_ranks_each_blocker_just_above_the_first_item_it_blocks(self):
|
|
ranked = self.ranked(TODO.replace("| 3 | 0.1.0 | |", "| 3 | 0.1.0 | 5, 2 |"))
|
|
self.assertIn("""| 4 | 0.1.0 | | security | **Rotate the leaked key.** | 3 | 3 | 5 | 9 | 2 | 2.1 by 1 | 12.3 |
|
|
| 2 | 0.1.0 | | | **Tighten the tests.** | 1 | 2 | 1 | 2 | 4 | 2.1 by 1 | 3.1 |
|
|
| 5 | 0.1.0 | | decision | **Move the writes out.** | 1 | 1 | 4 | 5 | 5 | 2.1 by 1 | 1.5 |
|
|
| 3 | 0.1.0 | 2, 5 | | **Make it fast, `a \\| b`.** | 2 | 1 | 2 | 3 | 7 | 2.1 by 8, 1 by 6 | 5.5 |
|
|
| 1 | 0.2.0 | | | **Ship the later thing.** | 1 | 1 | 1 | 1 | 1 | 1 by 9 | 228.4 |
|
|
""", ranked.text)
|
|
self.assertEqual(self.ranked(ranked.text), ranked)
|
|
|
|
def test_ranks_an_items_blockers_in_score_order(self):
|
|
rows = [line.split(" | ")[0] for line in self.ranked(TODO.replace("| 4 | 0.1.0 | |", "| 4 | 0.1.0 | 2, 3 |")).text.splitlines() if re.match(r"\| \d \| 0\.\d\.0 \|", line)]
|
|
self.assertEqual(rows, ["| 3", "| 2", "| 4", "| 5", "| 1"])
|
|
|
|
def test_ranks_a_blocker_above_its_blockers_blocker_and_takes_one_in_an_earlier_release_as_it_is(self):
|
|
chained = TODO.replace("| 3 | 0.1.0 | |", "| 3 | 0.1.0 | 5 |").replace("| 5 | 0.1.0 | |", "| 5 | 0.1.0 | 2 |").replace("| 1 | 0.2.0 | |", "| 1 | 0.2.0 | 3 |")
|
|
rows = [line.split(" | ")[0] for line in self.ranked(chained).text.splitlines() if re.match(r"\| \d \| 0\.\d\.0 \|", line)]
|
|
self.assertEqual(rows, ["| 4", "| 2", "| 5", "| 3", "| 1"])
|
|
|
|
def test_bundles_with_the_next_item_each_item_of_its_release_that_no_question_or_item_outside_the_chunk_blocks(self):
|
|
# Item 2 is the next item. Item 5 is a question, 3 waits on 5, and 4 waits on 3; item 1 is in a later release.
|
|
waits = todo_table.items(TODO.replace("| 3 | 0.1.0 | |", "| 3 | 0.1.0 | 5 |").replace("| 4 | 0.1.0 | | security |", "| 4 | 0.1.0 | 3 | security |").replace("| 5 | 0.1.0 | | decision |", "| 5 | 0.1.0 | | question |"))
|
|
self.assertEqual(todo_table.bundle_candidates(waits, todo_table.next_item(waits)), [])
|
|
answered = todo_table.items(TODO.replace("| 3 | 0.1.0 | |", "| 3 | 0.1.0 | 5 |").replace("| 4 | 0.1.0 | | security |", "| 4 | 0.1.0 | 3 | security |"))
|
|
self.assertEqual([item.id for item in todo_table.bundle_candidates(answered, todo_table.next_item(answered))], [3, 5])
|
|
|
|
def test_ships_an_item_above_r_5_alone(self):
|
|
# Item 4 has R 9: it joins no chunk, and none joins it. Item 5, at R 5, still joins.
|
|
found = todo_table.items(TODO)
|
|
self.assertNotIn(4, [item.id for item in todo_table.bundle_candidates(found, todo_table.next_item(found))])
|
|
self.assertIn(5, [item.id for item in todo_table.bundle_candidates(found, todo_table.next_item(found))])
|
|
self.assertEqual(todo_table.bundle_candidates(found, next(item for item in found if item.id == 4)), [])
|
|
# At R 6, item 3 ships alone too.
|
|
riskier = todo_table.items(TODO.replace("| 2 | 1 | 2 | 3 | 7 |", "| 3 | 2 | 3 | 6 | 7 |"))
|
|
self.assertNotIn(3, [item.id for item in todo_table.bundle_candidates(riskier, todo_table.next_item(riskier))])
|
|
# Before rank_todo.py fills its R cell, item 3's L 3, E 2 and I 3 still make R 6.
|
|
unranked = todo_table.items(TODO.replace("| 2 | 1 | 2 | 3 | 7 |", "| 3 | 2 | 3 | | 7 |"))
|
|
self.assertNotIn(3, [item.id for item in todo_table.bundle_candidates(unranked, todo_table.next_item(unranked))])
|
|
|
|
def test_drops_a_blocker_the_table_no_longer_holds_as_finished(self):
|
|
finished = TODO.replace("| 4 | 0.1.0 | | security | **Rotate the leaked key.** | 3 | 3 | 5 | 9 | 2 | 2.1 by 1 | 0 |\n", "").replace("| 2 | 0.1.0 | |", "| 2 | 0.1.0 | 4 |")
|
|
ranked = self.ranked(finished)
|
|
self.assertIn("| 2 | 0.1.0 | | | **Tighten the tests.**", ranked.text)
|
|
self.assertEqual(ranked.notes, ["item 2: dropped 4 from its Blocked by; the table no longer holds it"])
|
|
self.assertEqual(self.ranked(ranked.text).notes, [])
|
|
|
|
def test_refuses_a_blocker_in_a_later_release_a_loop_and_an_id_past_next_id(self):
|
|
cases = {
|
|
"| 3 | 0.1.0 | 1 |": "item 3, in release 0.1.0, is blocked by item 1, in later release 0.2.0; move item 1 to release 0.1.0 or earlier, or item 3 to release 0.2.0 or later",
|
|
"| 3 | 0.1.0 | 3 |": "item 3 is blocked by itself; drop 3 from its Blocked by",
|
|
"| 3 | 0.1.0 | 9 |": "item 3 is blocked by item 9, which is not under Next ID 6; name an item the table holds or held",
|
|
"| 3 | 0.1.0 | 6 |": "item 3 is blocked by item 6, which is not under Next ID 6; name an item the table holds or held",
|
|
}
|
|
for cell, reason in cases.items():
|
|
with self.subTest(cell=cell):
|
|
self.assertEqual(self.ranked(TODO.replace("| 3 | 0.1.0 | |", cell)), Failure(reason))
|
|
looped = TODO.replace("| 3 | 0.1.0 | |", "| 3 | 0.1.0 | 2 |").replace("| 2 | 0.1.0 | |", "| 2 | 0.1.0 | 5 |").replace("| 5 | 0.1.0 | |", "| 5 | 0.1.0 | 3 |")
|
|
self.assertEqual(self.ranked(looped), Failure("items 2, 3 and 5 block each other in a loop; drop one of the blockers"))
|
|
self.assertEqual(self.ranked(TODO.replace("| 3 | 0.1.0 | |", "| 3 | 0.1.0 | 2 and 5 |")), Failure("item 3's Blocked by '2 and 5' is not empty or IDs separated by \", \""))
|
|
|
|
def test_adds_the_column_to_a_table_without_it(self):
|
|
without = TODO.replace("| Blocked by ", "").replace("|---|---|---|---|---|---|---|---|---|---|---|---|", "|---|---|---|---|---|---|---|---|---|---|---|")
|
|
without = re.sub(r"\| (\d) \| (0\.[12]\.0) \| \| ", r"| \1 | \2 | ", without)
|
|
self.assertEqual(self.ranked(without), self.ranked(TODO))
|
|
unscored = TODO.replace(" | Score |", " |").replace("|---|---|---|---|---|---|---|---|---|---|---|---|", "|---|---|---|---|---|---|---|---|---|---|---|").replace(" | 0 |\n", " |\n")
|
|
self.assertEqual(self.ranked(unscored), self.ranked(TODO))
|
|
|
|
def test_refuses_a_column_it_does_not_know_rather_than_drop_it_and_one_an_older_shape_held(self):
|
|
extra = TODO.replace("| Moves | Score |\n|---|", "| Moves | Score | Later |\n|---|---|").replace(" | 0 |\n", " | 0 | x |\n")
|
|
self.assertEqual(self.ranked(extra), Failure("todo.md's items table has the column Later, which this rank_todo.py does not know; the columns it knows are ID, Release, Blocked by, Exempt, Item, L, E, I, R, S, Moves, Score. Rename it, or move this machine to the release that added it"))
|
|
self.assertEqual([item.id for item in todo_table.items(extra)], [1, 2, 3, 4, 5])
|
|
with_c = TODO.replace("| S | Moves |", "| S | C | Moves |").replace("|---|---|---|---|---|---|---|---|---|---|---|---|", "|---|---|---|---|---|---|---|---|---|---|---|---|---|")
|
|
with_c = re.sub(r"(\*\* \| \d+ \| \d+ \| \d+ \| \d+ \| \d+) \|", r"\1 | 3 |", with_c)
|
|
self.assertEqual(self.ranked(with_c), Failure("todo.md's items table has C, a column of an older shape; drop it, as rank_todo.py --help shows"))
|
|
|
|
def test_refuses_text_under_items_beside_the_table(self):
|
|
last = TODO.split("\n## Details")[0] + "\n"
|
|
self.assertIsInstance(self.ranked(last), ranking.Ranked)
|
|
beside = Failure("## Items holds text beside its table; an order goes in the scores or the Blocked by column, and a note in the item's detail")
|
|
for ordered in (TODO.replace("## Items\n\n", "## Items\n\nRelease 0.1.0 takes item 5 first.\n\n"), TODO.replace("## Items\n\n", "## Items\nTake item 5 first.\n\n"), TODO.replace("| 2.1 by 1 | 0 |\n\n## Details", "| 2.1 by 1 | 0 |\n\nTake item 5 first.\n\n## Details"), last + "\nTake item 5 first.\n"):
|
|
with self.subTest(ordered=ordered[-60:]):
|
|
self.assertEqual(self.ranked(ordered), beside)
|
|
|
|
def test_refuses_an_items_table_outside_items_a_second_one_and_a_repeated_column(self):
|
|
self.assertEqual(self.ranked(TODO.replace("## Items\n", "## Plan\n")).reason, "todo.md is not in the scored-table format; rank_todo.py --help shows a valid one")
|
|
outside = TODO.replace("## Items\n\n", "## Items\n\n## Rows\n\n")
|
|
self.assertEqual(self.ranked(outside).reason, "the items table sits under ## Rows, not ## Items; move it under ## Items")
|
|
table = TODO[TODO.index("| ID | Release |"):TODO.index("\n\n## Details")] + "\n"
|
|
above = table + "\n" + TODO.replace(table, "")
|
|
self.assertEqual(self.ranked(above).reason, "the items table sits under no ## heading; move it under ## Items")
|
|
header = "| ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score |\n|---|---|---|---|---|---|---|---|---|---|---|---|\n"
|
|
self.assertEqual(todo_table.items(TODO + "\n" + header.replace("| Score |", "| Later |")), Failure("todo.md holds 2 tables whose header opens | ID | Release |; keep one items table"))
|
|
repeated = TODO.replace("| Moves | Score |\n|---|", "| Moves | Score | Exempt |\n|---|---|").replace(" | 0 |\n", " | 0 | x |\n")
|
|
self.assertEqual(todo_table.items(repeated), Failure("todo.md's items table names the column Exempt twice; keep one"))
|
|
|
|
def test_reads_an_empty_table_and_fails_on_a_header_with_no_divider(self):
|
|
header = "| ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score |"
|
|
self.assertEqual(todo_table.items(f"## Items\n\n{header}\n|---|---|---|---|---|---|---|---|---|---|---|---|\n\n## Details\n"), [])
|
|
self.assertEqual(todo_table.items(f"## Items\n\n{header}"), Failure(f"the table under {header} has no |---| divider line"))
|