コード例 #1
0
class MultiExactMatchTestCase(AutocompleterTestCase):
    fixtures = ['stock_test_data_small.json', 'indicator_test_data_small.json']

    def setUp(self):
        super(MultiExactMatchTestCase, self).setUp()
        setattr(auto_settings, 'MAX_EXACT_MATCH_WORDS', 10)

        self.autocomp = Autocompleter("mixed")
        self.autocomp.store_all()
        

    def tearDown(self):
        setattr(auto_settings, 'MAX_EXACT_MATCH_WORDS', 0)
        self.autocomp.remove_all()

    def test_exact_suggest(self):
        """
        Exact matching works in multi-provider autocompleters
        """
        matches = self.autocomp.exact_suggest('ma')
        self.assertEqual(len(matches['stock']), 1)

        matches = self.autocomp.exact_suggest('US Unemployment Rate')
        self.assertEqual(len(matches['ind']), 1)

    def test_move_exact_matches_to_top(self):
        """
        MOVE_EXACT_MATCHES_TO_TOP works in multi-provider autocompleters
        """
        matches = self.autocomp.suggest('Ma')
        setattr(auto_settings, 'MOVE_EXACT_MATCHES_TO_TOP', True)
        matches2 = self.autocomp.suggest('Ma')
        self.assertNotEqual(matches['stock'][0]['search_name'], matches2['stock'][0]['search_name'])
        setattr(auto_settings, 'MOVE_EXACT_MATCHES_TO_TOP', False)
コード例 #2
0
    def test_store_and_remove_all_basic_with_caching(self):
        """
        Storing and removing items all the once works with caching turned on
        """
        # Let's turn on caching because that will store things in Redis and we want to make
        # sure we clean them up.
        setattr(auto_settings, 'CACHE_TIMEOUT', 3600)

        autocomp = Autocompleter("stock")

        autocomp.store_all()
        keys = self.redis.hkeys('djac.stock')
        self.assertEqual(len(keys), 101)

        autocomp = Autocompleter("stock")
        for i in range(0, 3):
            autocomp.suggest('a')
            autocomp.suggest('z')
            autocomp.exact_suggest('aapl')
            autocomp.exact_suggest('xyz')

        autocomp.remove_all()
        keys = self.redis.keys('djac.stock*')
        self.assertEqual(len(keys), 0)

        # Must set the setting back to where it was as it will persist
        setattr(auto_settings, 'CACHE_TIMEOUT', 0)
コード例 #3
0
    def test_dict_store_and_remove_all_basic_with_caching(self):
        """
        Storing and removing items all at once works with caching turned on on dict ac
        """
        # Let's turn on caching because that will store things in Redis and we want to make
        # sure we clean them up.
        setattr(auto_settings, 'CACHE_TIMEOUT', 3600)

        autocomp = Autocompleter("metric")
        autocomp.store_all()

        keys = self.redis.hkeys('djac.test.metric')
        self.assertEqual(len(keys), 8)

        autocomp = Autocompleter("metric")
        for i in range(0, 3):
            autocomp.suggest('m')
            autocomp.suggest('e')
            autocomp.exact_suggest('PE Ratio TTM')
            autocomp.exact_suggest('Market Cap')

        autocomp.remove_all()

        keys = self.redis.keys('djac.test.metric*')
        self.assertEqual(len(keys), 0)

        # Must set the setting back to where it was as it will persist
        setattr(auto_settings, 'CACHE_TIMEOUT', 0)
コード例 #4
0
    def test_store_and_remove_all_basic_with_caching(self):
        """
        Storing and removing items all at once works with caching turned on
        """
        # Let's turn on caching because that will store things in Redis and we want to make
        # sure we clean them up.
        setattr(auto_settings, 'CACHE_TIMEOUT', 3600)

        autocomp = Autocompleter("stock")
        autocomp.store_all()

        keys = self.redis.hkeys('djac.test.stock')
        self.assertEqual(len(keys), 101)

        autocomp = Autocompleter("stock")
        for i in range(0, 3):
            autocomp.suggest('a')
            autocomp.suggest('z')
            autocomp.exact_suggest('aapl')
            autocomp.exact_suggest('xyz')

        autocomp.remove_all()
        keys = self.redis.keys('djac.test.stock*')
        self.assertEqual(len(keys), 0)

        # Must set the setting back to where it was as it will persist
        setattr(auto_settings, 'CACHE_TIMEOUT', 0)
コード例 #5
0
class MultiExactMatchTestCase(AutocompleterTestCase):
    fixtures = ['stock_test_data_small.json', 'indicator_test_data_small.json']

    def setUp(self):
        super(MultiExactMatchTestCase, self).setUp()
        setattr(auto_settings, 'MAX_EXACT_MATCH_WORDS', 10)

        self.autocomp = Autocompleter("mixed")
        self.autocomp.store_all()

    def tearDown(self):
        setattr(auto_settings, 'MAX_EXACT_MATCH_WORDS', 0)
        self.autocomp.remove_all()

    def test_exact_suggest(self):
        """
        Exact matching works in multi-provider autocompleters
        """
        matches = self.autocomp.exact_suggest('ma')
        self.assertEqual(len(matches['stock']), 1)

        matches = self.autocomp.exact_suggest('US Unemployment Rate')
        self.assertEqual(len(matches['ind']), 1)

    def test_move_exact_matches_to_top(self):
        """
        MOVE_EXACT_MATCHES_TO_TOP works in multi-provider autocompleters
        """
        matches = self.autocomp.suggest('Ma')
        setattr(auto_settings, 'MOVE_EXACT_MATCHES_TO_TOP', True)
        matches2 = self.autocomp.suggest('Ma')
        self.assertNotEqual(matches['stock'][0]['search_name'],
                            matches2['stock'][0]['search_name'])
        setattr(auto_settings, 'MOVE_EXACT_MATCHES_TO_TOP', False)
コード例 #6
0
class StockExactMatchTestCase(AutocompleterTestCase):
    fixtures = ['stock_test_data_small.json']

    def setUp(self):
        super(StockExactMatchTestCase, self).setUp()
        setattr(auto_settings, 'MAX_EXACT_MATCH_WORDS', 10)

        self.autocomp = Autocompleter("stock")
        self.autocomp.store_all()

    def tearDown(self):
        setattr(auto_settings, 'MAX_EXACT_MATCH_WORDS', 0)
        self.autocomp.remove_all()

    def test_exact_suggest(self):
        """
        Exact matching works
        """
        matches_symbol = self.autocomp.exact_suggest('ma')
        self.assertEqual(len(matches_symbol), 1)

    def test_move_exact_matches_to_top_setting(self):
        """
        MOVE_EXACT_MATCHES_TO_TOP works
        """
        matches = self.autocomp.suggest('Ma')
        setattr(auto_settings, 'MOVE_EXACT_MATCHES_TO_TOP', True)
        matches2 = self.autocomp.suggest('Ma')
        self.assertNotEqual(matches[0]['search_name'], matches2[0]['search_name'])

        # Must set the setting back to where it was as it will persist
        setattr(auto_settings, 'MOVE_EXACT_MATCHES_TO_TOP', False)

    def test_move_exact_matches_overridable_at_ac_level(self):
        """
        MOVE_EXACT_MATCHES_TO_TOP can be set at the autocompleter level
        """
        matches = self.autocomp.suggest('Ma')
        registry.set_autocompleter_setting(self.autocomp.name, 'MOVE_EXACT_MATCHES_TO_TOP', True)
        matches2 = self.autocomp.suggest('Ma')
        registry.del_autocompleter_setting(self.autocomp.name, 'MOVE_EXACT_MATCHES_TO_TOP')
        self.assertNotEqual(matches[0]['search_name'], matches2[0]['search_name'])

    def test_exact_caching(self):
        """
        Exact caching works
        """
        matches = self.autocomp.exact_suggest('aapl')

        setattr(auto_settings, 'CACHE_TIMEOUT', 3600)

        for i in range(0, 10):
            matches2 = self.autocomp.exact_suggest('aapl')

        self.assertEqual(len(matches), len(matches2))

        # Must set the setting back to where it was as it will persist
        setattr(auto_settings, 'CACHE_TIMEOUT', 0)
コード例 #7
0
    def test_remove_intermediate_results_exact_suggest(self):
        """
        After exact_suggest call, all intermediate result sets are removed
        """
        setattr(auto_settings, 'MAX_EXACT_MATCH_WORDS', 2)
        autocomp = Autocompleter('stock')
        autocomp.store_all()

        autocomp.exact_suggest('aapl')
        keys = self.redis.keys('djac.results.*')
        self.assertEqual(len(keys), 0)

        setattr(auto_settings, 'MAX_EXACT_MATCH_WORDS', 0)
コード例 #8
0
class StockExactMatchTestCase(AutocompleterTestCase):
    fixtures = ['stock_test_data_small.json']

    def setUp(self):
        super(StockExactMatchTestCase, self).setUp()
        setattr(auto_settings, 'MAX_EXACT_MATCH_WORDS', 10)

        self.autocomp = Autocompleter("stock")
        self.autocomp.store_all()

    def tearDown(self):
        setattr(auto_settings, 'MAX_EXACT_MATCH_WORDS', 0)
        self.autocomp.remove_all()

    def test_exact_suggest(self):
        """
        Exact matching works
        """
        matches_symbol = self.autocomp.exact_suggest('ma')
        self.assertEqual(len(matches_symbol), 1)

    def test_move_exact_matches_to_top_setting(self):
        """
        MOVE_EXACT_MATCHES_TO_TOP works
        """
        matches = self.autocomp.suggest('Ma')
        setattr(auto_settings, 'MOVE_EXACT_MATCHES_TO_TOP', True)
        matches2 = self.autocomp.suggest('Ma')
        self.assertNotEqual(matches[0]['search_name'],
                            matches2[0]['search_name'])

        # Must set the setting back to where it was as it will persist
        setattr(auto_settings, 'MOVE_EXACT_MATCHES_TO_TOP', False)

    def test_exact_caching(self):
        """
        Exact caching works
        """
        matches = self.autocomp.exact_suggest('aapl')

        setattr(auto_settings, 'CACHE_TIMEOUT', 3600)

        for i in range(0, 10):
            matches2 = self.autocomp.exact_suggest('aapl')

        self.assertEqual(len(matches), len(matches2))

        # Must set the setting back to where it was as it will persist
        setattr(auto_settings, 'CACHE_TIMEOUT', 0)
コード例 #9
0
def exact_suggest(request, name):
    if settings.SUGGEST_PARAMETER_NAME in request.GET:
        term = request.GET[settings.SUGGEST_PARAMETER_NAME]
        ac = Autocompleter(name)
        results = ac.exact_suggest(term)

        json_response = json.dumps(results)
        return HttpResponse(json_response, content_type='application/json')
    return HttpResponseServerError("Search parameter not found.")
コード例 #10
0
    def test_exact_matches_respect_max_words(self):
        """
        We don't store exact matches greater than the number of words in MAX_EXACT_MATCH_WORDS
        """
        setattr(auto_settings, 'MAX_EXACT_MATCH_WORDS', 10)
        autocomp = Autocompleter("stock")
        autocomp.store_all()
        matches = autocomp.exact_suggest('International Business Machines Corporation')
        self.assertEqual(len(matches), 1)
        autocomp.remove_all()

        setattr(auto_settings, 'MAX_EXACT_MATCH_WORDS', 2)
        autocomp = Autocompleter("stock")
        autocomp.store_all()
        matches = autocomp.exact_suggest('International Business Machines Corporation')
        self.assertEqual(len(matches), 0)
        autocomp.remove_all()

        # Must set the setting back to where it was as it will persist
        setattr(auto_settings, 'MAX_EXACT_MATCH_WORDS', 0)
コード例 #11
0
    def test_exact_matches_respect_max_words(self):
        """
        We don't store exact matches greater than the number of words in MAX_EXACT_MATCH_WORDS
        """
        setattr(auto_settings, 'MAX_EXACT_MATCH_WORDS', 10)
        autocomp = Autocompleter("stock")
        autocomp.store_all()
        matches = autocomp.exact_suggest(
            'International Business Machines Corporation')
        self.assertEqual(len(matches), 1)
        autocomp.remove_all()

        setattr(auto_settings, 'MAX_EXACT_MATCH_WORDS', 2)
        autocomp = Autocompleter("stock")
        autocomp.store_all()
        matches = autocomp.exact_suggest(
            'International Business Machines Corporation')
        self.assertEqual(len(matches), 0)
        autocomp.remove_all()

        # Must set the setting back to where it was as it will persist
        setattr(auto_settings, 'MAX_EXACT_MATCH_WORDS', 0)
コード例 #12
0
class TestExactSuggestView(AutocompleterTestCase):
    fixtures = ['stock_test_data_small.json']

    def setUp(self):
        super(TestExactSuggestView, self).setUp()
        setattr(settings, 'MAX_EXACT_MATCH_WORDS', 10)
        self.autocomp = Autocompleter('stock')
        self.autocomp.store_all()

    def tearDown(self):
        setattr(settings, 'MAX_EXACT_MATCH_WORDS', 0)
        self.autocomp.remove_all()

    def test_simple_exact_suggest_match(self):
        """
        ExactSuggestView returns 200 status code and correct number of results on match
        """
        exact_suggest_url = reverse('exact_suggest', kwargs={'name': 'stock'})
        matches_symbol = self.autocomp.exact_suggest('ma')
        response = self.client.get(exact_suggest_url, data={settings.SUGGEST_PARAMETER_NAME: 'ma'})
        self.assertEqual(response.status_code, 200)

        json_response = json.loads(response.content.decode('utf-8'))
        self.assertGreaterEqual(len(json_response), 1)
        self.assertEqual(len(json_response), len(matches_symbol))

    def test_no_exact_suggest_match(self):
        """
        ExactSuggestView returns 200 status code when there is no match
        """
        url = reverse('exact_suggest', kwargs={'name': 'stock'})
        matches_symbol = self.autocomp.exact_suggest('gobblygook')
        response = self.client.get(url, data={settings.SUGGEST_PARAMETER_NAME: 'gobblygook'})
        self.assertEqual(response.status_code, 200)

        json_response = json.loads(response.content.decode('utf-8'))
        self.assertEqual(len(json_response), len(matches_symbol))
コード例 #13
0
class StockMatchTestCase(AutocompleterTestCase):
    fixtures = ['stock_test_data_small.json']

    def setUp(self):
        self.autocomp = Autocompleter("stock")
        self.autocomp.store_all()
        super(StockMatchTestCase, self).setUp()

    def tearDown(self):
        self.autocomp.remove_all()

    def test_simple_match(self):
        """
        Basic matching works
        """
        matches_symbol = self.autocomp.suggest('a')
        self.assertEqual(len(matches_symbol), 10)

    def test_exact_match(self):
        """
        Exact matching works
        """
        matches_symbol = self.autocomp.exact_suggest('ma')
        self.assertEqual(len(matches_symbol), 1)

    def test_no_match(self):
        """
        Phrases that match nothing work
        """
        matches_symbol = self.autocomp.suggest('gobblygook')
        self.assertEqual(len(matches_symbol), 0)

    def test_dual_term_matches(self):
        """
        Items in autocompleter can match against multiple unique terms
        """
        matches_symbol = self.autocomp.suggest('AAPL')
        self.assertEqual(len(matches_symbol), 1)

        matches_name = self.autocomp.suggest('Apple')
        self.assertEqual(len(matches_name), 1)

    def test_accented_matches(self):
        """
        Accented phrases match against both their orignal accented form, and their non-accented basic form.
        """
        matches = self.autocomp.suggest('estee lauder')
        self.assertEqual(len(matches), 1)
        self.assertEqual(matches[0]['search_name'], 'EL')

        matches = self.autocomp.suggest(u'estée lauder')
        self.assertEqual(len(matches), 1)
        self.assertEqual(matches[0]['search_name'], 'EL')

    def test_exact_matches_setting(self):
        """
        MOVE_EXACT_MATCHES_TO_TOP works
        """
        matches = self.autocomp.suggest('Ma')
        setattr(auto_settings, 'MOVE_EXACT_MATCHES_TO_TOP', True)
        matches2 = self.autocomp.suggest('Ma')
        self.assertNotEqual(matches[0]['search_name'], matches2[0]['search_name'])

        # Must set the setting back to where it was as it will persist
        setattr(auto_settings, 'MOVE_EXACT_MATCHES_TO_TOP', False)

    def test_max_results_setting(self):
        """
        MAX_RESULTS is respected.
        """
        matches = self.autocomp.suggest('a')
        self.assertEqual(len(matches), 10)
        setattr(auto_settings, 'MAX_RESULTS', 2)
        matches = self.autocomp.suggest('a')
        self.assertEqual(len(matches), 2)

        # Must set the setting back to where it was as it will persist
        setattr(auto_settings, 'MAX_RESULTS', 10)

    def test_ac_provider_specific_max_results_setting(self):
        """
        Autocompleter/Provider specific MAX_RESULTS is respected
        """
        matches = self.autocomp.suggest('a')
        self.assertEqual(len(matches), 10)

        registry.set_ac_provider_setting("stock", StockAutocompleteProvider, 'MAX_RESULTS', 5)
        matches = self.autocomp.suggest('a')
        self.assertEqual(len(matches), 5)

        # Must set the setting back to where it was as it will persist
        registry.del_ac_provider_setting("stock", StockAutocompleteProvider, 'MAX_RESULTS')

    def test_caching(self):
        """
        Caching works
        """
        matches = self.autocomp.suggest('a')

        setattr(auto_settings, 'CACHE_TIMEOUT', 3600)

        for i in range(0, 3):
            matches2 = self.autocomp.suggest('a')

        self.assertEqual(len(matches), len(matches2))

        # Must set the setting back to where it was as it will persist
        setattr(auto_settings, 'CACHE_TIMEOUT', 0)

    def test_exact_caching(self):
        """
        Exact caching works
        """
        matches = self.autocomp.exact_suggest('aapl')

        setattr(auto_settings, 'CACHE_TIMEOUT', 3600)

        for i in range(0, 10):
            matches2 = self.autocomp.exact_suggest('aapl')

        self.assertEqual(len(matches), len(matches2))

        # Must set the setting back to where it was as it will persist
        setattr(auto_settings, 'CACHE_TIMEOUT', 0)