def test_faulty_data1(self): s1_g = Species(instance=self.instance, genus="g1", species="", cultivar="", max_diameter=50.0, max_height=100.0) s1_g.save_with_system_user_bypass_auth() # TODO: READONLY restore when implemented # csv = """ # | point x | point y | diameter | read only | genus | tree height | # | -34.2 | 24.2 | q12 | true | | | # | 323 | 23.2 | 14 | falseo | | | # | 32.1 | 22.4 | 15 | true | | | # | 33.2 | 19.1 | 32 | true | | | # | 33.2 | q19.1 | -33.3 | true | gfail | | # | 32.1 | 12.1 | | false | g1 | 200 | # | 32.1 | 12.1 | 300 | false | g1 | | # """ csv = """ | point x | point y | diameter | genus | tree height | | -34.2 | 24.2 | q12 | | | | 323 | 23.2 | 14 | | | | 32.1 | 22.4 | 15 | | | | 33.2 | 19.1 | 32 | | | | 33.2 | q19.1 | -33.3 | gfail | | | 32.1 | 12.1 | | g1 | 200 | | 32.1 | 12.1 | 300 | g1 | | """ gflds = [fields.trees.POINT_X, fields.trees.POINT_Y] sflds = [ fields.trees.GENUS, fields.trees.SPECIES, fields.trees.CULTIVAR, fields.trees.OTHER_PART_OF_NAME, fields.trees.COMMON_NAME, ] j = self.run_through_process_views(csv) ierrors = self.extract_errors(j) self.assertEqual(ierrors["0"], [(errors.FLOAT_ERROR[0], [fields.trees.DIAMETER], None)]) # TODO: READONLY restore when implemented # self.assertEqual(ierrors['1'], # [(errors.BOOL_ERROR[0], # [fields.trees.READ_ONLY], None), # (errors.INVALID_GEOM[0], gflds, None)]) self.assertEqual(ierrors["1"], [(errors.INVALID_GEOM[0], gflds, None)]) self.assertNotIn("2", ierrors) self.assertNotIn("3", ierrors) self.assertEqual( ierrors["4"], [ (errors.POS_FLOAT_ERROR[0], [fields.trees.DIAMETER], None), (errors.FLOAT_ERROR[0], [fields.trees.POINT_Y], None), (errors.MISSING_FIELD[0], gflds, None), (errors.INVALID_SPECIES[0], sflds, "gfail"), ], ) self.assertEqual(ierrors["5"], [(errors.SPECIES_HEIGHT_TOO_HIGH[0], [fields.trees.TREE_HEIGHT], 100.0)]) self.assertEqual(ierrors["6"], [(errors.SPECIES_DBH_TOO_HIGH[0], [fields.trees.DIAMETER], 50.0)])
def test_update_species(self): with self._assert_updates_eco_rev(True): tree = Tree(instance=self.instance, plot=self.plot) tree.save_with_user(self.user) species = Species(common_name='foo', instance=self.instance) species.save_with_user(self.user) request_dict = {'tree.species': species.pk} update_map_feature(request_dict, self.user, self.plot)
def setUp(self): instance = make_instance() user = make_admin_user(instance) species = Species(instance=instance, genus="g1", species="", cultivar="", max_diameter=50.0, max_height=100.0) species.save_with_user(User.system_user()) login(self.client, user.username)
def setUp(self): self.instance = make_instance() user = make_commander_user(instance=self.instance) species = Species(instance=self.instance, otm_code='', common_name='', genus='') species.save_with_user(user)
class EcoTest(TestCase): def setUp(self): self.factory = RequestFactory() self.instance, system_user = tm.make_instance_and_system_user() self.user = User(username="******") self.user.save_with_user(system_user) self.user.roles.add(tm.make_commander_role(self.instance)) self.species = Species(symbol='CEDR', genus='cedrus', species='atlantica', max_dbh=2000, max_height=100) self.species.save() p1 = Point(-8515941.0, 4953519.0) self.plot = Plot(geom=p1, instance=self.instance, created_by=self.user) self.plot.save_with_user(self.user) self.tree = Tree(plot=self.plot, instance=self.instance, readonly=False, species=self.species, diameter=1630, created_by=self.user) self.tree.save_with_user(self.user) def test_group_eco(self): pass # TODO: Once filtering has been enabled def test_eco_benefit_sanity(self): req = self.factory.get('/%s/eco/benefit/tree/%s/' % (self.instance.pk, self.tree.pk)) response = tree_benefits(req, instance_id=self.instance.pk, tree_id=self.tree.pk, region='NoEastXXX') self.assertEqual(response.status_code, 200) rslt = json.loads(response.content) bens = rslt['benefits'] def assertBValue(benefit, unit, value): self.assertEqual(bens[benefit]['unit'], unit) self.assertEqual(int(float(bens[benefit]['value'])), value) assertBValue('energy', 'kwh', 1896) assertBValue('airquality', 'lbs/year', 6) assertBValue('stormwater', 'gal', 3185) assertBValue('co2', 'lbs/year', 563)
def setUp(self): # Example url for # CEAT, 1630 dbh, NoEastXXX # eco.json?otmcode=CEAT&diameter=1630®ion=NoEastXXX def mockbenefits(*args, **kwargs): benefits = { "Benefits": { "aq_nox_avoided": 0.6792, "aq_nox_dep": 0.371, "aq_ozone_dep": 0.775, "aq_pm10_avoided": 0.0436, "aq_pm10_dep": 0.491, "aq_sox_avoided": 0.372, "aq_sox_dep": 0.21, "aq_voc_avoided": 0.0254, "bvoc": -0.077, "co2_avoided": 255.5, "co2_sequestered": 0, "co2_storage": 6575, "electricity": 187, "hydro_interception": 12.06, "natural_gas": 5834.1 } } return (benefits, None) region = ITreeRegion.objects.get(code='NoEastXXX') p = region.geometry.point_on_surface converter = BenefitCurrencyConversion(currency_symbol='$', electricity_kwh_to_currency=1.0, natural_gas_kbtu_to_currency=1.0, co2_lb_to_currency=1.0, o3_lb_to_currency=1.0, nox_lb_to_currency=1.0, pm10_lb_to_currency=1.0, sox_lb_to_currency=1.0, voc_lb_to_currency=1.0, h20_gal_to_currency=1.0) converter.save() self.instance = make_instance(is_public=True, point=p) self.instance.eco_benefits_conversion = converter self.instance.save() self.user = make_commander_user(self.instance) self.species = Species(otm_code='CEAT', genus='cedrus', species='atlantica', max_diameter=2000, max_height=100, instance=self.instance) self.species.save_with_user(self.user) self.origBenefitFn = ecobackend.json_benefits_call ecobackend.json_benefits_call = mockbenefits
def setUp(self): instance = make_instance() user = make_admin_user(instance) species = Species(instance=instance, genus='g1', species='', cultivar='', max_diameter=50.0, max_height=100.0) species.save_with_user(User.system_user()) login(self.client, user.username)
def test_height_too_large_for_species_fails_validation(self): max_height = 1 s = Species(genus='Ulmus', species='rubra', cultivar='Columella', instance=self.instance, max_height=max_height) s.save_with_user(self.user) self.tree.species = s self.tree.height = str(max_height + 1) with self.assertRaises(ValidationError) as cm: self.tree.save_with_user(self.user) self.assertValidationErrorDictContainsKey(cm.exception, 'height')
def setUp(self): # Example url for # CEAT, 1630 dbh, NoEastXXX # eco.json?otmcode=CEAT&diameter=1630®ion=NoEastXXX def mockbenefits(*args, **kwargs): benefits = { "Benefits": { "aq_nox_avoided": 0.6792, "aq_nox_dep": 0.371, "aq_ozone_dep": 0.775, "aq_pm10_avoided": 0.0436, "aq_pm10_dep": 0.491, "aq_sox_avoided": 0.372, "aq_sox_dep": 0.21, "aq_voc_avoided": 0.0254, "bvoc": -0.077, "co2_avoided": 255.5, "co2_sequestered": 0, "co2_storage": 6575, "electricity": 187, "hydro_interception": 12.06, "natural_gas": 5834.1 } } return (benefits, None) region = ITreeRegion.objects.get(code='NoEastXXX') p = region.geometry.point_on_surface self.instance = make_instance(is_public=True, point=p) self.user = make_commander_user(self.instance) self.species = Species(otm_code='CEAT', genus='cedrus', species='atlantica', max_diameter=2000, max_height=100, instance=self.instance) self.species.save_with_user(self.user) self.plot = Plot(geom=p, instance=self.instance) self.plot.save_with_user(self.user) self.tree = Tree(plot=self.plot, instance=self.instance, readonly=False, species=self.species, diameter=1630) self.tree.save_with_user(self.user) self.origBenefitFn = ecobackend.json_benefits_call ecobackend.json_benefits_call = mockbenefits
def test_faulty_data1(self): s1_g = Species(symbol='S1GSC', scientific_name='', family='', genus='g1', species='', cultivar_name='', v_max_dbh=50.0, v_max_height=100.0) s1_g.save() csv = """ | point x | point y | diameter | read only | condition | genus | tree height | | -34.2 | 24.2 | q12 | true | Dead | | | | 323 | 23.2 | 14 | falseo | Critical | | | | 32.1 | 22.4 | 15 | true | Dead | | | | 33.2 | 19.1 | 32 | true | Arg | | | | 33.2 | q19.1 | -33.3 | true | Dead | gfail | | | 32.1 | 12.1 | | false | Dead | g1 | 200 | | 32.1 | 12.1 | 300 | false | Dead | g1 | | | 11.1 | 12.1 | | false | Dead | | | """ gflds = [fields.trees.POINT_X, fields.trees.POINT_Y] sflds = [ fields.trees.GENUS, fields.trees.SPECIES, fields.trees.CULTIVAR ] j = self.run_through_process_views(csv) ierrors = self.extract_errors(j) self.assertEqual( ierrors['0'], [(errors.FLOAT_ERROR[0], [fields.trees.DIAMETER], None), (errors.GEOM_OUT_OF_BOUNDS[0], gflds, None)]) self.assertEqual( ierrors['1'], [(errors.BOOL_ERROR[0], [fields.trees.READ_ONLY], None), (errors.INVALID_GEOM[0], gflds, None)]) self.assertNotIn('2', ierrors) self.assertEqual(ierrors['3'], [(errors.INVALID_CHOICE[0], [fields.trees.TREE_CONDITION], 'conditions')]) self.assertEqual( ierrors['4'], [(errors.POS_FLOAT_ERROR[0], [fields.trees.DIAMETER], None), (errors.FLOAT_ERROR[0], [fields.trees.POINT_Y], None), (errors.MISSING_POINTS[0], gflds, None), (errors.INVALID_SPECIES[0], sflds, 'gfail')]) self.assertEqual(ierrors['5'], [(errors.SPECIES_HEIGHT_TOO_HIGH[0], [fields.trees.TREE_HEIGHT], 100.0)]) self.assertEqual( ierrors['6'], [(errors.SPECIES_DBH_TOO_HIGH[0], [fields.trees.DIAMETER], 50.0)]) self.assertEqual(ierrors['7'], [(errors.EXCL_ZONE[0], gflds, None)])
def commit_row(self): # First validate if not self.validate_row(): return False # Get our data data = self.cleaned species_edited = False # Initially grab species from row if it exists # and edit it species = self.species # If not specified create a new one if species is None: species = Species() # Convert units self.convert_units(data, { fields.species.MAX_DIAMETER: self.import_event.max_diameter_conversion_factor, fields.species.MAX_HEIGHT: self.import_event.max_tree_height_conversion_factor }) #TODO: Update tree count nonsense for modelkey, importdatakey in SpeciesImportRow.SPECIES_MAP.iteritems(): importdata = data.get(importdatakey, None) if importdata is not None: species_edited = True setattr(species, modelkey, importdata) if species_edited: species.save() resource = data[fields.species.RESOURCE] species.resource.clear() species.resource.add(resource) species.save() resource.save() self.species = species self.status = TreeImportRow.SUCCESS self.save() return True
def test_faulty_data1(self): s1_g = Species(symbol='S1GSC', scientific_name='',family='', genus='g1', species='', cultivar_name='', v_max_dbh=50.0, v_max_height=100.0) s1_g.save() csv = """ | point x | point y | diameter | read only | condition | genus | tree height | | -34.2 | 24.2 | q12 | true | Dead | | | | 323 | 23.2 | 14 | falseo | Critical | | | | 32.1 | 22.4 | 15 | true | Dead | | | | 33.2 | 19.1 | 32 | true | Arg | | | | 33.2 | q19.1 | -33.3 | true | Dead | gfail | | | 32.1 | 12.1 | | false | Dead | g1 | 200 | | 32.1 | 12.1 | 300 | false | Dead | g1 | | | 11.1 | 12.1 | | false | Dead | | | """ gflds = [fields.trees.POINT_X, fields.trees.POINT_Y] sflds = [fields.trees.GENUS, fields.trees.SPECIES, fields.trees.CULTIVAR] j = self.run_through_process_views(csv) ierrors = self.extract_errors(j) self.assertEqual(ierrors['0'], [(errors.FLOAT_ERROR[0], [fields.trees.DIAMETER], None), (errors.GEOM_OUT_OF_BOUNDS[0], gflds, None)]) self.assertEqual(ierrors['1'], [(errors.BOOL_ERROR[0], [fields.trees.READ_ONLY], None), (errors.INVALID_GEOM[0], gflds, None)]) self.assertNotIn('2', ierrors) self.assertEqual(ierrors['3'], [(errors.INVALID_CHOICE[0], [fields.trees.TREE_CONDITION], 'conditions')]) self.assertEqual(ierrors['4'], [(errors.POS_FLOAT_ERROR[0], [fields.trees.DIAMETER], None), (errors.FLOAT_ERROR[0], [fields.trees.POINT_Y], None), (errors.MISSING_POINTS[0], gflds, None), (errors.INVALID_SPECIES[0], sflds, 'gfail')]) self.assertEqual(ierrors['5'], [(errors.SPECIES_HEIGHT_TOO_HIGH[0], [fields.trees.TREE_HEIGHT], 100.0)]) self.assertEqual(ierrors['6'], [(errors.SPECIES_DBH_TOO_HIGH[0], [fields.trees.DIAMETER], 50.0)]) self.assertEqual(ierrors['7'], [(errors.EXCL_ZONE[0], gflds, None)])
def test_species_diameter_and_height(self): s1_gsc = Species(instance=self.instance, genus='g1', species='s1', cultivar='c1', max_height=30, max_diameter=19) s1_gs = Species(instance=self.instance, genus='g1', species='s1', cultivar='', max_height=22, max_diameter=12) s1_gsc.save_with_system_user_bypass_auth() s1_gs.save_with_system_user_bypass_auth() row = {'point x': '16', 'point y': '20', 'genus': 'g1', 'species': 's1', 'diameter': '15', 'tree height': '18'} i = self.mkrow(row) i.validate_row() self.assertHasError(i, errors.SPECIES_DBH_TOO_HIGH) self.assertNotHasError(i, errors.SPECIES_HEIGHT_TOO_HIGH) row['tree height'] = 25 i = self.mkrow(row) i.validate_row() self.assertHasError(i, errors.SPECIES_DBH_TOO_HIGH) self.assertHasError(i, errors.SPECIES_HEIGHT_TOO_HIGH) row['cultivar'] = 'c1' i = self.mkrow(row) i.validate_row() self.assertNotHasError(i, errors.SPECIES_DBH_TOO_HIGH) self.assertNotHasError(i, errors.SPECIES_HEIGHT_TOO_HIGH)
def setUp(self): self.instance = make_instance(name='Test Instance') self.species = Species(instance=self.instance, common_name='Test Common Name', genus='Test Genus', cultivar='Test Cultivar', species='Test Species') self.species.save_base() self.user = make_user(username='******', password='******') self.plot = Plot(geom=Point(1, 1), instance=self.instance, address_street="123 Main Street") self.plot.save_base() self.tree = Tree(plot=self.plot, instance=self.instance) self.tree.save_base() self.boundary = make_simple_boundary("Test Boundary") self.role = Role(instance=self.instance, name='Test Role', rep_thresh=2) self.role.save() self.field_permission = FieldPermission( model_name="Tree", field_name="readonly", permission_level=FieldPermission.READ_ONLY, role=self.role, instance=self.instance) self.field_permission.save_base() self.audit = Audit(action=Audit.Type.Update, model="Tree", field="readonly", model_id=1, user=self.user, previous_value=True, current_value=False) self.audit.save_base() self.reputation_metric = ReputationMetric(instance=self.instance, model_name="Tree", action="Test Action") self.reputation_metric.save_base()
def setUp(self): self.factory = RequestFactory() self.instance, system_user = tm.make_instance_and_system_user() self.user = User(username="******") self.user.save_with_user(system_user) self.user.roles.add(tm.make_commander_role(self.instance)) self.species = Species(symbol='CEDR', genus='cedrus', species='atlantica', max_dbh=2000, max_height=100) self.species.save() p1 = Point(-8515941.0, 4953519.0) self.plot = Plot(geom=p1, instance=self.instance, created_by=self.user) self.plot.save_with_user(self.user) self.tree = Tree(plot=self.plot, instance=self.instance, readonly=False, species=self.species, diameter=1630, created_by=self.user) self.tree.save_with_user(self.user)
def setUp(self): region = ITreeRegion.objects.get(code='NoEastXXX') p = region.geometry.point_on_surface self.instance = make_instance(is_public=True, point=p) self.user = make_commander_user(self.instance) self.species = Species(otm_code='CEAT', genus='cedrus', species='atlantica', max_dbh=2000, max_height=100, instance=self.instance) self.species.save_with_user(self.user) self.plot = Plot(geom=p, instance=self.instance) self.plot.save_with_user(self.user) self.tree = Tree(plot=self.plot, instance=self.instance, readonly=False, species=self.species, diameter=1630) self.tree.save_with_user(self.user)
def add_species_to_instance(instance): from treemap.models import Species region_codes = [itr.code for itr in instance.itree_regions()] if region_codes: # Add species from all of the instance's i-Tree regions species_codes = species_codes_for_regions(region_codes) else: # Add all species species_codes = all_species_codes() # Convert the list to a set for fast lookups species_code_set = set(species_codes) # Create and save a Species for each otm_code # Saving one by one is SLOW. It takes many seconds # to do the average species list of ~250 items. # Using bulk_create bypasses auditing but keeps # speed up. # TODO: bulk create audit records for species rows instance_species_list = [] for species_dict in SPECIES: if species_dict['otm_code'] in species_code_set: species_dict['instance'] = instance instance_species_list.append(Species(**species_dict)) Species.objects.bulk_create(instance_species_list)
def test_like_filter(self): species = Species(instance=self.instance, common_name='this is a test species', genus='Genus-1', otm_code='S1') species.save_with_user(self.commander) p, t = self.create_tree_and_plot() t.species = species t.save_with_user(self.commander) species_like_filter = json.dumps( {'species.common_name': { 'LIKE': 's a tes' }}) plots = search.Filter(species_like_filter, '', self.instance)\ .get_objects(Plot) result = [o.pk for o in plots] self.assertEqual(result, [p.pk]) species.common_name = 'no match' species.save_with_user(self.commander) plots = search.Filter(species_like_filter, '', self.instance)\ .get_objects(Plot) self.assertEqual(len(plots), 0)
def test_faulty_data1(self): s1_g = Species(instance=self.instance, genus='g1', species='', cultivar='', max_diameter=50.0, max_height=100.0) s1_g.save_with_system_user_bypass_auth() csv = """ | point x | point y | diameter | read only | genus | tree height | | -34.2 | 24.2 | q12 | true | | | | 323 | 23.2 | 14 | falseo | | | | 32.1 | 22.4 | 15 | true | | | | 33.2 | 19.1 | 32 | true | | | | 33.2 | q19.1 | -33.3 | true | gfail | | | 32.1 | 12.1 | | false | g1 | 200 | | 32.1 | 12.1 | 300 | false | g1 | | """ gflds = [fields.trees.POINT_X, fields.trees.POINT_Y] sflds = [fields.trees.GENUS, fields.trees.SPECIES, fields.trees.CULTIVAR] j = self.run_through_process_views(csv) ierrors = self.extract_errors(j) self.assertEqual(ierrors['0'], [(errors.FLOAT_ERROR[0], [fields.trees.DIAMETER], None)]) self.assertEqual(ierrors['1'], [(errors.BOOL_ERROR[0], [fields.trees.READ_ONLY], None), (errors.INVALID_GEOM[0], gflds, None)]) self.assertNotIn('2', ierrors) self.assertNotIn('3', ierrors) self.assertEqual(ierrors['4'], [(errors.POS_FLOAT_ERROR[0], [fields.trees.DIAMETER], None), (errors.FLOAT_ERROR[0], [fields.trees.POINT_Y], None), (errors.MISSING_FIELD[0], gflds, None), (errors.INVALID_SPECIES[0], sflds, 'gfail')]) self.assertEqual(ierrors['5'], [(errors.SPECIES_HEIGHT_TOO_HIGH[0], [fields.trees.TREE_HEIGHT], 100.0)]) self.assertEqual(ierrors['6'], [(errors.SPECIES_DBH_TOO_HIGH[0], [fields.trees.DIAMETER], 50.0)])
def test_species_matching(self): csv = """ | genus | species | common name | i-tree code | usda symbol | alternative symbol | other part of scientific name | | testus1 | specieius1 | g1 s2 wowza | BDL_OTHER | | | | | genus | blah | common name | BDL_OTHER | s1 | | | | testus1 | specieius1 | g1 s2 wowza | BDL_OTHER | s2 | | | | testus2 | specieius2 | g1 s2 wowza | BDL_OTHER | s1 | a3 | | | genusN | speciesN | gN sN wowza | BDL_OTHER | | | var3 | """ j = self.run_through_process_views(csv) ierrors = self.extract_errors(j) # Errors for multiple species matches self.assertEqual(len(ierrors), 4) ie = SpeciesImportEvent.objects.get(pk=j['pk']) s1, s2, s3 = [s.pk for s in Species.objects.all()] s4s = Species(symbol='gnsn', scientific_name='', family='', genus='genusN', species='speciesN', cultivar_name='', other_part_of_name='var3', v_max_dbh=50.0, v_max_height=100.0) s4s.save() s4 = s4s.pk rows = ie.rows() matches = [] for row in rows: row.validate_row() matches.append(row.cleaned[fields.species.ORIG_SPECIES]) m1, m2, m3, m4, m5 = matches self.assertEqual(m1, {s1}) self.assertEqual(m2, {s1}) self.assertEqual(m3, {s1, s2}) self.assertEqual(m4, {s1, s2, s3}) self.assertEqual(m5, {s4})
def setUp(self): # Example url for # CEAT, 1630 dbh, NoEastXXX # eco.json?otmcode=CEAT&diameter=1630®ion=NoEastXXX def mockbenefits(*args, **kwargs): benefits = { "Benefits": { "aq_nox_avoided": 0.6792, "aq_nox_dep": 0.371, "aq_ozone_dep": 0.775, "aq_pm10_avoided": 0.0436, "aq_pm10_dep": 0.491, "aq_sox_avoided": 0.372, "aq_sox_dep": 0.21, "aq_voc_avoided": 0.0254, "bvoc": -0.077, "co2_avoided": 255.5, "co2_sequestered": 0, "co2_storage": 6575, "electricity": 187, "hydro_interception": 12.06, "natural_gas": 5834.1 } } return (benefits, None) region = ITreeRegion.objects.get(code='NoEastXXX') p = region.geometry.point_on_surface self.instance = make_instance(is_public=True, point=p) self.user = make_commander_user(self.instance) self.species = Species(otm_code='CEAT', genus='cedrus', species='atlantica', max_dbh=2000, max_height=100, instance=self.instance) self.species.save_with_user(self.user) self.plot = Plot(geom=p, instance=self.instance) self.plot.save_with_user(self.user) self.tree = Tree(plot=self.plot, instance=self.instance, readonly=False, species=self.species, diameter=1630) self.tree.save_with_user(self.user) self.origBenefitFn = ecobackend.json_benefits_call ecobackend.json_benefits_call = mockbenefits
def _process_record(self, rec): pk = rec['pk'] fields = rec['fields'] fields['max_height'] = fields['v_max_height'] or 10000 del fields['v_max_height'] fields['max_dbh'] = fields['v_max_dbh'] or 10000 del fields['v_max_dbh'] removed_fields = ['alternate_symbol', 'v_multiple_trunks', 'tree_count', 'resource', 'itree_code'] for f in removed_fields: del fields[f] s = Species(**fields) s.pk = pk s.save()
def test_species_id_search(self): species1 = Species( common_name='Species-1', genus='Genus-1', otm_code='S1', instance=self.instance) species1.save_with_user(self.commander) species2 = Species( common_name='Species-2', genus='Genus-2', otm_code='S1', instance=self.instance) species2.save_with_user(self.commander) p1, t1 = self.create_tree_and_plot() p2, t2 = self.create_tree_and_plot() p3, t3 = self.create_tree_and_plot() t1.species = species1 t1.save_with_user(self.commander) t2.species = species2 t2.save_with_user(self.commander) species1_filter = json.dumps({'species.id': species1.pk}) species2_filter = json.dumps({'species.id': species2.pk}) species3_filter = json.dumps({'species.id': -1}) plots =\ search.Filter(species1_filter, '', self.instance).get_objects(Plot) self.assertEqual( {p1.pk}, {p.pk for p in plots}) plots =\ search.Filter(species2_filter, '', self.instance).get_objects(Plot) self.assertEqual( {p2.pk}, {p.pk for p in plots}) plots =\ search.Filter(species3_filter, '', self.instance).get_objects(Plot) self.assertEqual( 0, len(plots))
def test_species_diameter_and_height(self): s1_gsc = Species( instance=self.instance, genus="g1", species="s1", cultivar="c1", max_height=30, max_diameter=19 ) s1_gs = Species(instance=self.instance, genus="g1", species="s1", cultivar="", max_height=22, max_diameter=12) s1_gsc.save_with_system_user_bypass_auth() s1_gs.save_with_system_user_bypass_auth() row = {"point x": "16", "point y": "20", "genus": "g1", "species": "s1", "diameter": "15", "tree height": "18"} i = self.mkrow(row) i.validate_row() self.assertHasError(i, errors.SPECIES_DBH_TOO_HIGH) self.assertNotHasError(i, errors.SPECIES_HEIGHT_TOO_HIGH) row["tree height"] = 25 i = self.mkrow(row) i.validate_row() self.assertHasError(i, errors.SPECIES_DBH_TOO_HIGH) self.assertHasError(i, errors.SPECIES_HEIGHT_TOO_HIGH) row["cultivar"] = "c1" i = self.mkrow(row) i.validate_row() self.assertNotHasError(i, errors.SPECIES_DBH_TOO_HIGH) self.assertNotHasError(i, errors.SPECIES_HEIGHT_TOO_HIGH)
def test_like_filter(self): species = Species( instance=self.instance, common_name='this is a test species', genus='Genus-1', otm_code='S1') species.save_with_user(self.commander) p, t = self.create_tree_and_plot() t.species = species t.save_with_user(self.commander) species_like_filter = json.dumps({ 'species.common_name': {'LIKE': 's a tes'}}) result = [o.pk for o in _execute_filter( self.instance, species_like_filter)] self.assertEqual(result, [p.pk]) species.common_name = 'no match' species.save_with_user(self.commander) result = _execute_filter( self.instance, species_like_filter) self.assertEqual(len(result), 0)
def test_like_filter(self): species = Species( instance=self.instance, common_name='this is a test species', genus='Genus-1', otm_code='S1') species.save_with_user(self.commander) p, t = self.create_tree_and_plot() t.species = species t.save_with_user(self.commander) species_like_filter = json.dumps({ 'species.common_name': {'LIKE': 's a tes'}}) plots = search.Filter(species_like_filter, '', self.instance)\ .get_objects(Plot) result = [o.pk for o in plots] self.assertEqual(result, [p.pk]) species.common_name = 'no match' species.save_with_user(self.commander) plots = search.Filter(species_like_filter, '', self.instance)\ .get_objects(Plot) self.assertEqual(len(plots), 0)
def get_species(otm_code, region_code): species = Species.get_by_code(instance, otm_code, region_code) if species is None: raise Http404( "Could not find species with OTM code %s in instance %s" % (otm_code, instance.url_name)) # The species may have been retrieved via an ITreeCodeOverride. In that # case, the species will not have an otm_code value and we need to set # it so it is available for the downstream code. if species.otm_code != otm_code: species.otm_code = otm_code print(species.common_name) return species
def test_tree_hash_to_model(self): test_plot = Plot(geom=Point(0, 0), instance=self.instance) test_plot.id = 95 test_plot.save_with_user(self.commander) test_species = Species(instance=self.instance, otm_code="1", common_name="asdfa", genus="sdfs") test_species.id = 85 test_species.save_with_user(self.commander) tree_dict = json.loads(self.tree_blob) tree = hash_to_model(MIGRATION_RULES, 'tree', tree_dict, self.instance) tree.save_with_user(self.commander) tree = Tree.objects.get(pk=tree.pk) self.assertEqual(tree.plot, test_plot) self.assertEqual(tree.species, test_species) self.assertEqual(tree.readonly, True) self.assertEqual(tree.diameter, 0.2900001566) self.assertEqual(tree.canopy_height, None) self.assertEqual(tree.date_planted, None) self.assertEqual(tree.date_removed, None)
def setUp(self): self.instance = make_instance(name='Test Instance') self.species = Species(instance=self.instance, common_name='Test Common Name', genus='Test Genus', cultivar='Test Cultivar', species='Test Species') self.species.save_base() self.user = make_user(username='******', password='******') self.import_event = ImportEvent(imported_by=self.user) self.import_event.save_base() self.plot = Plot(geom=Point(0, 0), instance=self.instance, address_street="123 Main Street") self.plot.save_base() self.tree = Tree(plot=self.plot, instance=self.instance) self.tree.save_base() self.boundary = make_simple_boundary("Test Boundary") self.role = make_commander_role(self.instance) self.role.name = "Test Role" self.role.save() self.field_permission = FieldPermission( model_name="Tree", field_name="readonly", permission_level=FieldPermission.READ_ONLY, role=self.role, instance=self.instance) self.field_permission.save_base() self.audit = Audit(action=Audit.Type.Update, model="Tree", field="readonly", model_id=1, user=self.user, previous_value=True, current_value=False) self.audit.save_base() self.reputation_metric = ReputationMetric(instance=self.instance, model_name="Tree", action="Test Action") self.reputation_metric.save_base()
def test_species_matching(self): csv = """ | genus | species | common name | i-tree code | usda symbol | alternative symbol | other part of scientific name | | testus1 | specieius1 | g1 s2 wowza | BDL_OTHER | | | | | genus | blah | common name | BDL_OTHER | s1 | | | | testus1 | specieius1 | g1 s2 wowza | BDL_OTHER | s2 | | | | testus2 | specieius2 | g1 s2 wowza | BDL_OTHER | s1 | a3 | | | genusN | speciesN | gN sN wowza | BDL_OTHER | | | var3 | """ j = self.run_through_process_views(csv) ierrors = self.extract_errors(j) # Errors for multiple species matches self.assertEqual(len(ierrors), 4) ie = SpeciesImportEvent.objects.get(pk=j['pk']) s1,s2,s3 = [s.pk for s in Species.objects.all()] s4s = Species(symbol='gnsn', scientific_name='',family='', genus='genusN', species='speciesN', cultivar_name='', other_part_of_name='var3', v_max_dbh=50.0, v_max_height=100.0) s4s.save() s4 = s4s.pk rows = ie.rows() matches = [] for row in rows: row.validate_row() matches.append(row.cleaned[fields.species.ORIG_SPECIES]) m1,m2,m3,m4,m5 = matches self.assertEqual(m1, {s1}) self.assertEqual(m2, {s1}) self.assertEqual(m3, {s1,s2}) self.assertEqual(m4, {s1,s2,s3}) self.assertEqual(m5, {s4})
def test_species_dbh_and_height(self): s1_gsc = Species(symbol='S1G__', scientific_name='', family='', genus='g1', species='s1', cultivar_name='c1', v_max_height=30, v_max_dbh=19) s1_gs = Species(symbol='S1GS_', scientific_name='', family='', genus='g1', species='s1', cultivar_name='', v_max_height=22, v_max_dbh=12) s1_gsc.save() s1_gs.save() row = { 'point x': '16', 'point y': '20', 'genus': 'g1', 'species': 's1', 'diameter': '15', 'tree height': '18' } i = self.mkrow(row) r = i.validate_row() self.assertHasError(i, errors.SPECIES_DBH_TOO_HIGH) self.assertNotHasError(i, errors.SPECIES_HEIGHT_TOO_HIGH) row['tree height'] = 25 i = self.mkrow(row) r = i.validate_row() self.assertHasError(i, errors.SPECIES_DBH_TOO_HIGH) self.assertHasError(i, errors.SPECIES_HEIGHT_TOO_HIGH) row['cultivar'] = 'c1' i = self.mkrow(row) r = i.validate_row() self.assertNotHasError(i, errors.SPECIES_DBH_TOO_HIGH) self.assertNotHasError(i, errors.SPECIES_HEIGHT_TOO_HIGH)
def setUp(self): self.instance = make_instance() self.instance.itree_region_default = 'GulfCoCHS' self.instance.save() self.maple = Species(otm_code='ACRU', genus='Acer', species='rubrum') self.honeylocust = Species(otm_code='GLTR', genus='Gleditsia', species='triacanthos') self.model_params = \ GrowthAndMortalityModel.get_default_params(self.instance) # This minimal model runs for 1 year and kills no trees. # Different tests add and exercise different parameters. self.scenario = { 'groups': [], 'trees': [], 'years': 1, 'replant_years': 0 }
def test_tree_dict_to_model(self): test_plot = Plot(geom=self.instance.center, instance=self.instance) test_plot.id = 95 test_plot.save_with_user(self.commander) test_species = Species(instance=self.instance, otm_code="1", common_name="asdfa", genus="sdfs") test_species.id = 85 test_species.save_with_user(self.commander) tree_dict = json.loads(self.tree_blob) tree = dict_to_model(MIGRATION_RULES, 'tree', tree_dict, self.instance) tree.save_with_user(self.commander) tree = Tree.objects.get(pk=tree.pk) self.assertEqual(tree.plot, test_plot) self.assertEqual(tree.species, test_species) self.assertEqual(tree.readonly, True) self.assertEqual(tree.diameter, 0.2900001566) self.assertEqual(tree.canopy_height, None) self.assertEqual(tree.date_planted, None) self.assertEqual(tree.date_removed, None)
def annotate_species_dict(sdict): sci_name = Species.get_scientific_name(sdict['genus'], sdict['species'], sdict['cultivar']) display_name = "%s [%s]" % (sdict['common_name'], sci_name) tokens = tokenize(species) sdict.update({ 'scientific_name': sci_name, 'value': display_name, 'tokens': tokens}) return sdict
def annotate_species_dict(sdict): sci_name = Species.get_scientific_name(sdict['genus'], sdict['species'], sdict['cultivar']) display_name = "%s [%s]" % (sdict['common_name'], sci_name) tokens = tokenize(species) sdict.update({ 'scientific_name': sci_name, 'value': display_name, 'tokens': tokens }) return sdict
def test_common_name_matching(self): apple = Species(instance=self.instance, genus='malus', common_name='Apple') apple.save_with_system_user_bypass_auth() csv = """ | point x | point y | genus | common name | | 45.59 | 31.1 | malus | apple | | 45.58 | 33.9 | malus | crab apple | | 45.58 | 33.9 | malus | | """ ieid = self.run_through_commit_views(csv) ie = TreeImportEvent.objects.get(pk=ieid) rows = ie.treeimportrow_set.order_by('idx').all() tree1 = rows[0].plot.current_tree() tree3 = rows[2].plot.current_tree() self.assertEqual(tree1.species.pk, apple.pk) self.assertIsNone(rows[1].plot) self.assertEqual(tree3.species.pk, apple.pk) # If we add a species, there will be more than one match for "malus" csv = """ | point x | point y | genus | common name | | 45.49 | 31.1 | malus | apple | | 45.48 | 33.9 | malus | crab apple | | 45.48 | 33.9 | malus | | """ crab_apple = Species(instance=self.instance, genus='malus', common_name='Crab Apple') crab_apple.save_with_system_user_bypass_auth() ieid = self.run_through_commit_views(csv) ie = TreeImportEvent.objects.get(pk=ieid) rows = ie.treeimportrow_set.order_by('idx').all() tree1 = rows[0].plot.current_tree() tree2 = rows[1].plot.current_tree() self.assertEqual(tree1.species.pk, apple.pk) self.assertEqual(tree2.species.pk, crab_apple.pk) self.assertIsNone(rows[2].plot)
def commit_row(self): # First validate if not self.validate_row(): return False # Get our data data = self.cleaned species_edited = False # Initially grab species from row if it exists # and edit it species = self.species # If not specified create a new one if species is None: species = Species() # Convert units self.convert_units( data, { fields.species.MAX_DIAMETER: self.import_event.max_diameter_conversion_factor, fields.species.MAX_HEIGHT: self.import_event.max_tree_height_conversion_factor }) #TODO: Update tree count nonsense for modelkey, importdatakey in SpeciesImportRow.SPECIES_MAP.iteritems( ): importdata = data.get(importdatakey, None) if importdata is not None: species_edited = True setattr(species, modelkey, importdata) if species_edited: species.save() resource = data[fields.species.RESOURCE] species.resource.clear() species.resource.add(resource) species.save() resource.save() self.species = species self.status = TreeImportRow.SUCCESS self.save() return True
def test_adding_override_invalidates_cache(self): instance = make_instance() user = make_commander_user(instance) species = Species(instance=instance, genus='g') species.save_with_user(user) species.refresh_from_db() ITreeCodeOverride(instance_species=species, region=ITreeRegion.objects.get(code='NMtnPrFNL'), itree_code='CEL OTHER').save_with_user(user) invalidate_ecoservice_cache_if_stale() self.assertTrue(self.cache_invalidated)
def check_species(self, row): # locate the species and instanciate the tree instance if not row["genus"]: self.log_verbose(" No genus information") return (False, None) genus = str(row['genus']).strip() name = '%s' % genus species = '' cultivar = '' gender = '' if row.get('species'): species = str(row['species']).strip() name += " %s" % species if row.get('cultivar'): cultivar = str(row['cultivar']).strip() name += " %s" % cultivar if row.get('gender'): gender = str(row['gender']).strip() name += " %s" % gender self.log_verbose(" Looking for species: %s" % name) found = Species.objects.filter(genus__iexact=genus).filter( species__iexact=species).filter( cultivar_name__iexact=cultivar).filter(gender__iexact=gender) if found: #species match found self.log_verbose(" Found species %r" % found[0]) return (True, found[0]) #species data but no match, add it self.log_verbose(" Adding unknown species %s %s %s" % (genus, species, cultivar)) species = Species(genus=genus, species=species, cultivar_name=cultivar, scientific_name=name, gender=gender) return (True, species)
def setUp(self): self.instance = make_instance(is_public=True) self.user = make_commander_user(self.instance) self.species = Species(otm_code='CEAT', genus='cedrus', species='atlantica', max_dbh=2000, max_height=100, instance=self.instance) self.species.save_with_user(self.user) ITreeRegion.objects.all().delete() p1 = Point(-8515941.0, 4953519.0) self.region_buffer_in_meters = 1000 ITreeRegion.objects.create( code='NoEastXXX', geometry=MultiPolygon([p1.buffer(1000)])) self.plot = Plot(geom=p1, instance=self.instance) self.plot.save_with_user(self.user) self.tree = Tree(plot=self.plot, instance=self.instance, readonly=False, species=self.species, diameter=1630) self.tree.save_with_user(self.user)
def test_species_dbh_and_height(self): s1_gsc = Species(symbol='S1G__', scientific_name='',family='', genus='g1', species='s1', cultivar_name='c1', v_max_height=30, v_max_dbh=19) s1_gs = Species(symbol='S1GS_', scientific_name='',family='', genus='g1', species='s1', cultivar_name='', v_max_height=22, v_max_dbh=12) s1_gsc.save() s1_gs.save() row = {'point x': '16', 'point y': '20', 'genus': 'g1', 'species': 's1', 'diameter': '15', 'tree height': '18'} i = self.mkrow(row) r = i.validate_row() self.assertHasError(i, errors.SPECIES_DBH_TOO_HIGH) self.assertNotHasError(i, errors.SPECIES_HEIGHT_TOO_HIGH) row['tree height'] = 25 i = self.mkrow(row) r = i.validate_row() self.assertHasError(i, errors.SPECIES_DBH_TOO_HIGH) self.assertHasError(i, errors.SPECIES_HEIGHT_TOO_HIGH) row['cultivar'] = 'c1' i = self.mkrow(row) r = i.validate_row() self.assertNotHasError(i, errors.SPECIES_DBH_TOO_HIGH) self.assertNotHasError(i, errors.SPECIES_HEIGHT_TOO_HIGH)
class EcoTest(UrlTestCase): def setUp(self): # Example url for # CEAT, 1630 dbh, NoEastXXX # eco.json?otmcode=CEAT&diameter=1630®ion=NoEastXXX def mockbenefits(*args, **kwargs): benefits = { "Benefits": { "aq_nox_avoided": 0.6792, "aq_nox_dep": 0.371, "aq_ozone_dep": 0.775, "aq_pm10_avoided": 0.0436, "aq_pm10_dep": 0.491, "aq_sox_avoided": 0.372, "aq_sox_dep": 0.21, "aq_voc_avoided": 0.0254, "bvoc": -0.077, "co2_avoided": 255.5, "co2_sequestered": 0, "co2_storage": 6575, "electricity": 187, "hydro_interception": 12.06, "natural_gas": 5834.1 } } return (benefits, None) region = ITreeRegion.objects.get(code='NoEastXXX') p = region.geometry.point_on_surface self.instance = make_instance(is_public=True, point=p) self.user = make_commander_user(self.instance) self.species = Species(otm_code='CEAT', genus='cedrus', species='atlantica', max_diameter=2000, max_height=100, instance=self.instance) self.species.save_with_user(self.user) self.plot = Plot(geom=p, instance=self.instance) self.plot.save_with_user(self.user) self.tree = Tree(plot=self.plot, instance=self.instance, readonly=False, species=self.species, diameter=1630) self.tree.save_with_user(self.user) self.origBenefitFn = ecobackend.json_benefits_call ecobackend.json_benefits_call = mockbenefits def tearDown(self): ecobackend.json_benefits_call = self.origBenefitFn def assert_benefit_value(self, bens, benefit, unit, value): self.assertEqual(bens[benefit]['unit'], unit) self.assertEqual(int(float(bens[benefit]['value'])), value) def test_eco_benefit_sanity(self): rslt, basis, error = TreeBenefitsCalculator()\ .benefits_for_object(self.instance, self.tree.plot) bens = rslt['plot'] self.assert_benefit_value(bens, BenefitCategory.ENERGY, 'kwh', 1896) self.assert_benefit_value(bens, BenefitCategory.AIRQUALITY, 'lbs', 6) self.assert_benefit_value(bens, BenefitCategory.STORMWATER, 'gal', 3185) self.assert_benefit_value(bens, BenefitCategory.CO2, 'lbs', 563) self.assert_benefit_value(bens, BenefitCategory.CO2STORAGE, 'lbs', 6575) def testSearchBenefits(self): request = make_request( {'q': json.dumps({'tree.readonly': { 'IS': False }})}) # all trees request.instance_supports_ecobenefits = self.instance\ .has_itree_region() result = search_tree_benefits(request, self.instance) benefits = result['benefits'] self.assertTrue(len(benefits) > 0) def test_group_basis_empty(self): basis = {} example = { 'group1': { 'n_objects_used': 5, 'n_objects_discarded': 8 }, 'group2': { 'n_objects_used': 10, 'n_objects_discarded': 12 } } _combine_benefit_basis(basis, example) self.assertEqual(basis, example) def test_group_basis_combine_new_group(self): # New groups are added basis = {'group1': {'n_objects_used': 5, 'n_objects_discarded': 8}} new_group = { 'group2': { 'n_objects_used': 13, 'n_objects_discarded': 4 } } target = { 'group1': { 'n_objects_used': 5, 'n_objects_discarded': 8 }, 'group2': { 'n_objects_used': 13, 'n_objects_discarded': 4 } } _combine_benefit_basis(basis, new_group) self.assertEqual(basis, target) def test_group_basis_combine_existing_groups(self): basis = {'group1': {'n_objects_used': 5, 'n_objects_discarded': 8}} update_group = { 'group1': { 'n_objects_used': 13, 'n_objects_discarded': 4 } } target = {'group1': {'n_objects_used': 18, 'n_objects_discarded': 12}} _combine_benefit_basis(basis, update_group) self.assertEqual(basis, target) def test_combine_benefit_groups_empty(self): # with and without currency base_group = { 'group1': { 'benefit1': { 'value': 3, 'currency': 9, 'unit': 'gal', 'label': BenefitCategory.STORMWATER, 'unit-name': 'eco' }, 'benefit2': { 'value': 3, 'currency': 9, 'unit': 'gal', 'label': BenefitCategory.STORMWATER, 'unit-name': 'eco' } } } groups = {} _combine_grouped_benefits(groups, base_group) self.assertEqual(groups, base_group) def test_combine_benefit_groups_no_overlap(self): base_group = { 'group1': { 'benefit1': { 'value': 3, 'currency': 9, 'unit': 'gal', 'label': BenefitCategory.STORMWATER, 'unit-name': 'eco' }, 'benefit2': { 'value': 4, 'currency': 10, 'unit': 'gal', 'label': BenefitCategory.STORMWATER, 'unit-name': 'eco' } } } new_group = { 'group2': { 'benefit1': { 'value': 5, 'currency': 11, 'unit': 'gal', 'label': BenefitCategory.STORMWATER, 'unit-name': 'eco' }, 'benefit2': { 'value': 6, 'currency': 19, 'unit': 'gal', 'label': BenefitCategory.STORMWATER, 'unit-name': 'eco' } } } groups = {} _combine_grouped_benefits(groups, base_group) _combine_grouped_benefits(groups, new_group) target = { 'group1': base_group['group1'], 'group2': new_group['group2'] } self.assertEqual(groups, target) def test_combine_benefit_groups_sums_benefits(self): base_group = { 'group1': { 'benefit1': { 'value': 3, 'unit': 'gal', 'label': BenefitCategory.STORMWATER, 'unit-name': 'eco' }, 'benefit2': { 'value': 4, 'currency': 10, 'unit': 'gal', 'label': BenefitCategory.STORMWATER, 'unit-name': 'eco' }, 'benefit3': { 'value': 32, 'currency': 919, 'unit': 'gal', 'label': BenefitCategory.STORMWATER, 'unit-name': 'eco' } } } new_group = { 'group1': { 'benefit1': { 'value': 5, 'currency': 11, 'unit': 'gal', 'label': BenefitCategory.STORMWATER, 'unit-name': 'eco' }, 'benefit2': { 'value': 7, 'unit': 'gal', 'currency': 19, 'label': BenefitCategory.STORMWATER, 'unit-name': 'eco' }, 'benefit4': { 'value': 7, 'unit': 'gal', 'label': BenefitCategory.STORMWATER, 'unit-name': 'eco' } } } groups = {} _combine_grouped_benefits(groups, base_group) _combine_grouped_benefits(groups, new_group) target = { 'group1': { 'benefit1': { 'value': 8, 'currency': 11, 'unit': 'gal', 'label': BenefitCategory.STORMWATER, 'unit-name': 'eco' }, 'benefit2': { 'value': 11, 'currency': 29, 'unit': 'gal', 'label': BenefitCategory.STORMWATER, 'unit-name': 'eco' }, 'benefit3': { 'value': 32, 'currency': 919, 'unit': 'gal', 'label': BenefitCategory.STORMWATER, 'unit-name': 'eco' }, 'benefit4': { 'value': 7, 'unit': 'gal', 'label': BenefitCategory.STORMWATER, 'unit-name': 'eco' } } } self.assertEqual(groups, target) def test_annotates_basis(self): basis = { 'group1': { 'n_objects_used': 5, 'n_objects_discarded': 15 }, 'group2': { 'n_objects_used': 2, 'n_objects_discarded': 18 } } target = { 'group1': { 'n_objects_used': 5, 'n_objects_discarded': 15, 'n_total': 20, 'n_pct_calculated': 0.25 }, 'group2': { 'n_objects_used': 2, 'n_objects_discarded': 18, 'n_total': 20, 'n_pct_calculated': 0.1 } } _annotate_basis_with_extra_stats(basis) self.assertEqual(basis, target)
def test_scientific_name_genus(self): s = Species(genus='Ulmus') self.assertEquals(s.scientific_name, 'Ulmus')
def test_scientific_name_genus_cultivar(self): s = Species(genus='Ulmus', cultivar='Columella') self.assertEquals(s.scientific_name, "Ulmus 'Columella'")
def test_scientific_name_all(self): s = Species(genus='Ulmus', species='rubra', cultivar='Columella') self.assertEquals(s.scientific_name, "Ulmus rubra 'Columella'")
def setUp(self): super(ExportSpeciesTaskTest, self).setUp() species = Species(common_name='foo', instance=self.instance) species.save_with_user(self.user)
def test_all_tree_data(self): s1_gsc = Species(instance=self.instance, genus='g1', species='s1', cultivar='c1') s1_gsc.save_with_system_user_bypass_auth() csv = """ | point x | point y | diameter | tree height | | 45.53 | 31.1 | 23.1 | 90.1 | """ ieid = self.run_through_commit_views(csv) ie = TreeImportEvent.objects.get(pk=ieid) tree = ie.treeimportrow_set.all()[0].plot.current_tree() self.assertEqual(tree.diameter, 23.1) self.assertEqual(tree.height, 90.1) csv = """ | point x | point y | canopy height | genus | species | cultivar | | 45.59 | 31.1 | 112 | | | | | 45.58 | 33.9 | | g1 | s1 | c1 | """ ieid = self.run_through_commit_views(csv) ie = TreeImportEvent.objects.get(pk=ieid) rows = ie.treeimportrow_set.order_by('idx').all() tree1 = rows[0].plot.current_tree() tree2 = rows[1].plot.current_tree() self.assertEqual(tree1.canopy_height, 112) self.assertIsNone(tree1.species) self.assertEqual(tree2.species.pk, s1_gsc.pk) # TODO: READONLY restore when implemented # csv = """ # | point x | point y | date planted | read only | # | 25.00 | 25.00 | 2012-02-03 | true | # """ csv = """ | point x | point y | date planted | | 25.00 | 25.00 | 2012-02-03 | """ ieid = self.run_through_commit_views(csv) ie = TreeImportEvent.objects.get(pk=ieid) tree = ie.treeimportrow_set.all()[0].plot.current_tree() dateplanted = date(2012, 2, 3) self.assertEqual(tree.date_planted, dateplanted) # TODO: READONLY restore when implemented # self.assertEqual(tree.readonly, True) psycopg2.extras.register_hstore(connection.cursor(), globally=True) UserDefinedFieldDefinition.objects.create( model_type='Plot', name='Flatness', datatype=json.dumps({'type': 'choice', 'choices': ['very', 'hardly', 'not']}), iscollection=False, instance=self.instance, ) UserDefinedFieldDefinition.objects.create( model_type='Tree', name='Cuteness', datatype=json.dumps({'type': 'choice', 'choices': ['lots', 'not much', 'none']}), iscollection=False, instance=self.instance, ) csv = """ | point x | point y | tree: cuteness | plot: flatness | | 26.00 | 26.00 | not much | very | """ ieid = self.run_through_commit_views(csv) ie = TreeImportEvent.objects.get(pk=ieid) plot = ie.treeimportrow_set.all()[0].plot tree = plot.current_tree() self.assertEqual(plot.udfs['Flatness'], 'very') self.assertEqual(tree.udfs['Cuteness'], 'not much')
def setUp(self): ###### # Request/Render mock ###### def local_render_to_response(*args, **kwargs): from django.template import loader, RequestContext from django.http import HttpResponse httpresponse_kwargs = {'mimetype': kwargs.pop('mimetype', None)} hr = HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs) if hasattr(args[1], 'dicts'): hr.request_context = args[1].dicts return hr django.shortcuts.render_to_response = local_render_to_response ###### # Content types ###### r1 = ReputationAction(name="edit verified", description="blah") r2 = ReputationAction(name="edit tree", description="blah") r3 = ReputationAction(name="Administrative Action", description="blah") r4 = ReputationAction(name="add tree", description="blah") r5 = ReputationAction(name="edit plot", description="blah") r6 = ReputationAction(name="add plot", description="blah") r7 = ReputationAction(name="add stewardship", description="blah") r8 = ReputationAction(name="remove stewardship", description="blah") self.ra = [r1, r2, r3, r4, r5, r6, r7, r8] for r in self.ra: r.save() ###### # Set up benefit values ###### bv = BenefitValues(co2=0.02, pm10=9.41, area="InlandValleys", electricity=0.1166, voc=4.69, ozone=5.0032, natural_gas=1.25278, nox=12.79, stormwater=0.0078, sox=3.72, bvoc=4.96) bv.save() self.bv = bv dbh = "[1.0, 2.0, 3.0]" rsrc = Resource(meta_species="BDM_OTHER", electricity_dbh=dbh, co2_avoided_dbh=dbh, aq_pm10_dep_dbh=dbh, region="Sim City", aq_voc_avoided_dbh=dbh, aq_pm10_avoided_dbh=dbh, aq_ozone_dep_dbh=dbh, aq_nox_avoided_dbh=dbh, co2_storage_dbh=dbh, aq_sox_avoided_dbh=dbh, aq_sox_dep_dbh=dbh, bvoc_dbh=dbh, co2_sequestered_dbh=dbh, aq_nox_dep_dbh=dbh, hydro_interception_dbh=dbh, natural_gas_dbh=dbh) rsrc.save() self.rsrc = rsrc ###### # Users ###### u = User.objects.filter(username="******") if u: u = u[0] else: u = User.objects.create_user("jim", "*****@*****.**", "jim") u.is_staff = True u.is_superuser = True u.save() up = UserProfile(user=u) u.reputation = Reputation(user=u) u.reputation.save() self.u = u ####### # Setup geometries -> Two stacked 100x100 squares ####### n1geom = MultiPolygon( Polygon(((0, 0), (100, 0), (100, 100), (0, 100), (0, 0)))) n2geom = MultiPolygon( Polygon(((0, 101), (101, 101), (101, 200), (0, 200), (0, 101)))) n1 = Neighborhood(name="n1", region_id=2, city="c1", state="PA", county="PAC", geometry=n1geom) n2 = Neighborhood(name="n2", region_id=2, city="c2", state="NY", county="NYC", geometry=n2geom) n1.save() n2.save() z1geom = MultiPolygon( Polygon(((0, 0), (100, 0), (100, 100), (0, 100), (0, 0)))) z2geom = MultiPolygon( Polygon(((0, 100), (100, 100), (100, 200), (0, 200), (0, 100)))) z1 = ZipCode(zip="19107", geometry=z1geom) z2 = ZipCode(zip="10001", geometry=z2geom) z1.save() z2.save() exgeom1 = MultiPolygon( Polygon(((0, 0), (25, 0), (25, 25), (0, 25), (0, 0)))) ex1 = ExclusionMask(geometry=exgeom1, type="building") ex1.save() agn1 = AggregateNeighborhood(annual_stormwater_management=0.0, annual_electricity_conserved=0.0, annual_energy_conserved=0.0, annual_natural_gas_conserved=0.0, annual_air_quality_improvement=0.0, annual_co2_sequestered=0.0, annual_co2_avoided=0.0, annual_co2_reduced=0.0, total_co2_stored=0.0, annual_ozone=0.0, annual_nox=0.0, annual_pm10=0.0, annual_sox=0.0, annual_voc=0.0, annual_bvoc=0.0, total_trees=0, total_plots=0, location=n1) agn2 = AggregateNeighborhood(annual_stormwater_management=0.0, annual_electricity_conserved=0.0, annual_energy_conserved=0.0, annual_natural_gas_conserved=0.0, annual_air_quality_improvement=0.0, annual_co2_sequestered=0.0, annual_co2_avoided=0.0, annual_co2_reduced=0.0, total_co2_stored=0.0, annual_ozone=0.0, annual_nox=0.0, annual_pm10=0.0, annual_sox=0.0, annual_voc=0.0, annual_bvoc=0.0, total_trees=0, total_plots=0, location=n2) agn1.save() agn2.save() self.agn1 = agn1 self.agn2 = agn2 self.z1 = z1 self.z2 = z2 self.n1 = n1 self.n2 = n2 ###### # And we could use a few species... ###### s1 = Species(symbol="s1", genus="testus1", species="specieius1") s2 = Species(symbol="s2", genus="testus2", species="specieius2") s1.save() s2.save() self.s1 = s1 self.s2 = s2 ####### # Create some basic plots ####### ie = ImportEvent(file_name='site_add') ie.save() self.ie = ie p1_no_tree = Plot(geometry=Point(50, 50), last_updated_by=u, import_event=ie, present=True, data_owner=u) p1_no_tree.save() p2_tree = Plot(geometry=Point(51, 51), last_updated_by=u, import_event=ie, present=True, data_owner=u) p2_tree.save() p3_tree_species1 = Plot(geometry=Point(50, 100), last_updated_by=u, import_event=ie, present=True, data_owner=u) p3_tree_species1.save() p4_tree_species2 = Plot(geometry=Point(50, 150), last_updated_by=u, import_event=ie, present=True, data_owner=u) p4_tree_species2.save() t1 = Tree(plot=p2_tree, species=None, last_updated_by=u, import_event=ie) t1.present = True t1.save() t2 = Tree(plot=p3_tree_species1, species=s1, last_updated_by=u, import_event=ie) t2.present = True t2.save() t3 = Tree(plot=p4_tree_species2, species=s2, last_updated_by=u, import_event=ie) t3.present = True t3.save() self.p1_no_tree = p1_no_tree self.p2_tree = p2_tree self.p3_tree_species1 = p3_tree_species1 self.p4_tree_species2 = p4_tree_species2 self.plots = [p1_no_tree, p2_tree, p3_tree_species1, p4_tree_species2] self.t1 = t1 self.t2 = t2 self.t3 = t3
def test_all_tree_data(self): s1_gsc = Species(symbol='S1G__', scientific_name='',family='', genus='g1', species='s1', cultivar_name='c1') s1_gsc.save() csv = """ | point x | point y | tree owner | tree steward | diameter | tree height | | 45.53 | 31.1 | jimmy | jane | 23.1 | 90.1 | """ ieid = self.run_through_commit_views(csv) ie = TreeImportEvent.objects.get(pk=ieid) tree = ie.treeimportrow_set.all()[0].plot.current_tree() self.assertEqual(tree.tree_owner, 'jimmy') self.assertEqual(tree.steward_name, 'jane') self.assertEqual(tree.dbh, 23.1) self.assertEqual(tree.height, 90.1) csv = """ | point x | point y | canopy height | genus | species | cultivar | | 45.59 | 31.1 | 112 | | | | | 45.58 | 33.9 | | g1 | s1 | c1 | """ ieid = self.run_through_commit_views(csv) ie = TreeImportEvent.objects.get(pk=ieid) rows = ie.treeimportrow_set.order_by('idx').all() tree1 = rows[0].plot.current_tree() tree2 = rows[1].plot.current_tree() self.assertEqual(tree1.canopy_height, 112) self.assertIsNone(tree1.species) self.assertEqual(tree2.species.pk, s1_gsc.pk) csv = """ | point x | point y | tree sponsor | date planted | read only | tree url | | 45.12 | 55.12 | treeluvr | 2012-02-03 | true | http://spam | """ ieid = self.run_through_commit_views(csv) ie = TreeImportEvent.objects.get(pk=ieid) tree = ie.treeimportrow_set.all()[0].plot.current_tree() dateplanted = date(2012,2,3) self.assertEqual(tree.sponsor, 'treeluvr') self.assertEqual(tree.date_planted, dateplanted) self.assertEqual(tree.readonly, True) self.assertEqual(tree.url, 'http://spam') csv = """ | point x | point y | condition | canopy condition | pests and diseases | local projects | | 45.66 | 53.13 | Dead | %s | %s | %s | """ % ('Full - No Gaps', 'Phytophthora alni', 'San Francisco Landmark') ieid = self.run_through_commit_views(csv) ie = TreeImportEvent.objects.get(pk=ieid) tree = ie.treeimportrow_set.all()[0].plot.current_tree() self.assertEqual(tree.condition, '1') self.assertEqual(tree.canopy_condition, '1') self.assertEqual(tree.pests, '9')
class ModelUnicodeTests(OTMTestCase): def setUp(self): self.instance = make_instance(name='Test Instance') self.species = Species(instance=self.instance, common_name='Test Common Name', genus='Test Genus', cultivar='Test Cultivar', species='Test Species') self.species.save_base() self.user = make_user(username='******', password='******') self.plot = Plot(geom=Point(1, 1), instance=self.instance, address_street="123 Main Street") self.plot.save_base() self.tree = Tree(plot=self.plot, instance=self.instance) self.tree.save_base() self.boundary = make_simple_boundary("Test Boundary") self.role = Role(instance=self.instance, name='Test Role', rep_thresh=2) self.role.save() self.field_permission = FieldPermission( model_name="Tree", field_name="readonly", permission_level=FieldPermission.READ_ONLY, role=self.role, instance=self.instance) self.field_permission.save_base() self.audit = Audit(action=Audit.Type.Update, model="Tree", field="readonly", model_id=1, user=self.user, previous_value=True, current_value=False) self.audit.save_base() self.reputation_metric = ReputationMetric(instance=self.instance, model_name="Tree", action="Test Action") self.reputation_metric.save_base() def test_instance_model(self): self.assertEqual(unicode(self.instance), "Test Instance") def test_species_model(self): self.assertEqual( unicode(self.species), "Test Common Name [Test Genus Test Species 'Test Cultivar']") def test_user_model(self): self.assertEqual(unicode(self.user), 'commander') def test_plot_model(self): self.assertEqual(unicode(self.plot), 'Plot (1.0, 1.0) 123 Main Street') def test_tree_model(self): self.assertEqual(unicode(self.tree), '') def test_boundary_model(self): self.assertEqual(unicode(self.boundary), 'Test Boundary') def test_role_model(self): self.assertEqual(unicode(self.role), 'Test Role (%s)' % self.role.pk) def test_field_permission_model(self): self.assertEqual(unicode(self.field_permission), 'Tree.readonly - Test Role (%s) - Read Only' % self.role.pk) def test_audit_model(self): self.assertEqual( unicode(self.audit), 'pk=%s - action=Update - Tree.readonly:(1) - True => False' % self.audit.pk) def test_reputation_metric_model(self): self.assertEqual(unicode(self.reputation_metric), 'Test Instance - Tree - Test Action')
def test_scientific_name_genus_species(self): s = Species(genus='Ulmus', species='rubra') self.assertEquals(s.scientific_name, 'Ulmus rubra')
def setupTreemapEnv(): def local_render_to_response(*args, **kwargs): from django.template import loader from django.http import HttpResponse httpresponse_kwargs = {'mimetype': kwargs.pop('mimetype', None)} hr = HttpResponse( loader.render_to_string(*args, **kwargs), **httpresponse_kwargs) if hasattr(args[1], 'dicts'): hr.request_context = args[1].dicts return hr django.shortcuts.render_to_response = local_render_to_response instance = make_instance(is_public=True) make_user_with_default_role(instance, 'jim') commander = make_commander_user(instance, 'commander') make_apprentice_user(instance, 'apprentice') n1geom = MultiPolygon(Polygon( ((0, 0), (100, 0), (100, 100), (0, 100), (0, 0)))) n2geom = MultiPolygon( Polygon(((0, 101), (101, 101), (101, 200), (0, 200), (0, 101)))) n1 = Boundary(name="n1", category='blah', sort_order=4, geom=n1geom) n2 = Boundary(name="n2", category='blah', sort_order=4, geom=n2geom) n1.save() n2.save() s1 = Species(otm_code="s1", genus="testus1", species="specieius1", cultivar='', instance=instance) s2 = Species(otm_code="s2", genus="testus2", species="specieius2", cultivar='', instance=instance) s3 = Species(otm_code="s3", genus="testus2", species="specieius3", cultivar='', instance=instance) s1.native_status = True s1.fall_conspicuous = True s1.flower_conspicuous = True s1.palatable_human = True s2.native_status = True s2.fall_conspicuous = False s2.flower_conspicuous = True s2.palatable_human = False s2.wildlife_value = True s3.wildlife_value = True s1.save_with_user(commander) s2.save_with_user(commander) s3.save_with_user(commander) return instance
class ModelUnicodeTests(OTMTestCase): def setUp(self): self.instance = make_instance(name='Test Instance') self.species = Species(instance=self.instance, common_name='Test Common Name', genus='Test Genus', cultivar='Test Cultivar', species='Test Species') self.species.save_base() self.user = make_user(username='******', password='******') self.plot = Plot(geom=Point(1, 1), instance=self.instance, address_street="123 Main Street") self.plot.save_base() self.tree = Tree(plot=self.plot, instance=self.instance) self.tree.save_base() self.boundary = make_simple_boundary("Test Boundary") self.role = Role(instance=self.instance, name='Test Role', rep_thresh=2) self.role.save() self.field_permission = FieldPermission( model_name="Tree", field_name="readonly", permission_level=FieldPermission.READ_ONLY, role=self.role, instance=self.instance) self.field_permission.save_base() self.audit = Audit(action=Audit.Type.Update, model="Tree", field="readonly", model_id=1, user=self.user, previous_value=True, current_value=False) self.audit.save_base() self.reputation_metric = ReputationMetric(instance=self.instance, model_name="Tree", action="Test Action") self.reputation_metric.save_base() def test_instance_model(self): self.assertEqual(unicode(self.instance), "Test Instance") def test_species_model(self): self.assertEqual( unicode(self.species), "Test Common Name [Test Genus Test Species 'Test Cultivar']") def test_user_model(self): self.assertEqual(unicode(self.user), 'commander') def test_plot_model(self): self.assertEqual(unicode(self.plot), 'Plot (1.0, 1.0) 123 Main Street') def test_tree_model(self): self.assertEqual(unicode(self.tree), '') def test_boundary_model(self): self.assertEqual(unicode(self.boundary), 'Test Boundary') def test_role_model(self): self.assertEqual(unicode(self.role), 'Test Role (%s)' % self.role.pk) def test_field_permission_model(self): self.assertEqual( unicode(self.field_permission), 'Tree.readonly - Test Role (%s) - Read Only' % self.role.pk) def test_audit_model(self): self.assertEqual( unicode(self.audit), 'pk=%s - action=Update - Tree.readonly:(1) - True => False' % self.audit.pk) def test_reputation_metric_model(self): self.assertEqual(unicode(self.reputation_metric), 'Test Instance - Tree - Test Action')