<commit_before>""" Testing Local Notes Module
"""

import unittest
import fakeredis
from connectors import redis_notes
from mock import MagicMock, patch, call
from ddt import ddt, data, unpack
from utils.search import get_search_request


@ddt
class LocalNotesTests(unittest.TestCase):
    """Unit Testing Local Notes"""

    def setUp(self):
        """Setup fake redis for testing."""
        self.redis = fakeredis.FakeStrictRedis()


    def tearDown(self):
        """Clear data in fakeredis."""
        self.redis.flushall()


    @unpack
    @data(
        ('redis_notes', True),
        ('redis_notes, local_notes', True),
        ('local_notes', False),
    )
    @patch('utils.configuration.load_config')
    def test_is_enabled(self, string, expectation, load_config):
        """Verifies is_enabled is properly functioning"""

        mock_config = MagicMock()
        mock_config.get.return_value = string
        load_config.return_value = mock_config

        self.assertEqual(expectation, redis_notes.is_enabled())


    @patch('connectors.redis_notes.add_redis_note')
    def test_save_note(self, add_redis_note):
        """Verifies save_note is properly functioning"""
        redis_notes.save_note(12345, "test note")
        add_redis_note.assert_called_once_with(12345, "test note")

    @patch('connectors.redis_notes.delete_redis_note')
    def test_delete_note(self, delete_redis_note):
        """Verifies delete_note is properly functioning"""
        redis_notes.delete_note(12345)
        delete_redis_note.assert_called_once_with(12345)


    @patch('connectors.redis_notes.save_note')
    @patch('connectors.redis_notes.delete_redis_note')
    def test_update_note(self, delete_redis_note, save_note):
        """Verifies update_note is properly functioning"""
        redis_notes.update_note(12345, 23456, "test note")
        delete_redis_note.assert_called_once_with(12345)
        save_note.assert_called_once_with(23456, "test note")


    @patch('utils.search.get_search_request')
    @patch('connectors.redis_notes.find_redis_notes')
    def test_find_notes(self, find_redis_notes, get_search_request):
        """Verifies update_note is properly functioning"""
        redis_notes.find_notes(["test note"])
        get_search_request.assert_called_once_with(["test note"])
        find_redis_notes.assert_called_once()


    @patch('connectors.redis_notes.get_redis_connection')
    def test_add_redis_note(self, get_redis_connection):
        """Verifies add_redis_note is properly functioning"""
        get_redis_connection.return_value = self.redis
        redis_notes.add_redis_note(1373500800, "today: this is a simple test #yolo")

        keys = self.redis.keys()
        keys.sort()

        self.assertListEqual(
            [
                b'day_11',
                b'hour_0',
                b'month_7',
                b'note_1373500800',
                b'w_#yolo',
                b'w_a',
                b'w_is',
                b'w_simple',
                b'w_test',
                b'w_this',
                b'w_today',
                b'w_yolo',
                b'weekday_3',
                b'year_2013'
            ], keys
        )


    @patch('connectors.redis_notes.get_redis_connection')
    def test_delete_redis_note(self, get_redis_connection):
        """Verifies add_redis_note is properly functioning"""
        get_redis_connection.return_value = self.redis
        redis_notes.add_redis_note(1373500800, "today: this is a simple test #yolo")
        redis_notes.add_redis_note(1450794188, "today: this is note 2")

        keys = self.redis.keys()
        keys.sort()

        self.assertListEqual(
            [
                b'day_11',
                b'day_22',
                b'hour_0',
                b'hour_14',
                b'month_12',
                b'month_7',
                b'note_1373500800',
                b'note_1450794188',
                b'w_#yolo',
                b'w_2',<commit_msg>Added additional coverage for delete redis note<commit_after>""" Testing Local Notes Module
"""

import unittest
import fakeredis
from connectors import redis_notes
from mock import MagicMock, patch, call
from ddt import ddt, data, unpack
from utils.search import get_search_request


@ddt
class LocalNotesTests(unittest.TestCase):
    """Unit Testing Local Notes"""

    def setUp(self):
        """Setup fake redis for testing."""
        self.redis = fakeredis.FakeStrictRedis()


    def tearDown(self):
        """Clear data in fakeredis."""
        self.redis.flushall()


    @unpack
    @data(
        ('redis_notes', True),
        ('redis_notes, local_notes', True),
        ('local_notes', False),
    )
    @patch('utils.configuration.load_config')
    def test_is_enabled(self, string, expectation, load_config):
        """Verifies is_enabled is properly functioning"""

        mock_config = MagicMock()
        mock_config.get.return_value = string
        load_config.return_value = mock_config

        self.assertEqual(expectation, redis_notes.is_enabled())


    @patch('connectors.redis_notes.add_redis_note')
    def test_save_note(self, add_redis_note):
        """Verifies save_note is properly functioning"""
        redis_notes.save_note(12345, "test note")
        add_redis_note.assert_called_once_with(12345, "test note")

    @patch('connectors.redis_notes.delete_redis_note')
    def test_delete_note(self, delete_redis_note):
        """Verifies delete_note is properly functioning"""
        redis_notes.delete_note(12345)
        delete_redis_note.assert_called_once_with(12345)


    @patch('connectors.redis_notes.save_note')
    @patch('connectors.redis_notes.delete_redis_note')
    def test_update_note(self, delete_redis_note, save_note):
        """Verifies update_note is properly functioning"""
        redis_notes.update_note(12345, 23456, "test note")
        delete_redis_note.assert_called_once_with(12345)
        save_note.assert_called_once_with(23456, "test note")


    @patch('utils.search.get_search_request')
    @patch('connectors.redis_notes.find_redis_notes')
    def test_find_notes(self, find_redis_notes, get_search_request):
        """Verifies update_note is properly functioning"""
        redis_notes.find_notes(["test note"])
        get_search_request.assert_called_once_with(["test note"])
        find_redis_notes.assert_called_once()


    @patch('connectors.redis_notes.get_redis_connection')
    def test_add_redis_note(self, get_redis_connection):
        """Verifies add_redis_note is properly functioning"""
        get_redis_connection.return_value = self.redis
        redis_notes.add_redis_note(1373500800, "today: this is a simple test #yolo")

        keys = self.redis.keys()
        keys.sort()

        self.assertListEqual(
            [
                b'day_11',
                b'hour_0',
                b'month_7',
                b'note_1373500800',
                b'w_#yolo',
                b'w_a',
                b'w_is',
                b'w_simple',
                b'w_test',
                b'w_this',
                b'w_today',
                b'w_yolo',
                b'weekday_3',
                b'year_2013'
            ], keys
        )


    @patch('connectors.redis_notes.get_redis_connection')
    def test_delete_redis_note_miss(self, get_redis_connection):
        """Verifies add_redis_note is properly functioning"""
        get_redis_connection.return_value = self.redis
        redis_notes.add_redis_note(1373500800, "today: this is note 1")
        redis_notes.add_redis_note(1450794188, "today: this is note 2")
        redis_notes.delete_redis_note(1373500801)
        self.assertEqual(b"today: this is note 1", self.redis.get('note_1373500800'))
        self.assertEqual(b"today: this is note 2", self.redis.get('note_1450794188'))


    @patch('connectors.redis_notes.get_redis_connection')
    def test_delete_redis_note_hit(self, get_redis_connection):
        """Verifies add_redis_note is properly functioning"""
        get_redis_connection.return_value = self.redis
        redis_notes.add_redis_note(1373500800, "today: this is a simple test #yolo")
        redis_notes.add_redis_note(1450794188, "today: this is note 2")

        keys = self.redis.keys()
        keys.sort()

        self.assertListEqual(
            [
                b'day_11',
                b'day_22',
                b'hour_0',
                b'hour_14',
                b'month_12',
                b'month_7',
                b'note_1373500800',
                b'note_1450794188',
                b'w_#yolo',
                b'w_2',
{'fraction_non_alphanumeric': 0.10162224797219004, 'fraction_numerical': 0.03429895712630359, 'mean_word_length': 2.5917602996254683, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6943515', 'n_tokens_mistral': 2974, 'n_tokens_neox': 2671, 'n_words': 477}