<commit_before>"""Tests for the search (sphinx) app."""
import os
import shutil
import socket
import time
import urllib

from django.test import TestCase, client
from django.utils import translation

import jingo
import mock
from nose import SkipTest
from nose.tools import eq_, assert_raises
from mock import Mock
from pyquery import PyQuery as pq
import test_utils

import amo
from amo.urlresolvers import reverse
from amo.tests.test_helpers import render
from manage import settings
from search import forms, views
from search.utils import start_sphinx, stop_sphinx, reindex, convert_version
from search.client import (Client as SearchClient, CollectionsClient,
                           PersonasClient, SearchError, get_category_id,
                           extract_from_query)
from addons.models import Addon, Category
from tags.models import Tag


def test_convert_version():

    def c(x, y):
        x = convert_version(x)
        y = convert_version(y)

        if (x > y):
            return 1
        elif (x < y):
            return - 1

        return 0

    v = ['1.9.0a1pre', '1.9.0a1', '1.9.1.b5', '1.9.1.b5', '1.9.1pre',
         '1.9.1', '1.9.0']

    eq_(c(v[0], v[1]), -1)
    eq_(c(v[1], v[2]), -1)
    eq_(c(v[2], v[3]), 0)
    eq_(c(v[3], v[4]), -1)
    eq_(c(v[4], v[5]), -1)
    eq_(c(v[5], v[6]), 1)


def test_extract_from_query():
    """Test that the correct terms are extracted from query strings."""

    eq_(("yslow ", "3.4",),
        extract_from_query("yslow voo:3.4", "voo", "[0-9.]+"))


def test_parse_bad_type():
    """
    Given a type that doesn't exist, we should not throw a KeyError.

    Note: This does not require sphinx to be running.
    """
    c = client.Client()
    try:
        c.get("/en-US/firefox/api/1.2/search/firebug%20type:dict")
    except KeyError:  # pragma: no cover
        assert False, ("We should not throw a KeyError just because we had a "
                       "nonexistent addon type.")


class SphinxTestCase(test_utils.TransactionTestCase):<commit_msg>fixing search tests<commit_after>"""Tests for the search (sphinx) app."""
import os
import shutil
import socket
import time
import urllib

from django.test import TestCase, client
from django.utils import translation

import jingo
import mock
from nose import SkipTest
from nose.tools import eq_, assert_raises
from mock import Mock
from pyquery import PyQuery as pq
import test_utils

import amo
import amo.test_utils
from amo.urlresolvers import reverse
from amo.tests.test_helpers import render
from manage import settings
from search import forms, views
from search.utils import start_sphinx, stop_sphinx, reindex, convert_version
from search.client import (Client as SearchClient, CollectionsClient,
                           PersonasClient, SearchError, get_category_id,
                           extract_from_query)
from addons.models import Addon, Category
from tags.models import Tag


def test_convert_version():

    def c(x, y):
        x = convert_version(x)
        y = convert_version(y)

        if (x > y):
            return 1
        elif (x < y):
            return - 1

        return 0

    v = ['1.9.0a1pre', '1.9.0a1', '1.9.1.b5', '1.9.1.b5', '1.9.1pre',
         '1.9.1', '1.9.0']

    eq_(c(v[0], v[1]), -1)
    eq_(c(v[1], v[2]), -1)
    eq_(c(v[2], v[3]), 0)
    eq_(c(v[3], v[4]), -1)
    eq_(c(v[4], v[5]), -1)
    eq_(c(v[5], v[6]), 1)


def test_extract_from_query():
    """Test that the correct terms are extracted from query strings."""

    eq_(("yslow ", "3.4",),
        extract_from_query("yslow voo:3.4", "voo", "[0-9.]+"))


def test_parse_bad_type():
    """
    Given a type that doesn't exist, we should not throw a KeyError.

    Note: This does not require sphinx to be running.
    """
    c = client.Client()
    try:
        c.get("/en-US/firefox/api/1.2/search/firebug%20type:dict")
    except KeyError:  # pragma: no cover
        assert False, ("We should not throw a KeyError just because we had a "
                       "nonexistent addon type.")


class SphinxTestCase(amo.test_utils.ExtraSetup, test_utils.TransactionTestCase):
{'fraction_non_alphanumeric': 0.12187424132070891, 'fraction_numerical': 0.027191065792668122, 'mean_word_length': 2.8254410399257197, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5354352', 'n_tokens_mistral': 1405, 'n_tokens_neox': 1366, 'n_words': 426}