<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
test_ntpdatetime
----------------------------------

Tests for `ntpdatetime` module.
"""
import unittest
from datetime import datetime
from socket import gaierror

from mock import Mock, patch
from ntplib import NTPClient, NTPException

from ntpdatetime import config, now, ntpdatetime

ntp_exception_mock = Mock()
ntp_exception_mock.side_effect = NTPException

socket_gaierror_mock = Mock()
socket_gaierror_mock.side_effect = gaierror


class TestNtpdatetime(unittest.TestCase):

    def test_instance(self):
        ntp_dt = ntpdatetime(2002, 12, 25)
        self.assertTrue(isinstance(ntp_dt, datetime))
        ntp_now, fetched = now()
        self.assertTrue(fetched)  # Fetched from NTP server
        self.assertTrue(isinstance(ntp_now, datetime))

    @patch.object(NTPClient, 'request', ntp_exception_mock)
    def test_ntp_exception(self):
        ntp_now, fetched = now()
        self.assertEqual(ntp_exception_mock.call_count,
                         self.poolservers)
        self.assertFalse(fetched)  # Not fetched from NTP server
        self.assertTrue(isinstance(ntp_now, datetime))

    @patch.object(NTPClient, 'request', socket_gaierror_mock)
    def test_gai_error(self):
        ntp_now, fetched = now()
        self.assertEqual(socket_gaierror_mock.call_count,<commit_msg>Mock all request<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
test_ntpdatetime
----------------------------------

Tests for `ntpdatetime` module.
"""
import unittest
from datetime import datetime
from socket import gaierror

from mock import Mock, patch
from ntplib import NTPClient, NTPException, NTPStats

from ntpdatetime import config, now, ntpdatetime

ntp_exception_mock = Mock()
ntp_exception_mock.side_effect = NTPException

socket_gaierror_mock = Mock()
socket_gaierror_mock.side_effect = gaierror


class TestNtpdatetime(unittest.TestCase):

    @patch.object(NTPClient, 'request')
    def test_instance(self, mock):
        ntp_dt = ntpdatetime(2002, 12, 25)
        self.assertTrue(isinstance(ntp_dt, datetime))
        mock.return_value = NTPStats()
        ntp_now, fetched = now()
        self.assertTrue(fetched)  # Fetched from NTP server
        self.assertTrue(isinstance(ntp_now, datetime))

    @patch.object(NTPClient, 'request', ntp_exception_mock)
    def test_ntp_exception(self):
        ntp_now, fetched = now()
        self.assertEqual(ntp_exception_mock.call_count,
                         self.poolservers)
        self.assertFalse(fetched)  # Not fetched from NTP server
        self.assertTrue(isinstance(ntp_now, datetime))

    @patch.object(NTPClient, 'request', socket_gaierror_mock)
    def test_gai_error(self):
        ntp_now, fetched = now()
        self.assertEqual(socket_gaierror_mock.call_count,
{'fraction_non_alphanumeric': 0.11508498583569406, 'fraction_numerical': 0.006373937677053824, 'mean_word_length': 3.4003115264797508, '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': '1277608', 'n_tokens_mistral': 911, 'n_tokens_neox': 890, 'n_words': 187}