示例#1
0
    def test_create_random(self):
        link1 = Link.create_with_random_short_path(self.URL, "foo")

        self.assertEqual(link1.long_url, self.URL)

        # pylint: disable=W0612
        for iteration in xrange(1, 10):
            # We loop 10 times in hopes of encountering an invalid short path
            Link.create_with_random_short_path(self.URL, "foo")
    def test_create_random_hash_conflict(self, mock_sha1, mock_histogram):
        """Check that we try again if we encounter hash conflicts"""
        link1 = Link.create_with_random_short_path(self.URL, 'foo')
        url2 = self.URL + '?foo'
        link2 = Link.create_with_random_short_path(url2, 'foo')

        self.assertEqual(link1.long_url, self.URL)
        self.assertEqual(link1.hash, 'foo/5')
        self.assertEqual(link2.long_url, url2)
        self.assertEqual(link2.hash, 'foo/19')

        self.assertEqual(mock_sha1.call_count, 2)
        mock_histogram.assert_has_calls([
            # First link gets generated in 1 try
            call('workforus.nb_tries_to_generate', 1, tags=['prefix:foo']),
            # Second one needs 2 tries
            call('workforus.nb_tries_to_generate', 2, tags=['prefix:foo'])
        ])
 def test_create_random_is_invalid(self, mock_is_valid, mock_histogram):
     """Check that we try a second time if we randomly generate an invalid hash, and that we count tries properly"""
     self.assertEqual(Link.create_with_random_short_path(self.URL, 'foo').long_url, self.URL)
     self.assertEqual(mock_is_valid.call_count, 2)
     mock_histogram.assert_called_once_with('workforus.nb_tries_to_generate', 2, tags=['prefix:foo'])
    def test_create_random_hash_overflow(self, mock_sha1, mock_histogram):
        """Check that we generate a new hash if we exceed the length of the current hash"""
        self.assertEqual(Link.create_with_random_short_path(self.URL, 'foo').long_url, self.URL)

        self.assertEqual(mock_sha1.call_count, 2)
        mock_histogram.assert_has_calls([call('workforus.nb_tries_to_generate', 1, tags=['prefix:foo'])])
 def test_create_random(self, mock_histogram):
     self.assertEqual(
         Link.create_with_random_short_path(self.URL, 'foo').long_url,
         self.URL)
     mock_histogram.assert_called_once_with('workforus.nb_tries_to_generate', 1, tags=['prefix:foo'])