Refs #29522 -- Fixed serializers/fixtures test crash if PyYAML isn't installed.

This commit is contained in:
Mariusz Felisiak 2024-09-19 08:19:43 +02:00 committed by Sarah Boyce
parent f8cc9285e1
commit 1fa8493640
2 changed files with 25 additions and 4 deletions

View File

@ -2,6 +2,7 @@
import json import json
import os import os
import re import re
import unittest
from io import StringIO from io import StringIO
from pathlib import Path from pathlib import Path
@ -55,6 +56,13 @@ from .models import (
Widget, Widget,
) )
try:
import yaml # NOQA
HAS_YAML = True
except ImportError:
HAS_YAML = False
_cur_dir = os.path.dirname(os.path.abspath(__file__)) _cur_dir = os.path.dirname(os.path.abspath(__file__))
@ -96,11 +104,13 @@ class TestFixtures(TestCase):
the serialized data for fields that have been removed the serialized data for fields that have been removed
from the database when not ignored. from the database when not ignored.
""" """
for fixture_file in ( test_fixtures = [
"sequence_extra", "sequence_extra",
"sequence_extra_jsonl", "sequence_extra_jsonl",
"sequence_extra_yaml", ]
): if HAS_YAML:
test_fixtures.append("sequence_extra_yaml")
for fixture_file in test_fixtures:
with ( with (
self.subTest(fixture_file=fixture_file), self.subTest(fixture_file=fixture_file),
self.assertRaises(DeserializationError), self.assertRaises(DeserializationError),
@ -147,6 +157,7 @@ class TestFixtures(TestCase):
) )
self.assertEqual(Animal.specimens.all()[0].name, "Eagle") self.assertEqual(Animal.specimens.all()[0].name, "Eagle")
@unittest.skipUnless(HAS_YAML, "No yaml library detected")
def test_loaddata_not_found_fields_ignore_yaml(self): def test_loaddata_not_found_fields_ignore_yaml(self):
management.call_command( management.call_command(
"loaddata", "loaddata",

View File

@ -1,14 +1,21 @@
import json import json
import unittest
from django.core.serializers.base import DeserializationError, DeserializedObject from django.core.serializers.base import DeserializationError, DeserializedObject
from django.core.serializers.json import Deserializer as JsonDeserializer from django.core.serializers.json import Deserializer as JsonDeserializer
from django.core.serializers.jsonl import Deserializer as JsonlDeserializer from django.core.serializers.jsonl import Deserializer as JsonlDeserializer
from django.core.serializers.python import Deserializer from django.core.serializers.python import Deserializer
from django.core.serializers.pyyaml import Deserializer as YamlDeserializer
from django.test import SimpleTestCase from django.test import SimpleTestCase
from .models import Author from .models import Author
try:
import yaml # NOQA
HAS_YAML = True
except ImportError:
HAS_YAML = False
class TestDeserializer(SimpleTestCase): class TestDeserializer(SimpleTestCase):
def setUp(self): def setUp(self):
@ -94,7 +101,10 @@ class TestDeserializer(SimpleTestCase):
self.assertEqual(first_item.object, self.jane) self.assertEqual(first_item.object, self.jane)
self.assertEqual(second_item.object, self.joe) self.assertEqual(second_item.object, self.joe)
@unittest.skipUnless(HAS_YAML, "No yaml library detected")
def test_yaml_bytes_input(self): def test_yaml_bytes_input(self):
from django.core.serializers.pyyaml import Deserializer as YamlDeserializer
test_string = """- pk: 1 test_string = """- pk: 1
model: serializers.author model: serializers.author
fields: fields: