def test_call_with_str_raises_error(): """ Numeric subset can't be called with a string in arguments low and high """ rog = RadiusOfGyration("2016-01-01", "2016-01-02") with pytest.raises(TypeError): rog.numeric_subset(col="value", low="foo", high=1) with pytest.raises(TypeError): rog.numeric_subset(col="value", low=1, high="bar")
def test_num_subset_can_be_stored(get_dataframe): """ Test that flowmachine.NumericSubset can be stored. """ rog = RadiusOfGyration("2016-01-01", "2016-01-02") low = 150 high = 155 rog_df = get_dataframe(rog).query("{low} <= value <= {high}".format( low=low, high=high)) sub = rog.numeric_subset(col="value", low=low, high=high) sub.store().result() assert sub.is_stored # Test that the store is of the right length sub = rog.numeric_subset(col="value", low=low, high=high) assert len(get_dataframe(sub)) == len(rog_df)
def test_can_numsubset_with_inf(get_dataframe): """ flowmachine.RadiusOfGyration can be subset between -Inf and Inf """ rog = RadiusOfGyration("2016-01-01", "2016-01-02") low = -float("Infinity") high = float("Infinity") sub = get_dataframe(rog.numeric_subset(col="value", low=low, high=high)) df = get_dataframe(rog).query("{low} <= value <= {high}".format(low=low, high=high)) pd.testing.assert_frame_equal(sub, df)
def test_can_numsubset_with_low_and_high(get_dataframe): """ flowmachine.RadiusOfGyration can be subset within a range """ rog = RadiusOfGyration("2016-01-01", "2016-01-02") low = 150 high = 155 rog_df = (get_dataframe(rog).query("{low} <= value <= {high}".format( low=low, high=high)).set_index("subscriber")) sub = get_dataframe(rog.numeric_subset(col="value", low=low, high=high)).set_index("subscriber") pd.testing.assert_frame_equal(sub, rog_df)
class TestNumericSubsetting(TestCase): def setUp(self): self.rog = RadiusOfGyration("2016-01-01", "2016-01-02") self.low = 150 self.high = 155 self.rog_df = self.rog.get_dataframe().query( "{low} <= rog <= {high}".format(low=self.low, high=self.high)) self.sub = self.rog.numeric_subset(col="rog", low=self.low, high=self.high) def _query_has_values(self, Q, expected_df): """ Test if the values of a dataframes columns are equal to certain values. """ query_df = Q.get_dataframe() assert_array_equal(expected_df.values, query_df.values) def test_can_numsubset_with_low_and_high(self): """ flowmachine.RadiusOfGyration can be subset within a range """ self._query_has_values(self.sub, self.rog_df) def test_can_numsubset_with_inf(self): """ flowmachine.RadiusOfGyration can be subset between -Inf and Inf """ low = -float("Infinity") high = float("Infinity") sub = self.rog.numeric_subset(col="rog", low=low, high=high) df = self.rog.get_dataframe().query("{low} <= rog <= {high}".format( low=low, high=high)) self._query_has_values(sub, df) def test_call_with_str_raises_error(self): """ Numeric subset can't be called with a string in arguments low and high """ with self.assertRaises(TypeError): self.rog.numeric_subset(col="rog", low="foo", high=self.high) with self.assertRaises(TypeError): self.rog.numeric_subset(col="rog", low=self.low, high="bar") def test_can_be_stored(self): """ Test that flowmachine.NumericSubset can be stored. """ self.sub.store().result() self.assertTrue(self.sub.is_stored) # Test that the store is of the right length sub = self.rog.numeric_subset(col="rog", low=self.low, high=self.high) self.assertEqual(len(sub.get_dataframe()), len(self.rog_df)) def tearDown(self): """ Remove stored table from "can_be_stored" test. """ self.sub.invalidate_db_cache()