Spaces:
Sleeping
Sleeping
File size: 5,749 Bytes
8648e23 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | """
Tests for src/harmonic/function_labeler.py
All tests use synthetic RomanNumeral objects β no file I/O required.
"""
import pytest
from fractions import Fraction
from src.core.data_structures import HarmonicFunction, RomanNumeral
from src.harmonic.function_labeler import FunctionLabeler
# ---------------------------------------------------------------------------
# Module-level helpers
# ---------------------------------------------------------------------------
def _rn(
degree: int,
quality: str = "major",
inversion: int = 0,
is_secondary: bool = False,
function: HarmonicFunction = None,
) -> RomanNumeral:
return RomanNumeral(
degree=degree,
quality=quality,
inversion=inversion,
local_key_tonic=0,
local_key_mode="major",
is_secondary=is_secondary,
secondary_target=None,
function=function,
)
FL = FunctionLabeler()
T = HarmonicFunction.TONIC
PD = HarmonicFunction.PREDOMINANT
D = HarmonicFunction.DOMINANT
AMB = HarmonicFunction.AMBIGUOUS
# ---------------------------------------------------------------------------
# TestLabel
# ---------------------------------------------------------------------------
class TestLabel:
def test_degree_1_gets_tonic(self):
assert FL.label(_rn(1)).function == T
def test_degree_2_gets_predominant(self):
assert FL.label(_rn(2)).function == PD
def test_degree_3_gets_ambiguous(self):
assert FL.label(_rn(3)).function == AMB
def test_degree_4_gets_predominant(self):
assert FL.label(_rn(4)).function == PD
def test_degree_5_gets_dominant(self):
assert FL.label(_rn(5)).function == D
def test_degree_6_gets_tonic(self):
assert FL.label(_rn(6)).function == T
def test_degree_7_gets_dominant(self):
assert FL.label(_rn(7)).function == D
def test_secondary_dominant_always_gets_dominant(self):
# is_secondary=True overrides the degree-6 tonic base function
rn = _rn(6, is_secondary=True)
assert FL.label(rn).function == D
def test_already_labelled_returned_unchanged(self):
rn = _rn(1, function=D) # manually set to DOMINANT
result = FL.label(rn)
assert result.function == D # not overwritten to TONIC
assert result is rn # same object (no copy needed)
def test_returns_new_object_when_unlabelled(self):
rn = _rn(1)
result = FL.label(rn)
assert result is not rn # new frozen instance created
def test_result_is_roman_numeral(self):
assert isinstance(FL.label(_rn(1)), RomanNumeral)
def test_non_secondary_degree_6_gets_tonic(self):
rn = _rn(6, is_secondary=False)
assert FL.label(rn).function == T
# ---------------------------------------------------------------------------
# TestLabelSequence
# ---------------------------------------------------------------------------
class TestLabelSequence:
def test_empty_input_returns_empty(self):
assert FL.label_sequence([]) == []
def test_single_rn_labelled(self):
result = FL.label_sequence([_rn(1)])
assert result[0].function == T
def test_all_rns_labelled(self):
rns = [_rn(d) for d in range(1, 8)]
result = FL.label_sequence(rns)
assert all(r.function is not None for r in result)
def test_vi_before_ii_becomes_predominant(self):
rns = [_rn(6), _rn(2)]
result = FL.label_sequence(rns)
# vi's function is relabelled to PREDOMINANT
assert result[0].function == PD
# ii's function is still PREDOMINANT (unchanged)
assert result[1].function == PD
def test_vi_before_IV_becomes_predominant(self):
rns = [_rn(6), _rn(4)]
result = FL.label_sequence(rns)
assert result[0].function == PD
def test_vi_before_V_stays_tonic(self):
# vi β V : no context override β vi stays TONIC
rns = [_rn(6), _rn(5)]
result = FL.label_sequence(rns)
assert result[0].function == T
def test_vi_before_I_stays_tonic(self):
rns = [_rn(6), _rn(1)]
result = FL.label_sequence(rns)
assert result[0].function == T
def test_iii_stays_ambiguous_regardless_of_context(self):
# iii is not in _CONTEXT_OVERRIDES as a trigger
rns = [_rn(3), _rn(2)]
result = FL.label_sequence(rns)
assert result[0].function == AMB
def test_does_not_mutate_input_list(self):
rns = [_rn(6), _rn(2)]
original_ids = [id(r) for r in rns]
FL.label_sequence(rns)
assert [id(r) for r in rns] == original_ids
def test_does_not_mutate_input_rn_objects(self):
rn = _rn(6)
original_function = rn.function
FL.label_sequence([rn, _rn(2)])
# rn.function must be unchanged (frozen dataclass, but just confirm)
assert rn.function == original_function
def test_returns_new_list(self):
rns = [_rn(1)]
result = FL.label_sequence(rns)
assert result is not rns
def test_long_sequence_all_labelled(self):
rns = [_rn(d) for d in [1, 4, 5, 1, 6, 2, 5, 1]]
result = FL.label_sequence(rns)
assert all(r.function is not None for r in result)
assert len(result) == 8
def test_context_override_only_affects_previous(self):
# vi β ii: result[0] changes; result[1] function is determined by ii alone
rns = [_rn(6), _rn(2), _rn(5)]
result = FL.label_sequence(rns)
assert result[0].function == PD # vi β PREDOMINANT (override)
assert result[1].function == PD # ii β PREDOMINANT (base)
assert result[2].function == D # V β DOMINANT (base)
|