From 8170f850c3bafb533fc244266de2b7397a0e32d8 Mon Sep 17 00:00:00 2001 From: Ryan P Kilby Date: Wed, 15 Nov 2017 14:01:38 -0500 Subject: [PATCH 1/3] Replace assertRaises with assertRaisesMessage --- tests/test_model_serializer.py | 47 ++++++++++++---------------------- 1 file changed, 16 insertions(+), 31 deletions(-) diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index 203e1fe7f4..de6a5d4619 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -123,10 +123,10 @@ class Meta: 'non_model_field': 'bar', }) serializer.is_valid() - with self.assertRaises(TypeError) as excinfo: - serializer.save() + msginitial = 'Got a `TypeError` when calling `OneFieldModel.objects.create()`.' - assert str(excinfo.exception).startswith(msginitial) + with self.assertRaisesMessage(TypeError, msginitial): + serializer.save() def test_abstract_model(self): """ @@ -147,10 +147,10 @@ class Meta: serializer = TestSerializer(data={ 'afield': 'foo', }) - with self.assertRaises(ValueError) as excinfo: - serializer.is_valid() + msginitial = 'Cannot use ModelSerializer with Abstract Models.' - assert str(excinfo.exception).startswith(msginitial) + with self.assertRaisesMessage(ValueError, msginitial): + serializer.is_valid() class TestRegularFieldMappings(TestCase): @@ -294,10 +294,9 @@ class Meta: model = RegularFieldsModel fields = ('auto_field', 'invalid') - with self.assertRaises(ImproperlyConfigured) as excinfo: - TestSerializer().fields expected = 'Field name `invalid` is not valid for model `RegularFieldsModel`.' - assert str(excinfo.exception) == expected + with self.assertRaisesMessage(ImproperlyConfigured, expected): + TestSerializer().fields def test_missing_field(self): """ @@ -311,13 +310,12 @@ class Meta: model = RegularFieldsModel fields = ('auto_field',) - with self.assertRaises(AssertionError) as excinfo: - TestSerializer().fields expected = ( "The field 'missing' was declared on serializer TestSerializer, " "but has not been included in the 'fields' option." ) - assert str(excinfo.exception) == expected + with self.assertRaisesMessage(AssertionError, expected): + TestSerializer().fields def test_missing_superclass_field(self): """ @@ -862,28 +860,20 @@ class Meta: model = MetaClassTestModel fields = 'text' - with self.assertRaises(TypeError) as result: + msginitial = "The `fields` option must be a list or tuple" + with self.assertRaisesMessage(TypeError, msginitial): ExampleSerializer().fields - exception = result.exception - assert str(exception).startswith( - "The `fields` option must be a list or tuple" - ) - def test_meta_class_exclude_option(self): class ExampleSerializer(serializers.ModelSerializer): class Meta: model = MetaClassTestModel exclude = 'text' - with self.assertRaises(TypeError) as result: + msginitial = "The `exclude` option must be a list or tuple" + with self.assertRaisesMessage(TypeError, msginitial): ExampleSerializer().fields - exception = result.exception - assert str(exception).startswith( - "The `exclude` option must be a list or tuple" - ) - def test_meta_class_fields_and_exclude_options(self): class ExampleSerializer(serializers.ModelSerializer): class Meta: @@ -891,15 +881,10 @@ class Meta: fields = ('text',) exclude = ('text',) - with self.assertRaises(AssertionError) as result: + msginitial = "Cannot set both 'fields' and 'exclude' options on serializer ExampleSerializer." + with self.assertRaisesMessage(AssertionError, msginitial): ExampleSerializer().fields - exception = result.exception - self.assertEqual( - str(exception), - "Cannot set both 'fields' and 'exclude' options on serializer ExampleSerializer." - ) - class Issue2704TestCase(TestCase): def test_queryset_all(self): From a5a08d7ed7c100fba6a4b067b13d6b7dffd332a7 Mon Sep 17 00:00:00 2001 From: Ryan P Kilby Date: Wed, 15 Nov 2017 14:03:27 -0500 Subject: [PATCH 2/3] Remove outdated implicit Meta.fields test --- tests/test_model_serializer.py | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index de6a5d4619..1b66ecd99e 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -346,22 +346,6 @@ class Meta: ExampleSerializer() - def test_fields_and_exclude_behavior(self): - class ImplicitFieldsSerializer(serializers.ModelSerializer): - class Meta: - model = RegularFieldsModel - fields = '__all__' - - class ExplicitFieldsSerializer(serializers.ModelSerializer): - class Meta: - model = RegularFieldsModel - fields = '__all__' - - implicit = ImplicitFieldsSerializer() - explicit = ExplicitFieldsSerializer() - - assert implicit.data == explicit.data - class TestDurationFieldMapping(TestCase): def test_duration_field(self): From 47b46827e47f3cd06e78c6ea1c8e1ad6ceccaa0b Mon Sep 17 00:00:00 2001 From: Ryan P Kilby Date: Wed, 15 Nov 2017 14:39:21 -0500 Subject: [PATCH 3/3] Simplify parent declared field test --- tests/test_model_serializer.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index 1b66ecd99e..b45a0b64f7 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -325,13 +325,7 @@ def test_missing_superclass_field(self): class TestSerializer(serializers.ModelSerializer): missing = serializers.ReadOnlyField() - class Meta: - model = RegularFieldsModel - fields = '__all__' - class ChildSerializer(TestSerializer): - missing = serializers.ReadOnlyField() - class Meta: model = RegularFieldsModel fields = ('auto_field',)