You've already forked etude_lego_jurassic_world
Ajoute les visualisations des couleurs de têtes de minifigs et jalons
This commit is contained in:
@@ -62,6 +62,7 @@ def test_download_multiple_rebrickable_files(tmp_path: Path) -> None:
|
||||
"inventory_parts.csv.gz",
|
||||
"parts.csv.gz",
|
||||
"colors.csv.gz",
|
||||
"part_categories.csv.gz",
|
||||
]
|
||||
compressed_bodies = {}
|
||||
for file_name in file_names:
|
||||
@@ -82,6 +83,7 @@ def test_download_multiple_rebrickable_files(tmp_path: Path) -> None:
|
||||
tmp_path / "inventory_parts.csv",
|
||||
tmp_path / "parts.csv",
|
||||
tmp_path / "colors.csv",
|
||||
tmp_path / "part_categories.csv",
|
||||
]
|
||||
assert len(responses.calls) == len(file_names)
|
||||
for file_name in file_names:
|
||||
|
||||
137
tests/test_minifig_heads.py
Normal file
137
tests/test_minifig_heads.py
Normal file
@@ -0,0 +1,137 @@
|
||||
"""Tests d'extraction des couleurs de têtes de minifigs."""
|
||||
|
||||
import csv
|
||||
from pathlib import Path
|
||||
|
||||
from lib.rebrickable.colors_by_set import build_colors_lookup
|
||||
from lib.rebrickable.minifig_heads import (
|
||||
aggregate_head_colors_by_set,
|
||||
aggregate_head_colors_by_year,
|
||||
build_head_part_set,
|
||||
load_parts_filtered,
|
||||
write_head_colors_by_set,
|
||||
write_head_colors_by_year,
|
||||
)
|
||||
|
||||
|
||||
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_extract_minifig_heads_and_colors(tmp_path: Path) -> None:
|
||||
"""Filtre les têtes de minifigs, agrège par set puis par année."""
|
||||
parts_filtered = tmp_path / "parts_filtered.csv"
|
||||
parts_catalog = tmp_path / "parts.csv"
|
||||
colors_path = tmp_path / "colors.csv"
|
||||
heads_by_set_path = tmp_path / "heads_by_set.csv"
|
||||
heads_by_year_path = tmp_path / "heads_by_year.csv"
|
||||
|
||||
write_csv(
|
||||
parts_filtered,
|
||||
[
|
||||
"part_num",
|
||||
"color_rgb",
|
||||
"is_translucent",
|
||||
"set_num",
|
||||
"set_id",
|
||||
"year",
|
||||
"quantity_in_set",
|
||||
"is_spare",
|
||||
"is_minifig_part",
|
||||
],
|
||||
[
|
||||
["3626b", "FFE1BD", "false", "1000-1", "1000", "2020", "1", "false", "true"],
|
||||
["3626bpr1", "E7B68F", "false", "1000-1", "1000", "2020", "1", "false", "true"],
|
||||
["3001", "FFFFFF", "false", "1000-1", "1000", "2020", "2", "false", "false"],
|
||||
["3626b", "FFE1BD", "false", "2000-1", "2000", "2021", "2", "false", "true"],
|
||||
["3626b", "FFE1BD", "false", "2000-1", "2000", "2021", "1", "true", "true"],
|
||||
],
|
||||
)
|
||||
write_csv(
|
||||
parts_catalog,
|
||||
["part_num", "name", "part_cat_id", "part_material"],
|
||||
[
|
||||
["3626b", "Minifig Head", "59", "Plastic"],
|
||||
["3626bpr1", "Minifig Head with Print", "59", "Plastic"],
|
||||
["3001", "Brick 2 x 4", "11", "Plastic"],
|
||||
],
|
||||
)
|
||||
write_csv(
|
||||
colors_path,
|
||||
["id", "name", "rgb", "is_trans", "num_parts", "num_sets", "y1", "y2"],
|
||||
[
|
||||
["1", "Light Flesh", "FFE1BD", "False", "0", "0", "0", "0"],
|
||||
["2", "Medium Dark Flesh", "E7B68F", "False", "0", "0", "0", "0"],
|
||||
["3", "White", "FFFFFF", "False", "0", "0", "0", "0"],
|
||||
],
|
||||
)
|
||||
|
||||
parts_rows = load_parts_filtered(parts_filtered)
|
||||
head_parts = build_head_part_set(parts_catalog)
|
||||
colors_lookup = build_colors_lookup(colors_path)
|
||||
heads_by_set = aggregate_head_colors_by_set(parts_rows, head_parts, colors_lookup)
|
||||
heads_by_year = aggregate_head_colors_by_year(heads_by_set)
|
||||
write_head_colors_by_set(heads_by_set_path, heads_by_set)
|
||||
write_head_colors_by_year(heads_by_year_path, heads_by_year)
|
||||
|
||||
with heads_by_set_path.open() as csv_file:
|
||||
rows_by_set = list(csv.DictReader(csv_file))
|
||||
with heads_by_year_path.open() as csv_file:
|
||||
rows_by_year = list(csv.DictReader(csv_file))
|
||||
|
||||
assert rows_by_set == [
|
||||
{
|
||||
"set_num": "1000-1",
|
||||
"set_id": "1000",
|
||||
"year": "2020",
|
||||
"color_rgb": "FFE1BD",
|
||||
"is_translucent": "false",
|
||||
"color_name": "Light Flesh",
|
||||
"quantity": "1",
|
||||
},
|
||||
{
|
||||
"set_num": "1000-1",
|
||||
"set_id": "1000",
|
||||
"year": "2020",
|
||||
"color_rgb": "E7B68F",
|
||||
"is_translucent": "false",
|
||||
"color_name": "Medium Dark Flesh",
|
||||
"quantity": "1",
|
||||
},
|
||||
{
|
||||
"set_num": "2000-1",
|
||||
"set_id": "2000",
|
||||
"year": "2021",
|
||||
"color_rgb": "FFE1BD",
|
||||
"is_translucent": "false",
|
||||
"color_name": "Light Flesh",
|
||||
"quantity": "2",
|
||||
},
|
||||
]
|
||||
assert rows_by_year == [
|
||||
{
|
||||
"year": "2020",
|
||||
"color_rgb": "FFE1BD",
|
||||
"is_translucent": "false",
|
||||
"color_name": "Light Flesh",
|
||||
"quantity": "1",
|
||||
},
|
||||
{
|
||||
"year": "2020",
|
||||
"color_rgb": "E7B68F",
|
||||
"is_translucent": "false",
|
||||
"color_name": "Medium Dark Flesh",
|
||||
"quantity": "1",
|
||||
},
|
||||
{
|
||||
"year": "2021",
|
||||
"color_rgb": "FFE1BD",
|
||||
"is_translucent": "false",
|
||||
"color_name": "Light Flesh",
|
||||
"quantity": "2",
|
||||
},
|
||||
]
|
||||
33
tests/test_minifig_heads_plot.py
Normal file
33
tests/test_minifig_heads_plot.py
Normal file
@@ -0,0 +1,33 @@
|
||||
"""Tests des visualisations des couleurs de têtes de minifigs."""
|
||||
|
||||
import matplotlib
|
||||
from pathlib import Path
|
||||
|
||||
from lib.plots.minifig_heads import plot_global_shares, plot_shares_by_year
|
||||
|
||||
|
||||
matplotlib.use("Agg")
|
||||
|
||||
|
||||
def test_plot_minifig_heads(tmp_path: Path) -> None:
|
||||
"""Produit les graphes part par année et global."""
|
||||
heads_path = tmp_path / "minifig_heads_by_year.csv"
|
||||
milestones_path = tmp_path / "milestones.csv"
|
||||
shares_dest = tmp_path / "figures" / "step16" / "minifig_heads_shares.png"
|
||||
global_dest = tmp_path / "figures" / "step16" / "minifig_heads_global.png"
|
||||
heads_path.write_text(
|
||||
"year,color_rgb,is_translucent,color_name,quantity\n"
|
||||
"2020,FFE1BD,false,Light Flesh,2\n"
|
||||
"2020,FFFF00,false,Yellow,1\n"
|
||||
"2021,FFE1BD,false,Light Flesh,1\n"
|
||||
"2021,E7B68F,false,Medium Dark Flesh,3\n"
|
||||
)
|
||||
milestones_path.write_text("year,description\n2020,Jalon Test\n")
|
||||
|
||||
plot_shares_by_year(heads_path, shares_dest, milestones_path=milestones_path)
|
||||
plot_global_shares(heads_path, global_dest)
|
||||
|
||||
assert shares_dest.exists()
|
||||
assert shares_dest.stat().st_size > 0
|
||||
assert global_dest.exists()
|
||||
assert global_dest.stat().st_size > 0
|
||||
Reference in New Issue
Block a user