1. """
    
  2. This module has the mock object definitions used to hold reference geometry
    
  3. for the GEOS and GDAL tests.
    
  4. """
    
  5. import json
    
  6. import os
    
  7. 
    
  8. from django.utils.functional import cached_property
    
  9. 
    
  10. # Path where reference test data is located.
    
  11. TEST_DATA = os.path.join(os.path.dirname(__file__), "data")
    
  12. 
    
  13. 
    
  14. def tuplize(seq):
    
  15.     "Turn all nested sequences to tuples in given sequence."
    
  16.     if isinstance(seq, (list, tuple)):
    
  17.         return tuple(tuplize(i) for i in seq)
    
  18.     return seq
    
  19. 
    
  20. 
    
  21. def strconvert(d):
    
  22.     "Converts all keys in dictionary to str type."
    
  23.     return {str(k): v for k, v in d.items()}
    
  24. 
    
  25. 
    
  26. def get_ds_file(name, ext):
    
  27.     return os.path.join(TEST_DATA, name, name + ".%s" % ext)
    
  28. 
    
  29. 
    
  30. class TestObj:
    
  31.     """
    
  32.     Base testing object, turns keyword args into attributes.
    
  33.     """
    
  34. 
    
  35.     def __init__(self, **kwargs):
    
  36.         for key, value in kwargs.items():
    
  37.             setattr(self, key, value)
    
  38. 
    
  39. 
    
  40. class TestDS(TestObj):
    
  41.     """
    
  42.     Object for testing GDAL data sources.
    
  43.     """
    
  44. 
    
  45.     def __init__(self, name, *, ext="shp", **kwargs):
    
  46.         # Shapefile is default extension, unless specified otherwise.
    
  47.         self.name = name
    
  48.         self.ds = get_ds_file(name, ext)
    
  49.         super().__init__(**kwargs)
    
  50. 
    
  51. 
    
  52. class TestGeom(TestObj):
    
  53.     """
    
  54.     Testing object used for wrapping reference geometry data
    
  55.     in GEOS/GDAL tests.
    
  56.     """
    
  57. 
    
  58.     def __init__(self, *, coords=None, centroid=None, ext_ring_cs=None, **kwargs):
    
  59.         # Converting lists to tuples of certain keyword args
    
  60.         # so coordinate test cases will match (JSON has no
    
  61.         # concept of tuple).
    
  62.         if coords:
    
  63.             self.coords = tuplize(coords)
    
  64.         if centroid:
    
  65.             self.centroid = tuple(centroid)
    
  66.         self.ext_ring_cs = ext_ring_cs and tuplize(ext_ring_cs)
    
  67.         super().__init__(**kwargs)
    
  68. 
    
  69. 
    
  70. class TestGeomSet:
    
  71.     """
    
  72.     Each attribute of this object is a list of `TestGeom` instances.
    
  73.     """
    
  74. 
    
  75.     def __init__(self, **kwargs):
    
  76.         for key, value in kwargs.items():
    
  77.             setattr(self, key, [TestGeom(**strconvert(kw)) for kw in value])
    
  78. 
    
  79. 
    
  80. class TestDataMixin:
    
  81.     """
    
  82.     Mixin used for GEOS/GDAL test cases that defines a `geometries`
    
  83.     property, which returns and/or loads the reference geometry data.
    
  84.     """
    
  85. 
    
  86.     @cached_property
    
  87.     def geometries(self):
    
  88.         # Load up the test geometry data from fixture into global.
    
  89.         with open(os.path.join(TEST_DATA, "geometries.json")) as f:
    
  90.             geometries = json.load(f)
    
  91.         return TestGeomSet(**strconvert(geometries))