Skip to content

Commit

Permalink
Add tests for the query_args function used by areas views
Browse files Browse the repository at this point in the history
  • Loading branch information
mhl committed Oct 3, 2017
1 parent 09b7b97 commit 7c88b24
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions mapit/tests/test_query_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# coding=utf-8

from django.test import TestCase
# from django.conf import settings
# from django.contrib.gis.geos import Polygon
# from django.utils.encoding import smart_text

# from mapit.models import Type, Area, Generation, Name, NameType
# import mapit_gb.countries
# import mapit.models

from mapit.models import Generation
from mapit.views.areas import query_args


class FakeRequest(object):
def __init__(self, params):
self.GET = params


class QueryArgsTest(TestCase):

def setUp(self):
self.old_generation = Generation.objects.create(
active=False,
description="Old test generation",
)
self.active_generation = Generation.objects.create(
active=True,
description="Test generation",
)

def test_no_type(self):
args = query_args(FakeRequest({}), 'json', None)
self.assertEqual(
args,
{
'generation_high__gte': self.active_generation.id,
'generation_low__lte': self.active_generation.id,
}
)

def test_one_type_in_query(self):
args = query_args(FakeRequest({'type': 'WMC'}), 'json', None)
self.assertEqual(
args,
{
'generation_high__gte': self.active_generation.id,
'generation_low__lte': self.active_generation.id,
'type__code': 'WMC',
}
)

def test_two_types_in_query(self):
args = query_args(FakeRequest({'type': 'WMC,EUR'}), 'json', None)
self.assertEqual(
args,
{
'generation_high__gte': self.active_generation.id,
'generation_low__lte': self.active_generation.id,
'type__code__in': ['WMC', 'EUR'],
}
)

def test_one_type_in_params(self):
args = query_args(FakeRequest({}), 'json', 'WMC')
self.assertEqual(
args,
{
'generation_high__gte': self.active_generation.id,
'generation_low__lte': self.active_generation.id,
'type__code': 'WMC',
}
)

def test_two_types_in_params(self):
args = query_args(FakeRequest({}), 'json', 'WMC,EUR')
self.assertEqual(
args,
{
'generation_high__gte': self.active_generation.id,
'generation_low__lte': self.active_generation.id,
'type__code__in': ['WMC', 'EUR'],
}
)

def test_generation_specified(self):
args = query_args(
FakeRequest({'generation': self.old_generation.id}),
'json',
None)
self.assertEqual(
args,
{
'generation_high__gte': self.old_generation.id,
'generation_low__lte': self.old_generation.id,
}
)

def test_min_generation_specified(self):
args = query_args(
FakeRequest({'min_generation': self.old_generation.id}),
'json',
None)
self.assertEqual(
args,
{
'generation_high__gte': self.old_generation.id,
'generation_low__lte': self.active_generation.id,
}
)

0 comments on commit 7c88b24

Please sign in to comment.