1

Création d'un fichier intermédiaire pour les statistiques sur les couleurs

This commit is contained in:
2025-12-01 22:15:48 +01:00
parent cf83f51f89
commit 4f42303eac
5 changed files with 228 additions and 3 deletions

101
tests/test_colors_by_set.py Normal file
View File

@@ -0,0 +1,101 @@
"""Tests d'agrégation des couleurs par set."""
import csv
from pathlib import Path
from lib.rebrickable.colors_by_set import (
aggregate_colors_by_set,
build_colors_lookup,
load_parts,
write_colors_by_set,
)
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_aggregate_colors_by_set(tmp_path: Path) -> None:
"""Regroupe les quantités par set et par couleur."""
parts_path = tmp_path / "parts_filtered.csv"
colors_path = tmp_path / "colors.csv"
destination_path = tmp_path / "colors_by_set.csv"
write_csv(
parts_path,
[
"part_num",
"color_rgb",
"is_translucent",
"set_num",
"set_id",
"year",
"quantity_in_set",
"is_spare",
"is_minifig_part",
],
[
["3001", "FFFFFF", "false", "1000-1", "1000", "2020", "2", "false", "false"],
["3002", "FFFFFF", "false", "1000-1", "1000", "2020", "1", "true", "false"],
["3003", "000000", "true", "1000-1", "1000", "2020", "4", "false", "true"],
["4001", "FFFFFF", "false", "2000-1", "2000", "2021", "3", "false", "true"],
],
)
write_csv(
colors_path,
["id", "name", "rgb", "is_trans", "num_parts", "num_sets", "y1", "y2"],
[
["1", "White", "FFFFFF", "False", "0", "0", "0", "0"],
["2", "Trans-Black", "000000", "True", "0", "0", "0", "0"],
],
)
parts = load_parts(parts_path)
colors_lookup = build_colors_lookup(colors_path)
aggregated = aggregate_colors_by_set(parts, colors_lookup)
write_colors_by_set(destination_path, aggregated)
with destination_path.open() as csv_file:
rows = list(csv.DictReader(csv_file))
assert rows == [
{
"set_num": "1000-1",
"set_id": "1000",
"year": "2020",
"color_rgb": "000000",
"is_translucent": "true",
"color_name": "Trans-Black",
"quantity_total": "4",
"quantity_non_spare": "4",
"quantity_minifig": "4",
"quantity_non_minifig": "0",
},
{
"set_num": "1000-1",
"set_id": "1000",
"year": "2020",
"color_rgb": "FFFFFF",
"is_translucent": "false",
"color_name": "White",
"quantity_total": "3",
"quantity_non_spare": "2",
"quantity_minifig": "0",
"quantity_non_minifig": "3",
},
{
"set_num": "2000-1",
"set_id": "2000",
"year": "2021",
"color_rgb": "FFFFFF",
"is_translucent": "false",
"color_name": "White",
"quantity_total": "3",
"quantity_non_spare": "3",
"quantity_minifig": "3",
"quantity_non_minifig": "0",
},
]

View File

@@ -26,10 +26,10 @@ def test_write_parts_filtered(tmp_path: Path) -> None:
write_csv(
sets_path,
["set_num", "set_id", "name", "num_parts"],
["set_num", "set_id", "name", "num_parts", "year"],
[
["1234-1", "1234", "Sample Set A", "9"],
["5678-1", "5678", "Sample Set B", "2"],
["1234-1", "1234", "Sample Set A", "9", "2020"],
["5678-1", "5678", "Sample Set B", "2", "2021"],
],
)
write_csv(
@@ -98,8 +98,10 @@ def test_write_parts_filtered(tmp_path: Path) -> None:
"is_translucent": "false",
"set_num": "1234-1",
"set_id": "1234",
"year": "2020",
"quantity_in_set": "4",
"is_spare": "false",
"is_minifig_part": "false",
},
{
"part_num": "3002",
@@ -107,8 +109,10 @@ def test_write_parts_filtered(tmp_path: Path) -> None:
"is_translucent": "true",
"set_num": "1234-1",
"set_id": "1234",
"year": "2020",
"quantity_in_set": "1",
"is_spare": "true",
"is_minifig_part": "false",
},
{
"part_num": "mf-1",
@@ -116,8 +120,10 @@ def test_write_parts_filtered(tmp_path: Path) -> None:
"is_translucent": "true",
"set_num": "1234-1",
"set_id": "1234",
"year": "2020",
"quantity_in_set": "1",
"is_spare": "false",
"is_minifig_part": "true",
},
{
"part_num": "mf-2",
@@ -125,8 +131,10 @@ def test_write_parts_filtered(tmp_path: Path) -> None:
"is_translucent": "false",
"set_num": "1234-1",
"set_id": "1234",
"year": "2020",
"quantity_in_set": "2",
"is_spare": "false",
"is_minifig_part": "true",
},
{
"part_num": "3003",
@@ -134,7 +142,9 @@ def test_write_parts_filtered(tmp_path: Path) -> None:
"is_translucent": "false",
"set_num": "5678-1",
"set_id": "5678",
"year": "2021",
"quantity_in_set": "2",
"is_spare": "false",
"is_minifig_part": "false",
},
]