Example #1
0
 def test_parsing_with_no_localized_grammars(self):
     mgr = ConvertibleParseManager(Grammar())
     parse_tree1 = mgr.parse('message == "2009-07-13"')
     parse_tree2 = mgr.parse('message == "2009-07-13"', None)
     expected_tree = ConvertibleParseTree(
         Equal(PlaceholderVariable("message"), String("2009-07-13")))
     eq_(parse_tree1, parse_tree2)
     eq_(parse_tree1, expected_tree)
Example #2
0
 def test_parsing_with_localized_grammars(self):
     castilian_grammar = Grammar(decimal_separator=",",
                                 thousands_separator=".")
     mgr = ConvertibleParseManager(Grammar(), es=castilian_grammar)
     parse_tree = mgr.parse(u"tráfico:peatones_cruzando_calle <= 3,00", "es")
     expected_tree = ConvertibleParseTree(LessEqual(
         PlaceholderVariable("peatones_cruzando_calle", (u"tráfico", )),
         Number(3.0)
     ))
     eq_(parse_tree, expected_tree)
Example #3
0
 def test_unlimited_caching(self):
     """
     The cache may have no limit.
     
     If so, the cache counter will be disabled.
     
     """
     manager = ConvertibleParseManager(Grammar(), cache_limit=None)
     manager.parse('today == "2009-07-13"')
     manager.parse('yesterday < "2009-07-13"')
     manager.parse('tomorrow > "2009-07-13"')
     manager.parse('hoy > "1999-01-06"')
     manager.parse('ayer > "1999-01-06"')
     # Checking the cache:
     eq_(manager._cache.counter, 5)
     eq_(len(manager._cache.cache_by_locale[None]), 5)
     eq_(len(manager._cache.latest_expressions), 5)
Example #4
0
 def setUp(self):
     castilian_grammar = Grammar(decimal_separator=",",
                                 thousands_separator=".")
     self.manager = ConvertibleParseManager(Grammar(), cache_limit=3,
                                            es=castilian_grammar)
Example #5
0
class TestManagersWithCaching(object):
    """
    Tests for the parse managers with caching enabled.
    
    """
    
    def setUp(self):
        castilian_grammar = Grammar(decimal_separator=",",
                                    thousands_separator=".")
        self.manager = ConvertibleParseManager(Grammar(), cache_limit=3,
                                               es=castilian_grammar)
    
    def test_empty_by_default(self):
        """The cache must be empty by default."""
        eq_(self.manager._cache.counter, 0)
        eq_(len(self.manager._cache.cache_by_locale), 0)
        eq_(len(self.manager._cache.latest_expressions), 0)
    
    def test_parsing_not_yet_cached_result(self):
        """Not yet cached trees must be cached after parsing."""
        # The first parse:
        expr1 = 'today == "2009-07-13"'
        locale1 = "es_VE"
        tree1 = self.manager.parse(expr1, locale1)
        eq_(self.manager._cache.counter, 1)
        eq_(len(self.manager._cache.cache_by_locale[locale1]), 1)
        eq_(self.manager._cache.cache_by_locale[locale1][expr1], tree1)
        eq_(self.manager._cache.latest_expressions, [(locale1, expr1)])
        # The second parse:
        expr2 = 'yesterday > "1999-06-01"'
        locale2 = "es_ES"
        tree2 = self.manager.parse(expr2, locale2)
        eq_(self.manager._cache.counter, 2)
        eq_(len(self.manager._cache.cache_by_locale[locale2]), 1)
        eq_(self.manager._cache.cache_by_locale[locale2][expr2], tree2)
        eq_(self.manager._cache.latest_expressions, [(locale2, expr2),
                                                     (locale1, expr1)])
    
    def test_parsing_already_cached_result(self):
        """Already parsed expressions must not be parsed again."""
        expr = 'today == "2009-07-13"'
        locale = "es_VE"
        # Parsing it twice:
        tree1 = self.manager.parse(expr, locale)
        tree2 = self.manager.parse(expr, locale)
        eq_(tree1, tree2)
        # Checking the cache:
        eq_(self.manager._cache.counter, 1)
        eq_(len(self.manager._cache.cache_by_locale[locale]), 1)
        eq_(self.manager._cache.cache_by_locale[locale][expr], tree1)
        eq_(self.manager._cache.latest_expressions, [(locale, expr)])
    
    def test_limit_reached(self):
        """
        When the cache limit has been reached, the oldest items must be removed.
        
        """
        expr1 = 'today == "2009-07-13"'
        expr2 = 'yesterday < "2009-07-13"'
        expr3 = 'tomorrow > "2009-07-13"'
        expr4 = 'today > "1999-01-06"'
        # Parsing the expressions:
        tree1 = self.manager.parse(expr1)
        tree2 = self.manager.parse(expr2)
        tree3 = self.manager.parse(expr3)
        tree4 = self.manager.parse(expr4)
        # Checking the cache:
        eq_(self.manager._cache.counter, 3)
        eq_(len(self.manager._cache.cache_by_locale[None]), 3)
        eq_(self.manager._cache.cache_by_locale[None][expr2], tree2)
        eq_(self.manager._cache.cache_by_locale[None][expr3], tree3)
        eq_(self.manager._cache.cache_by_locale[None][expr4], tree4)
        latest = [(None, expr4), (None, expr3), (None, expr2)]
        eq_(self.manager._cache.latest_expressions, latest)
    
    def test_limit_reached_in_different_locales(self):
        """
        When the cache limit has been reached among all the locales, the
        oldest items must be removed.
        
        """
        expr1 = 'today == "2009-07-13"'
        expr2 = 'yesterday < "2009-07-13"'
        expr3 = 'tomorrow > "2009-07-13"'
        expr4 = 'hoy > "1999-01-06"'
        # Parsing the expressions:
        tree1 = self.manager.parse(expr1)
        tree2 = self.manager.parse(expr2, "fr")
        tree3 = self.manager.parse(expr3)
        tree4 = self.manager.parse(expr4, "es")
        # Checking the cache:
        eq_(self.manager._cache.counter, 3)
        eq_(len(self.manager._cache.cache_by_locale[None]), 1)
        eq_(len(self.manager._cache.cache_by_locale['es']), 1)
        eq_(len(self.manager._cache.cache_by_locale['fr']), 1)
        eq_(self.manager._cache.cache_by_locale['fr'][expr2], tree2)
        eq_(self.manager._cache.cache_by_locale[None][expr3], tree3)
        eq_(self.manager._cache.cache_by_locale['es'][expr4], tree4)
        latest = [("es", expr4), (None, expr3), ("fr", expr2)]
        eq_(self.manager._cache.latest_expressions, latest)
    
    def test_unlimited_caching(self):
        """
        The cache may have no limit.
        
        If so, the cache counter will be disabled.
        
        """
        manager = ConvertibleParseManager(Grammar(), cache_limit=None)
        manager.parse('today == "2009-07-13"')
        manager.parse('yesterday < "2009-07-13"')
        manager.parse('tomorrow > "2009-07-13"')
        manager.parse('hoy > "1999-01-06"')
        manager.parse('ayer > "1999-01-06"')
        # Checking the cache:
        eq_(manager._cache.counter, 5)
        eq_(len(manager._cache.cache_by_locale[None]), 5)
        eq_(len(manager._cache.latest_expressions), 5)