def test_merges(self): p1 = mkPlot(self.instance, self.user) p2 = mkPlot(self.instance, self.user) t1 = Tree(plot=p1, species=self.s1, instance=self.instance) t2 = Tree(plot=p2, species=self.s2, instance=self.instance) for tree in (t1, t2): tree.save_with_system_user_bypass_auth() r = HttpRequest() r.REQUEST = { 'species_to_delete': self.s1.pk, 'species_to_replace_with': self.s2.pk } r.user = self.user r.user.is_staff = True merge_species(r, self.instance) self.assertRaises(Species.DoesNotExist, Species.objects.get, pk=self.s1.pk) # Requery the Trees to assert that species has changed t1r = Tree.objects.get(pk=t1.pk) t2r = Tree.objects.get(pk=t2.pk) self.assertEqual(t1r.species.pk, self.s2.pk) self.assertEqual(t2r.species.pk, self.s2.pk)
def test_proximity(self): p1 = mkPlot(self.instance, self.user, geom=Point(25.0000001, 25.0000001, srid=4326)) p2 = mkPlot(self.instance, self.user, geom=Point(25.0000002, 25.0000002, srid=4326)) p3 = mkPlot(self.instance, self.user, geom=Point(25.0000003, 25.0000003, srid=4326)) p4 = mkPlot(self.instance, self.user, geom=Point(27.0000001, 27.0000001, srid=4326)) n1 = {p.pk for p in [p1, p2, p3]} n2 = {p4.pk} i = self.mkrow({'point x': '25.00000025', 'point y': '25.00000025'}) i.validate_row() self.assertHasError(i, errors.NEARBY_TREES, n1, set) i = self.mkrow({'point x': '27.00000015', 'point y': '27.00000015'}) i.validate_row() self.assertHasError(i, errors.NEARBY_TREES, n2, set) i = self.mkrow({'point x': '30.00000015', 'point y': '30.00000015'}) i.validate_row() self.assertNotHasError(i, errors.NEARBY_TREES)
def test_proximity(self): p1 = mkPlot(self.instance, self.user, geom=Point(25.0000001, 25.0000001, srid=4326)) p2 = mkPlot(self.instance, self.user, geom=Point(25.0000002, 25.0000002, srid=4326)) p3 = mkPlot(self.instance, self.user, geom=Point(25.0000003, 25.0000003, srid=4326)) p4 = mkPlot(self.instance, self.user, geom=Point(27.0000001, 27.0000001, srid=4326)) n1 = {p.pk for p in [p1, p2, p3]} n2 = {p4.pk} i = self.mkrow({"point x": "25.0000001", "point y": "25.0000001"}) i.validate_row() self.assertHasError(i, errors.DUPLICATE_TREE) i = self.mkrow({"point x": "25.00000025", "point y": "25.00000025"}) i.validate_row() self.assertHasError(i, errors.NEARBY_TREES, n1, set) i = self.mkrow({"point x": "27.00000015", "point y": "27.00000015"}) i.validate_row() self.assertHasError(i, errors.NEARBY_TREES, n2, set) i = self.mkrow({"point x": "30.00000015", "point y": "30.00000015"}) i.validate_row() self.assertNotHasError(i, errors.NEARBY_TREES)
def test_proximity(self): setupTreemapEnv() user = User.objects.get(username="******") bie1 = ImportEvent(file_name="bie1") bie2 = ImportEvent(file_name="bie2") bie3 = ImportEvent(file_name="bie3") bie4 = ImportEvent(file_name="bie4") for bie in [bie1, bie2, bie3, bie4]: bie.save() p1 = mkPlot(user, geom=Point(25.0000001,25.0000001)) p1.import_event = bie1 p1.save() p2 = mkPlot(user, geom=Point(25.0000002,25.0000002)) p2.import_event = bie2 p2.save() p3 = mkPlot(user, geom=Point(25.0000003,25.0000003)) p3.import_event = bie3 p3.save() p4 = mkPlot(user, geom=Point(27.0000001,27.0000001)) p4.import_event = bie4 p4.save() n1 = { p.pk for p in [p1,p2,p3] } n2 = { p4.pk } i = self.mkrow({'point x': '25.00000025', 'point y': '25.00000025'}) r = i.validate_row() self.assertHasError(i, errors.NEARBY_TREES, n1, set) i = self.mkrow({'point x': '27.00000015', 'point y': '27.00000015'}) r = i.validate_row() self.assertHasError(i, errors.NEARBY_TREES, n2, set) i = self.mkrow({'point x': '30.00000015', 'point y': '30.00000015'}) r = i.validate_row() self.assertNotHasError(i, errors.NEARBY_TREES)
def test_otm_id(self): # silly invalid-int-errors should be caught i = self.mkrow({"point x": "16", "point y": "20", "opentreemap plot id": "44b"}) r = i.validate_row() self.assertFalse(r) self.assertHasError(i, errors.INT_ERROR, None) i = self.mkrow({"point x": "25", "point y": "25", "opentreemap plot id": "-22"}) r = i.validate_row() self.assertFalse(r) self.assertHasError(i, errors.POS_INT_ERROR) # With no plots in the system, all ids should fail i = self.mkrow({"point x": "25", "point y": "25", "opentreemap plot id": "44"}) r = i.validate_row() self.assertFalse(r) self.assertHasError(i, errors.INVALID_OTM_ID) p = mkPlot(self.instance, self.user) # With an existing plot it should be fine i = self.mkrow({"point x": "25", "point y": "25", "opentreemap plot id": p.pk}) r = i.validate_row() self.assertNotHasError(i, errors.INVALID_OTM_ID) self.assertNotHasError(i, errors.INT_ERROR)
def test_faulty_data2(self): p1 = mkPlot(self.instance, self.user, geom=Point(25.0000001, 25.0000001, srid=4326)) string_too_long = 'a' * 256 csv = """ | point x | point y | opentreemap plot id | date planted | | 25.0000002 | 25.0000002 | | 2012-02-18 | | 25.1000002 | 25.1000002 | 133 | | | 25.1000002 | 25.1000002 | -3 | 2023-FF-33 | | 25.1000002 | 25.1000002 | bar | 2012-02-91 | | 25.1000002 | 25.1000002 | %s | | """ % (p1.pk) gflds = [fields.trees.POINT_X, fields.trees.POINT_Y] j = self.run_through_process_views(csv) ierrors = self.extract_errors(j) self.assertEqual(ierrors['0'], [(errors.NEARBY_TREES[0], gflds, [p1.pk])]) self.assertEqual(ierrors['1'], [(errors.INVALID_OTM_ID[0], [fields.trees.OPENTREEMAP_PLOT_ID], None)]) self.assertEqual( ierrors['2'], [(errors.POS_INT_ERROR[0], [fields.trees.OPENTREEMAP_PLOT_ID ], None), (errors.INVALID_DATE[0], [fields.trees.DATE_PLANTED], None)]) self.assertEqual( ierrors['3'], [(errors.INT_ERROR[0], [fields.trees.OPENTREEMAP_PLOT_ID], None), (errors.INVALID_DATE[0], [fields.trees.DATE_PLANTED], None)]) self.assertNotIn('4', ierrors)
def test_proximity(self): setupTreemapEnv() user = User.objects.get(username="******") bie1 = ImportEvent(file_name="bie1") bie2 = ImportEvent(file_name="bie2") bie3 = ImportEvent(file_name="bie3") bie4 = ImportEvent(file_name="bie4") for bie in [bie1, bie2, bie3, bie4]: bie.save() p1 = mkPlot(user, geom=Point(25.0000001, 25.0000001)) p1.import_event = bie1 p1.save() p2 = mkPlot(user, geom=Point(25.0000002, 25.0000002)) p2.import_event = bie2 p2.save() p3 = mkPlot(user, geom=Point(25.0000003, 25.0000003)) p3.import_event = bie3 p3.save() p4 = mkPlot(user, geom=Point(27.0000001, 27.0000001)) p4.import_event = bie4 p4.save() n1 = {p.pk for p in [p1, p2, p3]} n2 = {p4.pk} i = self.mkrow({'point x': '25.00000025', 'point y': '25.00000025'}) r = i.validate_row() self.assertHasError(i, errors.NEARBY_TREES, n1, set) i = self.mkrow({'point x': '27.00000015', 'point y': '27.00000015'}) r = i.validate_row() self.assertHasError(i, errors.NEARBY_TREES, n2, set) i = self.mkrow({'point x': '30.00000015', 'point y': '30.00000015'}) r = i.validate_row() self.assertNotHasError(i, errors.NEARBY_TREES)
def test_otm_id(self): # silly invalid-int-errors should be caught i = self.mkrow({ 'point x': '16', 'point y': '20', 'opentreemap id number': '44b' }) r = i.validate_row() self.assertFalse(r) self.assertHasError(i, errors.INT_ERROR, None) i = self.mkrow({ 'point x': '16', 'point y': '20', 'opentreemap id number': '-22' }) r = i.validate_row() self.assertFalse(r) self.assertHasError(i, errors.POS_INT_ERROR) # With no plots in the system, all ids should fail i = self.mkrow({ 'point x': '16', 'point y': '20', 'opentreemap id number': '44' }) r = i.validate_row() self.assertFalse(r) self.assertHasError(i, errors.INVALID_OTM_ID) # Add in plot setupTreemapEnv() # We need the whole darn thing # just so we can add a plot :( # SetupTME provides a special user for us to use # as well as particular neighborhood user = User.objects.get(username="******") p = mkPlot(user, geom=Point(25, 25)) p.save() # With an existing plot it should be fine i = self.mkrow({ 'point x': '16', 'point y': '20', 'opentreemap id number': p.pk }) r = i.validate_row() self.assertNotHasError(i, errors.INVALID_OTM_ID) self.assertNotHasError(i, errors.INT_ERROR)
def test_proximity(self): p1 = mkPlot(self.instance, self.user, geom=Point(25.0000001, 25.0000001, srid=4326)) p2 = mkPlot(self.instance, self.user, geom=Point(25.0000002, 25.0000002, srid=4326)) p3 = mkPlot(self.instance, self.user, geom=Point(25.0000003, 25.0000003, srid=4326)) p4 = mkPlot(self.instance, self.user, geom=Point(27.0000001, 27.0000001, srid=4326)) n1 = {p.pk for p in [p1, p2, p3]} n2 = {p4.pk} i = self.mkrow({'point x': '25.0000001', 'point y': '25.0000001'}) i.validate_row() self.assertHasError(i, errors.DUPLICATE_TREE) i = self.mkrow({'point x': '25.00000025', 'point y': '25.00000025'}) i.validate_row() self.assertHasError(i, errors.NEARBY_TREES, n1, set) i = self.mkrow({'point x': '27.00000015', 'point y': '27.00000015'}) i.validate_row() self.assertHasError(i, errors.NEARBY_TREES, n2, set) i = self.mkrow({'point x': '30.00000015', 'point y': '30.00000015'}) i.validate_row() self.assertNotHasError(i, errors.NEARBY_TREES)
def test_override_with_opentreemap_id(self): p1 = mkPlot(self.instance, self.user) csv = """ | point x | point y | opentreemap plot id | | 45.53 | 31.1 | %s | """ % p1.pk self.run_through_commit_views(csv) p1_geom = Plot.objects.get(pk=p1.pk).geom p1_geom.transform(4326) self.assertEqual(int(p1_geom.x*100), 4553) self.assertEqual(int(p1_geom.y*100), 3109)
def test_override_with_opentreemap_id(self): p1 = mkPlot(self.user, geom=Point(55.0,25.0)) p1.save() csv = """ | point x | point y | opentreemap id number | data source | | 45.53 | 31.1 | %s | trees r us | """ % p1.pk self.run_through_commit_views(csv) p1b = Plot.objects.get(pk=p1.pk) self.assertEqual(int(p1b.geometry.x*100), 4553) self.assertEqual(int(p1b.geometry.y*100), 3110)
def test_override_with_opentreemap_id(self): p1 = mkPlot(self.user, geom=Point(55.0, 25.0)) p1.save() csv = """ | point x | point y | opentreemap id number | data source | | 45.53 | 31.1 | %s | trees r us | """ % p1.pk self.run_through_commit_views(csv) p1b = Plot.objects.get(pk=p1.pk) self.assertEqual(int(p1b.geometry.x * 100), 4553) self.assertEqual(int(p1b.geometry.y * 100), 3110)
def test_override_with_opentreemap_id(self): p1 = mkPlot(self.instance, self.user) csv = """ | point x | point y | opentreemap plot id | | 45.53 | 31.1 | %s | """ % p1.pk self.run_through_commit_views(csv) p1_geom = Plot.objects.get(pk=p1.pk).geom p1_geom.transform(4326) self.assertEqual(int(p1_geom.x * 100), 4553) self.assertEqual(int(p1_geom.y * 100), 3109)
def test_otm_id(self): # silly invalid-int-errors should be caught i = self.mkrow({ 'point x': '16', 'point y': '20', 'opentreemap plot id': '44b' }) r = i.validate_row() self.assertFalse(r) self.assertHasError(i, errors.INT_ERROR, None) i = self.mkrow({ 'point x': '25', 'point y': '25', 'opentreemap plot id': '-22' }) r = i.validate_row() self.assertFalse(r) self.assertHasError(i, errors.POS_INT_ERROR) # With no plots in the system, all ids should fail i = self.mkrow({ 'point x': '25', 'point y': '25', 'opentreemap plot id': '44' }) r = i.validate_row() self.assertFalse(r) self.assertHasError(i, errors.INVALID_OTM_ID) p = mkPlot(self.instance, self.user) # With an existing plot it should be fine i = self.mkrow({ 'point x': '25', 'point y': '25', 'opentreemap plot id': p.pk }) r = i.validate_row() self.assertNotHasError(i, errors.INVALID_OTM_ID) self.assertNotHasError(i, errors.INT_ERROR)
def test_otm_id(self): # silly invalid-int-errors should be caught i = self.mkrow({'point x': '16', 'point y': '20', 'opentreemap id number': '44b'}) r = i.validate_row() self.assertFalse(r) self.assertHasError(i, errors.INT_ERROR, None) i = self.mkrow({'point x': '16', 'point y': '20', 'opentreemap id number': '-22'}) r = i.validate_row() self.assertFalse(r) self.assertHasError(i, errors.POS_INT_ERROR) # With no plots in the system, all ids should fail i = self.mkrow({'point x': '16', 'point y': '20', 'opentreemap id number': '44'}) r = i.validate_row() self.assertFalse(r) self.assertHasError(i, errors.INVALID_OTM_ID) # Add in plot setupTreemapEnv() # We need the whole darn thing # just so we can add a plot :( # SetupTME provides a special user for us to use # as well as particular neighborhood user = User.objects.get(username="******") p = mkPlot(user, geom=Point(25,25)) p.save() # With an existing plot it should be fine i = self.mkrow({'point x': '16', 'point y': '20', 'opentreemap id number': p.pk}) r = i.validate_row() self.assertNotHasError(i, errors.INVALID_OTM_ID) self.assertNotHasError(i, errors.INT_ERROR)
def test_faulty_data2(self): p1 = mkPlot(self.user, geom=Point(25.0000001,25.0000001)) p1.save() string_too_long = 'a' * 256 csv = """ | point x | point y | opentreemap id number | tree steward | date planted | | 25.0000002 | 25.0000002 | | | 2012-02-18 | | 25.1000002 | 25.1000002 | 133 | | | | 25.1000002 | 25.1000002 | -3 | | 2023-FF-33 | | 25.1000002 | 25.1000002 | bar | | 2012-02-91 | | 25.1000002 | 25.1000002 | %s | %s | | """ % (p1.pk, string_too_long) gflds = [fields.trees.POINT_X, fields.trees.POINT_Y] j = self.run_through_process_views(csv) ierrors = self.extract_errors(j) self.assertEqual(ierrors['0'], [(errors.NEARBY_TREES[0], gflds, [p1.pk])]) self.assertEqual(ierrors['1'], [(errors.INVALID_OTM_ID[0], [fields.trees.OPENTREEMAP_ID_NUMBER], None)]) self.assertEqual(ierrors['2'], [(errors.POS_INT_ERROR[0], [fields.trees.OPENTREEMAP_ID_NUMBER], None), (errors.INVALID_DATE[0], [fields.trees.DATE_PLANTED], None)]) self.assertEqual(ierrors['3'], [(errors.INT_ERROR[0], [fields.trees.OPENTREEMAP_ID_NUMBER], None), (errors.INVALID_DATE[0], [fields.trees.DATE_PLANTED], None)]) self.assertEqual(ierrors['4'], [(errors.STRING_TOO_LONG[0], [fields.trees.STEWARD], None)])
def test_faulty_data2(self): p1 = mkPlot(self.instance, self.user, geom=Point(25.0000001, 25.0000001, srid=4326)) string_too_long = 'a' * 256 csv = """ | point x | point y | opentreemap plot id | date planted | | 25.0000002 | 25.0000002 | | 2012-02-18 | | 25.1000002 | 25.1000002 | 133 | | | 25.1000002 | 25.1000002 | -3 | 2023-FF-33 | | 25.1000002 | 25.1000002 | bar | 2012-02-91 | | 25.1000002 | 25.1000002 | %s | | """ % (p1.pk) gflds = [fields.trees.POINT_X, fields.trees.POINT_Y] j = self.run_through_process_views(csv) ierrors = self.extract_errors(j) self.assertEqual(ierrors['0'], [(errors.NEARBY_TREES[0], gflds, [p1.pk])]) self.assertEqual(ierrors['1'], [(errors.INVALID_OTM_ID[0], [fields.trees.OPENTREEMAP_PLOT_ID], None)]) self.assertEqual(ierrors['2'], [(errors.POS_INT_ERROR[0], [fields.trees.OPENTREEMAP_PLOT_ID], None), (errors.INVALID_DATE[0], [fields.trees.DATE_PLANTED], None)]) self.assertEqual(ierrors['3'], [(errors.INT_ERROR[0], [fields.trees.OPENTREEMAP_PLOT_ID], None), (errors.INVALID_DATE[0], [fields.trees.DATE_PLANTED], None)]) self.assertNotIn('4', ierrors)
def test_otm_id(self): # silly invalid-int-errors should be caught i = self.mkrow({'point x': '16', 'point y': '20', 'opentreemap plot id': '44b'}) r = i.validate_row() self.assertFalse(r) self.assertHasError(i, errors.INT_ERROR, None) i = self.mkrow({'point x': '25', 'point y': '25', 'opentreemap plot id': '-22'}) r = i.validate_row() self.assertFalse(r) self.assertHasError(i, errors.POS_INT_ERROR) # With no plots in the system, all ids should fail i = self.mkrow({'point x': '25', 'point y': '25', 'opentreemap plot id': '44'}) r = i.validate_row() self.assertFalse(r) self.assertHasError(i, errors.INVALID_OTM_ID) p = mkPlot(self.instance, self.user) # With an existing plot it should be fine i = self.mkrow({'point x': '25', 'point y': '25', 'opentreemap plot id': p.pk}) r = i.validate_row() self.assertNotHasError(i, errors.INVALID_OTM_ID) self.assertNotHasError(i, errors.INT_ERROR)
def test_faulty_data2(self): p1 = mkPlot(self.user, geom=Point(25.0000001, 25.0000001)) p1.save() string_too_long = 'a' * 256 csv = """ | point x | point y | opentreemap id number | tree steward | date planted | | 25.0000002 | 25.0000002 | | | 2012-02-18 | | 25.1000002 | 25.1000002 | 133 | | | | 25.1000002 | 25.1000002 | -3 | | 2023-FF-33 | | 25.1000002 | 25.1000002 | bar | | 2012-02-91 | | 25.1000002 | 25.1000002 | %s | %s | | """ % (p1.pk, string_too_long) gflds = [fields.trees.POINT_X, fields.trees.POINT_Y] j = self.run_through_process_views(csv) ierrors = self.extract_errors(j) self.assertEqual(ierrors['0'], [(errors.NEARBY_TREES[0], gflds, [p1.pk])]) self.assertEqual(ierrors['1'], [(errors.INVALID_OTM_ID[0], [fields.trees.OPENTREEMAP_ID_NUMBER], None)]) self.assertEqual( ierrors['2'], [(errors.POS_INT_ERROR[0], [fields.trees.OPENTREEMAP_ID_NUMBER ], None), (errors.INVALID_DATE[0], [fields.trees.DATE_PLANTED], None)]) self.assertEqual( ierrors['3'], [(errors.INT_ERROR[0], [fields.trees.OPENTREEMAP_ID_NUMBER], None), (errors.INVALID_DATE[0], [fields.trees.DATE_PLANTED], None)]) self.assertEqual( ierrors['4'], [(errors.STRING_TOO_LONG[0], [fields.trees.STEWARD], None)])