def test_get_image_returns_previous_image_when_no_messages_match(self): notification = ImageNotification() updater = ImageNotificationUpdater() updater.add_image_filter("/tmp/happy.png", ContainsTextFilter("123")) updater.add_image_filter("/tmp/default.png") updater.update(notification, Message("123")) self.assertEqual('/tmp/happy.png', notification.get_image_path()) updater.update(notification, Message("Test")) self.assertEqual('/tmp/happy.png', notification.get_image_path())
def test_image_output_produced_contains_url_for_last_match_if_no_current_images_match( self): notification = ImageDependingOnMessageContent() notification.add_image_filter("/tmp/happy.png", ContainsTextFilter("123")) notification.add_image_filter("/tmp/default.png") image_output = notification.create([Message("123")]) self.assertEqual('/tmp/happy.png', image_output.image_path) image_output = notification.create([Message("Test")]) self.assertEqual('/tmp/happy.png', image_output.image_path)
def test_get_image_returns_correct_url_for_filtered_messages(self): notification = ImageNotification() updater = ImageNotificationUpdater() updater.add_image_filter("/tmp/happy.png", ContainsTextFilter("123")) updater.add_image_filter("/tmp/sad.png", MatchesRegexFilter("[4-6]+")) updater.update(notification, Message("123")) self.assertEqual('/tmp/happy.png', notification.get_image_path()) updater.update(notification, Message("456")) self.assertEqual('/tmp/sad.png', notification.get_image_path())
def test_image_output_produced_contains_url_for_filter_that_matches_the_last_message( self): notification = ImageDependingOnMessageContent() notification.add_image_filter("/tmp/happy.png", ContainsTextFilter("123")) notification.add_image_filter("/tmp/sad.png", MatchesRegexFilter("[4-6]+")) image_output = notification.create([Message("123")]) self.assertEqual('/tmp/happy.png', image_output.image_path) image_output = notification.create([Message("456")]) self.assertEqual('/tmp/sad.png', image_output.image_path)
def _create_filter(image_config_section): pattern_exists = "if-matches" in image_config_section contains_exists = "if-contains" in image_config_section if not pattern_exists and not contains_exists: raise MissingRequiredOptionException( "Expected either 'if-contains' or 'if-matches' option to exist" ) if pattern_exists and contains_exists: raise MissingRequiredOptionException( "Expected either 'if-contains' or 'if-matches' option, but not both" ) if pattern_exists: return MatchesRegexFilter(image_config_section["if-matches"]) else: return ContainsTextFilter(image_config_section["if-contains"])
def test_filter_true_if_entity_contains_text(self): entity = Message('1') self.assertTrue(ContainsTextFilter('1').filter(entity))
def test_filter_true_if_matching_text_empty(self): entity = Message('1') self.assertTrue(ContainsTextFilter('').filter(entity))
def test_filter_false_if_entity_does_not_contain_text(self): entity = Message('2') self.assertFalse(ContainsTextFilter('1').filter(entity))