def test_contact_reference_location_stats(get_dataframe, statistic, msisdn, spatial_unit_type, want): """ Test a few hand-picked ContactReferenceLocationStats. """ cb = ContactBalance("2016-01-01", "2016-01-03") ml = ModalLocation(*[ daily_location( d, spatial_unit=make_spatial_unit(spatial_unit_type), subscriber_subset=cb.counterparts_subset(include_subscribers=True), ) for d in list_of_dates("2016-01-01", "2016-01-03") ]) cb.store() ml.store() query = ContactReferenceLocationStats(cb, ml, statistic=statistic) df = get_dataframe(query).set_index("subscriber") assert df.value[msisdn] == pytest.approx(want)
def test_contact_reference_location_stats_custom_geometry(get_dataframe): """ Test ContactReferenceLocationStats with custom geometry column. """ cb = ContactBalance("2016-01-01", "2016-01-03") ml = ModalLocation(*[ daily_location( d, spatial_unit=make_spatial_unit("versioned-cell"), subscriber_subset=cb.counterparts_subset(include_subscribers=True), ) for d in list_of_dates("2016-01-01", "2016-01-03") ]) cb.store() ml.store() ml = CustomQuery( f"SELECT subscriber, ST_POINT(lon, lat) AS loc FROM ({ml.get_query()}) _", ["subscriber", "loc"], ) query = ContactReferenceLocationStats(cb, ml, statistic="avg", geom_column="loc") df = get_dataframe(query).set_index("subscriber") assert df.value["gwAynWXp4eWvxGP7"] == pytest.approx(298.7215)