35 lines
984 B
Python
35 lines
984 B
Python
|
|
import pytest
|
||
|
|
|
||
|
|
from election_data.elections import INDEPENDENT, OTHER, PARTY_A, apportion, party_group
|
||
|
|
|
||
|
|
pytestmark = pytest.mark.unit
|
||
|
|
|
||
|
|
|
||
|
|
class TestApportion:
|
||
|
|
def test_total_is_preserved(self) -> None:
|
||
|
|
parts = apportion(101, [1.0, 1.0, 1.0])
|
||
|
|
assert sum(parts) == 101
|
||
|
|
assert sorted(parts) == [33, 34, 34]
|
||
|
|
|
||
|
|
def test_follows_weights(self) -> None:
|
||
|
|
assert apportion(100, [3.0, 1.0]) == [75, 25]
|
||
|
|
|
||
|
|
def test_zero_weights_split_evenly(self) -> None:
|
||
|
|
assert apportion(10, [0.0, 0.0]) == [5, 5]
|
||
|
|
|
||
|
|
def test_empty(self) -> None:
|
||
|
|
assert apportion(10, []) == []
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.parametrize(
|
||
|
|
("raw", "expected"),
|
||
|
|
[
|
||
|
|
(PARTY_A, PARTY_A),
|
||
|
|
("無黨籍及未經政黨推薦", INDEPENDENT),
|
||
|
|
("無黨團結聯盟", OTHER), # 這是政黨名稱,不是無黨籍
|
||
|
|
("小民參政歐巴桑聯盟", OTHER),
|
||
|
|
],
|
||
|
|
)
|
||
|
|
def test_party_group(raw: str, expected: str) -> None:
|
||
|
|
assert party_group(raw) == expected
|