1
1
#!/usr/bin/env python
2
2
#-*- coding:utf-8 -*-
3
3
4
+ from nose .tools import assert_raises
5
+
4
6
import bitly
5
7
6
8
def test_urls_shortening_scenario ():
7
9
8
10
api = bitly .Api (login = 'jcfigueiredo' , apikey = 'R_1cf5dc0fa14c2df34261fb620bd256aa' )
11
+ yield should_raise_when_an_invalid_credential_is_provided
12
+ yield should_have_an_API_authenticated_by_my_credentials , api
13
+ yield should_shorten_an_url_consistently_when_a_single_string_urls_is_provided , api
14
+ yield should_shorten_many_urls_consistently_when_a_list_of_urls_is_provided , api
15
+
16
+ def should_raise_when_an_invalid_credential_is_provided ():
17
+ api = bitly .Api (login = 'inexistent_login' , apikey = 'or_invalid_key' )
18
+ assert_raises (bitly .BitlyError , api .shorten , 'http://anylong.url' )
19
+
20
+ def should_have_an_API_authenticated_by_my_credentials (api ):
21
+ assert api , 'Should have a valid API'
22
+
23
+ def should_shorten_an_url_consistently_when_a_single_string_urls_is_provided (api ):
24
+ url_to_be_shortened = 'http://globoesporte.globo.com/motor/formula-1/noticia/2010/10/apos-maus-resultados-ferrari-reforca-apoio-massa-no-fim-da-temporada.html'
25
+ expected_url = 'http://bit.ly/9n93fw'
26
+
27
+ shortened_url = api .shorten (longURLs = url_to_be_shortened )
28
+
29
+ assert shortened_url == expected_url , 'The shortened version of %s url should\' ve been %s but was %s' % (url_to_be_shortened , expected_url , shortened_url )
30
+
31
+ def should_shorten_many_urls_consistently_when_a_list_of_urls_is_provided (api ):
32
+ urls_to_be_shortened = [ 'http://globoesporte.globo.com/motor/formula-1/noticia/2010/10/apos-maus-resultados-ferrari-reforca-apoio-massa-no-fim-da-temporada.html'
33
+ ,
34
+ 'http://globoesporte.globo.com/basquete/noticia/2010/10/leandrinho-faz-19-pontos-na-vitoria-do-toronto-sobre-o-philadelphia.html'
35
+ ]
36
+
37
+ expected_urls = ['http://bit.ly/9n93fw' ,'http://bit.ly/aprECg' ]
9
38
10
- i_have_an_API_validated_by_my_credentials ( api )
39
+ shortened_urls = api . shorten ( longURLs = urls_to_be_shortened )
11
40
12
- def i_have_an_API_validated_by_my_credentials ( api ) :
13
- assert api , 'Should have a valid API'
41
+ for expected_url in expected_urls :
42
+ assert expected_url in shortened_urls , 'The list os shortened urls should contain %s but it wasn \' t found in %s' % ( expected_url , shortened_urls )
0 commit comments