from rest_framework.test import APITestCase from apps.locations.models import City, Neighborhood class LocationListFilterTests(APITestCase): @classmethod def setUpTestData(cls): cls.tehran = City.objects.create(name='تهران', slug='tehran') cls.mashhad = City.objects.create(name='مشهد', slug='mashhad') cls.vanak = Neighborhood.objects.create(city=cls.tehran, name='ونک', slug='vanak') cls.tajrish = Neighborhood.objects.create(city=cls.tehran, name='تجریش', slug='tajrish') cls.mashhad_hood = Neighborhood.objects.create(city=cls.mashhad, name='احمدآباد', slug='ahmadabad') def test_search_city_by_name(self): response = self.client.get('/api/v1/cities/', {'search': 'مشهد'}) self.assertEqual(response.status_code, 200) uuids = {city['uuid'] for city in response.data['results']} self.assertEqual(uuids, {str(self.mashhad.uuid)}) def test_filter_neighborhood_by_city(self): response = self.client.get('/api/v1/neighborhoods/', {'city': str(self.tehran.uuid)}) self.assertEqual(response.status_code, 200) uuids = {n['uuid'] for n in response.data['results']} self.assertEqual(uuids, {str(self.vanak.uuid), str(self.tajrish.uuid)}) def test_search_neighborhood_by_name(self): response = self.client.get('/api/v1/neighborhoods/', {'search': 'ونک'}) self.assertEqual(response.status_code, 200) uuids = {n['uuid'] for n in response.data['results']} self.assertEqual(uuids, {str(self.vanak.uuid)}) def test_search_neighborhood_by_city_name(self): response = self.client.get('/api/v1/neighborhoods/', {'search': 'مشهد'}) self.assertEqual(response.status_code, 200) uuids = {n['uuid'] for n in response.data['results']} self.assertEqual(uuids, {str(self.mashhad_hood.uuid)})