def test_filter_data_re(self): data = self.get_data_2() # ~ result = sdt.filter_data(data, 'name', '~', 'tom|dick').index.tolist() mask = data['name'] \ .apply(lambda x: re.search('tom|dick', x, flags=re.I)).astype(bool) expected = data[mask].index.tolist() self.assertEqual(result, expected) # !~ result = sdt.filter_data(data, 'name', '!~', 'tom|dick').index.tolist() mask = data['name'] \ .apply(lambda x: not bool(re.search('tom|dick', x, flags=re.I))) expected = data[mask].index.tolist() self.assertEqual(result, expected)
def test_filter_data_lte(self): data = self.get_data_2() result = sdt.filter_data(data, 'age', '<=', 23).index.tolist() mask = data['age'].apply(lambda x: x <= 23) expected = data[mask].index.tolist() self.assertEqual(result, expected)
def test_filter_data_errors(self): data = self.get_data_2() # data with self.assertRaises(EnforceError): sdt.filter_data('id', 'age', '==', 24) # column expected = 'Column must be a str. 123 is not str.' with self.assertRaisesRegexp(EnforceError, expected): sdt.filter_data(data, 123, '==', 24) expected = 'Given columns not found in data. ' expected += r"\['pizza'\] not in " expected += r"\['id', 'age', 'name', 'group', 'date'\]\." with self.assertRaisesRegexp(EnforceError, expected): sdt.filter_data(data, 'pizza', '==', 24) # comparator expected = 'Illegal comparator. ' expected += r"= not in \[==, !=, >, >=, <, <=, ~, !~\]\." with self.assertRaisesRegexp(EnforceError, expected): sdt.filter_data(data, 'age', '=', 24) # regex comparator expected = 'Value must be string if comparator is ~ or !~. 24 is not str.' with self.assertRaisesRegexp(EnforceError, expected): sdt.filter_data(data, 'age', '~', 24) expected = 'Value must be string if comparator is ~ or !~. 24 is not str.' with self.assertRaisesRegexp(EnforceError, expected): sdt.filter_data(data, 'age', '!~', 24)