def test_command_without_locale(self): with StringIO() as stdout: call_command("collectstatic_js_choices", stdout=stdout) self.assertEqual(stdout.getvalue().strip(), "choices-en-us.js saved to static root!") self.assertTrue(storage.exists("choices-en-us.js")) with storage.open("choices-en-us.js") as f: js_content = generate_js() self.assertEqual(f.read(), js_content)
def test_pairs_without_jsmin(self): json_content = generate_js() 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_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")
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_view(self): json_content = generate_js() response = self.client.get("/inline/") self.assertContains(response, json_content)
def choices_js(request): locale = request.GET.get("lang", None) or request.GET.get("locale", None) response_body = generate_js(locale) return HttpResponse(response_body, **{"content_type": "application/javascript"})
def choices_js(request): locale = request.GET.get('lang', None) or request.GET.get('locale', None) response_body = generate_js(locale) return HttpResponse(response_body, **{'content_type': 'application/javascript'})