Ticket #15277: 15277.2.patch

File 15277.2.patch, 3.9 KB (added by jpaulett, 3 years ago)

Adds ogrinspect tests (though we are unable to currently generate a shapefile with a OGRTime field)

  • django/contrib/gis/tests/__init__.py

    diff --git django/contrib/gis/tests/__init__.py django/contrib/gis/tests/__init__.py
    index 3d04242..14f1ccc 100644
    def geodjango_suite(apps=True): 
    6161    from django.contrib.gis.tests import test_measure
    6262    suite.addTest(test_measure.suite())
    6363
     64    # Adding the ogrinspect tests.
     65    from django.contrib.gis.tests import test_ogrinspect
     66    suite.addTest(test_ogrinspect.suite())
     67
    6468    # Adding GDAL tests, and any test suite that depends on GDAL, to the
    6569    # suite if GDAL is available.
    6670    from django.contrib.gis.gdal import HAS_GDAL
  • new file django/contrib/gis/tests/test_ogrinspect.py

    diff --git django/contrib/gis/tests/test_ogrinspect.py django/contrib/gis/tests/test_ogrinspect.py
    new file mode 100644
    index 0000000..1e79b86
    - +  
     1import os
     2from django.contrib.gis.utils.ogrinspect import ogrinspect
     3from django.contrib.gis.geometry.test_data import TEST_DATA
     4from django.utils import unittest
     5
     6class OGRInspectTest(unittest.TestCase):
     7    def test_poly(self):
     8        shp_file = os.path.join(TEST_DATA, 'test_poly', 'test_poly.shp')
     9        model_def = ogrinspect(shp_file, 'MyModel')
     10
     11        expected = [
     12            '# This is an auto-generated Django model module created by ogrinspect.',
     13            'from django.contrib.gis.db import models',
     14            '',
     15            'class MyModel(models.Model):',
     16            '    float = models.FloatField()',
     17            '    int = models.FloatField()',
     18            '    str = models.CharField(max_length=80)',
     19            '    geom = models.PolygonField(srid=-1)',
     20            '    objects = models.GeoManager()',
     21        ]
     22
     23        self.assertEqual(model_def, '\n'.join(expected))
     24
     25    def test_date_field(self):
     26        shp_file = os.path.join(TEST_DATA, 'cities', 'cities.shp')
     27        model_def = ogrinspect(shp_file, 'City')
     28
     29        expected = [
     30            '# This is an auto-generated Django model module created by ogrinspect.',
     31            'from django.contrib.gis.db import models',
     32            '',
     33            'class City(models.Model):',
     34            '    name = models.CharField(max_length=80)',
     35            '    population = models.FloatField()',
     36            '    density = models.FloatField()',
     37            '    created = models.DateField()',
     38            '    geom = models.PointField(srid=-1)',
     39            '    objects = models.GeoManager()',
     40        ]
     41
     42        self.assertEqual(model_def, '\n'.join(expected))
     43
     44def suite():
     45    s = unittest.TestSuite()
     46    s.addTest(unittest.makeSuite(OGRInspectTest))
     47    return s
     48
     49def run(verbosity=2):
     50    unittest.TextTestRunner(verbosity=verbosity).run(suite())
     51
     52if __name__=="__main__":
     53    run()
  • django/contrib/gis/utils/ogrinspect.py

    diff --git django/contrib/gis/utils/ogrinspect.py django/contrib/gis/utils/ogrinspect.py
    index fe4443c..aa4e209 100644
    Author: Travis Pinney, Dane Springmeyer, & Justin Bronn 
    88from itertools import izip
    99# Requires GDAL to use.
    1010from django.contrib.gis.gdal import DataSource
    11 from django.contrib.gis.gdal.field import OFTDate, OFTDateTime, OFTInteger, OFTReal, OFTString
     11from django.contrib.gis.gdal.field import OFTDate, OFTDateTime, OFTInteger, OFTReal, OFTString, OFTTime
    1212
    1313def mapping(data_source, geom_name='geom', layer_key=0, multi_geom=False):
    1414    """
    def _ogrinspect(data_source, model_name, geom_name='geom', layer_key=0, srid=Non 
    189189            yield '    %s = models.DateField(%s)' % (mfield, kwargs_str[2:])
    190190        elif field_type is OFTDateTime:
    191191            yield '    %s = models.DateTimeField(%s)' % (mfield, kwargs_str[2:])
    192         elif field_type is OFTDate:
     192        elif field_type is OFTTime:
    193193            yield '    %s = models.TimeField(%s)' % (mfield, kwargs_str[2:])
    194194        else:
    195195            raise TypeError('Unknown field type %s in %s' % (field_type, mfield))
Back to Top