def test_write_uploaded_file(self): with patch('eventkit_cloud.ui.helpers.open', new_callable=mock_open()) as m: test_file = Mock() test_file.chunks = Mock(return_value=['1', '2','3','4','5']) file_path = '/path/to/file.txt' ret = write_uploaded_file(test_file, file_path) self.assertTrue(ret) m.assert_called_once_with(file_path, 'wb+') test_file.chunks.assert_called_once self.assertEqual(m.return_value.__enter__.return_value.write.call_count, 5) m.side_effect = Exception('whoops!') self.assertRaises(Exception, write_uploaded_file, test_file, file_path)
def convert_to_geojson(request): in_memory_file = request.FILES.get("file", None) if not in_memory_file: return HttpResponse("No file supplied in the POST request", status=400) try: output_file = write_uploaded_file(in_memory_file) geojson = file_to_geojson(output_file) return HttpResponse(json.dumps(geojson), content_type="application/json", status=200) except Exception as e: logger.error(e) return HttpResponse(str(e), status=400)
def test_write_uploaded_file(self): with patch('eventkit_cloud.ui.helpers.open', new_callable=mock_open()) as m: test_file = Mock() test_file.chunks = Mock(return_value=['1', '2', '3', '4', '5']) file_path = '/path/to/file.txt' ret = write_uploaded_file(test_file, file_path) self.assertTrue(ret) m.assert_called_once_with(file_path, 'wb+') test_file.chunks.assert_called_once self.assertEqual( m.return_value.__enter__.return_value.write.call_count, 5) m.side_effect = Exception('whoops!') self.assertRaises(Exception, write_uploaded_file, test_file, file_path)
def test_write_uploaded_file(self, mock_uuid): example_uuid = str(uuid4()) mock_uuid.return_value = example_uuid with patch("eventkit_cloud.ui.helpers.open", new_callable=mock_open()) as m: test_file = Mock() test_file.chunks = Mock(return_value=["1", "2", "3", "4", "5"]) test_file_stem_name = "file" test_file_name = f"{test_file_stem_name}.txt" test_file.name = test_file_name file_path = "/path/to/file.txt" ret = write_uploaded_file(test_file) self.assertTrue(ret) m.assert_called_once_with( f"/var/lib/eventkit/exports_stage/{example_uuid}/in_{test_file_stem_name}-{example_uuid}.txt", "wb+" ) test_file.chunks.assert_called_once self.assertEqual(m.return_value.__enter__.return_value.write.call_count, 5) m.side_effect = Exception("whoops!") self.assertRaises(Exception, write_uploaded_file, test_file, file_path)