def test_non_standard_params(self):
     """
     Non-standard parameters don't trigger reading the cache.
     """
     dl = daily_location("2016-01-01", level="cell")
     table_name = dl.table_name
     dl.store().result()
     dl2 = daily_location("2016-01-01", level="cell", method="most-common")
     self.assertFalse(table_name in dl2.get_query())
     dl.invalidate_db_cache()
 def setUp(self):
     self.dls = [
         daily_location("2016-01-01 18:00:00",
                        stop="2016-01-02 06:00:00",
                        method="most-common"),
         daily_location("2016-01-02 18:00:00",
                        stop="2016-01-03 06:00:00",
                        method="most-common"),
         daily_location("2016-01-03 18:00:00",
                        stop="2016-01-04 06:00:00",
                        method="most-common"),
     ]
    def test_hours(self):
        """
        Test that daily locations handles the hours parameter
        """

        # Lower level test test that subsetdates handles this correctly
        # we're just testing that it is passed on in this case.

        dl1 = daily_location("2016-01-01", level="cell")
        dl2 = daily_location("2016-01-01", level="cell", hours=(19, 23))
        dl3 = daily_location("2016-01-01", level="cell", hours=(19, 20))

        self.assertTrue(len(dl1) > len(dl2) > len(dl3))
    def test_can_be_stored(self):
        """
        We can store a daily_location() in the right place.
        """

        schema = "cache"
        dl = daily_location("2016-01-01", level="cell")
        table_name = dl.table_name.split(".")[1]
        dl.store().result()
        self.assertTrue(dl.connection.has_table(table_name, schema=schema))
        dl = daily_location("2016-01-01", level="cell")
        self.assertTrue(table_name in dl.get_query())
        dl.invalidate_db_cache()
    def test_daily_locs_errors(self):
        """
        daily_location() errors when we ask for a date that does not exist.
        """

        with self.assertRaises(MissingDateError):
            dl = daily_location("2016-01-31")
    def test_can_return_df(self):
        """
        daily_location() can be initialised and return a dataframe.
        """

        dl = daily_location("2016-01-01")
        self.assertIs(type(dl.get_dataframe()), pd.DataFrame)
    def setUp(self):

        self.hl = HomeLocation(*[
            daily_location(d)
            for d in list_of_dates("2016-01-01", "2016-01-03")
        ])
        self.hdf = self.hl.get_dataframe()
        self.hdf = self.hl.get_dataframe().set_index("subscriber")
    def test_works_with_pcods(self):
        """
        We can get daily locations with p-codes rather than the standard names.
        """

        dl = daily_location("2016-01-05",
                            level="admin3",
                            column_name="admin3pcod")
        df = dl.get_dataframe()
        self.assertTrue(df.admin3pcod[0].startswith("524"))
    def test_storing_updates(self):
        """
        Storing updates the query after storing.
        """

        dl = daily_location("2016-01-01", level="cell")
        dl.store().result()
        sql = dl.get_query()
        self.assertTrue(sql.startswith("SELECT * FROM cache"))
        dl.invalidate_db_cache()
    def test_equivalent_to_locate_subscribers(self):
        """
        daily_location() is equivalent to the MostFrequentLocation().
        """
        mfl = MostFrequentLocation("2016-01-01", "2016-01-02")
        mfl_df = mfl.get_dataframe()

        dl = daily_location("2016-01-01", method="most-common")
        dl_df = dl.get_dataframe()

        self.assertTrue((dl_df == mfl_df).all().all())
 def test_can_be_aggregated_latlong(self):
     """
     Query can be aggregated to a spatial level with lat-lon data.
     """
     hl = HomeLocation(*[
         daily_location(d, level="lat-lon", method="last")
         for d in list_of_dates("2016-01-01", "2016-01-03")
     ])
     agg = hl.aggregate()
     df = agg.get_dataframe()
     self.assertIs(type(df), pd.DataFrame)
     self.assertEqual(list(df.columns), ["lat", "lon", "total"])
 def test_force(self):
     """
     We can force an overwrite of the table.
     """
     dl = daily_location("2016-01-01", level="cell")
     dl.store().result()
     sql = "DELETE FROM {}".format(dl.table_name)
     dl.connection.engine.execute(sql)
     # This should not raise an error
     dl.store().result()
     self.assertTrue(len(dl) == 0)
     dl.store(force=True)
     self.assertTrue(len(dl.head()) != 0)
     dl.invalidate_db_cache()
    def test_equivalent_to_locate_subscribers_with_time(self):
        """
        daily_location() is equivalent to the MostFrequentLocation() with timestamps.
        """
        mfl = MostFrequentLocation("2016-01-01 18:00:00",
                                   "2016-01-02 06:00:00")
        mfl_df = mfl.get_dataframe()

        dl = daily_location("2016-01-01 18:00:00",
                            stop="2016-01-02 06:00:00",
                            method="most-common")
        dl_df = dl.get_dataframe()

        self.assertTrue((dl_df == mfl_df).all().all())