def test_kml_upload_form_with_with_valid_data(self): """ Test the import kml form. This test uses a sample kmz file with one placemark. This code based on stack overflow question at http://stackoverflow.com/questions/7304248/writing-tests-for-forms-in-django :return: """ upload_file = open('mlp/fixtures/MLP_test.kmz', 'rb') post_dict = {} file_dict = { 'kmlfileUpload': SimpleUploadedFile(upload_file.name, upload_file.read()) } upload_file.close() form = UploadKMLForm(post_dict, file_dict) self.assertTrue(form.is_valid()) # get current number of occurrence records in DB and verify count occurrence_starting_record_count = Occurrence.objects.count() self.assertEqual(occurrence_starting_record_count, 87) # follow redirect to confirmation page response = self.client.post('/mlp/upload/', file_dict, follow=True) # test redirect to confirmation page self.assertRedirects(response, '/mlp/confirmation/') self.assertEqual(response.status_code, 200) self.assertContains( response, 'upload was successful!') # test message in conf page # test that new occurence was added to DB self.assertEqual(Occurrence.objects.count(), occurrence_starting_record_count + 1)
def test_kml_upload_form_with_with_valid_data_no_login(self): """ Test the import kml form. This test uses a sample kmz file with one placemark. This code based on stack overflow question at http://stackoverflow.com/questions/7304248/writing-tests-for-forms-in-django :return: """ upload_file = open('mlp/fixtures/MLP_test.kmz', 'rb') post_dict = {} file_dict = {'kmlfileUpload': SimpleUploadedFile(upload_file.name, upload_file.read())} upload_file.close() form = UploadKMLForm(post_dict, file_dict) self.assertTrue(form.is_valid()) # get current number of occurrence records in DB and verify count occurrence_starting_record_count = Occurrence.objects.count() # follow redirect to login page response = self.client.post('/projects/mlp/upload/', file_dict, follow=True) self.assertEqual(response.status_code, 200) self.assertRedirects(response, 'login/?next=/projects/mlp/upload/') # test we hit the login page self.assertContains(response, 'login') # test we hit the login page # test that new occurrence was added to DB self.assertEqual(Occurrence.objects.count(), occurrence_starting_record_count) # test nothing added to DB
def test_kml_upload_form_with_with_valid_data_and_login(self): """ Test the import kml form. This test uses a sample kmz file with one placemark. This code based on stack overflow question at http://stackoverflow.com/questions/7304248/writing-tests-for-forms-in-django :return: """ upload_file = open('mlp/fixtures/MLP_test.kmz', 'rb') post_dict = {} file_dict = {'kmlfileUpload': SimpleUploadedFile(upload_file.name, upload_file.read())} upload_file.close() form = UploadKMLForm(post_dict, file_dict) self.assertTrue(form.is_valid()) # get current number of occurrence records in DB and verify count occurrence_starting_record_count = Occurrence.objects.count() # login and test redirect to confirmation page self.client.login(username='******', password='******') # login response = self.client.post('/projects/mlp/upload/', file_dict, follow=True) self.assertRedirects(response, '/projects/mlp/confirmation/') self.assertEqual(response.status_code, 200) self.assertContains(response, 'file upload was successful!') # test message in conf page # test that new occurrence was added to DB self.assertEqual(Occurrence.objects.count(), occurrence_starting_record_count+1) # test one record added
def test_kml_form_with_no_data(self): # get starting count of records in DB occurrence_starting_record_count = Occurrence.objects.count() # create an empty form post_dict = {} file_dict = {} form = UploadKMLForm(post_dict, file_dict) # test that form is not valid with empty data self.assertFalse(form.is_valid()) # get the post response and test page reload and error message response = self.client.post(reverse('projects:mlp:mlp_upload_kml'), file_dict, follow=True) self.assertEqual(response.status_code, 200) # test reload self.assertContains(response, 'Upload a') self.assertContains(response, 'This field is required') # test nothing is saved to DB self.assertEqual(Occurrence.objects.count(), occurrence_starting_record_count)
def test_kml_form_with_no_data(self): # get starting count of records in DB occurrence_starting_record_count = Occurrence.objects.count() self.assertEqual(occurrence_starting_record_count, 87) # create an empty form post_dict = {} file_dict = {} form = UploadKMLForm(post_dict, file_dict) # test that form is not valid with empty data self.assertFalse(form.is_valid()) # get the post response and test page reload and error message response = self.client.post(reverse('mlp:mlp_upload_kml'), file_dict, follow=True) self.assertEqual(response.status_code, 200) # test reload self.assertContains(response, 'Upload a') self.assertContains(response, 'This field is required') # test nothing is saved to DB self.assertEqual(Occurrence.objects.count(), occurrence_starting_record_count)