def test_earthquake_report_list_post(self): report_multipart = { 'language': u'en', 'report_pdf': File(open(self.data_path(u'20150619200628-en.pdf'))), 'report_image': File(open(self.data_path(u'20150619200628-en.png'))), 'report_thumbnail': File(open(self.data_path(u'20150619200628-thumb-en.png'))), } response = self.client.post(reverse( 'realtime:earthquake_report_list', kwargs={'shake_id': u'20150619200628'}), report_multipart, format='multipart') self.assertEqual(response.status_code, status.HTTP_201_CREATED) response = self.client.get( reverse('realtime:earthquake_report_list', kwargs={'shake_id': u'20150619200628'})) self.assertEqual(response.status_code, status.HTTP_200_OK) serializer = PageNumberPaginationSerializer( serializer_class=EarthquakeReportSerializer, data=response.data) self.assertEqual(serializer.count, 2) # delete data report = EarthquakeReport.objects.get( earthquake__shake_id=u'20150619200628', language='en') report.delete()
def test_flood_report_post(self): report_dict = { 'event_id': u'2015112518-3-rw', 'language': u'id', 'impact_report': File(open(self.data_path('impact-table-id.pdf'))), 'impact_map': File(open(self.data_path('impact-map-id.pdf'))) } response = self.client.post(reverse( 'realtime:flood_report_list', kwargs={'event_id': report_dict['event_id']}), report_dict, format='multipart') self.assertEqual(response.status_code, status.HTTP_201_CREATED) response = self.client.get(reverse('realtime:flood_report_list')) # Test the json returns self.assertEqual(response.data['count'], 2) self.assertEqualDictionaryWithFiles(response.data['results'][0], report_dict) # Test using serializer serializer = PageNumberPaginationSerializer( serializer_class=FloodReportSerializer, data=response.data) self.assertEqual(serializer.count, 2) self.assertEqualDictionaryWithFiles(serializer.results[0], report_dict) report = FloodReport.objects.get( flood__event_id=report_dict['event_id'], language=u'id') serializer = FloodReportSerializer(report) self.assertEqualDictionaryWithFiles(serializer.data, report_dict) report.delete()
def test_earthquake_list_post(self): shake_json = { 'shake_id': u'20150619200629', 'magnitude': 4.6, 'time': u'2015-06-19T12:59:28Z', 'depth': 10.0, 'location': { u'type': u'Point', u'coordinates': [126.52, 4.16] }, 'location_description': u'Manado' } response = self.client.post(reverse('realtime:earthquake_list'), shake_json, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) response = self.client.get(reverse('realtime:earthquake_list')) serializer = PageNumberPaginationSerializer( serializer_class=EarthquakeSerializer, data=response.data) self.assertEqual(serializer.count, 2) earthquake = Earthquake.objects.get(shake_id=u'20150619200629') serializer = EarthquakeSerializer(earthquake) for key, value in shake_json.iteritems(): if isinstance(serializer.data[key], GeoJsonDict): self.compare_geo_json_dict(value, serializer.data[key]) else: self.assertEqual(value, serializer.data[key]) earthquake.delete()
def test_flood_post(self): flood_json = { 'event_id': u'2015112518-6-rw', 'time': u'2015-11-25T18:00:00Z', 'interval': 3, 'source': u'Peta Jakarta', 'region': u'Jakarta', 'hazard_layer': File( open(self.data_path('hazard.zip')) ) } response = self.client.post( reverse('realtime:flood_list'), flood_json, format='multipart' ) self.assertEqual(response.status_code, status.HTTP_201_CREATED) response = self.client.get(reverse('realtime:flood_list')) serializer = PageNumberPaginationSerializer( serializer_class=FloodSerializer, data=response.data ) self.assertEqual(serializer.count, 2) flood = Flood.objects.get(event_id=flood_json['event_id']) serializer = FloodSerializer(flood) self.assertEqualDictionaryWithFiles(serializer.data, flood_json) flood.delete()
def test_earthquake_list(self): response = self.client.get(reverse('realtime:earthquake_list')) expected_shake_id = '20150619200628' self.assertEqual(response.status_code, status.HTTP_200_OK) serializer = PageNumberPaginationSerializer( serializer_class=EarthquakeSerializer, data=response.data) actual_results = serializer.results self.assertEqual(serializer.count, 1) self.assertEqual(actual_results[0]['shake_id'], expected_shake_id)
def test_flood_list(self): response = self.client.get(reverse('realtime:flood_list')) expected_event_id = '2015112518-3-rw' self.assertEqual(response.status_code, status.HTTP_200_OK) serializer = PageNumberPaginationSerializer( serializer_class=FloodSerializer, data=response.data) actual_results = serializer.results self.assertEqual(serializer.count, 1) self.assertEqual(actual_results[0]['event_id'], expected_event_id)
def _check_response(test_instance, query, result_count): """Shortcut to validate query.""" r = test_instance.client.get( reverse('realtime:earthquake_list'), data=query) test_instance.assertEqual(r.status_code, status.HTTP_200_OK) s = PageNumberPaginationSerializer( serializer_class=EarthquakeSerializer, data=r.data) test_instance.assertEqual(s.count, result_count)
def test_earthquake_report_delete(self): report_multipart = { 'language': u'en', 'report_pdf': File(open(self.data_path(u'20150619200628-en.pdf'))), 'report_image': File(open(self.data_path(u'20150619200628-en.png'))), 'report_thumbnail': File(open(self.data_path(u'20150619200628-thumb-en.png'))), } response = self.client.post(reverse( 'realtime:earthquake_report_list', kwargs={'shake_id': u'20150619200628'}), report_multipart, format='multipart') self.assertEqual(response.status_code, status.HTTP_201_CREATED) response = self.client.get( reverse('realtime:earthquake_report_list', kwargs={'shake_id': u'20150619200628'})) self.assertEqual(response.status_code, status.HTTP_200_OK) serializer = PageNumberPaginationSerializer( serializer_class=EarthquakeReportSerializer, data=response.data) self.assertEqual(serializer.count, 2) # delete using REST response = self.client.delete( reverse('realtime:earthquake_report_detail', kwargs={ 'shake_id': u'20150619200628', 'language': u'en' })) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) # check saved objects reports = EarthquakeReport.objects.filter( earthquake__shake_id=u'20150619200628') self.assertEqual(len(reports), 1) self.assertEqual(reports[0].language, u'id')
def test_earthquake_report_list_post(self): report_multipart = { 'language': u'en', 'report_pdf': File( open( self.data_path(u'20150619200628-en.pdf'))), 'report_image': File( open( self.data_path(u'20150619200628-en.png'))), 'report_thumbnail': File( open( self.data_path( u'20150619200628-thumb-en.png'))), } response = self.client.post( reverse( 'realtime:earthquake_report_list', kwargs={ 'shake_id': u'20150619200628', 'source_type': u'initial' } ), report_multipart, format='multipart' ) self.assertEqual(response.status_code, status.HTTP_201_CREATED) # Check get REST API response = self.client.get( reverse( 'realtime:earthquake_report_list', kwargs={ 'shake_id': u'20150619200628', 'source_type': u'initial' } )) self.assertEqual(response.status_code, status.HTTP_200_OK) serializer = PageNumberPaginationSerializer( serializer_class=EarthquakeReportSerializer, data=response.data) self.assertEqual(serializer.count, 2) # Push another product which is corrected shakemap with the same shake # id shake_json = { 'shake_id': u'20150619200628', 'source_type': u'corrected', 'magnitude': 4.6, 'time': u'2015-06-19T12:59:28Z', 'depth': 10.0, 'location': { u'type': u'Point', u'coordinates': [126.52, 4.16] }, 'location_description': u'Manado' } response = self.client.post( reverse('realtime:earthquake_list'), shake_json, format='json' ) self.assertEqual(response.status_code, status.HTTP_201_CREATED) # Push another report for corrected shakemaps report_multipart = { 'language': u'en', 'report_pdf': File( open( self.data_path(u'20150619200628-en.pdf'))), 'report_image': File( open( self.data_path(u'20150619200628-en.png'))), 'report_thumbnail': File( open( self.data_path( u'20150619200628-thumb-en.png'))), } response = self.client.post( reverse( 'realtime:earthquake_report_list', kwargs={ 'shake_id': u'20150619200628', 'source_type': u'corrected' } ), report_multipart, format='multipart' ) self.assertEqual(response.status_code, status.HTTP_201_CREATED) # Check get REST API with only shake_id filter response = self.client.get( reverse( 'realtime:earthquake_report_list', kwargs={ 'shake_id': u'20150619200628' } )) self.assertEqual(response.status_code, status.HTTP_200_OK) serializer = PageNumberPaginationSerializer( serializer_class=EarthquakeReportSerializer, data=response.data) self.assertEqual(serializer.count, 3) # delete data report = EarthquakeReport.objects.get( earthquake__shake_id=u'20150619200628', earthquake__source_type=u'initial', language='en') report.delete() report = EarthquakeReport.objects.get( earthquake__shake_id=u'20150619200628', earthquake__source_type=u'corrected', language='en') report.delete() earthquake = Earthquake.objects.get( shake_id=u'20150619200628', source_type=u'initial') earthquake.delete()
def test_earthquake_list_post(self): # Test pushing corrected shakemaps shake_json = { 'shake_id': u'20150619200628', 'source_type': u'corrected', 'magnitude': 4.6, 'time': u'2015-06-19T12:59:28Z', 'depth': 10.0, 'location': { u'type': u'Point', u'coordinates': [126.52, 4.16] }, 'location_description': u'Manado' } response = self.client.post( reverse('realtime:earthquake_list'), shake_json, format='json' ) self.assertEqual(response.status_code, status.HTTP_201_CREATED) # Earthquake lists without filter response = self.client.get(reverse('realtime:earthquake_list')) serializer = PageNumberPaginationSerializer( serializer_class=EarthquakeSerializer, data=response.data) self.assertEqual(serializer.count, 2) # Earthquake lists with shake_id filter response = self.client.get( reverse('realtime:earthquake_list', kwargs={ 'shake_id': u'20150619200628' })) serializer = PageNumberPaginationSerializer( serializer_class=EarthquakeSerializer, data=response.data) self.assertEqual(serializer.count, 2) # Earthquake lists with shake_id and source_type filters response = self.client.get( reverse('realtime:earthquake_detail', kwargs={ 'shake_id': u'20150619200628', 'source_type': u'corrected' })) self.assertEqual(response.status_code, status.HTTP_200_OK) # Using models filter self.assertEqual( Earthquake.objects.filter( shake_id=u'20150619200628').count(), 2) self.assertEqual( Earthquake.objects.filter( shake_id=u'20150619200628', source_type=u'corrected').count(), 1) earthquake = Earthquake.objects.get( shake_id=u'20150619200628', source_type=u'corrected') serializer = EarthquakeSerializer(earthquake) for key, value in shake_json.iteritems(): if isinstance(serializer.data[key], GeoJsonDict): self.compare_geo_json_dict(value, serializer.data[key]) else: self.assertEqual(value, serializer.data[key]) earthquake.delete()
def test_earthquake_list(self): response = self.client.get(reverse('realtime:earthquake_list')) expected_shake_id = '20150619200628' self.assertEqual(response.status_code, status.HTTP_200_OK) serializer = PageNumberPaginationSerializer( serializer_class=EarthquakeSerializer, data=response.data) actual_results = serializer.results self.assertEqual(serializer.count, 1) self.assertEqual(actual_results[0]['shake_id'], expected_shake_id) # Test basic filters def _check_response(test_instance, query, result_count): """Shortcut to validate query.""" r = test_instance.client.get( reverse('realtime:earthquake_list'), data=query) test_instance.assertEqual(r.status_code, status.HTTP_200_OK) s = PageNumberPaginationSerializer( serializer_class=EarthquakeSerializer, data=r.data) test_instance.assertEqual(s.count, result_count) # Depth filters depth_query = { 'min_depth': 9 } _check_response(self, depth_query, 1) depth_query = { 'min_depth': 11 } _check_response(self, depth_query, 0) depth_query = { 'max_depth': 9 } _check_response(self, depth_query, 0) depth_query = { 'max_depth': 11 } _check_response(self, depth_query, 1) depth_query = { 'min_depth': 9, 'max_depth': 11 } _check_response(self, depth_query, 1) depth_query = { 'min_depth': 11, 'max_depth': 9 } _check_response(self, depth_query, 0) # Magnitude filters magnitude_query = { 'min_magnitude': 4 } _check_response(self, magnitude_query, 1) magnitude_query = { 'min_magnitude': 5 } _check_response(self, magnitude_query, 0) magnitude_query = { 'max_magnitude': 5 } _check_response(self, magnitude_query, 1) magnitude_query = { 'max_magnitude': 4 } _check_response(self, magnitude_query, 0) magnitude_query = { 'min_magnitude': 4, 'max_magnitude': 5 } _check_response(self, magnitude_query, 1) magnitude_query = { 'min_magnitude': 5, 'max_magnitude': 4 } _check_response(self, magnitude_query, 0) # Time based filters time_query = { 'min_time': '2015-06-19' } _check_response(self, time_query, 1) time_query = { 'max_time': '2015-06-19' } _check_response(self, time_query, 0) # manipulate time so we can filter by since_ query previous_time = self.earthquake.time days = 30 hours = days * 24 today = pytz.utc.fromutc(datetime.datetime.utcnow()) new_time = today - datetime.timedelta(days=30) # temporarily change time value, so we can be sure we are filtering # by this value self.earthquake.time = new_time self.earthquake.save() time_query = { 'since_last_days': days + 1 } _check_response(self, time_query, 1) time_query = { 'since_last_days': days - 1 } _check_response(self, time_query, 0) time_query = { 'since_last_hours': hours + 1 } _check_response(self, time_query, 1) time_query = { 'since_last_hours': hours - 1 } _check_response(self, time_query, 0) self.earthquake.time = previous_time self.earthquake.save()