Ejemplo n.º 1
0
    def test_should_call_function_only_once_called_with_the_same_parameter(
            self, tmpdir, mock_f: Mock):

        cached_f = create_str_cache(mock_f, str(tmpdir))
        cached_f(URL_1)
        assert cached_f(URL_1) == mock_f.return_value
        assert mock_f.call_count == 1
Ejemplo n.º 2
0
    def test_should_call_function_multiple_times_if_called_with_the_different_parameter(
            self, tmpdir, mock_f: Mock):

        cached_f = create_str_cache(mock_f, str(tmpdir))
        cached_f(URL_1)
        assert cached_f(URL_2) == mock_f.return_value
        assert mock_f.call_count == 2
        mock_f.assert_called_with(URL_2)
Ejemplo n.º 3
0
    def test_should_call_function_once_if_cache_has_not_yet_expired(
            self, tmpdir, mock_f: Mock, now: Mock, getmtime: Mock):

        now.return_value = datetime(2018, 1, 1)
        getmtime.return_value = now.return_value.timestamp()

        cached_f = create_str_cache(mock_f, str(tmpdir), expire_after_secs=10)
        cached_f(URL_1)

        now.return_value = now.return_value + timedelta(seconds=9)

        assert cached_f(URL_1) == mock_f.return_value
        assert mock_f.call_count == 1
Ejemplo n.º 4
0
 def test_should_call_function_with_unicode(self, tmpdir, mock_f: Mock):
     cached_f = create_str_cache(mock_f, str(tmpdir))
     assert cached_f(UNICODE_URL_1) == mock_f.return_value
     mock_f.assert_called_with(UNICODE_URL_1)
Ejemplo n.º 5
0
 def test_should_call_function_and_return_value_if_not_in_cache(self, tmpdir, mock_f: Mock):
     cached_f = create_str_cache(mock_f, str(tmpdir))
     assert cached_f(URL_1) == mock_f.return_value
     mock_f.assert_called_with(URL_1)