From f1236ed6595ef20afee57346cb8f70c99332ee25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenzo=20Pe=C3=B1a?= Date: Sat, 27 Feb 2021 13:15:53 -0500 Subject: [PATCH] Added tests for generated javascript --- .../django_js_choices/choices_js.tpl | 16 +- djsc_sandbox/tests/test_core.py | 147 ++++++++++++------ pyproject.toml | 1 + 3 files changed, 116 insertions(+), 48 deletions(-) diff --git a/django_js_choices/templates/django_js_choices/choices_js.tpl b/django_js_choices/templates/django_js_choices/choices_js.tpl index f706bc8..cfe2207 100644 --- a/django_js_choices/templates/django_js_choices/choices_js.tpl +++ b/django_js_choices/templates/django_js_choices/choices_js.tpl @@ -4,12 +4,20 @@ return { pairs: function(name) { var choices = rawChoices[namedChoices[name]]; - return choices && choices.map(pair => ({value: pair[0], label: pair[1]})); + var mapper = function (pair) { + return { + value: pair[0], + label: pair[1] + }; + } + return choices && choices.map(mapper); }, display: function(name, choice) { - var choices = rawChoices[namedChoices[name]], - finder = choice !== Object(choice) ? (pair => pair[0] == choice) : (pair => pair[0] == choice[name]), - pair = choices && choices.find(finder); + var choices = rawChoices[namedChoices[name]]; + var finder = choice !== Object(choice) + ? function (pair) { return pair[0] == choice; } + : function (pair) { return pair[0] == choice[name]; }; + var pair = choices && choices.filter(finder)[0]; return pair && pair[1]; } }; diff --git a/djsc_sandbox/tests/test_core.py b/djsc_sandbox/tests/test_core.py index 1613e1b..1a2e607 100644 --- a/djsc_sandbox/tests/test_core.py +++ b/djsc_sandbox/tests/test_core.py @@ -2,9 +2,11 @@ from django.test import override_settings from django.test.testcases import SimpleTestCase -from django.utils.translation import gettext_lazy as _ +from django.utils.translation import gettext_lazy as _, override +from dukpy import evaljs from django_js_choices.core import generate_choices, generate_js, prepare_choices +from djsc_sandbox.myapp.choices import MEDAL_TYPES, MEDIA_CHOICES, YEAR_IN_SCHOOL_CHOICES class PrepareChoicesTestCase(SimpleTestCase): @@ -98,52 +100,109 @@ class GenerateJSTestCase(SimpleTestCase): Test generate_js function. """ + def get_display(self, json_content, field, key): + return evaljs(f"{json_content} Choices.display('{field}', '{key}')") + + def get_pairs(self, json_content, field): + return evaljs(f"{json_content} Choices.pairs('{field}')") + + @override_settings(JS_CHOICES_JS_MINIFY=True) + def test_display_with_jsmin(self): + json_content = generate_js() + self.assertTrue(evaljs(f"{json_content} Choices.display !== undefined")) + self.assertEqual(self.get_display(json_content, "modela_year_in_school", "FR"), "Freshman") + self.assertEqual(self.get_display(json_content, "modela_year_in_school", "SO"), "Sophomore") + self.assertEqual(self.get_display(json_content, "modela_year_in_school", "JR"), "Junior") + self.assertEqual(self.get_display(json_content, "modela_year_in_school", "SR"), "Senior") + self.assertEqual(self.get_display(json_content, "modela_year_in_school", "GR"), "Graduate") + self.assertEqual(self.get_display(json_content, "modelb_year_in_school", "GR"), None) + self.assertEqual(self.get_display(json_content, "modelb_media", "vinyl"), "Vinyl") + self.assertEqual(self.get_display(json_content, "modelb_media", "cd"), "CD") + self.assertEqual(self.get_display(json_content, "modelb_media", "vhs"), "VHS Tape") + self.assertEqual(self.get_display(json_content, "modelb_media", "dvd"), "DVD") + self.assertEqual(self.get_display(json_content, "modelb_media", "unknown"), "Unknown") + self.assertEqual(self.get_display(json_content, "modelc_medals", "GOLD"), "Gold") + self.assertEqual(self.get_display(json_content, "modelc_medals", "SILVER"), "Silver") + self.assertEqual(self.get_display(json_content, "modelc_medals", "BRONZE"), "Bronze") + + @override_settings(JS_CHOICES_JS_MINIFY=False) + def test_display_without_jsmin(self): + json_content = generate_js() + self.assertTrue(evaljs(f"{json_content} Choices.display !== undefined")) + self.assertEqual(self.get_display(json_content, "modela_year_in_school", "FR"), "Freshman") + self.assertEqual(self.get_display(json_content, "modela_year_in_school", "SO"), "Sophomore") + self.assertEqual(self.get_display(json_content, "modela_year_in_school", "JR"), "Junior") + self.assertEqual(self.get_display(json_content, "modela_year_in_school", "SR"), "Senior") + self.assertEqual(self.get_display(json_content, "modela_year_in_school", "GR"), "Graduate") + self.assertEqual(self.get_display(json_content, "modelb_year_in_school", "GR"), None) + self.assertEqual(self.get_display(json_content, "modelb_media", "vinyl"), "Vinyl") + self.assertEqual(self.get_display(json_content, "modelb_media", "cd"), "CD") + self.assertEqual(self.get_display(json_content, "modelb_media", "vhs"), "VHS Tape") + self.assertEqual(self.get_display(json_content, "modelb_media", "dvd"), "DVD") + self.assertEqual(self.get_display(json_content, "modelb_media", "unknown"), "Unknown") + self.assertEqual(self.get_display(json_content, "modelc_medals", "GOLD"), "Gold") + self.assertEqual(self.get_display(json_content, "modelc_medals", "SILVER"), "Silver") + self.assertEqual(self.get_display(json_content, "modelc_medals", "BRONZE"), "Bronze") + + def test_display_with_locale(self): + json_content = generate_js("es") + self.assertTrue(evaljs(f"{json_content} Choices.display !== undefined")) + self.assertEqual(self.get_display(json_content, "modela_year_in_school", "FR"), "Estudiante de primer año") + self.assertEqual(self.get_display(json_content, "modela_year_in_school", "SO"), "Estudiante de segundo año") + self.assertEqual(self.get_display(json_content, "modela_year_in_school", "JR"), "Estudiante de tercer año") + self.assertEqual(self.get_display(json_content, "modela_year_in_school", "SR"), "Estudiante de último año") + self.assertEqual(self.get_display(json_content, "modela_year_in_school", "GR"), "Graduado") + self.assertEqual(self.get_display(json_content, "modelb_year_in_school", "GR"), None) + self.assertEqual(self.get_display(json_content, "modelb_media", "vinyl"), "Vinil") + self.assertEqual(self.get_display(json_content, "modelb_media", "cd"), "CD") + self.assertEqual(self.get_display(json_content, "modelb_media", "vhs"), "Cinta VHS") + self.assertEqual(self.get_display(json_content, "modelb_media", "dvd"), "DVD") + self.assertEqual(self.get_display(json_content, "modelb_media", "unknown"), "Desconocido") + self.assertEqual(self.get_display(json_content, "modelc_medals", "GOLD"), "Oro") + self.assertEqual(self.get_display(json_content, "modelc_medals", "SILVER"), "Plata") + self.assertEqual(self.get_display(json_content, "modelc_medals", "BRONZE"), "Bronce") + @override_settings(JS_CHOICES_JS_MINIFY=True) - def test_generation_with_jsmin(self): + def test_pairs_with_jsmin(self): json_content = generate_js() - self.assertIn("Freshman", json_content) - self.assertIn("Sophomore", json_content) - self.assertIn("Junior", json_content) - self.assertIn("Senior", json_content) - self.assertIn("Graduate", json_content) - self.assertIn("Vinyl", json_content) - self.assertIn("CD", json_content) - self.assertIn("VHS Tape", json_content) - self.assertIn("DVD", json_content) - self.assertIn("Unknown", json_content) - self.assertIn("Gold", json_content) - self.assertIn("Silver", json_content) - self.assertIn("Bronze", json_content) + self.assertTrue(evaljs(f"{json_content} Choices.pairs !== undefined")) + years_in_school = [{"value": value, "label": str(label)} for value, label in YEAR_IN_SCHOOL_CHOICES] + media_choices = [ + {"value": value, "label": str(label)} + for value, label in MEDIA_CHOICES[0][1] + MEDIA_CHOICES[1][1] + (MEDIA_CHOICES[2],) + ] + medal_types = [{"value": value, "label": str(label)} for value, label in MEDAL_TYPES] + self.assertEqual(self.get_pairs(json_content, "modela_year_in_school"), years_in_school) + self.assertEqual(self.get_pairs(json_content, "modelb_year_in_school"), years_in_school[:-1]) + self.assertEqual(self.get_pairs(json_content, "modelb_media"), media_choices) + self.assertEqual(self.get_pairs(json_content, "modelc_medals"), medal_types) @override_settings(JS_CHOICES_JS_MINIFY=False) - def test_generation_without_jsmin(self): + def test_pairs_without_jsmin(self): json_content = generate_js() - self.assertIn("Freshman", json_content) - self.assertIn("Sophomore", json_content) - self.assertIn("Junior", json_content) - self.assertIn("Senior", json_content) - self.assertIn("Graduate", json_content) - self.assertIn("Vinyl", json_content) - self.assertIn("CD", json_content) - self.assertIn("VHS Tape", json_content) - self.assertIn("DVD", json_content) - self.assertIn("Unknown", json_content) - self.assertIn("Gold", json_content) - self.assertIn("Silver", json_content) - self.assertIn("Bronze", json_content) - - def test_generation_with_locale(self): + self.assertTrue(evaljs(f"{json_content} Choices.pairs !== undefined")) + years_in_school = [{"value": value, "label": str(label)} for value, label in YEAR_IN_SCHOOL_CHOICES] + media_choices = [ + {"value": value, "label": str(label)} + for value, label in MEDIA_CHOICES[0][1] + MEDIA_CHOICES[1][1] + (MEDIA_CHOICES[2],) + ] + medal_types = [{"value": value, "label": str(label)} for value, label in MEDAL_TYPES] + self.assertEqual(self.get_pairs(json_content, "modela_year_in_school"), years_in_school) + self.assertEqual(self.get_pairs(json_content, "modelb_year_in_school"), years_in_school[:-1]) + self.assertEqual(self.get_pairs(json_content, "modelb_media"), media_choices) + self.assertEqual(self.get_pairs(json_content, "modelc_medals"), medal_types) + + def test_pairs_with_locale(self): json_content = generate_js("es") - self.assertIn("Estudiante de primer a\\u00f1o", json_content) - self.assertIn("Estudiante de segundo a\\u00f1o", json_content) - self.assertIn("Estudiante de tercer a\\u00f1o", json_content) - self.assertIn("Estudiante de \\u00faltimo a\\u00f1o", json_content) - self.assertIn("Graduado", json_content) - self.assertIn("Vinil", json_content) - self.assertIn("CD", json_content) - self.assertIn("Cinta VHS", json_content) - self.assertIn("DVD", json_content) - self.assertIn("Desconocido", json_content) - self.assertIn("Oro", json_content) - self.assertIn("Plata", json_content) - self.assertIn("Bronce", json_content) + with override("es"): + self.assertTrue(evaljs(f"{json_content} Choices.pairs !== undefined")) + years_in_school = [{"value": value, "label": str(label)} for value, label in YEAR_IN_SCHOOL_CHOICES] + media_choices = [ + {"value": value, "label": str(label)} + for value, label in MEDIA_CHOICES[0][1] + MEDIA_CHOICES[1][1] + (MEDIA_CHOICES[2],) + ] + medal_types = [{"value": value, "label": str(label)} for value, label in MEDAL_TYPES] + self.assertEqual(self.get_pairs(json_content, "modela_year_in_school"), years_in_school) + self.assertEqual(self.get_pairs(json_content, "modelb_year_in_school"), years_in_school[:-1]) + self.assertEqual(self.get_pairs(json_content, "modelb_media"), media_choices) + self.assertEqual(self.get_pairs(json_content, "modelc_medals"), medal_types) diff --git a/pyproject.toml b/pyproject.toml index fd1438c..34fdc2d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,6 +26,7 @@ rjsmin = "^1.1.0" black = "^20.8b1" coverage = {extras = ["toml"], version = "^5.1"} django-multiselectfield = "^0.1.12" +dukpy = "^0.2.3" flake8 = "^3.8.4" flake8-bugbear = "^20.11.1" flake8-comprehensions = "^3.3.1"