Try fixing regex lookup
Adds tests
This commit is contained in:
1 parent
4e41af0bd8
commit
2a037fbe8f
2 files changed
+37
-5
No files matched your search
+8
-3
@@ -10,9 +10,14 @@ filters.LOOKUP_TYPES = ['iregex', 'exact', 'iexact']
|
||||
class AliasFilter(django_filters.FilterSet):
|
||||
# TODO: These do not work
|
||||
# Ref: https://docs.djangoproject.com/en/1.7/ref/models/querysets/#iregex
|
||||
source_regex = django_filters.CharFilter(name='source', lookup_expr=['iregex'])
|
||||
destination_regex = django_filters.CharFilter(name='destination', lookup_expr=['iregex'])
|
||||
source_regex = django_filters.CharFilter(name='source')
|
||||
destination_regex = django_filters.CharFilter(name='destination')
|
||||
|
||||
class Meta:
|
||||
model = Alias
|
||||
fields = ['domain', 'domain__name', 'source_regex', 'destination_regex']
|
||||
fields = {
|
||||
'domain': ['exact'],
|
||||
'domain__name': ['iexact'],
|
||||
'source_regex': ['iregex', 'regex'],
|
||||
'destination_regex': ['iregex', 'regex']
|
||||
}
|
||||
+29
-2
@@ -1,3 +1,30 @@
|
||||
from django.test import TestCase
|
||||
from django.contrib.auth.models import User
|
||||
from django.urls import reverse
|
||||
from rest_framework.test import APITestCase
|
||||
|
||||
# Create your tests here.
|
||||
from dpdapi.models import Domain, Alias
|
||||
|
||||
|
||||
class RegexTestCase(APITestCase):
|
||||
aliases = ['ops-alert@example.com', 'yolo-announce@example.com']
|
||||
|
||||
def setUp(self):
|
||||
user = User.objects.create(username='yolo')
|
||||
d = Domain.objects.create(name='example.com')
|
||||
for a in self.aliases:
|
||||
Alias.objects.create(source=a, destination='@example.com', domain=d)
|
||||
self.client.force_login(user)
|
||||
|
||||
def test_get_all(self):
|
||||
res = self.client.get(reverse('alias-list'))
|
||||
self.assertEqual(len(res.data), len(self.aliases))
|
||||
|
||||
def test_domain_filter(self):
|
||||
params = {'domain__name': 'example.com'}
|
||||
res = self.client.get(reverse('alias-list'), data=params)
|
||||
self.assertEqual(len(res.data), len(self.aliases))
|
||||
|
||||
def test_regex_filter(self):
|
||||
params = {'source_regex': '^ops-|^ops@'}
|
||||
res = self.client.get(reverse('alias-list'), data=params)
|
||||
self.assertEqual(len(res.data), 1)
|
||||
Reference in new issue
Block a user