Coverage for dao/tournament_groups.py: 100.00%

27 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2026-11-30 15:03 +0000

1""" 

2Tournament group format (SB-1752). 

3 

4A tournament match carries one free-text ``tournament_group``. Events that run 

5several competitions side by side (MLS NEXT Fest: Homegrown Division, Academy 

6Division) reuse the same labels in each — both have a "Group A" — so the stored 

7value is namespaced by division:: 

8 

9 "HD · A" division "HD", label "A" 

10 "HD · Showcase" division "HD", label "Showcase" 

11 "Bracket A" legacy, no division — displayed unchanged 

12 

13**The format is frozen.** Bracket follows store this string, so changing the 

14separator orphans every follow already saved. ``frontend/src/utils/ 

15tournamentGroups.js`` mirrors this module; change one and change the other. 

16 

17The frozen format has three parts, and the two implementations must agree on 

18each byte for byte: 

19 

20- the separator, ``GROUP_SEPARATOR``; 

21- the characters trimmed from a part, ``GROUP_WHITESPACE`` — an explicit list, 

22 because ``str.strip()`` and JavaScript's ``trim()`` disagree (JS trims 

23 U+FEFF, Python trims U+001C-U+001F and U+0085); 

24- the compose rule: a division cannot contain the separator, nor can a label 

25 that has no division, since either would split back into different parts. 

26 A label containing the separator *with* a division is legal — the split is 

27 on the first separator only. 

28 

29Pure functions only — no DAO or database imports. 

30""" 

31 

32# Space, U+00B7 MIDDLE DOT, space. 

33GROUP_SEPARATOR = " · " 

34 

35# Every character trimmed from a part: the union of what str.strip() and JS 

36# trim() remove. Same literal as tournamentGroups.js. 

37GROUP_WHITESPACE = ( 

38 "\t\n\u000b\u000c\r \u001c\u001d\u001e\u001f\u0085\u00a0\u1680" 

39 "\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a" 

40 "\u2028\u2029\u202f\u205f\u3000\ufeff" 

41) 

42 

43DIVISION_SEPARATOR_ERROR = "A tournament group division cannot contain ' · ' or end with ' ·'" 

44LABEL_SEPARATOR_ERROR = "A tournament group label cannot contain ' · ' unless a division is given" 

45 

46 

47def _clean(part: str | None) -> str | None: 

48 """Trim GROUP_WHITESPACE from a part; empty and whitespace-only collapse to None.""" 

49 if part is None: 

50 return None 

51 part = str(part).strip(GROUP_WHITESPACE) 

52 return part or None 

53 

54 

55def compose_group(division: str | None, label: str | None) -> str | None: 

56 """Build the stored ``tournament_group`` value from its two parts. 

57 

58 A missing part is simply left out, so a label on its own stays a legacy 

59 un-namespaced value. Returns None when both are missing. 

60 

61 Raises ValueError for parts that would not split back into themselves: a 

62 division that contains the separator (or ends with " ·", which forms one 

63 with the separator that follows), and a label that contains the separator 

64 when there is no division. 

65 """ 

66 division = _clean(division) 

67 label = _clean(label) 

68 if division and f"{division}{GROUP_SEPARATOR}".index(GROUP_SEPARATOR) != len(division): 

69 raise ValueError(DIVISION_SEPARATOR_ERROR) 

70 if label and not division and GROUP_SEPARATOR in label: 

71 raise ValueError(LABEL_SEPARATOR_ERROR) 

72 if division and label: 

73 return f"{division}{GROUP_SEPARATOR}{label}" 

74 return division or label 

75 

76 

77def split_group(value: str | None) -> dict[str, str | None]: 

78 """Split a stored ``tournament_group`` into ``{"division", "label"}``. 

79 

80 Splits on the first separator only. A value with no separator is a legacy 

81 group: division None, label the value as stored. Never raises — a stored 

82 value always splits, whatever ``compose_group`` would say about it today. 

83 """ 

84 value = _clean(value) 

85 if value is None: 

86 return {"division": None, "label": None} 

87 if GROUP_SEPARATOR not in value: 

88 return {"division": None, "label": value} 

89 division, label = value.split(GROUP_SEPARATOR, 1) 

90 return {"division": _clean(division), "label": _clean(label)}