# Path: spillway/urls.py
# def tilepath(regex):
# def is_tilepath(path):
# 
# Path: spillway/views.py
# class RasterTileView(mixins.ResponseExceptionMixin, GenericAPIView):
# class TileView(mixins.ResponseExceptionMixin, BaseGeoView, ListAPIView):
#     def get(self, request, *args, **kwargs):
#     def get(self, request, *args, **kwargs):
# 
# Path: spillway/compat.py
# class JSONEncoder(json.JSONEncoder):
# class Mapnik(object):
#     def __getattr__(self, attr):
# 
# Path: tests/models.py
# class Location(AbstractLocation):
#     """Test location."""
#     objects = GeoQuerySet.as_manager()
# 
# Path: tests/test_models.py
# class RasterStoreTestBase(RasterTestBase, TestCase):
#     def setUp(self):
#         super(RasterStoreTestBase, self).setUp()
#         self.object = RasterStore.objects.create(image=self.data['image'].name)
#         self.qs = RasterStore.objects.all()


from io import BytesIO
from django.core.exceptions import ImproperlyConfigured
from django.core.files import File
from django.contrib.gis import geos
from rest_framework.test import APIRequestFactory, APITestCase
from PIL import Image
from spillway import urls, views
from spillway.compat import mapnik
from .models import Location
from .test_models import RasterStoreTestBase
import unittest

    def test_clipped_response(self):
        response = self.client.get('%s/?clip=true' % self.url)
        d = response.json()
        self.assertFalse(self.is_polygon_equal(d))
        # This particular geometry clipped to a tile should have +1 coords.
        self.assertEqual(len(d['features'][0]['geometry']['coordinates'][0]),
                         len(self.geometry['coordinates'][0]) + 1)

    def test_unclipped_response(self):
        response = self.client.get('%s.geojson' % self.url)
        self.assertTrue(self.is_polygon_equal(response.json()))

    @unittest.skipUnless(has_mapnik, 'requires mapnik')
    def test_png_response(self):
        response = self.client.get('%s.png' % self.url)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response['content-type'], 'image/png')
        im = Image.open(BytesIO(response.content))
        self.assertEqual(im.size, (256, 256))

    @unittest.skipUnless(has_mapnik, 'requires mapnik')
    def test_tile_outside_extent(self):
        response = self.client.get('/vectiles/4/7/8.png')
        self.assertEqual(response.status_code, 404)
        self.assertEqual(response['content-type'], 'text/html')

    def test_is_tilepath(self):
        self.assertTrue(urls.is_tilepath(self.url))
        self.assertTrue(urls.is_tilepath('%s.png' % self.url))

# Path: spillway/urls.py
# def tilepath(regex):
# def is_tilepath(path):
# 
# Path: spillway/views.py
# class RasterTileView(mixins.ResponseExceptionMixin, GenericAPIView):
# class TileView(mixins.ResponseExceptionMixin, BaseGeoView, ListAPIView):
#     def get(self, request, *args, **kwargs):
#     def get(self, request, *args, **kwargs):
# 
# Path: spillway/compat.py
# class JSONEncoder(json.JSONEncoder):
# class Mapnik(object):
#     def __getattr__(self, attr):
# 
# Path: tests/models.py
# class Location(AbstractLocation):
#     """Test location."""
#     objects = GeoQuerySet.as_manager()
# 
# Path: tests/test_models.py
# class RasterStoreTestBase(RasterTestBase, TestCase):
#     def setUp(self):
#         super(RasterStoreTestBase, self).setUp()
#         self.object = RasterStore.objects.create(image=self.data['image'].name)
#         self.qs = RasterStore.objects.all()


# Path: tests/test_views.py
from io import BytesIO
from django.core.exceptions import ImproperlyConfigured
from django.core.files import File
from django.contrib.gis import geos
from rest_framework.test import APIRequestFactory, APITestCase
from PIL import Image
from spillway import urls, views
from spillway.compat import mapnik
from .models import Location
from .test_models import RasterStoreTestBase
import unittest


    def test_clipped_response(self):
        response = self.client.get('%s/?clip=true' % self.url)
        d = response.json()
        self.assertFalse(self.is_polygon_equal(d))
        # This particular geometry clipped to a tile should have +1 coords.
        self.assertEqual(len(d['features'][0]['geometry']['coordinates'][0]),
                         len(self.geometry['coordinates'][0]) + 1)

    def test_unclipped_response(self):
        response = self.client.get('%s.geojson' % self.url)
        self.assertTrue(self.is_polygon_equal(response.json()))

    @unittest.skipUnless(has_mapnik, 'requires mapnik')
    def test_png_response(self):
        response = self.client.get('%s.png' % self.url)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response['content-type'], 'image/png')
        im = Image.open(BytesIO(response.content))
        self.assertEqual(im.size, (256, 256))

    @unittest.skipUnless(has_mapnik, 'requires mapnik')
    def test_tile_outside_extent(self):
        response = self.client.get('/vectiles/4/7/8.png')
        self.assertEqual(response.status_code, 404)
        self.assertEqual(response['content-type'], 'text/html')

    def test_is_tilepath(self):
        self.assertTrue(urls.is_tilepath(self.url))
        self.assertTrue(urls.is_tilepath('%s.png' % self.url))

        self.assertFalse(urls.is_tilepath('/blog/2010/03/'))