|
2 | 2 | from __future__ import unicode_literals |
3 | 3 |
|
4 | 4 | import contextlib |
| 5 | +import json |
| 6 | +import pkgutil |
5 | 7 | import uuid |
6 | 8 |
|
7 | 9 | import pytest |
@@ -61,6 +63,14 @@ def _validate_completion_criteria(data): |
61 | 63 | ) |
62 | 64 |
|
63 | 65 |
|
| 66 | +def _load_language_codes(): |
| 67 | + """Return all language codes from languagelookup.json.""" |
| 68 | + data = pkgutil.get_data("le_utils", "resources/languagelookup.json") |
| 69 | + codes = list(json.loads(data).keys()) |
| 70 | + assert len(codes) > 0, "languagelookup.json should not be empty" |
| 71 | + return codes |
| 72 | + |
| 73 | + |
64 | 74 | @contextlib.contextmanager |
65 | 75 | def _assert_not_raises(not_expected): |
66 | 76 | try: |
@@ -479,6 +489,31 @@ def test_embed__topics__missing_channel_id(): |
479 | 489 | ) |
480 | 490 |
|
481 | 491 |
|
| 492 | +@skip_if_jsonschema_unavailable |
| 493 | +def test_embed__topics__all_languagelookup_codes_valid(): |
| 494 | + """Every language code in languagelookup.json must be accepted by the topics schema. |
| 495 | +
|
| 496 | + Regression test for https://github.com/learningequality/le-utils/issues/209 |
| 497 | + where codes like zh-Hans-CN were rejected. |
| 498 | + """ |
| 499 | + test_id = str(uuid.uuid4()) |
| 500 | + for code in _load_language_codes(): |
| 501 | + with _assert_not_raises(jsonschema.ValidationError): |
| 502 | + _validate_embed_topics_request( |
| 503 | + { |
| 504 | + "topics": [ |
| 505 | + { |
| 506 | + "id": test_id, |
| 507 | + "channel_id": test_id, |
| 508 | + "title": "Test topic", |
| 509 | + "description": "Test description", |
| 510 | + "language": code, |
| 511 | + } |
| 512 | + ], |
| 513 | + } |
| 514 | + ) |
| 515 | + |
| 516 | + |
482 | 517 | @skip_if_jsonschema_unavailable |
483 | 518 | def test_embed__content__valid(): |
484 | 519 | with _assert_not_raises(jsonschema.ValidationError): |
@@ -703,6 +738,34 @@ def test_embed__content__invalid_preset_files(): |
703 | 738 | ) |
704 | 739 |
|
705 | 740 |
|
| 741 | +@skip_if_jsonschema_unavailable |
| 742 | +def test_embed__content__all_languagelookup_codes_valid(): |
| 743 | + """Every language code in languagelookup.json must be accepted by the content schema. |
| 744 | +
|
| 745 | + Regression test for https://github.com/learningequality/le-utils/issues/209 |
| 746 | + where codes like zh-Hans-CN were rejected. |
| 747 | + """ |
| 748 | + test_id = str(uuid.uuid4()) |
| 749 | + for code in _load_language_codes(): |
| 750 | + with _assert_not_raises(jsonschema.ValidationError): |
| 751 | + _validate_embed_content_request( |
| 752 | + { |
| 753 | + "resources": [ |
| 754 | + { |
| 755 | + "id": test_id, |
| 756 | + "channel_id": test_id, |
| 757 | + "title": "Resource title", |
| 758 | + "description": "Resource description", |
| 759 | + "text": "Resource text", |
| 760 | + "language": code, |
| 761 | + "content_id": test_id, |
| 762 | + "channel_version": 1, |
| 763 | + }, |
| 764 | + ], |
| 765 | + } |
| 766 | + ) |
| 767 | + |
| 768 | + |
706 | 769 | def _validate_learning_objectives(data): |
707 | 770 | """ |
708 | 771 | :param data: Dictionary of data to validate |
|
0 commit comments