<s> try : <EOL> from urllib import urlencode <EOL> except ImportError : <EOL> from urllib . parse import urlencode <EOL> from pyshorteners import Shortener , Shorteners <EOL> from pyshorteners . exceptions import ( ShorteningErrorException , <EOL> ExpandingErrorException ) <EOL> import responses <EOL> import pytest <EOL> token = '<STR_LIT>' <EOL> s = Shortener ( Shorteners . BITLY , bitly_token = token ) <EOL> shorten = '<STR_LIT>' <EOL> expanded = '<STR_LIT>' <EOL> @ responses . activate <EOL> def test_bitly_short_method ( ) : <EOL> body = shorten <EOL> params = urlencode ( dict ( <EOL> uri = expanded , <EOL> access_token = token , <EOL> format = '<STR_LIT>' <EOL> ) ) <EOL> url = '<STR_LIT>' . format ( s . api_url , '<STR_LIT>' , params ) <EOL> responses . add ( responses . GET , url , body = body , match_querystring = True ) <EOL> shorten_result = s . short ( expanded ) <EOL> assert shorten_result == shorten <EOL> assert s . shorten == shorten_result <EOL> assert s . expanded == expanded <EOL> @ responses . activate <EOL> def test_bitly_short_method_bad_response ( ) : <EOL> body = shorten <EOL> params = urlencode ( dict ( <EOL> uri = expanded , <EOL> access_token = token , <EOL> format = '<STR_LIT>' <EOL> ) ) <EOL> url = '<STR_LIT>' . format ( s . api_url , '<STR_LIT>' , params ) <EOL> responses . add ( responses . GET , url , body = body , status = <NUM_LIT> , <EOL> match_querystring = True ) <EOL> with pytest . raises ( ShorteningErrorException ) : <EOL> s . short ( expanded ) <EOL> @ responses . activate <EOL> def test_bitly_expand_method ( ) : <EOL> body = expanded <EOL> params = urlencode ( dict ( <EOL> shortUrl = shorten , <EOL> access_token = token , <EOL> format = '<STR_LIT>' <EOL> ) ) <EOL> url = '<STR_LIT>' . format ( s . api_url , '<STR_LIT>' , params ) <EOL> responses . add ( responses . GET , url , body = body , match_querystring = True ) <EOL> assert s . expand ( shorten ) == expanded <EOL> @ responses . activate <EOL> def test_bitly_expand_method_bad_response ( ) : <EOL> body = expanded <EOL> params = urlencode ( dict ( <EOL> shortUrl = shorten , <EOL> access_token = token , <EOL> format = '<STR_LIT>' <EOL> ) ) <EOL> url = '<STR_LIT>' . format ( s . api_url , '<STR_LIT>' , params ) <EOL> responses . add ( responses . GET , url , body = body , status = <NUM_LIT> , <EOL> match_querystring = True ) <EOL> with pytest . raises ( ExpandingErrorException ) : <EOL> s . expand ( shorten ) <EOL> def test_bitly_bad_keys ( ) : <EOL> s = Shortener ( Shorteners . BITLY ) <EOL> with pytest . raises ( TypeError ) : <EOL> s . short ( expanded ) <EOL> with pytest . raises ( TypeError ) : <EOL> s . expand ( shorten ) <EOL> @ responses . activate <EOL> def test_bitly_total_clicks ( ) : <EOL> body = '<STR_LIT>' <EOL> params = urlencode ( dict ( <EOL> link = shorten , <EOL> access_token = token , <EOL> format = '<STR_LIT>' <EOL> ) ) <EOL> url = '<STR_LIT>' . format ( s . api_url , '<STR_LIT>' , params ) <EOL> responses . add ( responses . GET , url , body = body , match_querystring = True ) <EOL> body = shorten <EOL> params = urlencode ( dict ( <EOL> uri = expanded , <EOL> access_token = token , <EOL> format = '<STR_LIT>' <EOL> ) ) <EOL> url = '<STR_LIT>' . format ( s . api_url , '<STR_LIT>' , params ) <EOL> responses . add ( responses . GET , url , body = body , match_querystring = True ) <EOL> s . short ( expanded ) <EOL> assert s . total_clicks ( ) == <NUM_LIT:20> <EOL> assert s . total_clicks ( shorten ) == <NUM_LIT:20> <EOL> @ responses . activate <EOL> def test_bitly_total_clicks_bad_response ( ) : <EOL> body = '<STR_LIT>' <EOL> params = urlencode ( dict ( <EOL> link = shorten , <EOL> access_token = token , <EOL> format = '<STR_LIT>' <EOL> ) ) <EOL> url = '<STR_LIT>' . format ( s . api_url , '<STR_LIT>' , params ) <EOL> responses . add ( responses . GET , url , body = body , status = <NUM_LIT> , <EOL> match_querystring = True ) <EOL> body = shorten <EOL> params = urlencode ( dict ( <EOL> uri = expanded , <EOL> access_token = token , <EOL> format = '<STR_LIT>' <EOL> ) ) <EOL> url = '<STR_LIT>' . format ( s . api_url , '<STR_LIT>' , params ) <EOL> responses . add ( responses . GET , url , body = body , match_querystring = True ) <EOL> s . short ( expanded ) <EOL> assert s . total_clicks ( ) == <NUM_LIT:0> <EOL> assert s . total_clicks ( shorten ) == <NUM_LIT:0> </s>
