Exemple #1
0
    def test_md5_not_exist(self):
        """test a cache miss by testing new HTML"""
        ssml_to_cache = "ssml that will expire"
        html_we_scraped = "old html"
        CACHE_TIMEOUT = 1
        past_time_as_int = int(
            time.time()) - (CACHE_TIMEOUT * 60 * 60 + 10
                            )  # 1 hour in past (and a little more for skew)
        brewery_name = "changing brewery"
        cloudredis.cache_ssml(brewery=brewery_name,
                              html=html_we_scraped,
                              ssml=ssml_to_cache,
                              cached_time=past_time_as_int)

        assert not cloudredis.md5_exists(brewery=brewery_name, html="new html")
Exemple #2
0
    def ssml_taplist(self) -> str:
        """Create the SSML that drives speech for the list of beers"""
        # first things first - check the cache!
        beer_str = cloudredis.ssml_from_cache(self._brewery_name)
        if beer_str is not None:
            return beer_str

        # create a string for the tap list we have
        beer_str = 'on tap at ' + self._brewery_name + '<break strength="strong"/>'
        if not self._beer_list:
            return beer_str + "no beers listed"

        # okay, we have some beers, so iterate through them
        vowels = "aeiou"
        for beer in self._beer_list:
            if 'IT' in beer.name:
                beer_name = beer.name.replace('IT ', '<sub alias="it"> IT </sub>')
            else:
                beer_name = beer.name
            beer_str += ' ' + self.spell_out_strings(beer_name)
            if beer.style is not None:
                beer_style = self.spell_out_strings(beer.style)
                if beer.style[0].lower() in vowels:
                    beer_str += ", an " + beer_style
                else:
                    beer_str += ", a " + beer_style
            if beer.abv is not None:
                beer_str += " that is " + beer.abv + " alcohol"
            if beer.has_hops():
                beer_str += ", hopped with "
                if len(beer.hops) == 1:
                    beer_str += beer.hops[0]
                else:
                    beer_str += "{} and {}".format(", ".join(beer.hops[:-1]), beer.hops[-1])

            beer_str += "."

        beer_str = beer_str.replace(' & ', ' and ')
        beer_str = beer_str.replace('&', ' and ')

        # okay, let's cache this
        cloudredis.cache_ssml(self._brewery_name, self._cached_response, beer_str, int(time.time()))
        return beer_str
Exemple #3
0
    def test_cache_expiration(self):
        ssml_to_cache = "ssml that will expire"
        html_we_scraped = "html that will expire"
        CACHE_TIMEOUT = 1
        past_time_as_int = int(
            time.time()) - (CACHE_TIMEOUT * 60 * 60 + 10
                            )  # 1 hour in past (and a little more for skew)
        brewery_name = "expiring brewery"
        cloudredis.cache_ssml(brewery=brewery_name,
                              html=html_we_scraped,
                              ssml=ssml_to_cache,
                              cached_time=past_time_as_int)

        assert cloudredis.is_cached(brewery_name, html_we_scraped)

        # okay it's cached, now let's expire it
        cloudredis.expired(brewery_name, CACHE_TIMEOUT)

        assert not cloudredis.is_cached(brewery_name, html_we_scraped)
Exemple #4
0
    def test_value_error(self):
        """force a bad timestamp value to test value error exception"""
        ssml_to_cache = "ssml that will expire"
        html_we_scraped = "html that will expire"
        CACHE_TIMEOUT = 1
        past_time_as_int = int(
            time.time()) - (CACHE_TIMEOUT * 60 * 60 + 10
                            )  # 1 hour in past (and a little more for skew)
        brewery_name = "expiring brewery"
        cloudredis.cache_ssml(brewery=brewery_name,
                              html=html_we_scraped,
                              ssml=ssml_to_cache,
                              cached_time=past_time_as_int)

        # now overwrite the timestamp entry
        cloudredis.REDIS_SERVER.set(cloudredis.timestamp_key(brewery_name),
                                    float(past_time_as_int))

        # now check it
        assert not cloudredis.is_cached(brewery_name, html_we_scraped)
Exemple #5
0
    def test_cache_ssml(self):
        """test that we can cache our SSML"""
        ssml_to_cache = "ssml to cache"
        html_we_scraped = "html we scraped"
        current_time_as_int = int(time.time())
        brewery_name = "bogus brewing"
        cloudredis.cache_ssml(brewery=brewery_name,
                              html=html_we_scraped,
                              ssml=ssml_to_cache,
                              cached_time=current_time_as_int)

        assert cloudredis.exists(cloudredis.md5_key(brewery_name))
        assert cloudredis.exists(cloudredis.ssml_key(brewery_name))
        assert cloudredis.exists(cloudredis.timestamp_key(brewery_name))

        assert not cloudredis.exists(cloudredis.md5_key("not " + brewery_name))
        assert not cloudredis.exists(
            cloudredis.ssml_key("not " + brewery_name))
        assert not cloudredis.exists(
            cloudredis.timestamp_key("not " + brewery_name))