22import json
33import tempfile
44import gzip
5- import json
6- tempfile
75from datetime import datetime , timedelta
86from django .test import TestCase
97from django .core .serializers import serialize
108import fiona
11- gzip
129from django .urls import reverse
1310from django .conf import settings
1411import re
1512from publications .models import Publication
1613from publications .views import generate_geopackage
17- from publications .tasks import regenerate_geojson_cache , generate_data_dump_filename , cleanup_old_data_dumps
14+ from publications .tasks import (
15+ regenerate_geojson_cache ,
16+ regenerate_geopackage_cache ,
17+ generate_data_dump_filename ,
18+ cleanup_old_data_dumps ,
19+ )
1820from pathlib import Path
1921
2022class GeoDataAlternativeTestCase (TestCase ):
@@ -101,12 +103,9 @@ def test_data_endpoint(self):
101103 self .assertEqual (response .status_code , 200 , "Data endpoint should return status 200" )
102104 content = response .content .decode ()
103105 self .assertIn (f"Data dumps run every { settings .DATA_DUMP_INTERVAL_HOURS } hour" , content )
104- # find the most recent JSON dump
105106 cache_dir = Path (tempfile .gettempdir ()) / 'optimap_cache'
106107 dumps = sorted (cache_dir .glob ('optimap_data_dump_*.geojson' ), reverse = True )
107108 self .assertTrue (dumps , "At least one data dump should exist for Last updated check" )
108- last = dumps [0 ]
109- mtime = last .stat ().st_mtime
110109 self .assertIn ("Last updated:" , content )
111110
112111 def test_download_geojson_gzip (self ):
@@ -122,12 +121,12 @@ def test_download_geopackage_endpoint(self):
122121 url = reverse ('optimap:download_geopackage' )
123122 response = self .client .get (url )
124123 self .assertEqual (response .status_code , 200 , "GeoPackage endpoint should return 200" )
125- self .assertEqual (response ['Content-Type' ], 'application/geopackage+sqlite3' , "Content-Type should be application/geopackage+sqlite3" )
124+ self .assertEqual (response ['Content-Type' ], 'application/geopackage+sqlite3' ,
125+ "Content-Type should be application/geopackage+sqlite3" )
126126 self .assertRegex (response ['Content-Disposition' ], r'optimap_data_dump_.*\.gpkg' )
127127
128128 def test_regenerate_geojson_cache_creates_files (self ):
129129 cache_dir = Path (tempfile .gettempdir ()) / 'optimap_cache'
130- # clean out all existing dump files
131130 for f in cache_dir .glob ('optimap_data_dump_*' ):
132131 f .unlink ()
133132
@@ -162,9 +161,8 @@ def test_download_geopackage(self):
162161 self .assertEqual (response ['Content-Type' ], 'application/geopackage+sqlite3' )
163162 self .assertRegex (response ['Content-Disposition' ], r'optimap_data_dump_.*\.gpkg' )
164163
165- def test_data_page_hides_links_when_missing_cache (self ):
164+ def test_data_page_hides_and_shows_links_correctly (self ):
166165 cache_dir = Path (tempfile .gettempdir ()) / 'optimap_cache'
167- # clear only our dynamic dumps
168166 for f in cache_dir .glob ('optimap_data_dump_*' ):
169167 f .unlink ()
170168
@@ -178,3 +176,9 @@ def test_data_page_hides_links_when_missing_cache(self):
178176 content = response .content .decode ()
179177 self .assertIn ('Download GeoJSON' , content )
180178 self .assertNotIn ('Download GeoPackage' , content )
179+
180+ regenerate_geopackage_cache ()
181+ response = self .client .get (reverse ('optimap:data' ))
182+ content = response .content .decode ()
183+ self .assertIn ('Download GeoJSON' , content )
184+ self .assertIn ('Download GeoPackage' , content )
0 commit comments