Permalink
Browse files

fixed tests for state and zip

  • Loading branch information...
1 parent 5049f75 commit f3f15764fde2f853da4fc0c2e5fce3a8b1c23618 @dwillis dwillis committed Jul 24, 2012
Showing with 13 additions and 14 deletions.
  1. +6 −11 nytcampfin.py
  2. +7 −3 test.py
View
@@ -14,11 +14,6 @@
CURRENT_CYCLE = 2012
-try:
- API_KEY = os.environ['NYT_CAMPFIN_API_KEY']
-except:
- print "Remember to set your NYT_CAMPFIN_API_KEY as an environment value"
-
requests_cache.configure(expire_after=5)
# Error classes
@@ -39,7 +34,7 @@ class Client(object):
BASE_URI = "http://api.nytimes.com/svc/elections/us/v3/finances"
- def __init__(self, apikey=API_KEY):
+ def __init__(self, apikey):
self.apikey = apikey
def fetch(self, path, *args, **kwargs):
@@ -265,13 +260,13 @@ def detail(self, candidate_id, cycle=CURRENT_CYCLE, offset=0):
def state(self, state_abbrev, cycle=CURRENT_CYCLE, offset=0):
"Returns state totals for presidential candidates"
path = "/%s/president/states/%s"
- result = self.fetch(path, cycle, state_abbrev, offset=offset)
+ result = self.fetch(path, cycle, state_abbrev, offset=offset, parse=lambda r: r['results'])
return result
def zipcode(self, zipcode, cycle=CURRENT_CYCLE, offset=0):
"Returns zip code totals for presidential candidates"
path = "/%s/president/zips/%s"
- result = self.fetch(path, cycle, zipcode, offset=offset)
+ result = self.fetch(path, cycle, zipcode, offset=offset, parse=lambda r: r['results'])
return result
class LateContributionClient(Client):
@@ -302,14 +297,14 @@ class NytCampfin(Client):
functions that add on your API key and trim fat off responses.
Create a new instance with your API key, or set an environment
- variable called NYT_CAMPFIN_API_KEY.
+ variable and pass that in.
NytCampfin uses requests and the requests-cache library. By default,
it uses a sqlite database named cache.sqlite, but other cache options
may be used.
"""
-
- def __init__(self, apikey=os.environ.get('NYT_CAMPFIN_API_KEY')):
+
+ def __init__(self, apikey):
super(NytCampfin, self).__init__(apikey)
self.filings = FilingsClient(self.apikey)
self.committees = CommitteesClient(self.apikey)
View
10 test.py
@@ -6,7 +6,11 @@
from nytcampfin import NytCampfin, NytCampfinError, NytNotFoundError
CURRENT_CYCLE = 2012
-API_KEY = os.environ['NYT_CAMPFIN_API_KEY']
+try:
+ API_KEY = os.environ['NYT_CAMPFIN_API_KEY']
+except:
+ print "Please set your API Key as an environment variable"
+
class APITest(unittest.TestCase):
@@ -209,12 +213,12 @@ def test_detail_using_name(self):
def test_state_total(self):
state = self.finance.president.state("AZ")
url = "http://api.nytimes.com/svc/elections/us/v3/finances/2012/president/states/AZ.json?api-key=%s" % API_KEY
- self.check_response(state, url, parse=lambda r: r['results'][0])
+ self.check_response(state, url)
def test_zip_total(self):
zipcode = self.finance.president.zipcode("33407")
url = "http://api.nytimes.com/svc/elections/us/v3/finances/2012/president/zips/33407.json?api-key=%s" % API_KEY
- self.check_response(zipcode, url, parse=lambda r: r['results'][0])
+ self.check_response(zipcode, url)
if __name__ == "__main__":

0 comments on commit f3f1576

Please sign in to comment.