1

Ajoute l'agrégation annuelle des palettes de couleurs

This commit is contained in:
2025-12-01 22:40:56 +01:00
parent f8a2464447
commit fb2ef5f16f
4 changed files with 267 additions and 0 deletions

View File

@@ -0,0 +1,110 @@
"""Tests des agrégats annuels de palettes de couleurs."""
import csv
from pathlib import Path
from lib.rebrickable.colors_timeline import (
build_year_color_matrix,
compute_yearly_stats,
load_colors_by_set,
write_year_color_matrix,
write_yearly_stats,
)
def write_csv(path: Path, headers: list[str], rows: list[list[str]]) -> None:
"""Écrit un CSV simple pour les besoins des tests."""
with path.open("w", newline="") as csv_file:
writer = csv.writer(csv_file)
writer.writerow(headers)
writer.writerows(rows)
def test_compute_yearly_stats(tmp_path: Path) -> None:
"""Calcule les métriques annuelles sur les palettes."""
source_path = tmp_path / "colors_by_set.csv"
timeline_path = tmp_path / "colors_timeline.csv"
matrix_path = tmp_path / "colors_year_color_matrix.csv"
write_csv(
source_path,
[
"set_num",
"set_id",
"year",
"color_rgb",
"is_translucent",
"color_name",
"quantity_total",
"quantity_non_spare",
"quantity_minifig",
"quantity_non_minifig",
],
[
["1000-1", "1000", "2020", "AAAAAA", "false", "Gray", "2", "2", "0", "2"],
["1000-1", "1000", "2020", "BBBBBB", "true", "Trans-Black", "1", "1", "0", "1"],
["2000-1", "2000", "2021", "BBBBBB", "true", "Trans-Black", "3", "3", "0", "3"],
["2000-1", "2000", "2021", "CCCCCC", "false", "Blue", "4", "4", "4", "0"],
],
)
rows = load_colors_by_set(source_path)
timeline = compute_yearly_stats(rows)
matrix = build_year_color_matrix(rows)
write_yearly_stats(timeline_path, timeline)
write_year_color_matrix(matrix_path, matrix)
with timeline_path.open() as csv_file:
timeline_rows = list(csv.DictReader(csv_file))
with matrix_path.open() as csv_file:
matrix_rows = list(csv.DictReader(csv_file))
assert timeline_rows == [
{
"year": "2020",
"colors_distinct": "2",
"colors_new": "2",
"colors_lost": "0",
"share_translucent": "0.3333",
"total_quantity": "3",
"top_colors": "Gray (2), Trans-Black (1)",
},
{
"year": "2021",
"colors_distinct": "2",
"colors_new": "1",
"colors_lost": "1",
"share_translucent": "0.4286",
"total_quantity": "7",
"top_colors": "Blue (4), Trans-Black (3)",
},
]
assert matrix_rows == [
{
"year": "2020",
"color_rgb": "AAAAAA",
"is_translucent": "false",
"color_name": "Gray",
"quantity_total": "2",
},
{
"year": "2020",
"color_rgb": "BBBBBB",
"is_translucent": "true",
"color_name": "Trans-Black",
"quantity_total": "1",
},
{
"year": "2021",
"color_rgb": "CCCCCC",
"is_translucent": "false",
"color_name": "Blue",
"quantity_total": "4",
},
{
"year": "2021",
"color_rgb": "BBBBBB",
"is_translucent": "true",
"color_name": "Trans-Black",
"quantity_total": "3",
},
]