class PropertyStandListTest(TestCase): """ test web service to grab json of user's propertie's stands [{stand-attrs}, ...] """ def setUp(self): self.client = Client() import_rasters() self.user = User.objects.create_user("featuretest", "*****@*****.**", password="******") self.baduser = User.objects.create_user("baduser", "*****@*****.**", password="******") self.stand1 = Stand(user=self.user, name="My Stand", geometry_orig=g1) self.stand1.save() self.stand2 = Stand(user=self.user, name="My Stand2", geometry_orig=g1) self.stand2.save() self.prop1 = ForestProperty(user=self.user, name="My Property", geometry_final=p1) self.prop1.save() self.prop1.add(self.stand1) enable_sharing() def test_unauth(self): link = self.prop1.options.get_link("Property Stands GeoJSON") url = link.reverse(self.prop1) response = self.client.get(url) self.assertEqual(response.status_code, 401) def test_notexists(self): self.client.login(username="******", password="******") link = self.prop1.options.get_link("Property Stands GeoJSON") url = link.reverse(self.prop1) url = url.replace(self.prop1.uid, "trees_forestproperty_123456789") response = self.client.get(url) self.assertEqual(response.status_code, 404) def test_baduser(self): self.client.login(username="******", password="******") link = self.prop1.options.get_link("Property Stands GeoJSON") url = link.reverse(self.prop1) response = self.client.get(url) self.assertEqual(response.status_code, 403) def test_jsonlist(self): self.client.login(username="******", password="******") link = self.prop1.options.get_link("Property Stands GeoJSON") url = link.reverse(self.prop1) response = self.client.get(url) self.assertEqual(response.status_code, 200) stand_list = loads(response.content) self.assertEquals(stand_list["features"][0]["properties"]["name"], "My Stand", stand_list) self.prop1.add(self.stand2) response = self.client.get(url) self.assertEqual(response.status_code, 200) stand_list = loads(response.content) names = [stand["properties"]["name"] for stand in stand_list["features"]] names.sort() expected_names = ["My Stand", "My Stand2"] expected_names.sort() self.assertEqual(names, expected_names)
def test_add_stand_to_property(self): prop1 = ForestProperty(user=self.user, name="My Property", geometry_final=p1) prop1.save() prop1.add(self.stand1) self.assertEqual(self.stand1.collection, prop1) self.assertTrue(self.stand1 in prop1.feature_set()) prop1.remove(self.stand1) self.assertEqual(self.stand1.collection, None) self.assertTrue(self.stand1 not in prop1.feature_set())
class ScenarioTest(TestCase): def setUp(self): self.client = Client() import_rasters() self.user = User.objects.create_user("featuretest", "*****@*****.**", password="******") self.stand1 = Stand(user=self.user, name="My Stand 1", geometry_orig=g1) self.stand1.save() self.stand2 = Stand(user=self.user, name="My Stand 2", geometry_orig=g1) self.stand2.save() self.stand3 = Stand(user=self.user, name="My Stand 3 (not on property)", geometry_orig=g1) self.stand3.save() self.prop1 = ForestProperty(user=self.user, name="My Property", geometry_final=p1) self.prop1.save() self.prop1.add(self.stand1) self.prop1.add(self.stand2) self.options = Scenario.get_options() self.create_url = self.options.get_create_form() self.rx1 = Rx.objects.get(internal_name=self.prop1.variant.code + "1").id self.rx2 = Rx.objects.get(internal_name=self.prop1.variant.code + "2").id enable_sharing() def test_create_scenario(self): s1 = Scenario( user=self.user, name="My Scenario", input_target_boardfeet=2000, input_target_carbon=1, input_property=self.prop1, input_rxs={self.stand1.pk: self.rx1, self.stand2.pk: self.rx2}, ) s1.save() self.assertEquals(Scenario.objects.get(name="My Scenario").input_target_boardfeet, 2000.0) def test_scenario_results(self): s1 = Scenario( user=self.user, name="My Scenario", input_target_boardfeet=2000, input_target_carbon=1, input_property=self.prop1, input_rxs={self.stand1.pk: self.rx1, self.stand2.pk: self.rx2}, ) s1.save() out = s1.output_property_metrics self.assertTrue(out.has_key("__all__")) # TODO out = s1.output_stand_metrics def test_post(self): self.client.login(username="******", password="******") response = self.client.post( self.create_url, { "name": "My Scenario", "input_target_boardfeet": 2000, "input_target_carbon": 1, "input_age_class": 1, "input_site_diversity": 1, "input_property": self.prop1.pk, "input_rxs": dumps({self.stand1.pk: self.rx1, self.stand2.pk: self.rx2}), }, ) self.assertEqual(response.status_code, 201) def test_post_invalid_rx(self): self.client.login(username="******", password="******") response = self.client.post( self.create_url, { "name": "My Scenario", "input_target_boardfeet": 2000, "input_target_carbon": 1, "input_age_class": 1, "input_site_diversity": 1, "input_property": self.prop1.pk, "input_rxs": dumps({self.stand1.pk: 9879898, self.stand2.pk: self.rx2}), }, ) self.assertEqual(response.status_code, 400, response.content) def test_post_invalid_stand(self): self.client.login(username="******", password="******") response = self.client.post( self.create_url, { "name": "My Scenario", "input_target_boardfeet": 2000, "input_target_carbon": 1, "input_age_class": 1, "input_site_diversity": 1, "input_property": self.prop1.pk, "input_rxs": dumps({self.stand3.pk: self.rx1, self.stand2.pk: self.rx2}), }, ) self.assertEqual(response.status_code, 400, response.content) def test_json_results(self): s1 = Scenario( user=self.user, name="My Scenario", input_target_boardfeet=2000, input_target_carbon=1, input_property=self.prop1, input_rxs={self.stand1.pk: self.rx1, self.stand2.pk: self.rx2}, ) s1.save() geojson_link = ForestProperty.get_options().get_link("Property Scenarios") url = geojson_link.reverse(self.prop1) # not logged in yet response = self.client.get(url) self.assertEqual(response.status_code, 401, response.content) # now we log in self.client.login(username="******", password="******") response = self.client.get(url) self.assertEqual(response.status_code, 200, response.content)
class SpatialTest(TestCase): """ Tests the spatial representations of Stands and ForestProperties """ def setUp(self): self.client = Client() import_rasters() self.user = User.objects.create_user("featuretest", "*****@*****.**", password="******") self.stand1 = Stand(user=self.user, name="My Stand", geometry_orig=g1) self.stand1.save() self.stand2 = Stand(user=self.user, name="My Stand2", geometry_orig=g1) self.stand2.save() self.prop1 = ForestProperty(user=self.user, name="My Property", geometry_final=p1) self.prop1.save() self.prop1.add(self.stand1) def test_rest_defaultkml_url(self): self.client.login(username="******", password="******") link = self.stand1.options.get_link("KML") url = link.reverse(self.stand1) response = self.client.get(url) errors = kml_errors(response.content) self.assertFalse(errors, "invalid KML %s" % str(errors)) def test_stand_json(self): thejson = self.stand1.geojson() d = loads(thejson) self.assertEquals(d["properties"]["name"], "My Stand") def test_property_json(self): thejson = self.prop1.feature_set_geojson() d = loads(thejson) self.assertEquals(len(d["features"]), 1) self.assertEquals(d["features"][0]["properties"]["name"], "My Stand") self.prop1.add(self.stand2) thejson = self.prop1.feature_set_geojson() d = loads(thejson) self.assertEquals(len(d["features"]), 2) def test_property_bbox(self): thejson = self.prop1.feature_set_geojson() d = loads(thejson) for x, y in zip(d["bbox"], self.prop1.bbox): self.assertAlmostEquals(x, y, places=5) def test_stand_json_url(self): self.client.login(username="******", password="******") link = self.stand1.options.get_link("GeoJSON") url = link.reverse(self.stand1) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertTrue("application/json" in response["Content-Type"]) d = loads(response.content) self.assertEquals(d["features"][0]["properties"]["name"], "My Stand") def test_property_json_url(self): self.client.login(username="******", password="******") link = self.stand1.options.get_link("GeoJSON") url = link.reverse(self.prop1) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertTrue("application/json" in response["Content-Type"]) d = loads(response.content) self.assertEquals(d["features"][0]["properties"]["name"], "My Property") def test_multistand_json_url(self): self.client.login(username="******", password="******") uids = [self.stand1.uid, self.stand2.uid] link = Stand.get_options().get_link("GeoJSON") url = link.reverse([self.stand1, self.stand2]) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertTrue("application/json" in response["Content-Type"]) d = loads(response.content) self.assertEquals(len(d["features"]), 2) foundit = False for f in d["features"]: if f["properties"]["name"] == "My Stand2": foundit = True self.assertTrue(foundit) def test_property_stands_json_url(self): self.prop1.add(self.stand2) self.client.login(username="******", password="******") link = self.prop1.options.get_link("Property Stands GeoJSON") url = link.reverse(self.prop1) response = self.client.get(url) self.assertEqual(response.status_code, 200, response) self.assertTrue("application/json" in response["Content-Type"]) d = loads(response.content) self.assertEquals(len(d["features"]), 2) foundit = False for f in d["features"]: if f["properties"]["name"] == "My Stand2": foundit = True self.assertTrue(foundit)
class ScenarioTest(TestCase): def setUp(self): self.client = Client() import_rasters() self.user = User.objects.create_user( 'featuretest', '*****@*****.**', password='******') self.stand1 = Stand(user=self.user, name="My Stand 1", geometry_orig=g1) self.stand1.save() self.stand2 = Stand(user=self.user, name="My Stand 2", geometry_orig=g1) self.stand2.save() self.stand3 = Stand(user=self.user, name="My Stand 3 (not on property)", geometry_orig=g1) self.stand3.save() self.prop1 = ForestProperty(user=self.user, name="My Property", geometry_final=p1) self.prop1.save() self.prop1.add(self.stand1) self.prop1.add(self.stand2) self.options = Scenario.get_options() self.create_url = self.options.get_create_form() self.rx1 = Rx.objects.get(internal_name=self.prop1.variant.code + "1").id self.rx2 = Rx.objects.get(internal_name=self.prop1.variant.code + "2").id enable_sharing() def test_create_scenario(self): s1 = Scenario(user=self.user, name="My Scenario", input_target_boardfeet=2000, input_target_carbon=1, input_property=self.prop1, input_rxs={self.stand1.pk: self.rx1, self.stand2.pk: self.rx2}, ) s1.save() self.assertEquals(Scenario.objects.get(name="My Scenario").input_target_boardfeet, 2000.0) def test_scenario_results(self): s1 = Scenario(user=self.user, name="My Scenario", input_target_boardfeet=2000, input_target_carbon=1, input_property=self.prop1, input_rxs={self.stand1.pk: self.rx1, self.stand2.pk: self.rx2}, ) s1.save() out = s1.output_property_metrics self.assertTrue(out.has_key("__all__")) # TODO out = s1.output_stand_metrics def test_post(self): self.client.login(username='******', password='******') response = self.client.post(self.create_url, { 'name': "My Scenario", 'input_target_boardfeet': 2000, 'input_target_carbon': 1, 'input_age_class': 1, 'input_site_diversity': 1, 'input_property': self.prop1.pk, 'input_rxs': dumps({self.stand1.pk: self.rx1, self.stand2.pk: self.rx2}), }) self.assertEqual(response.status_code, 201) def test_post_invalid_rx(self): self.client.login(username='******', password='******') response = self.client.post(self.create_url, { 'name': "My Scenario", 'input_target_boardfeet': 2000, 'input_target_carbon': 1, 'input_age_class': 1, 'input_site_diversity': 1, 'input_property': self.prop1.pk, 'input_rxs': dumps({self.stand1.pk: 9879898, self.stand2.pk: self.rx2}), }) self.assertEqual(response.status_code, 400, response.content) def test_post_invalid_stand(self): self.client.login(username='******', password='******') response = self.client.post(self.create_url, { 'name': "My Scenario", 'input_target_boardfeet': 2000, 'input_target_carbon': 1, 'input_age_class': 1, 'input_site_diversity': 1, 'input_property': self.prop1.pk, 'input_rxs': dumps({self.stand3.pk: self.rx1, self.stand2.pk: self.rx2}), }) self.assertEqual(response.status_code, 400, response.content) def test_json_results(self): s1 = Scenario(user=self.user, name="My Scenario", input_target_boardfeet=2000, input_target_carbon=1, input_property=self.prop1, input_rxs={self.stand1.pk: self.rx1, self.stand2.pk: self.rx2}, ) s1.save() geojson_link = ForestProperty.get_options().get_link('Property Scenarios') url = geojson_link.reverse(self.prop1) # not logged in yet response = self.client.get(url) self.assertEqual(response.status_code, 401, response.content) # now we log in self.client.login(username='******', password='******') response = self.client.get(url) self.assertEqual(response.status_code, 200, response.content)
class SpatialTest(TestCase): ''' Tests the spatial representations of Stands and ForestProperties ''' def setUp(self): self.client = Client() import_rasters() self.user = User.objects.create_user( 'featuretest', '*****@*****.**', password='******') self.stand1 = Stand(user=self.user, name="My Stand", geometry_orig=g1) self.stand1.save() self.stand2 = Stand(user=self.user, name="My Stand2", geometry_orig=g1) self.stand2.save() self.prop1 = ForestProperty(user=self.user, name="My Property", geometry_final=p1) self.prop1.save() self.prop1.add(self.stand1) def test_rest_defaultkml_url(self): self.client.login(username='******', password='******') link = self.stand1.options.get_link('KML') url = link.reverse(self.stand1) response = self.client.get(url) errors = kml_errors(response.content) self.assertFalse(errors,"invalid KML %s" % str(errors)) def test_stand_json(self): thejson = self.stand1.geojson() d = loads(thejson) self.assertEquals(d['properties']['name'], 'My Stand') def test_property_json(self): thejson = self.prop1.feature_set_geojson() d = loads(thejson) self.assertEquals(len(d['features']), 1) self.assertEquals(d['features'][0]['properties']['name'], 'My Stand') self.prop1.add(self.stand2) thejson = self.prop1.feature_set_geojson() d = loads(thejson) self.assertEquals(len(d['features']), 2) def test_property_bbox(self): thejson = self.prop1.feature_set_geojson() d = loads(thejson) for x, y in zip(d['bbox'], self.prop1.bbox): self.assertAlmostEquals(x, y, places=5) def test_stand_json_url(self): self.client.login(username='******', password='******') link = self.stand1.options.get_link('GeoJSON') url = link.reverse(self.stand1) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertTrue('application/json' in response['Content-Type']) d = loads(response.content) self.assertEquals(d['features'][0]['properties']['name'], 'My Stand') def test_property_json_url(self): self.client.login(username='******', password='******') link = self.stand1.options.get_link('GeoJSON') url = link.reverse(self.prop1) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertTrue('application/json' in response['Content-Type']) d = loads(response.content) self.assertEquals(d['features'][0]['properties']['name'], 'My Property') def test_multistand_json_url(self): self.client.login(username='******', password='******') uids = [self.stand1.uid, self.stand2.uid] link = Stand.get_options().get_link('GeoJSON') url = link.reverse([self.stand1, self.stand2]) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertTrue('application/json' in response['Content-Type']) d = loads(response.content) self.assertEquals(len(d['features']), 2) foundit = False for f in d['features']: if f['properties']['name'] == 'My Stand2': foundit = True self.assertTrue(foundit) def test_property_stands_json_url(self): self.prop1.add(self.stand2) self.client.login(username='******', password='******') link = self.prop1.options.get_link('Property Stands GeoJSON') url = link.reverse(self.prop1) response = self.client.get(url) self.assertEqual(response.status_code, 200, response) self.assertTrue('application/json' in response['Content-Type']) d = loads(response.content) self.assertEquals(len(d['features']), 2) foundit = False for f in d['features']: if f['properties']['name'] == 'My Stand2': foundit = True self.assertTrue(foundit)
class PropertyStandListTest(TestCase): ''' test web service to grab json of user's propertie's stands [{stand-attrs}, ...] ''' def setUp(self): self.client = Client() import_rasters() self.user = User.objects.create_user( 'featuretest', '*****@*****.**', password='******') self.baduser = User.objects.create_user( 'baduser', '*****@*****.**', password='******') self.stand1 = Stand(user=self.user, name="My Stand", geometry_orig=g1) self.stand1.save() self.stand2 = Stand(user=self.user, name="My Stand2", geometry_orig=g1) self.stand2.save() self.prop1 = ForestProperty(user=self.user, name="My Property", geometry_final=p1) self.prop1.save() self.prop1.add(self.stand1) enable_sharing() def test_unauth(self): link = self.prop1.options.get_link('Property Stands GeoJSON') url = link.reverse(self.prop1) response = self.client.get(url) self.assertEqual(response.status_code, 401) def test_notexists(self): self.client.login(username='******', password='******') link = self.prop1.options.get_link('Property Stands GeoJSON') url = link.reverse(self.prop1) url = url.replace(self.prop1.uid, 'trees_forestproperty_123456789') response = self.client.get(url) self.assertEqual(response.status_code, 404) def test_baduser(self): self.client.login(username='******', password='******') link = self.prop1.options.get_link('Property Stands GeoJSON') url = link.reverse(self.prop1) response = self.client.get(url) self.assertEqual(response.status_code, 403) def test_jsonlist(self): self.client.login(username='******', password='******') link = self.prop1.options.get_link('Property Stands GeoJSON') url = link.reverse(self.prop1) response = self.client.get(url) self.assertEqual(response.status_code, 200) stand_list = loads(response.content) self.assertEquals(stand_list['features'][0]['properties']['name'], 'My Stand', stand_list) self.prop1.add(self.stand2) response = self.client.get(url) self.assertEqual(response.status_code, 200) stand_list = loads(response.content) names = [stand['properties']['name'] for stand in stand_list['features']] names.sort() expected_names = ['My Stand', 'My Stand2'] expected_names.sort() self.assertEqual(names, expected_names)