66from _pytest .logging import LogCaptureFixture
77
88from questionpy_common .constants import MANIFEST_FILENAME
9- from questionpy_common .manifest import Manifest , PartialPackagePermissions
9+ from questionpy_common .manifest import Bcp47LanguageTag , Manifest , PartialPackagePermissions
1010from questionpy_sdk ._package import DirBuildTarget , build_qpy_package
11- from questionpy_sdk ._package ._validate import validate_dist_structure , validate_requested_lms_attributes
11+ from questionpy_sdk ._package ._validate import (
12+ validate_dist_structure ,
13+ validate_package_name_and_description ,
14+ validate_requested_lms_attributes ,
15+ )
1216from questionpy_sdk ._package .source import PackageSource
1317
1418
@@ -24,10 +28,13 @@ def manifest(dist_dir: Path) -> Manifest:
2428 return Manifest .model_validate_json ((dist_dir / MANIFEST_FILENAME ).read_text ())
2529
2630
31+ def get_warnings (caplog : LogCaptureFixture ) -> list [str ]:
32+ return [record .message for record in caplog .records if record .levelno == logging .WARNING ]
33+
34+
2735def test_should_not_warn_for_valid_package (dist_dir : Path , manifest : Manifest , caplog : LogCaptureFixture ) -> None :
2836 validate_dist_structure (manifest , dist_dir )
29- for record in caplog .records :
30- assert record .levelno < logging .WARNING
37+ assert len (get_warnings (caplog )) == 0
3138
3239
3340def test_warn_when_dist_contains_another_python_package (
@@ -38,7 +45,7 @@ def test_warn_when_dist_contains_another_python_package(
3845
3946 validate_dist_structure (manifest , dist_dir )
4047
41- warn_messages = [ record . message for record in caplog . records if record . levelno == logging . WARNING ]
48+ warn_messages = get_warnings ( caplog )
4249 assert len (warn_messages ) == 1
4350 assert "- python/another/package\n " in warn_messages [0 ]
4451 assert "- python/another/package/__init__.py\n " in warn_messages [0 ]
@@ -52,7 +59,7 @@ def test_warn_when_dist_contains_wrong_js_dir(dist_dir: Path, manifest: Manifest
5259
5360 validate_dist_structure (manifest , dist_dir )
5461
55- warn_messages = [ record . message for record in caplog . records if record . levelno == logging . WARNING ]
62+ warn_messages = get_warnings ( caplog )
5663 assert len (warn_messages ) == 1
5764 assert "- js\n " in warn_messages [0 ]
5865 assert "- js/bundle.js\n " in warn_messages [0 ]
@@ -68,22 +75,88 @@ def test_warn_when_dist_is_missing_python_dir(dist_dir: Path, manifest: Manifest
6875
6976def test_should_not_warn_when_requesting_no_lms_attributes (manifest : Manifest , caplog : LogCaptureFixture ) -> None :
7077 validate_requested_lms_attributes (manifest )
71- for record in caplog . records :
72- assert record . levelno < logging . WARNING
78+
79+ assert len ( get_warnings ( caplog )) == 0
7380
7481
7582def test_should_not_warn_when_requesting_known_lms_attributes (manifest : Manifest , caplog : LogCaptureFixture ) -> None :
7683 manifest .permissions = PartialPackagePermissions (lms_attributes = {"attempt_id" , "lms_sdk_xyz" , "profile_field_xyz" })
84+
7785 validate_requested_lms_attributes (manifest )
78- for record in caplog . records :
79- assert record . levelno < logging . WARNING
86+
87+ assert len ( get_warnings ( caplog )) == 0
8088
8189
8290def test_warn_when_requesting_unknown_lms_attributes (manifest : Manifest , caplog : LogCaptureFixture ) -> None :
8391 manifest .permissions = PartialPackagePermissions (lms_attributes = {"foo" , "bar" })
92+
8493 validate_requested_lms_attributes (manifest )
8594
86- warn_messages = [ record . message for record in caplog . records if record . levelno == logging . WARNING ]
95+ warn_messages = get_warnings ( caplog )
8796 assert len (warn_messages ) == 1
8897 assert "- foo" in warn_messages [0 ]
8998 assert "- bar" in warn_messages [0 ]
99+
100+
101+ def test_warn_when_name_translations_are_missing (manifest : Manifest , caplog : LogCaptureFixture ) -> None :
102+ manifest .languages = [Bcp47LanguageTag ("en" ), Bcp47LanguageTag ("de" )]
103+ manifest .name = {Bcp47LanguageTag ("en" ): "Test Name" }
104+
105+ validate_package_name_and_description (manifest )
106+
107+ warn_messages = get_warnings (caplog )
108+ assert len (warn_messages ) == 1
109+ assert "- de" in warn_messages [0 ]
110+ assert "- en" not in warn_messages [0 ]
111+
112+
113+ def test_warn_when_description_translations_are_missing (manifest : Manifest , caplog : LogCaptureFixture ) -> None :
114+ manifest .languages = [Bcp47LanguageTag ("en" ), Bcp47LanguageTag ("de" )]
115+ manifest .name = {Bcp47LanguageTag ("en" ): "Test Name" , Bcp47LanguageTag ("de" ): "Test Name" }
116+ manifest .description = {Bcp47LanguageTag ("en" ): "Test Name" }
117+
118+ validate_package_name_and_description (manifest )
119+
120+ warn_messages = get_warnings (caplog )
121+ assert len (warn_messages ) == 1
122+ assert "- de" in warn_messages [0 ]
123+ assert "- en" not in warn_messages [0 ]
124+
125+
126+ def test_warn_when_name_translation_is_give_in_missing_language (manifest : Manifest , caplog : LogCaptureFixture ) -> None :
127+ manifest .languages = [Bcp47LanguageTag ("en" )]
128+ manifest .name = {Bcp47LanguageTag ("en" ): "Test Name" , Bcp47LanguageTag ("de" ): "Test Name" }
129+
130+ validate_package_name_and_description (manifest )
131+
132+ warn_messages = get_warnings (caplog )
133+ assert len (warn_messages ) == 1
134+ assert "- de" in warn_messages [0 ]
135+ assert "- en" not in warn_messages [0 ]
136+
137+
138+ def test_warn_when_description_translation_is_give_in_missing_language (
139+ manifest : Manifest , caplog : LogCaptureFixture
140+ ) -> None :
141+ manifest .languages = [Bcp47LanguageTag ("en" )]
142+ manifest .name = {Bcp47LanguageTag ("en" ): "Test Name" }
143+ manifest .description = {Bcp47LanguageTag ("en" ): "Test Description" , Bcp47LanguageTag ("de" ): "Test Beschreibung" }
144+
145+ validate_package_name_and_description (manifest )
146+
147+ warn_messages = get_warnings (caplog )
148+ assert len (warn_messages ) == 1
149+ assert "- de" in warn_messages [0 ]
150+ assert "- en" not in warn_messages [0 ]
151+
152+
153+ def test_warn_when_description_is_given_but_not_in_english (manifest : Manifest , caplog : LogCaptureFixture ) -> None :
154+ manifest .languages = [Bcp47LanguageTag ("en" ), Bcp47LanguageTag ("de" )]
155+ manifest .name = {Bcp47LanguageTag ("en" ): "Test Name" , Bcp47LanguageTag ("de" ): "Test Name" }
156+ manifest .description = {Bcp47LanguageTag ("de" ): "Beschreibung" }
157+
158+ validate_package_name_and_description (manifest )
159+
160+ warn_messages = get_warnings (caplog )
161+ assert len (warn_messages ) == 1
162+ assert "description should be available in English" in warn_messages [0 ]
0 commit comments