Skip to content

Commit

Permalink
Fix tests relying on strictness
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan P Kilby committed Oct 19, 2017
1 parent fce5248 commit b98f254
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 30 deletions.
27 changes: 7 additions & 20 deletions tests/test_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,25 +1095,9 @@ class Meta:
self.assertEqual(list(F().qs), list(User.objects.all()))
self.assertEqual(list(F({'username': 'alex'}).qs),
[User.objects.get(username='alex')])
self.assertEqual(list(F({'username': 'jose'}).qs),
list())

def test_filtering_without_strict(self):
User.objects.create(username='alex')
User.objects.create(username='jacob')
User.objects.create(username='aaron')

class F(FilterSet):
username = AllValuesFilter()

class Meta:
model = User
fields = ['username']
strict = False

self.assertEqual(list(F().qs), list(User.objects.all()))
self.assertEqual(list(F({'username': 'alex'}).qs),
[User.objects.get(username='alex')])
# invalid choice
self.assertFalse(F({'username': 'jose'}).is_valid())
self.assertEqual(list(F({'username': 'jose'}).qs),
list(User.objects.all()))

Expand All @@ -1137,8 +1121,11 @@ class Meta:
[User.objects.get(username='alex')])
self.assertEqual(list(F({'username': ['alex', 'jacob']}).qs),
list(User.objects.filter(username__in=['alex', 'jacob'])))
self.assertEqual(list(F({'username': ['jose']}).qs),
list())

# invalid choice
self.assertFalse(F({'username': 'jose'}).is_valid())
self.assertEqual(list(F({'username': 'jose'}).qs),
list(User.objects.all()))


class FilterMethodTests(TestCase):
Expand Down
9 changes: 3 additions & 6 deletions tests/test_filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import unittest

import django
from django.core.exceptions import ValidationError
from django.db import models
from django.test import TestCase, override_settings
from django.test import TestCase

from django_filters.constants import STRICTNESS
from django_filters.filters import (
Expand Down Expand Up @@ -703,8 +702,7 @@ class Meta:
self.assertEqual(f.qs.count(), 2)

f = F({'username': 'alex'}, queryset=self.qs)
with self.assertRaises(ValidationError):
f.qs.count()
self.assertFalse(f.is_valid())

f = F({'username': 'alex', 'status': 1}, queryset=self.qs)
self.assertEqual(f.qs.count(), 1)
Expand All @@ -722,8 +720,7 @@ class Meta:
self.assertEqual(f.qs.count(), 2)

f = F({'username': 'alex'}, queryset=self.qs)
with self.assertRaises(ValidationError):
f.qs.count()
self.assertFalse(f.is_valid())

f = F({'username': 'alex', 'status': 1}, queryset=self.qs)
self.assertEqual(f.qs.count(), 1)
Expand Down
5 changes: 1 addition & 4 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,10 @@ class F(FilterSet):
class Meta:
model = Article
fields = ['id', 'author', 'name']
strict = STRICTNESS.RAISE_VALIDATION_ERROR

f = F(data={'id': 'foo', 'author': 'bar', 'name': 'baz'})
with self.assertRaises(ValidationError) as exc:
f.qs

self.assertDictEqual(raw_validation(exc.exception), {
self.assertDictEqual(raw_validation(f.errors), {
'id': ['Enter a number.'],
'author': ['Select a valid choice. That choice is not one of the available choices.'],
})

0 comments on commit b98f254

Please sign in to comment.