|
| 1 | +"""Tests for starter templates — v1.0.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import bpy |
| 6 | + |
| 7 | +from stage.core.templates import TEMPLATES, apply_template_to_studio |
| 8 | + |
| 9 | + |
| 10 | +def _fresh_scene(name: str = "stage_template_test"): |
| 11 | + if name in bpy.data.scenes: |
| 12 | + bpy.data.scenes.remove(bpy.data.scenes[name], do_unlink=True) |
| 13 | + return bpy.data.scenes.new(name) |
| 14 | + |
| 15 | + |
| 16 | +def _new_studio(scene, name: str = "Test"): |
| 17 | + s = scene.stage_data.studios.add() |
| 18 | + s.name = name |
| 19 | + s.uuid = f"tpl-test-{name}" |
| 20 | + return s |
| 21 | + |
| 22 | + |
| 23 | +# --- TEMPLATES dict -------------------------------------------------------- |
| 24 | + |
| 25 | + |
| 26 | +def test_templates_has_three_entries(): |
| 27 | + """v1.0 ships with the documented three render-settings templates.""" |
| 28 | + assert "cycles_quality" in TEMPLATES |
| 29 | + assert "eevee_preview" in TEMPLATES |
| 30 | + assert "print_4k" in TEMPLATES |
| 31 | + |
| 32 | + |
| 33 | +def test_template_required_fields(): |
| 34 | + """Every template has the keys apply_template_to_studio relies on.""" |
| 35 | + required = {"display_name", "engine", "render_settings"} |
| 36 | + for tid, t in TEMPLATES.items(): |
| 37 | + missing = required - set(t) |
| 38 | + assert not missing, f"{tid} missing keys: {missing}" |
| 39 | + |
| 40 | + |
| 41 | +# --- apply_template_to_studio ---------------------------------------------- |
| 42 | + |
| 43 | + |
| 44 | +def test_apply_cycles_quality_sets_render_facet(): |
| 45 | + scene = _fresh_scene() |
| 46 | + studio = _new_studio(scene) |
| 47 | + |
| 48 | + apply_template_to_studio(studio, TEMPLATES["cycles_quality"]) |
| 49 | + |
| 50 | + f = studio.facet_render |
| 51 | + assert f.captured is True |
| 52 | + assert f.engine == "CYCLES" |
| 53 | + assert f.resolution_x == 1920 |
| 54 | + assert f.resolution_y == 1080 |
| 55 | + # Engine-specific bag populated with each render_settings entry |
| 56 | + paths = {e.data_path for e in f.settings} |
| 57 | + assert "cycles.samples" in paths |
| 58 | + assert "cycles.use_denoising" in paths |
| 59 | + assert "cycles.max_bounces" in paths |
| 60 | + |
| 61 | + |
| 62 | +def test_apply_eevee_preview_sets_engine(): |
| 63 | + scene = _fresh_scene() |
| 64 | + studio = _new_studio(scene) |
| 65 | + apply_template_to_studio(studio, TEMPLATES["eevee_preview"]) |
| 66 | + assert studio.facet_render.engine == "BLENDER_EEVEE" |
| 67 | + assert studio.facet_render.resolution_x == 1280 |
| 68 | + |
| 69 | + |
| 70 | +def test_apply_print_4k_resolution(): |
| 71 | + scene = _fresh_scene() |
| 72 | + studio = _new_studio(scene) |
| 73 | + apply_template_to_studio(studio, TEMPLATES["print_4k"]) |
| 74 | + assert studio.facet_render.resolution_x == 3840 |
| 75 | + assert studio.facet_render.resolution_y == 2160 |
| 76 | + |
| 77 | + |
| 78 | +def test_apply_template_replaces_settings_bag(): |
| 79 | + """Applying a template clears any existing settings entries first.""" |
| 80 | + scene = _fresh_scene() |
| 81 | + studio = _new_studio(scene) |
| 82 | + |
| 83 | + # Pre-populate with stale data |
| 84 | + pre = studio.facet_render.settings.add() |
| 85 | + pre.data_path = "stale.path" |
| 86 | + pre.value_repr = "999" |
| 87 | + pre.value_type = 'INT' |
| 88 | + |
| 89 | + apply_template_to_studio(studio, TEMPLATES["cycles_quality"]) |
| 90 | + |
| 91 | + paths = {e.data_path for e in studio.facet_render.settings} |
| 92 | + assert "stale.path" not in paths, "stale entries should be cleared" |
| 93 | + assert "cycles.samples" in paths |
| 94 | + |
| 95 | + |
| 96 | +# --- operator integration -------------------------------------------------- |
| 97 | + |
| 98 | + |
| 99 | +def test_operator_creates_studio_with_template_settings(): |
| 100 | + scene = _fresh_scene() |
| 101 | + bpy.context.window.scene = scene |
| 102 | + |
| 103 | + with bpy.context.temp_override(scene=scene): |
| 104 | + result = bpy.ops.stage.studio_add_from_template(template_id="cycles_quality") |
| 105 | + |
| 106 | + assert result == {'FINISHED'} |
| 107 | + studios = scene.stage_data.studios |
| 108 | + assert len(studios) == 1 |
| 109 | + s = studios[0] |
| 110 | + assert s.name == "Cycles Quality" |
| 111 | + assert s.facet_render.engine == "CYCLES" |
| 112 | + assert s.facet_render.resolution_x == 1920 |
| 113 | + |
| 114 | + |
| 115 | +def test_operator_unknown_template_cancelled(): |
| 116 | + scene = _fresh_scene() |
| 117 | + bpy.context.window.scene = scene |
| 118 | + |
| 119 | + raised = False |
| 120 | + try: |
| 121 | + with bpy.context.temp_override(scene=scene): |
| 122 | + bpy.ops.stage.studio_add_from_template(template_id="not_a_real_template") |
| 123 | + except RuntimeError: |
| 124 | + # bpy.ops raises when the operator reports {'ERROR'} — expected. |
| 125 | + raised = True |
| 126 | + |
| 127 | + assert raised, "expected RuntimeError on unknown template" |
| 128 | + assert len(scene.stage_data.studios) == 0 |
0 commit comments