From 1fa84936408b84a6530d56cc1efed83ce6e7160c Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Thu, 19 Sep 2024 08:19:43 +0200 Subject: [PATCH] Refs #29522 -- Fixed serializers/fixtures test crash if PyYAML isn't installed. --- tests/fixtures_regress/tests.py | 17 ++++++++++++++--- tests/serializers/test_deserialization.py | 12 +++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/tests/fixtures_regress/tests.py b/tests/fixtures_regress/tests.py index ef79c2f382..4a982c7262 100644 --- a/tests/fixtures_regress/tests.py +++ b/tests/fixtures_regress/tests.py @@ -2,6 +2,7 @@ import json import os import re +import unittest from io import StringIO from pathlib import Path @@ -55,6 +56,13 @@ from .models import ( Widget, ) +try: + import yaml # NOQA + + HAS_YAML = True +except ImportError: + HAS_YAML = False + _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 from the database when not ignored. """ - for fixture_file in ( + test_fixtures = [ "sequence_extra", "sequence_extra_jsonl", - "sequence_extra_yaml", - ): + ] + if HAS_YAML: + test_fixtures.append("sequence_extra_yaml") + for fixture_file in test_fixtures: with ( self.subTest(fixture_file=fixture_file), self.assertRaises(DeserializationError), @@ -147,6 +157,7 @@ class TestFixtures(TestCase): ) 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): management.call_command( "loaddata", diff --git a/tests/serializers/test_deserialization.py b/tests/serializers/test_deserialization.py index 3c4af2ce33..0bbb46b7ce 100644 --- a/tests/serializers/test_deserialization.py +++ b/tests/serializers/test_deserialization.py @@ -1,14 +1,21 @@ import json +import unittest from django.core.serializers.base import DeserializationError, DeserializedObject from django.core.serializers.json import Deserializer as JsonDeserializer from django.core.serializers.jsonl import Deserializer as JsonlDeserializer from django.core.serializers.python import Deserializer -from django.core.serializers.pyyaml import Deserializer as YamlDeserializer from django.test import SimpleTestCase from .models import Author +try: + import yaml # NOQA + + HAS_YAML = True +except ImportError: + HAS_YAML = False + class TestDeserializer(SimpleTestCase): def setUp(self): @@ -94,7 +101,10 @@ class TestDeserializer(SimpleTestCase): self.assertEqual(first_item.object, self.jane) self.assertEqual(second_item.object, self.joe) + @unittest.skipUnless(HAS_YAML, "No yaml library detected") def test_yaml_bytes_input(self): + from django.core.serializers.pyyaml import Deserializer as YamlDeserializer + test_string = """- pk: 1 model: serializers.author fields: