|
1 | 1 | #!/usr/local/bin/python |
2 | 2 | """ |
3 | 3 | This is a py.test script |
| 4 | +
|
| 5 | +Example usage on Unix: |
| 6 | +bitly-api-python $ BITLY_ACCESS_TOKEN=<accesstoken> nosetests |
| 7 | +or 'export' the two environment variables prior to running nosetests |
4 | 8 | """ |
| 9 | +import os |
5 | 10 | import sys |
6 | 11 | sys.path.append('../') |
7 | 12 | import bitly_api |
8 | 13 |
|
| 14 | +BITLY_ACCESS_TOKEN = "BITLY_ACCESS_TOKEN" |
| 15 | + |
| 16 | + |
| 17 | +def get_connection(): |
| 18 | + """Create a Connection base on username and access token credentials""" |
| 19 | + if BITLY_ACCESS_TOKEN not in os.environ: |
| 20 | + raise ValueError("Environment variable '{}' required".format(BITLY_ACCESS_TOKEN)) |
| 21 | + access_token = os.getenv(BITLY_ACCESS_TOKEN) |
| 22 | + bitly = bitly_api.Connection(access_token=access_token) |
| 23 | + return bitly |
| 24 | + |
| 25 | + |
9 | 26 | def testApi(): |
10 | | - bitly = bitly_api.Connection('bitlyapidemo','R_0da49e0a9118ff35f52f629d2d71bf07') |
| 27 | + bitly = get_connection() |
11 | 28 | data = bitly.shorten('http://google.com/') |
12 | | - assert data != None |
| 29 | + assert data is not None |
13 | 30 | assert data['long_url'] == 'http://google.com/' |
14 | | - assert data['hash'] != None |
15 | | - |
| 31 | + assert data['hash'] is not None |
| 32 | + |
| 33 | + |
16 | 34 | def testExpand(): |
17 | | - bitly = bitly_api.Connection('bitlyapidemo','R_0da49e0a9118ff35f52f629d2d71bf07') |
18 | | - data = bitly.expand(hash='test1') |
19 | | - assert data != None |
| 35 | + bitly = get_connection() |
| 36 | + data = bitly.expand(hash='test1_random_fjslfjieljfklsjflkas') |
| 37 | + assert data is not None |
20 | 38 | assert len(data) == 1 |
21 | 39 | assert data[0]['error'] == 'NOT_FOUND' |
22 | | - |
| 40 | + |
| 41 | + |
23 | 42 | def testReferrer(): |
24 | | - bitly = bitly_api.Connection('bitlyapidemo','R_0da49e0a9118ff35f52f629d2d71bf07') |
| 43 | + bitly = get_connection() |
25 | 44 | data = bitly.referrers(hash='a') |
26 | | - assert data != None |
| 45 | + assert data is not None |
27 | 46 | assert len(data) > 1 |
28 | 47 |
|
| 48 | + |
29 | 49 | def testProDomain(): |
30 | | - bitly = bitly_api.Connection('bitlyapidemo','R_0da49e0a9118ff35f52f629d2d71bf07') |
| 50 | + bitly = get_connection() |
31 | 51 | test_data = { |
32 | 52 | 'cnn.com': False, |
33 | 53 | 'nyti.ms': True, |
|
0 commit comments