def test_pass_kwargs(db, db_path, geocoder): # geocode it once geocode_table(db, TABLE_NAME, geocoder, "{id}") runner = CliRunner() result = runner.invoke( cli, [ "mapbox", str(db_path), TABLE_NAME, # already geocoded, so no calls "--location", "{id}", "--bbox", "-71.553765", "42.163302", "-70.564995", "42.533755", "--proximity", "-71.0", "42.3", "--api-key", "fake-key", ], ) assert 0 == result.exit_code
def test_label_results(db, geocoder): table = db[TABLE_NAME] # geocode it once geocode_table(db, TABLE_NAME, geocoder, "{id}") for row in table.rows: assert "geocoder" in row assert row["geocoder"] == geocoder.__class__.__name__
def test_resume_table(db, geocoder): table = db[TABLE_NAME] # geocode it once geocode_table(db, TABLE_NAME, geocoder, "{id}") # undo it for some results, to pretend we're resuming texas = list(table.rows_where('"state" = "TX"')) for row in texas: table.update(row["id"], {"latitude": None, "longitude": None}) count = geocode_table(db, TABLE_NAME, geocoder, "{id}") assert count == len(texas)
def test_geocode_list(db, geocoder): table = db[TABLE_NAME] utah = list(table.pks_and_rows_where('"state" = "UT"')) assert len(utah) == 10 gen = geocode_list(utah, geocoder.geocode, "{id}") assert inspect.isgenerator(gen) done = list(gen) # geocode the whole table, to cheeck results geocode_table(db, TABLE_NAME, geocoder, "{id}") for pk, row, success in done: assert success assert row == table.get(pk)
def test_spatialite_geocode_table(db, geocoder): db.init_spatialite() table = db[TABLE_NAME] geo_table = db[GEO_TABLE] geocode_table(db, TABLE_NAME, geocoder, "{id}", spatialite=True) for pk, row in table.pks_and_rows_where(): assert "latitude" not in row assert "longitude" not in row assert type(row.get("geometry")) == bytes expected = json.loads(geo_table.get(pk)["geometry"]) geometry = json.loads( db.execute("select AsGeoJSON(?)", [row["geometry"]]).fetchone()[0]) assert geometry["type"] == expected["type"] assert expected["coordinates"] == pytest.approx( geometry["coordinates"])
def test_rate_limiting(db, db_path, geocoder): table = db[TABLE_NAME] runner = CliRunner() # geocode once geocode_table(db, TABLE_NAME, geocoder, "{id}") # un-geocode Utah, which has 10 locations utah = list(table.rows_where('"state" = "UT"')) assert len(utah) == 10 for row in utah: table.update(row["id"], {"latitude": None, "longitude": None}) # re-geocode those 10 rows, with a --delay argument # and time it start = datetime.datetime.now() result = runner.invoke( cli, [ "test", str(db_path), str(TABLE_NAME), "-p", str(db_path), "--location", "{id}", "--delay", "1", ], ) end = datetime.datetime.now() diff = end - start print(result.stdout) assert 0 == result.exit_code assert diff.total_seconds() >= len(utah) - 1 # delay is after, so one less
def test_geocode_table(db, geocoder): table = db[TABLE_NAME] geo_table = db[GEO_TABLE] assert "latitude" not in table.columns_dict assert "longitude" not in table.columns_dict count = geocode_table(db, TABLE_NAME, geocoder, "{id}") # did we get the whole table? assert count == table.count for row in table.rows: assert type(row.get("latitude")) == float assert type(row.get("longitude")) == float result = geo_table.get(row["id"])