def test_add_property_to_stand(self): prop1 = ForestProperty(user=self.user, name="My Property", geometry_final=p1) prop1.save() self.stand1.add_to_collection(prop1) self.assertEqual(self.stand1.collection, prop1) self.assertTrue(self.stand1 in prop1.feature_set()) self.stand1.remove_from_collection() self.assertEqual(self.stand1.collection, None) self.assertTrue(self.stand1 not in prop1.feature_set())
class StandImportTest(TestCase): def setUp(self): import_rasters() d = os.path.dirname(__file__) self.shp_path = os.path.abspath(os.path.join(d, "..", "fixtures", "testdata", "test_stands.shp")) self.bad_shp_path = os.path.abspath(os.path.join(d, "..", "fixtures", "testdata", "test_stands_bad.shp")) self.condid_shp_path = os.path.abspath(os.path.join(d, "..", "fixtures", "testdata", "test_stands_condid.shp")) self.client = Client() self.user = User.objects.create_user("featuretest", "*****@*****.**", password="******") self.prop1 = ForestProperty(user=self.user, name="My Property", geometry_final=p1) self.prop1.save() def test_shps_exists(self): self.assertTrue(os.path.exists(self.shp_path), self.shp_path) self.assertTrue(os.path.exists(self.bad_shp_path), self.bad_shp_path) self.assertTrue(os.path.exists(self.condid_shp_path), self.condid_shp_path) def test_importer_py(self): self.assertEqual(len(Stand.objects.all()), 0) self.assertEqual(len(self.prop1.feature_set()), 0) s = StandImporter(self.user) s.import_ogr(self.shp_path, forest_property=self.prop1) self.assertEqual(len(Stand.objects.all()), 37) # from the default 'name' field this time self.assertEqual(len(Stand.objects.filter(name="001A")), 0) self.assertEqual(len(Stand.objects.filter(name="277")), 1) self.assertEqual(len(self.prop1.feature_set()), 37) def test_importer_py_newproperty(self): self.assertEqual(len(Stand.objects.all()), 0) self.assertEqual(len(self.prop1.feature_set()), 0) s = StandImporter(self.user) s.import_ogr(self.shp_path, new_property_name="Another Property") self.assertEqual(len(Stand.objects.all()), 37) # from the default 'name' field this time self.assertEqual(len(Stand.objects.filter(name="001A")), 0) self.assertEqual(len(Stand.objects.filter(name="277")), 1) self.assertEqual(len(self.prop1.feature_set()), 0) new_stand = ForestProperty.objects.get(name="Another Property") self.assertEqual(len(new_stand.feature_set()), 37) def test_importer_py_fieldmap(self): self.assertEqual(len(Stand.objects.all()), 0) self.assertEqual(len(self.prop1.feature_set()), 0) s = StandImporter(self.user) field_mapping = {"name": "STAND_TEXT"} s.import_ogr(self.shp_path, field_mapping, forest_property=self.prop1) self.assertEqual(len(Stand.objects.all()), 37) # from the 'STAND_TEXT' field this time self.assertEqual(len(Stand.objects.filter(name="001A")), 1) self.assertEqual(len(Stand.objects.filter(name="277")), 0) self.assertEqual(len(self.prop1.feature_set()), 37) def test_importer_multi(self): """ Test for handling of multipart polygons """ d = os.path.dirname(__file__) multi_path = os.path.abspath(os.path.join(d, "..", "fixtures", "testdata", "test_stands_multi.zip")) url = reverse("trees-upload_stands") self.client.login(username="******", password="******") with open(multi_path) as f: response = self.client.post(url, {"new_property_name": "Test Multi", "ogrfile": f}) self.assertEqual(response.status_code, 201, response.content) self.assertEqual(len(Stand.objects.all()), 27) prop = ForestProperty.objects.get(name="Test Multi") self.assertEqual(len(prop.geometry_final), 2) def test_importer_holes(self): """ Test for handling of slivers """ d = os.path.dirname(__file__) holes_path = os.path.abspath(os.path.join(d, "..", "fixtures", "testdata", "test_stands_holes.zip")) url = reverse("trees-upload_stands") self.client.login(username="******", password="******") # With default threshold, the "sliver" should not show up with open(holes_path) as f: response = self.client.post(url, {"new_property_name": "holes", "ogrfile": f}) self.assertEqual(response.status_code, 201, response.content) self.assertEqual(len(Stand.objects.all()), 35) prop = ForestProperty.objects.get(name="holes") self.assertEqual(prop.geometry_final[0].num_interior_rings, 1) # Now try it with a tighter tolerance, the "sliver" should show up as another interior ring Stand.objects.all().delete() settings.SLIVER_THRESHOLD = 1.0 with open(holes_path) as f: response = self.client.post(url, {"new_property_name": "holes2", "ogrfile": f}) self.assertEqual(response.status_code, 201, response.content) self.assertEqual(len(Stand.objects.all()), 35) prop2 = ForestProperty.objects.get(name="holes2") self.assertEqual(prop2.geometry_final[0].num_interior_rings, 2) def test_importer_http(self): self.client.login(username="******", password="******") self.assertEqual(len(self.prop1.feature_set()), 0) d = os.path.dirname(__file__) ogr_path = os.path.abspath(os.path.join(d, "..", "fixtures", "testdata", "test_stands.zip")) f = open(ogr_path) url = reverse("trees-upload_stands") response = self.client.post(url, {"property_pk": self.prop1.pk, "ogrfile": f}) f.close() self.assertEqual(response.status_code, 201, response.content) self.assertNotEqual(response.content.find("X-Madrona-Select"), -1, response.content) self.assertEqual(len(self.prop1.feature_set()), 37) def test_importer_http_unauth(self): self.assertEqual(len(self.prop1.feature_set()), 0) d = os.path.dirname(__file__) ogr_path = os.path.abspath(os.path.join(d, "..", "fixtures", "testdata", "test_stands.zip")) f = open(ogr_path) url = reverse("trees-upload_stands") response = self.client.post(url, {"property_pk": self.prop1.pk, "ogrfile": f}) f.close() self.assertEqual(response.status_code, 401) self.assertEqual(len(self.prop1.feature_set()), 0) def test_importer_http_noname(self): self.client.login(username="******", password="******") self.assertEqual(len(self.prop1.feature_set()), 0) d = os.path.dirname(__file__) ogr_path = os.path.abspath(os.path.join(d, "..", "fixtures", "testdata", "test_stands_noname.zip")) f = open(ogr_path) url = reverse("trees-upload_stands") response = self.client.post(url, {"property_pk": self.prop1.pk, "ogrfile": f}) f.close() self.assertEqual(response.status_code, 201, response.content) self.assertNotEqual(response.content.find("X-Madrona-Select"), -1, response.content) self.assertEqual(len(self.prop1.feature_set()), 37) def test_importer_http_badproperty(self): """ If no property is found belonging to the user, should get a 404 """ self.client.login(username="******", password="******") self.assertEqual(len(self.prop1.feature_set()), 0) d = os.path.dirname(__file__) ogr_path = os.path.abspath(os.path.join(d, "..", "fixtures", "testdata", "test_stands_bad.zip")) f = open(ogr_path) url = reverse("trees-upload_stands") response = self.client.post(url, {"property_pk": 8675309, "ogrfile": f}) f.close() self.assertEqual(response.status_code, 404, response.content) self.assertEqual(len(self.prop1.feature_set()), 0) def test_importer_http_newproperty(self): """ If no property is found belonging to the user, should get a 404 """ self.client.login(username="******", password="******") self.assertEqual(len(self.prop1.feature_set()), 0) d = os.path.dirname(__file__) ogr_path = os.path.abspath(os.path.join(d, "..", "fixtures", "testdata", "test_stands.zip")) f = open(ogr_path) url = reverse("trees-upload_stands") response = self.client.post(url, {"new_property_name": "Another Property", "ogrfile": f}) f.close() self.assertEqual(response.status_code, 201, response.content) self.assertNotEqual(response.content.find("X-Madrona-Select"), -1, response.content) self.assertEqual(len(self.prop1.feature_set()), 0) new_stand = ForestProperty.objects.get(name="Another Property") self.assertEqual(len(new_stand.feature_set()), 37)
class StandImportTest(TestCase): def setUp(self): import_rasters() d = os.path.dirname(__file__) self.shp_path = os.path.abspath(os.path.join(d, '..', 'fixtures', 'testdata', 'test_stands.shp')) self.bad_shp_path = os.path.abspath(os.path.join(d, '..', 'fixtures', 'testdata', 'test_stands_bad.shp')) self.condid_shp_path = os.path.abspath(os.path.join(d, '..', 'fixtures', 'testdata', 'test_stands_condid.shp')) self.client = Client() self.user = User.objects.create_user( 'featuretest', '*****@*****.**', password='******') self.prop1 = ForestProperty(user=self.user, name="My Property", geometry_final=p1) self.prop1.save() def test_shps_exists(self): self.assertTrue(os.path.exists(self.shp_path), self.shp_path) self.assertTrue(os.path.exists(self.bad_shp_path), self.bad_shp_path) self.assertTrue(os.path.exists(self.condid_shp_path), self.condid_shp_path) def test_importer_py(self): self.assertEqual(len(Stand.objects.all()), 0) self.assertEqual(len(self.prop1.feature_set()), 0) s = StandImporter(self.user) s.import_ogr(self.shp_path, forest_property=self.prop1) self.assertEqual(len(Stand.objects.all()), 37) # from the default 'name' field this time self.assertEqual(len(Stand.objects.filter(name='001A')), 0) self.assertEqual(len(Stand.objects.filter(name='277')), 1) self.assertEqual(len(self.prop1.feature_set()), 37) def test_importer_py_newproperty(self): self.assertEqual(len(Stand.objects.all()), 0) self.assertEqual(len(self.prop1.feature_set()), 0) s = StandImporter(self.user) s.import_ogr(self.shp_path, new_property_name="Another Property") self.assertEqual(len(Stand.objects.all()), 37) # from the default 'name' field this time self.assertEqual(len(Stand.objects.filter(name='001A')), 0) self.assertEqual(len(Stand.objects.filter(name='277')), 1) self.assertEqual(len(self.prop1.feature_set()), 0) new_stand = ForestProperty.objects.get(name="Another Property") self.assertEqual(len(new_stand.feature_set()), 37) def test_importer_py_fieldmap(self): self.assertEqual(len(Stand.objects.all()), 0) self.assertEqual(len(self.prop1.feature_set()), 0) s = StandImporter(self.user) field_mapping = {'name': 'STAND_TEXT'} s.import_ogr(self.shp_path, field_mapping, forest_property=self.prop1) self.assertEqual(len(Stand.objects.all()), 37) # from the 'STAND_TEXT' field this time self.assertEqual(len(Stand.objects.filter(name='001A')), 1) self.assertEqual(len(Stand.objects.filter(name='277')), 0) self.assertEqual(len(self.prop1.feature_set()), 37) def test_importer_multi(self): ''' Test for handling of multipart polygons ''' d = os.path.dirname(__file__) multi_path = os.path.abspath(os.path.join(d, '..', 'fixtures', 'testdata', 'test_stands_multi.zip')) url = reverse('trees-upload_stands') self.client.login(username='******', password='******') with open(multi_path) as f: response = self.client.post(url, {'new_property_name': 'Test Multi', 'ogrfile': f}) self.assertEqual(response.status_code, 201, response.content) self.assertEqual(len(Stand.objects.all()), 27) prop = ForestProperty.objects.get(name="Test Multi") self.assertEqual(len(prop.geometry_final), 2) def test_importer_holes(self): ''' Test for handling of slivers ''' d = os.path.dirname(__file__) holes_path = os.path.abspath(os.path.join(d, '..', 'fixtures', 'testdata', 'test_stands_holes.zip')) url = reverse('trees-upload_stands') self.client.login(username='******', password='******') # With default threshold, the "sliver" should not show up with open(holes_path) as f: response = self.client.post(url, {'new_property_name': 'holes', 'ogrfile': f}) self.assertEqual(response.status_code, 201, response.content) self.assertEqual(len(Stand.objects.all()), 35) prop = ForestProperty.objects.get(name="holes") self.assertEqual(prop.geometry_final[0].num_interior_rings, 1) # Now try it with a tighter tolerance, the "sliver" should show up as another interior ring Stand.objects.all().delete() settings.SLIVER_THRESHOLD = 1.0 with open(holes_path) as f: response = self.client.post(url, {'new_property_name': 'holes2', 'ogrfile': f}) self.assertEqual(response.status_code, 201, response.content) self.assertEqual(len(Stand.objects.all()), 35) prop2 = ForestProperty.objects.get(name="holes2") self.assertEqual(prop2.geometry_final[0].num_interior_rings, 2) def test_importer_http(self): self.client.login(username='******', password='******') self.assertEqual(len(self.prop1.feature_set()), 0) d = os.path.dirname(__file__) ogr_path = os.path.abspath(os.path.join(d, '..', 'fixtures', 'testdata', 'test_stands.zip')) f = open(ogr_path) url = reverse('trees-upload_stands') response = self.client.post(url, {'property_pk': self.prop1.pk, 'ogrfile': f}) f.close() self.assertEqual(response.status_code, 201, response.content) self.assertNotEqual(response.content.find('X-Madrona-Select'), -1, response.content) self.assertEqual(len(self.prop1.feature_set()), 37) def test_importer_http_unauth(self): self.assertEqual(len(self.prop1.feature_set()), 0) d = os.path.dirname(__file__) ogr_path = os.path.abspath(os.path.join(d, '..', 'fixtures', 'testdata', 'test_stands.zip')) f = open(ogr_path) url = reverse('trees-upload_stands') response = self.client.post(url, {'property_pk': self.prop1.pk, 'ogrfile': f}) f.close() self.assertEqual(response.status_code, 401) self.assertEqual(len(self.prop1.feature_set()), 0) def test_importer_http_noname(self): self.client.login(username='******', password='******') self.assertEqual(len(self.prop1.feature_set()), 0) d = os.path.dirname(__file__) ogr_path = os.path.abspath(os.path.join(d, '..', 'fixtures', 'testdata', 'test_stands_noname.zip')) f = open(ogr_path) url = reverse('trees-upload_stands') response = self.client.post(url, {'property_pk': self.prop1.pk, 'ogrfile': f}) f.close() self.assertEqual(response.status_code, 201, response.content) self.assertNotEqual(response.content.find('X-Madrona-Select'), -1, response.content) self.assertEqual(len(self.prop1.feature_set()), 37) def test_importer_http_badproperty(self): ''' If no property is found belonging to the user, should get a 404 ''' self.client.login(username='******', password='******') self.assertEqual(len(self.prop1.feature_set()), 0) d = os.path.dirname(__file__) ogr_path = os.path.abspath(os.path.join(d, '..', 'fixtures', 'testdata', 'test_stands_bad.zip')) f = open(ogr_path) url = reverse('trees-upload_stands') response = self.client.post(url, {'property_pk': 8675309, 'ogrfile': f}) f.close() self.assertEqual(response.status_code, 404, response.content) self.assertEqual(len(self.prop1.feature_set()), 0) def test_importer_http_newproperty(self): ''' If no property is found belonging to the user, should get a 404 ''' self.client.login(username='******', password='******') self.assertEqual(len(self.prop1.feature_set()), 0) d = os.path.dirname(__file__) ogr_path = os.path.abspath(os.path.join(d, '..', 'fixtures', 'testdata', 'test_stands.zip')) f = open(ogr_path) url = reverse('trees-upload_stands') response = self.client.post(url, {'new_property_name': 'Another Property', 'ogrfile': f}) f.close() self.assertEqual(response.status_code, 201, response.content) self.assertNotEqual(response.content.find('X-Madrona-Select'), -1, response.content) self.assertEqual(len(self.prop1.feature_set()), 0) new_stand = ForestProperty.objects.get(name="Another Property") self.assertEqual(len(new_stand.feature_set()), 37)
class StandImportTest(TestCase): ''' TODO test bad shapefiles (other geom types, bad mapping dict, projection) TODO assert that mapped attributes are populated TODO strata from shp ''' def setUp(self): import_rasters() d = os.path.dirname(__file__) self.shp_path = os.path.abspath(os.path.join(d, '..', 'fixtures', 'testdata', 'test_stands.shp')) self.bad_shp_path = os.path.abspath(os.path.join(d, '..', 'fixtures', 'testdata', 'test_stands_bad.shp')) self.client = Client() self.user = User.objects.create_user( 'featuretest', '*****@*****.**', password='******') self.prop1 = ForestProperty(user=self.user, name="My Property", geometry_final=p1) self.prop1.save() def test_shp_exists(self): self.assertTrue(os.path.exists(self.shp_path), self.shp_path) def test_importer_py(self): self.assertEqual(len(Stand.objects.all()), 0) self.assertEqual(len(self.prop1.feature_set()), 0) s = StandImporter(self.user) s.import_ogr(self.shp_path, forest_property=self.prop1) self.assertEqual(len(Stand.objects.all()), 37) # from the default 'name' field this time self.assertEqual(len(Stand.objects.filter(name='001A')), 0) self.assertEqual(len(Stand.objects.filter(name='277')), 1) self.assertEqual(len(self.prop1.feature_set()), 37) def test_importer_py_newproperty(self): self.assertEqual(len(Stand.objects.all()), 0) self.assertEqual(len(self.prop1.feature_set()), 0) s = StandImporter(self.user) s.import_ogr(self.shp_path, new_property_name="Another Property") self.assertEqual(len(Stand.objects.all()), 37) # from the default 'name' field this time self.assertEqual(len(Stand.objects.filter(name='001A')), 0) self.assertEqual(len(Stand.objects.filter(name='277')), 1) self.assertEqual(len(self.prop1.feature_set()), 0) new_stand = ForestProperty.objects.get(name="Another Property") self.assertEqual(len(new_stand.feature_set()), 37) def test_importer_py_fieldmap(self): self.assertEqual(len(Stand.objects.all()), 0) self.assertEqual(len(self.prop1.feature_set()), 0) s = StandImporter(self.user) field_mapping = {'name': 'STAND_TEXT'} s.import_ogr(self.shp_path, field_mapping, forest_property=self.prop1) self.assertEqual(len(Stand.objects.all()), 37) # from the 'STAND_TEXT' field this time self.assertEqual(len(Stand.objects.filter(name='001A')), 1) self.assertEqual(len(Stand.objects.filter(name='277')), 0) self.assertEqual(len(self.prop1.feature_set()), 37) def test_importer_multi(self): ''' Test for handling of multipart polygons ''' d = os.path.dirname(__file__) multi_path = os.path.abspath(os.path.join(d, '..', 'fixtures', 'testdata', 'test_stands_multi.zip')) url = reverse('trees-upload_stands') self.client.login(username='******', password='******') with open(multi_path) as f: response = self.client.post(url, {'new_property_name': 'Test Multi', 'ogrfile': f}) self.assertEqual(response.status_code, 201, response.content) self.assertEqual(len(Stand.objects.all()), 27) prop = ForestProperty.objects.get(name="Test Multi") self.assertEqual(len(prop.geometry_final), 2) def test_importer_holes(self): ''' Test for handling of slivers ''' d = os.path.dirname(__file__) holes_path = os.path.abspath(os.path.join(d, '..', 'fixtures', 'testdata', 'test_stands_holes.zip')) url = reverse('trees-upload_stands') self.client.login(username='******', password='******') # With default threshold, the "sliver" should not show up with open(holes_path) as f: response = self.client.post(url, {'new_property_name': 'holes', 'ogrfile': f}) self.assertEqual(response.status_code, 201, response.content) self.assertEqual(len(Stand.objects.all()), 35) prop = ForestProperty.objects.get(name="holes") self.assertEqual(prop.geometry_final[0].num_interior_rings, 1) # Now try it with a tighter tolerance, the "sliver" should show up as another interior ring Stand.objects.all().delete() settings.SLIVER_THRESHOLD = 1.0 with open(holes_path) as f: response = self.client.post(url, {'new_property_name': 'holes2', 'ogrfile': f}) self.assertEqual(response.status_code, 201, response.content) self.assertEqual(len(Stand.objects.all()), 35) prop2 = ForestProperty.objects.get(name="holes2") self.assertEqual(prop2.geometry_final[0].num_interior_rings, 2) def test_importer_http(self): self.client.login(username='******', password='******') self.assertEqual(len(self.prop1.feature_set()), 0) d = os.path.dirname(__file__) ogr_path = os.path.abspath(os.path.join(d, '..', 'fixtures', 'testdata', 'test_stands.zip')) f = open(ogr_path) url = reverse('trees-upload_stands') response = self.client.post(url, {'property_pk': self.prop1.pk, 'ogrfile': f}) f.close() self.assertEqual(response.status_code, 201, response.content) self.assertNotEqual(response.content.find('X-Madrona-Select'), -1, response.content) self.assertEqual(len(self.prop1.feature_set()), 37) def test_importer_http_unauth(self): self.assertEqual(len(self.prop1.feature_set()), 0) d = os.path.dirname(__file__) ogr_path = os.path.abspath(os.path.join(d, '..', 'fixtures', 'testdata', 'test_stands.zip')) f = open(ogr_path) url = reverse('trees-upload_stands') response = self.client.post(url, {'property_pk': self.prop1.pk, 'ogrfile': f}) f.close() self.assertEqual(response.status_code, 401) self.assertEqual(len(self.prop1.feature_set()), 0) def test_importer_http_noname(self): self.client.login(username='******', password='******') self.assertEqual(len(self.prop1.feature_set()), 0) d = os.path.dirname(__file__) ogr_path = os.path.abspath(os.path.join(d, '..', 'fixtures', 'testdata', 'test_stands_noname.zip')) f = open(ogr_path) url = reverse('trees-upload_stands') response = self.client.post(url, {'property_pk': self.prop1.pk, 'ogrfile': f}) f.close() self.assertEqual(response.status_code, 201, response.content) self.assertNotEqual(response.content.find('X-Madrona-Select'), -1, response.content) self.assertEqual(len(self.prop1.feature_set()), 37) def test_importer_http_badproperty(self): ''' If no property is found belonging to the user, should get a 404 ''' self.client.login(username='******', password='******') self.assertEqual(len(self.prop1.feature_set()), 0) d = os.path.dirname(__file__) ogr_path = os.path.abspath(os.path.join(d, '..', 'fixtures', 'testdata', 'test_stands_bad.zip')) f = open(ogr_path) url = reverse('trees-upload_stands') response = self.client.post(url, {'property_pk': 8675309, 'ogrfile': f}) f.close() self.assertEqual(response.status_code, 404, response.content) self.assertEqual(len(self.prop1.feature_set()), 0) def test_importer_http_newproperty(self): ''' If no property is found belonging to the user, should get a 404 ''' self.client.login(username='******', password='******') self.assertEqual(len(self.prop1.feature_set()), 0) d = os.path.dirname(__file__) ogr_path = os.path.abspath(os.path.join(d, '..', 'fixtures', 'testdata', 'test_stands.zip')) f = open(ogr_path) url = reverse('trees-upload_stands') response = self.client.post(url, {'new_property_name': 'Another Property', 'ogrfile': f}) f.close() self.assertEqual(response.status_code, 201, response.content) self.assertNotEqual(response.content.find('X-Madrona-Select'), -1, response.content) self.assertEqual(len(self.prop1.feature_set()), 0) new_stand = ForestProperty.objects.get(name="Another Property") self.assertEqual(len(new_stand.feature_set()), 37)