<commit_before>from __future__ import unicode_literals
__author__ = 'laharah'

import pytest
import mock
import os
import shutil

from fixtures import *
import httpretty

import calibre_access.calibre_access as calibre_access
import calibre_access.utilities as utilities


def test_download_database(mock_geolite_download):
    with temp_user_dir():
        shutil.rmtree(calibre_access.USER_DIR)
        assert calibre_access.download_database() == os.path.join(calibre_access.USER_DIR,
                                                                  'GeoLiteCity.dat')
        assert os.path.exists(calibre_access.USER_DIR)
        assert not os.path.exists(os.path.join(calibre_access.USER_DIR,
                                               'GeoLiteCity.dat.gz'))
        with open(os.path.join(calibre_access.USER_DIR, 'GeoLiteCity.dat')) as fin:
            assert fin.read() == 'Mocked geolite data...'


@pytest.mark.usefixtures('mock_platform')
class TestLocateLogsOSX():
    p_form = 'Darwin'

    def test_get_search_dir(self):
        assert calibre_access.get_search_dir() == os.path.expanduser(
            '~/Library/Preferences/calibre')

    def test_osx(self, mock_access_logs_default):
        assert calibre_access.locate_logs() == mock_access_logs_default

    def test_osx_current_folder(self, mock_access_logs_local, user_expansion):
        assert calibre_access.locate_logs() == mock_access_logs_local

    def test_osx_missing(self, user_expansion, tmpdir):
        p = tmpdir.chdir()
        try:
            with pytest.raises(IOError):
                print(calibre_access.locate_logs())
        finally:
            p.chdir()


@pytest.mark.usefixtures('mock_platform')
class TestLocateLogsWindows():
    p_form = 'Windows'

    def test_get_search_dir(self, monkeypatch, user_expansion):
        assert calibre_access.get_search_dir() == os.path.join(user_expansion, 'calibre')

    def test_windows(self, monkeypatch, mock_access_logs_default):
        assert calibre_access.locate_logs() == mock_access_logs_default

    def test_windows_current_folder(self, monkeypatch, mock_access_logs_local):
        monkeypatch.setenv('APPDATA', 'NON EXISTENT!')
        assert calibre_access.locate_logs() == mock_access_logs_local

    def test_windows_missing(self, monkeypatch, tmpdir):
        try:
            p = tmpdir.chdir()
            monkeypatch.setenv('APPDATA', 'NON EXISTENT')
            with pytest.raises(IOError):
                calibre_access.locate_logs()
        finally:
            p.chdir()


@pytest.mark.usefixtures('mock_platform')
class TestLocateLogslinux():
    p_form = 'Linux'

    def test_get_search_dir(self):
        assert calibre_access.get_search_dir() == os.path.expanduser('~/.config/calibre')

    def test_linux(self, mock_access_logs_default):
        assert calibre_access.locate_logs() == mock_access_logs_default

    def test_linux_current_folder(self, mock_access_logs_local, user_expansion):
        assert calibre_access.locate_logs() == mock_access_logs_local

    def test_linux_missing(self, user_expansion, tmpdir):
        p = tmpdir.chdir()
        try:
            with pytest.raises(IOError):
                calibre_access.locate_logs()
        finally:
            p.chdir()


@mock.patch('calibre_access.calibre_access.pygeoip.GeoIP', autospec=True)
class TestGetDatabase():
    def test_missing_dat(self, mock_geo, mock_geolite_download):
        with temp_user_dir():
            result = calibre_access.get_database()
            assert result == mock_geo.return_value

    def test_current_dat(self, mock_geo, mock_geolite_dat):
        assert calibre_access.get_database() == mock_geo.return_value

    def test_dat_too_old(self, mock_geo, mock_geolite_dat, mock_geolite_download):
        t = os.path.getmtime(mock_geolite_dat)
        t -= 2628000
        os.utime(mock_geolite_dat, (t, t))
        result = calibre_access.get_database()
        assert result == mock_geo.return_value
        assert os.path.getmtime(mock_geolite_dat) - t >= 2628000

    def test_old_dat_no_download(self, mock_geo, mock_geolite_dat, monkeypatch):
        def error_download():
            raise calibre_access.requests.ConnectionError('no internet')

        monkeypatch.setattr('calibre_access.calibre_access.download_database',
                            error_download)
        t = os.path.getmtime(mock_geolite_dat)
        t -= 2628000
        os.utime(mock_geolite_dat, (t, t))
        result = calibre_access.get_database()
        assert result == mock_geo.return_value
        assert abs(os.path.getmtime(mock_geolite_dat) - t) < 1

    @pytest.mark.skipif(sys.version_info.major < 3, reason="mock problem")
    def test_force_refresh(self, mock_geo, mock_geolite_download, mock_geolite_dat):
        httpretty.reset()
        result = calibre_access.get_database()
        assert httpretty.has_request() == False
        assert result == mock_geo.return_value
        result = calibre_access.get_database(force_refresh=True)
        assert result == mock_geo.return_value
        assert httpretty.has_request() == True


def test_search_coro():
    coro = calibre_access.search_coro()
    next(coro)
    line = '172.16.58.3 - - [20/Oct/2014:08:18:26] "GET /browse/search?query=dresden+files HTTP/1.1" 200 7814 "http://localhost:8080/browse/search?query=iron+druid" "Mozilla/5.0 (Linux; U; Android 4.2.2; en-us; KFAPWI Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Silk/3.32 like Chrome/34.0.1847.137 Safari/537.36"'
    result = coro.send(line)<commit_msg>added warning capture and assertion for old database tests<commit_after>from __future__ import unicode_literals
__author__ = 'laharah'

import pytest
import mock
import os
import shutil

from fixtures import *
import httpretty

import calibre_access.calibre_access as calibre_access
import calibre_access.utilities as utilities


def test_download_database(mock_geolite_download):
    with temp_user_dir():
        shutil.rmtree(calibre_access.USER_DIR)
        assert calibre_access.download_database() == os.path.join(calibre_access.USER_DIR,
                                                                  'GeoLiteCity.dat')
        assert os.path.exists(calibre_access.USER_DIR)
        assert not os.path.exists(os.path.join(calibre_access.USER_DIR,
                                               'GeoLiteCity.dat.gz'))
        with open(os.path.join(calibre_access.USER_DIR, 'GeoLiteCity.dat')) as fin:
            assert fin.read() == 'Mocked geolite data...'


@pytest.mark.usefixtures('mock_platform')
class TestLocateLogsOSX():
    p_form = 'Darwin'

    def test_get_search_dir(self):
        assert calibre_access.get_search_dir() == os.path.expanduser(
            '~/Library/Preferences/calibre')

    def test_osx(self, mock_access_logs_default):
        assert calibre_access.locate_logs() == mock_access_logs_default

    def test_osx_current_folder(self, mock_access_logs_local, user_expansion):
        assert calibre_access.locate_logs() == mock_access_logs_local

    def test_osx_missing(self, user_expansion, tmpdir):
        p = tmpdir.chdir()
        try:
            with pytest.raises(IOError):
                print(calibre_access.locate_logs())
        finally:
            p.chdir()


@pytest.mark.usefixtures('mock_platform')
class TestLocateLogsWindows():
    p_form = 'Windows'

    def test_get_search_dir(self, monkeypatch, user_expansion):
        assert calibre_access.get_search_dir() == os.path.join(user_expansion, 'calibre')

    def test_windows(self, monkeypatch, mock_access_logs_default):
        assert calibre_access.locate_logs() == mock_access_logs_default

    def test_windows_current_folder(self, monkeypatch, mock_access_logs_local):
        monkeypatch.setenv('APPDATA', 'NON EXISTENT!')
        assert calibre_access.locate_logs() == mock_access_logs_local

    def test_windows_missing(self, monkeypatch, tmpdir):
        try:
            p = tmpdir.chdir()
            monkeypatch.setenv('APPDATA', 'NON EXISTENT')
            with pytest.raises(IOError):
                calibre_access.locate_logs()
        finally:
            p.chdir()


@pytest.mark.usefixtures('mock_platform')
class TestLocateLogslinux():
    p_form = 'Linux'

    def test_get_search_dir(self):
        assert calibre_access.get_search_dir() == os.path.expanduser('~/.config/calibre')

    def test_linux(self, mock_access_logs_default):
        assert calibre_access.locate_logs() == mock_access_logs_default

    def test_linux_current_folder(self, mock_access_logs_local, user_expansion):
        assert calibre_access.locate_logs() == mock_access_logs_local

    def test_linux_missing(self, user_expansion, tmpdir):
        p = tmpdir.chdir()
        try:
            with pytest.raises(IOError):
                calibre_access.locate_logs()
        finally:
            p.chdir()


@mock.patch('calibre_access.calibre_access.pygeoip.GeoIP', autospec=True)
class TestGetDatabase():
    def test_missing_dat(self, mock_geo, mock_geolite_download):
        with temp_user_dir():
            result = calibre_access.get_database()
            assert result == mock_geo.return_value

    def test_current_dat(self, mock_geo, mock_geolite_dat):
        assert calibre_access.get_database() == mock_geo.return_value

    def test_dat_too_old(self, mock_geo, mock_geolite_dat, mock_geolite_download):
        t = os.path.getmtime(mock_geolite_dat)
        t -= 2628000
        os.utime(mock_geolite_dat, (t, t))
        result = calibre_access.get_database()
        assert result == mock_geo.return_value
        assert os.path.getmtime(mock_geolite_dat) - t >= 2628000

    def test_old_dat_no_download(self, mock_geo, mock_geolite_dat, monkeypatch):
        def error_download():
            raise calibre_access.requests.ConnectionError('no internet')

        monkeypatch.setattr('calibre_access.calibre_access.download_database',
                            error_download)
        t = os.path.getmtime(mock_geolite_dat)
        t -= 2628000
        os.utime(mock_geolite_dat, (t, t))
        with pytest.warns(UserWarning):
            result = calibre_access.get_database()
        assert result == mock_geo.return_value
        assert abs(os.path.getmtime(mock_geolite_dat) - t) < 1

    @pytest.mark.skipif(sys.version_info.major < 3, reason="mock problem")
    def test_force_refresh(self, mock_geo, mock_geolite_download, mock_geolite_dat):
        httpretty.reset()
        result = calibre_access.get_database()
        assert httpretty.has_request() == False
        assert result == mock_geo.return_value
        with pytest.warns(UserWarning):
            result = calibre_access.get_database(force_refresh=True)
        assert result == mock_geo.return_value
        assert httpretty.has_request() == True


def test_search_coro():
    coro = calibre_access.search_coro()
    next(coro)
    line = '172.16.58.3 - - [20/Oct/2014:08:18:26] "GET /browse/search?query=dresden+files HTTP/1.1" 200 7814 "http://localhost:8080/browse/search?query=iron+druid" "Mozilla/5.0 (Linux; U; Android 4.2.2; en-us; KFAPWI Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Silk/3.32 like Chrome/34.0.1847.137 Safari/537.36"'
    result = coro.send(line)
{'fraction_non_alphanumeric': 0.0991015034249622, 'fraction_numerical': 0.015301129792723068, 'mean_word_length': 3.152936830439601, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '636853', 'n_tokens_mistral': 3888, 'n_tokens_neox': 3685, 'n_words': 605}