add tests, and associated fixage/harness support to main software
This commit is contained in:
parent
c91ab410c0
commit
fefac7548d
|
@ -47,6 +47,15 @@ Installation
|
|||
6. Consider putting the import command as a daily cron job, or
|
||||
remember to run it by hand!
|
||||
|
||||
Testing
|
||||
=======
|
||||
|
||||
There are some very high-level functional tests that you can run using::
|
||||
|
||||
(pyenv)~/pyenv/src/ckan$ nosetests --ckan ../ckanext-googleanalytics/tests/
|
||||
|
||||
(note -- that's run from the CKAN software root, not the extension root)
|
||||
|
||||
TODO
|
||||
====
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import logging
|
||||
import datetime
|
||||
from pylons import config
|
||||
from pylons import config as pylonsconfig
|
||||
from ckan.lib.cli import CkanCommand
|
||||
from gdata.analytics import client
|
||||
import ckan.model as model
|
||||
|
@ -21,11 +21,14 @@ class LoadAnalytics(CkanCommand):
|
|||
usage = __doc__
|
||||
max_args = 0
|
||||
min_args = 0
|
||||
TEST_HOST = None
|
||||
CONFIG = pylonsconfig
|
||||
|
||||
def command(self):
|
||||
self._load_config()
|
||||
self.resource_url_tag = config.get('googleanalytics.resource_prefix',
|
||||
DEFAULT_RESOURCE_URL_TAG)
|
||||
self.resource_url_tag = self.CONFIG.get(
|
||||
'googleanalytics.resource_prefix',
|
||||
DEFAULT_RESOURCE_URL_TAG)
|
||||
self.setup_ga_connection()
|
||||
# funny dance we need to do to make sure we've got a
|
||||
# configured session
|
||||
|
@ -67,12 +70,16 @@ class LoadAnalytics(CkanCommand):
|
|||
|
||||
def setup_ga_connection(self):
|
||||
SOURCE_APP_NAME = "CKAN Google Analytics Plugin"
|
||||
username = config.get('googleanalytics.username')
|
||||
password = config.get('googleanalytics.password')
|
||||
profile_name = config.get('googleanalytics.profile_name')
|
||||
username = self.CONFIG.get('googleanalytics.username')
|
||||
password = self.CONFIG.get('googleanalytics.password')
|
||||
profile_name = self.CONFIG.get('googleanalytics.profile_name')
|
||||
if not username or not password or not profile_name:
|
||||
raise Exception("No googleanalytics profile info in config")
|
||||
my_client = client.AnalyticsClient(source=SOURCE_APP_NAME)
|
||||
if self.TEST_HOST:
|
||||
my_client = client.AnalyticsClient(source=SOURCE_APP_NAME,
|
||||
http_client=self.TEST_HOST)
|
||||
else:
|
||||
my_client = client.AnalyticsClient(source=SOURCE_APP_NAME)
|
||||
my_client.ClientLogin(username,
|
||||
password,
|
||||
SOURCE_APP_NAME)
|
||||
|
@ -129,4 +136,3 @@ class LoadAnalytics(CkanCommand):
|
|||
'ga:uniquePageviews').value or 0
|
||||
packages.setdefault(package, {})[date_name] = count
|
||||
return packages
|
||||
|
||||
|
|
|
@ -61,7 +61,8 @@ def update_package_visits(package_id, recently, ever):
|
|||
WHERE package_id = '%s'""" % package_id).fetchone()
|
||||
if count[0]:
|
||||
connection.execute(
|
||||
"""UPDATE package_stats SET visits = %s
|
||||
"""UPDATE package_stats SET visits_recently = %s,
|
||||
visits_ever = %s
|
||||
WHERE package_id = '%s'""" % (recently, ever, package_id)
|
||||
)
|
||||
else:
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
<ns0:feed ns1:etag="W/"CkQNRHo4fSp7I2A9WhZTEEw."" ns1:kind="analytics#accounts" xmlns:ns0="http://www.w3.org/2005/Atom" xmlns:ns1="http://schemas.google.com/g/2005"><ns0:updated>2011-03-13T01:59:55.435-08:00</ns0:updated><ns0:id>http://www.google.com/analytics/feeds/accounts/seb.bacon@okfn.org</ns0:id><ns0:generator version="1.0">Google Analytics</ns0:generator><ns0:author><ns0:name>Google Analytics</ns0:name></ns0:author><ns0:link href="https://www.google.com/analytics/feeds/accounts/default?max-results=300" rel="self" type="application/atom+xml" /><ns2:segment id="gaid::-1" name="All Visits" xmlns:ns2="http://schemas.google.com/analytics/2009"><ns2:definition> </ns2:definition></ns2:segment><ns2:segment id="gaid::-2" name="New Visitors" xmlns:ns2="http://schemas.google.com/analytics/2009"><ns2:definition>ga:visitorType==New Visitor</ns2:definition></ns2:segment><ns2:segment id="gaid::-3" name="Returning Visitors" xmlns:ns2="http://schemas.google.com/analytics/2009"><ns2:definition>ga:visitorType==Returning Visitor</ns2:definition></ns2:segment><ns2:segment id="gaid::-4" name="Paid Search Traffic" xmlns:ns2="http://schemas.google.com/analytics/2009"><ns2:definition>ga:medium==cpa,ga:medium==cpc,ga:medium==cpm,ga:medium==cpp,ga:medium==cpv,ga:medium==ppc</ns2:definition></ns2:segment><ns2:segment id="gaid::-5" name="Non-paid Search Traffic" xmlns:ns2="http://schemas.google.com/analytics/2009"><ns2:definition>ga:medium==organic</ns2:definition></ns2:segment><ns2:segment id="gaid::-6" name="Search Traffic" xmlns:ns2="http://schemas.google.com/analytics/2009"><ns2:definition>ga:medium==cpa,ga:medium==cpc,ga:medium==cpm,ga:medium==cpp,ga:medium==cpv,ga:medium==organic,ga:medium==ppc</ns2:definition></ns2:segment><ns2:segment id="gaid::-7" name="Direct Traffic" xmlns:ns2="http://schemas.google.com/analytics/2009"><ns2:definition>ga:medium==(none)</ns2:definition></ns2:segment><ns2:segment id="gaid::-8" name="Referral Traffic" xmlns:ns2="http://schemas.google.com/analytics/2009"><ns2:definition>ga:medium==referral</ns2:definition></ns2:segment><ns2:segment id="gaid::-9" name="Visits with Conversions" xmlns:ns2="http://schemas.google.com/analytics/2009"><ns2:definition>ga:goalCompletionsAll>0</ns2:definition></ns2:segment><ns2:segment id="gaid::-10" name="Visits with Transactions" xmlns:ns2="http://schemas.google.com/analytics/2009"><ns2:definition>ga:transactions>0</ns2:definition></ns2:segment><ns2:segment id="gaid::-11" name="Mobile Traffic" xmlns:ns2="http://schemas.google.com/analytics/2009"><ns2:definition>ga:isMobile==Yes</ns2:definition></ns2:segment><ns2:segment id="gaid::-12" name="Non-bounce Visits" xmlns:ns2="http://schemas.google.com/analytics/2009"><ns2:definition>ga:bounces==0</ns2:definition></ns2:segment><ns2:startIndex xmlns:ns2="http://a9.com/-/spec/opensearchrss/1.0/">1</ns2:startIndex><ns2:totalResults xmlns:ns2="http://a9.com/-/spec/opensearchrss/1.0/">1</ns2:totalResults><ns2:itemsPerPage xmlns:ns2="http://a9.com/-/spec/opensearchrss/1.0/">300</ns2:itemsPerPage><ns0:entry ns1:etag="W/"CkQNRHo4fSp7I2A9WhZTEEw."" ns1:kind="analytics#account"><ns0:id>http://www.google.com/analytics/feeds/accounts/ga:42156377</ns0:id><ns2:tableId xmlns:ns2="http://schemas.google.com/analytics/2009">ga:42156377</ns2:tableId><ns0:updated>2011-03-13T01:59:55.435-08:00</ns0:updated><ns2:property name="ga:accountId" value="21313878" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns2:property name="ga:accountName" value="borf" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns2:property name="ga:profileId" value="42156377" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns2:property name="ga:webPropertyId" value="UA-21313878-1" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns2:property name="ga:currency" value="USD" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns2:property name="ga:timezone" value="Europe/London" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns0:title>borf</ns0:title><ns0:link href="http://www.google.com/analytics" rel="alternate" type="text/html" /></ns0:entry><ns0:title>Profile list for seb.bacon@okfn.org</ns0:title></ns0:feed>
|
|
@ -0,0 +1,8 @@
|
|||
<ns0:feed ns1:etag="W/"AkYHSXo5eCp7I2A9WhZSGUQ."" ns1:kind="analytics#data" xmlns:ns0="http://www.w3.org/2005/Atom" xmlns:ns1="http://schemas.google.com/g/2005"><ns0:id>http://www.google.com/analytics/feeds/data?ids=ga:42156377&dimensions=ga:pagePath&metrics=ga:newVisits,ga:uniquePageviews,ga:visitors,ga:visits&filters=ga:pagePath%3D~%5E/downloads/&start-date=2011-03-22&end-date=2011-04-05</ns0:id><ns2:dataSource xmlns:ns2="http://schemas.google.com/analytics/2009"><ns2:tableName>datagm.staging.ckan.net/</ns2:tableName><ns2:tableId>ga:42156377</ns2:tableId><ns2:property name="ga:profileId" value="42156377" /><ns2:property name="ga:webPropertyId" value="UA-21313878-1" /><ns2:property name="ga:accountName" value="http://datagm.staging.ckan.net/" /></ns2:dataSource><ns2:aggregates xmlns:ns2="http://schemas.google.com/analytics/2009"><ns2:metric confidenceInterval="0.0" name="ga:visits" type="integer" value="2" /><ns2:metric confidenceInterval="0.0" name="ga:visitors" type="integer" value="157" /><ns2:metric confidenceInterval="0.0" name="ga:newVisits" type="integer" value="0" /><ns2:metric confidenceInterval="0.0" name="ga:uniquePageviews" type="integer" value="164" /></ns2:aggregates><ns0:updated>2011-04-05T03:08:58.420-07:00</ns0:updated><ns2:containsSampledData xmlns:ns2="http://schemas.google.com/analytics/2009">false</ns2:containsSampledData>
|
||||
|
||||
<ns0:entry ns1:etag="W/"C0EEQX47eSp7I2A9WhZSGUs."" ns1:kind="analytics#datarow"><ns0:id>http://www.google.com/analytics/feeds/data?ids=ga:42156377&ga:pagePath=/downloads/http%3A%2F%2Fwww.annakarenina.com%2Findex.json&filters=ga:pagePath%3D~%5E/downloads/&start-date=2011-03-22&end-date=2011-04-05</ns0:id><ns0:updated>2011-04-04T17:00:00.001-07:00</ns0:updated><ns2:dimension name="ga:pagePath" value="/downloads/http://www.annakarenina.com/index.json" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns0:title>ga:pagePath=/downloads/http%3A%2F%2Fwww.annakarenina.com%2Findex.json</ns0:title><ns2:metric confidenceInterval="0.0" name="ga:visits" type="integer" value="0" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns2:metric confidenceInterval="0.0" name="ga:visitors" type="integer" value="4" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns2:metric confidenceInterval="0.0" name="ga:newVisits" type="integer" value="0" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns2:metric confidenceInterval="0.0" name="ga:uniquePageviews" type="integer" value="4" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns0:link href="http://www.google.com/analytics" rel="alternate" type="text/html" /></ns0:entry>
|
||||
|
||||
|
||||
<ns0:entry ns1:etag="W/"C0EEQX47eSp7I2A9WhZSGUs."" ns1:kind="analytics#datarow"><ns0:id>missingthing</ns0:id><ns0:updated>2011-04-04T17:00:00.001-07:00</ns0:updated><ns2:dimension name="ga:pagePath" value="/downloads/missingthing" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns0:title>ga:pagePath=/downloads/missingthing</ns0:title><ns2:metric confidenceInterval="0.0" name="ga:visits" type="integer" value="0" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns2:metric confidenceInterval="0.0" name="ga:visitors" type="integer" value="3" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns2:metric confidenceInterval="0.0" name="ga:newVisits" type="integer" value="0" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns2:metric confidenceInterval="0.0" name="ga:uniquePageviews" type="integer" value="4" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns0:link href="http://www.google.com/analytics" rel="alternate" type="text/html" /></ns0:entry>
|
||||
|
||||
<ns0:generator version="1.0">Google Analytics</ns0:generator><ns2:startIndex xmlns:ns2="http://a9.com/-/spec/opensearchrss/1.0/">1</ns2:startIndex><ns0:title>Google Analytics Data for Profile 42156377</ns0:title><ns0:author><ns0:name>Google Analytics</ns0:name></ns0:author><ns0:link href="https://www.google.com/analytics/feeds/data?max-results=10000&sort=-ga%3AnewVisits&end-date=2011-04-05&start-date=2011-03-22&metrics=ga%3Avisits%2Cga%3Avisitors%2Cga%3AnewVisits%2Cga%3AuniquePageviews&ids=ga%3A42156377&dimensions=ga%3ApagePath&filters=ga%3ApagePath%3D%7E%5E%2Fdownloads%2F" rel="self" type="application/atom+xml" /><ns2:endDate xmlns:ns2="http://schemas.google.com/analytics/2009">2011-04-05</ns2:endDate><ns2:totalResults xmlns:ns2="http://a9.com/-/spec/opensearchrss/1.0/">43</ns2:totalResults><ns2:startDate xmlns:ns2="http://schemas.google.com/analytics/2009">2011-03-22</ns2:startDate><ns2:itemsPerPage xmlns:ns2="http://a9.com/-/spec/opensearchrss/1.0/">10000</ns2:itemsPerPage></ns0:feed>
|
|
@ -0,0 +1,66 @@
|
|||
import os
|
||||
import BaseHTTPServer
|
||||
import threading
|
||||
import gdata.data
|
||||
import atom.core
|
||||
|
||||
here_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
|
||||
class MockHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
if "feeds/accounts/default" in self.path:
|
||||
self.send_response(200)
|
||||
self.end_headers()
|
||||
fixture = os.path.join(here_dir, "accountsfixture.xml")
|
||||
content = open(fixture, "r").read()
|
||||
elif "analytics/feeds/data" in self.path:
|
||||
if "package" in self.path:
|
||||
fixture = os.path.join(here_dir,
|
||||
"packagefixture.xml")
|
||||
elif "download" in self.path:
|
||||
fixture = os.path.join(here_dir,
|
||||
"downloadfixture.xml")
|
||||
self.send_response(200)
|
||||
self.end_headers()
|
||||
content = open(fixture, "r").read()
|
||||
else:
|
||||
self.send_response(200)
|
||||
self.end_headers()
|
||||
content = "empty"
|
||||
self.wfile.write(content)
|
||||
|
||||
def do_POST(self):
|
||||
if "ClientLogin" in self.path:
|
||||
self.send_response(200)
|
||||
self.end_headers()
|
||||
content = "Auth=blah"
|
||||
else:
|
||||
self.send_response(200)
|
||||
self.end_headers()
|
||||
content = "empty"
|
||||
self.wfile.write(content)
|
||||
|
||||
def do_QUIT(self):
|
||||
self.send_response(200)
|
||||
self.end_headers()
|
||||
self.server.stop = True
|
||||
|
||||
|
||||
class ReusableServer(BaseHTTPServer.HTTPServer):
|
||||
allow_reuse_address = 1
|
||||
|
||||
def serve_til_quit(self):
|
||||
self.stop = False
|
||||
while not self.stop:
|
||||
self.handle_request()
|
||||
|
||||
|
||||
def runmockserver():
|
||||
server_address = ('localhost', 6969)
|
||||
httpd = ReusableServer(server_address,
|
||||
MockHandler)
|
||||
httpd_thread = threading.Thread(target=httpd.serve_til_quit)
|
||||
httpd_thread.setDaemon(True)
|
||||
httpd_thread.start()
|
||||
return httpd_thread
|
|
@ -0,0 +1,9 @@
|
|||
<ns0:feed ns1:etag="W/"AkYHRn87fip7I2A9WhZSGUQ."" ns1:kind="analytics#data" xmlns:ns0="http://www.w3.org/2005/Atom" xmlns:ns1="http://schemas.google.com/g/2005"><ns0:id>http://www.google.com/analytics/feeds/data?ids=ga:42156377&dimensions=ga:pagePath&metrics=ga:newVisits,ga:uniquePageviews,ga:visitors,ga:visits&filters=ga:pagePath%3D~%5E/package/&start-date=2011-03-22&end-date=2011-04-05</ns0:id><ns2:dataSource xmlns:ns2="http://schemas.google.com/analytics/2009"><ns2:tableName>datagm.staging.ckan.net/</ns2:tableName><ns2:tableId>ga:42156377</ns2:tableId><ns2:property name="ga:profileId" value="42156377" /><ns2:property name="ga:webPropertyId" value="UA-21313878-1" /><ns2:property name="ga:accountName" value="http://datagm.staging.ckan.net/" /></ns2:dataSource><ns2:aggregates xmlns:ns2="http://schemas.google.com/analytics/2009"><ns2:metric confidenceInterval="0.0" name="ga:visits" type="integer" value="122" /><ns2:metric confidenceInterval="0.0" name="ga:visitors" type="integer" value="526" /><ns2:metric confidenceInterval="0.0" name="ga:newVisits" type="integer" value="83" /><ns2:metric confidenceInterval="0.0" name="ga:uniquePageviews" type="integer" value="606" /></ns2:aggregates><ns0:updated>2011-04-05T03:08:57.106-07:00</ns0:updated><ns2:containsSampledData xmlns:ns2="http://schemas.google.com/analytics/2009">false</ns2:containsSampledData>
|
||||
|
||||
<ns0:entry ns1:etag="W/"C0EEQX47eSp7I2A9WhZSGUs."" ns1:kind="analytics#datarow"><ns0:id>http://www.google.com/analytics/feeds/data?ids=ga:42156377&ga:pagePath=/package/annakarenina&filters=ga:pagePath%3D~%5E/package/&start-date=2011-03-22&end-date=2011-04-05</ns0:id><ns0:updated>2011-04-04T17:00:00.001-07:00</ns0:updated><ns2:dimension name="ga:pagePath" value="/package/annakarenina" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns0:title>ga:pagePath=/package/annakarenina</ns0:title><ns2:metric confidenceInterval="0.0" name="ga:visits" type="integer" value="0" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns2:metric confidenceInterval="0.0" name="ga:visitors" type="integer" value="2" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns2:metric confidenceInterval="0.0" name="ga:newVisits" type="integer" value="0" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns2:metric confidenceInterval="0.0" name="ga:uniquePageviews" type="integer" value="2" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns0:link href="http://www.google.com/analytics" rel="alternate" type="text/html" /></ns0:entry>
|
||||
|
||||
<ns0:entry ns1:etag="W/"C0EEQX47eSp7I2A9WhZSGUs."" ns1:kind="analytics#datarow"><ns0:id>http://www.google.com/analytics/feeds/data?ids=ga:42156377&ga:pagePath=/package/annakarenina/invalid&filters=ga:pagePath%3D~%5E/package/&start-date=2011-03-22&end-date=2011-04-05</ns0:id><ns0:updated>2011-04-04T17:00:00.001-07:00</ns0:updated><ns2:dimension name="ga:pagePath" value="/package/annakarenina/invalid" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns0:title>ga:pagePath=/package/annakarenina/invalid</ns0:title><ns2:metric confidenceInterval="0.0" name="ga:visits" type="integer" value="0" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns2:metric confidenceInterval="0.0" name="ga:visitors" type="integer" value="2" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns2:metric confidenceInterval="0.0" name="ga:newVisits" type="integer" value="0" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns2:metric confidenceInterval="0.0" name="ga:uniquePageviews" type="integer" value="2" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns0:link href="http://www.google.com/analytics" rel="alternate" type="text/html" /></ns0:entry>
|
||||
|
||||
<ns0:entry ns1:etag="W/"C0EEQX47eSp7I2A9WhZSGUs."" ns1:kind="analytics#datarow"><ns0:id>http://www.google.com/analytics/feeds/data?ids=ga:42156377&ga:pagePath=/package/annakarenina-invalid&filters=ga:pagePath%3D~%5E/package/&start-date=2011-03-22&end-date=2011-04-05</ns0:id><ns0:updated>2011-04-04T17:00:00.001-07:00</ns0:updated><ns2:dimension name="ga:pagePath" value="/package/annakarenina-invalid" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns0:title>ga:pagePath=/package/annakarenina-invalid</ns0:title><ns2:metric confidenceInterval="0.0" name="ga:visits" type="integer" value="0" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns2:metric confidenceInterval="0.0" name="ga:visitors" type="integer" value="2" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns2:metric confidenceInterval="0.0" name="ga:newVisits" type="integer" value="0" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns2:metric confidenceInterval="0.0" name="ga:uniquePageviews" type="integer" value="2" xmlns:ns2="http://schemas.google.com/analytics/2009" /><ns0:link href="http://www.google.com/analytics" rel="alternate" type="text/html" /></ns0:entry>
|
||||
|
||||
<ns0:generator version="1.0">Google Analytics</ns0:generator><ns2:startIndex xmlns:ns2="http://a9.com/-/spec/opensearchrss/1.0/">1</ns2:startIndex><ns0:title>Google Analytics Data for Profile 42156377</ns0:title><ns0:author><ns0:name>Google Analytics</ns0:name></ns0:author><ns0:link href="https://www.google.com/analytics/feeds/data?max-results=10000&sort=-ga%3AnewVisits&end-date=2011-04-05&start-date=2011-03-22&metrics=ga%3Avisits%2Cga%3Avisitors%2Cga%3AnewVisits%2Cga%3AuniquePageviews&ids=ga%3A42156377&dimensions=ga%3ApagePath&filters=ga%3ApagePath%3D%7E%5E%2Fpackage%2F" rel="self" type="application/atom+xml" /><ns2:endDate xmlns:ns2="http://schemas.google.com/analytics/2009">2011-04-05</ns2:endDate><ns2:totalResults xmlns:ns2="http://a9.com/-/spec/opensearchrss/1.0/">152</ns2:totalResults><ns2:startDate xmlns:ns2="http://schemas.google.com/analytics/2009">2011-03-22</ns2:startDate><ns2:itemsPerPage xmlns:ns2="http://a9.com/-/spec/opensearchrss/1.0/">10000</ns2:itemsPerPage></ns0:feed>
|
|
@ -0,0 +1,87 @@
|
|||
import httplib
|
||||
|
||||
|
||||
from ckan.config.middleware import make_app
|
||||
from paste.deploy import appconfig
|
||||
import paste.fixture
|
||||
from ckan.tests import conf_dir, url_for, CreateTestData
|
||||
|
||||
from mockgoogleanalytics import runmockserver
|
||||
from ckanext.googleanalytics.commands import LoadAnalytics
|
||||
from ckanext.googleanalytics import dbutil
|
||||
|
||||
|
||||
class MockClient(httplib.HTTPConnection):
|
||||
def request(self, http_request):
|
||||
filters = http_request.uri.query.get('filters')
|
||||
path = http_request.uri.path
|
||||
if filters:
|
||||
if "package" in filters:
|
||||
path += "/package"
|
||||
else:
|
||||
path += "/download"
|
||||
httplib.HTTPConnection.request(self,
|
||||
http_request.method,
|
||||
path)
|
||||
resp = self.getresponse()
|
||||
return resp
|
||||
|
||||
|
||||
class TestConfig:
|
||||
def test_config(self):
|
||||
config = appconfig('config:test.ini', relative_to=conf_dir)
|
||||
config.local_conf['ckan.plugins'] = 'googleanalytics'
|
||||
command = LoadAnalytics("loadanalytics")
|
||||
command.CONFIG = config.local_conf
|
||||
command.run([])
|
||||
|
||||
@classmethod
|
||||
def teardown_class(cls):
|
||||
CreateTestData.delete()
|
||||
|
||||
|
||||
class TestLoadCommand:
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
config = appconfig('config:test.ini', relative_to=conf_dir)
|
||||
config.local_conf['ckan.plugins'] = 'googleanalytics'
|
||||
config.local_conf['googleanalytics.username'] \
|
||||
= 'borf'
|
||||
config.local_conf['googleanalytics.password'] \
|
||||
= 'borf'
|
||||
config.local_conf['googleanalytics.profile_name'] \
|
||||
= 'borf'
|
||||
cls.config = config.local_conf
|
||||
wsgiapp = make_app(config.global_conf, **config.local_conf)
|
||||
env = {'HTTP_ACCEPT': ('text/html;q=0.9,text/plain;'
|
||||
'q=0.8,image/png,*/*;q=0.5')}
|
||||
cls.app = paste.fixture.TestApp(wsgiapp, extra_environ=env)
|
||||
CreateTestData.create()
|
||||
runmockserver()
|
||||
|
||||
@classmethod
|
||||
def teardown_class(cls):
|
||||
CreateTestData.delete()
|
||||
conn = httplib.HTTPConnection("localhost:%d" % 6969)
|
||||
conn.request("QUIT", "/")
|
||||
conn.getresponse()
|
||||
|
||||
def test_top_packages(self):
|
||||
command = LoadAnalytics("loadanalytics")
|
||||
command.TEST_HOST = MockClient('localhost', 6969)
|
||||
command.CONFIG = self.config
|
||||
command.run([])
|
||||
packages = dbutil.get_top_packages()
|
||||
resources = dbutil.get_top_resources()
|
||||
assert packages[0][1] == 2
|
||||
assert resources[0][1] == 4
|
||||
|
||||
def test_download_count_inserted(self):
|
||||
command = LoadAnalytics("loadanalytics")
|
||||
command.TEST_HOST = MockClient('localhost', 6969)
|
||||
command.CONFIG = self.config
|
||||
command.run([])
|
||||
response = self.app.get(url_for(controller='package',
|
||||
action='read',
|
||||
id='annakarenina'))
|
||||
assert "(4 downloads)" in response.body, response.body
|
Loading…
Reference in New Issue