I developed a function that returns the US state in which a point is, based on latitude and longitude.
get_state.py:
import geopandas as gpd
def geopandas_read_file(filename):
return gpd.read_file(filename)
def get_region_info(lat, lon):
# Create geometry object with (lat, lon) point
point = g.Point(lat, lon)
# Read U.S. States Shapefiles
gdf_states = geopandas_read_file("USA_States.zip")
# Query State
states_query = gdf_states[gdf_states.geometry.intersects(point)]
retun states_query.STATE_ABBR.values[0]
And as it is good practice I want to develop tests for this function and add them to the CI/CD pipeline. The files will not be imported to the repository, that's why I think I need to patch/mock the file.
test_get_state.py
import get_state as gt
from unittest.mock import patch
f = open("gdf_states_head(5).txt", "r")
print(f.read())
@patch('get_state.geopandas_read_file', side_effect=[f])
def test_happy_path(test_some_file):
assert gt.get_region_info(-74.50, 40.34) == 'NJ'
assert test_some_file.call_count == 1
The previous version replaced geopandas_read_file("USA_States.zip")
with gpd.read_file("USA_States.zip")
, but I decided to change it to how it is because this way I could patch/mock the function.
The problem is that when I run pytest test_get_state.py
it returns AttributeError: '_io.TextIOWrapper' object has no attribute 'geometry'
I thought I was going in the right direction but apart from uploading the files to the repository I don't see what else I can do.