Remove prettyprinting unless DEBUG is set in env and add a new
Remove prettyprinting unless DEBUG is set in env and add a new
test for actually making the calls.

--- a/ckanext/ga_report/download_analytics.py
+++ b/ckanext/ga_report/download_analytics.py
@@ -1,3 +1,4 @@
+import os
 import logging
 import datetime
 
@@ -110,10 +111,10 @@
                                  max_results=10000,
                                  end_date=end_date).execute()
 
-
-        import pprint
-        pprint.pprint(results)
-        print 'Total results: %s' % results.get('totalResults')
+        if os.getenv('DEBUG'):
+            import pprint
+            pprint.pprint(results)
+            print 'Total results: %s' % results.get('totalResults')
 
         packages = []
         for entry in results.get('rows'):

--- a/ckanext/ga_report/ga_model.py
+++ b/ckanext/ga_report/ga_model.py
@@ -76,7 +76,6 @@
 
 
 def update_url_stats(period_name, period_complete_day, url_data):
-    table = get_table('ga_url')
     for url, views, next_page in url_data:
         url = _normalize_url(url)
         department_id = _get_department_id_of_url(url)

--- /dev/null
+++ b/ckanext/ga_report/tests/test_api.py
@@ -1,1 +1,51 @@
+import os
+import datetime
+from nose.tools import assert_equal
+from ckanext.ga_report.download_analytics import DownloadAnalytics
+from ckanext.ga_report.ga_auth import (init_service, get_profile_id)
+from ckanext.ga_report.ga_model import init_tables
 
+class TestAPI:
+
+    @classmethod
+    def setup_class(cls):
+        if not os.path.exists("token.dat") or not os.path.exists("credentials.json"):
+            print '*' * 60
+            print "Tests may not run without first having run the auth process"
+            print '*' * 60
+        init_tables()
+
+    @classmethod
+    def teardown_class(cls):
+        pass
+
+    def test_latest(self):
+        svc = init_service("token.dat", "credentials.json")
+        try:
+            downloader = DownloadAnalytics(svc, profile_id=get_profile_id(svc))
+            downloader.latest()
+        except Exception as e:
+            assert False, e
+
+
+    def test_since(self):
+        svc = init_service("token.dat", "credentials.json")
+        downloader = DownloadAnalytics(svc, profile_id=get_profile_id(svc))
+        try:
+            downloader.since_date(datetime.datetime.now() - datetime.timedelta(days=-30))
+        except Exception as e:
+            assert False, e
+
+"""
+        downloader = DownloadAnalytics(svc, profile_id=get_profile_id(svc))
+
+        time_period = self.args[1] if self.args and len(self.args) > 1 \
+            else 'latest'
+        if time_period == 'all':
+            downloader.all_()
+        elif time_period == 'latest':
+            downloader.latest()
+        else:
+            since_date = datetime.datetime.strptime(time_period, '%Y-%m-%d')
+            downloader.since_date(since_date)
+"""