def test_drilldown(self): response = self.app.get(url(controller='api2', action='aggregate', dataset='cra', drilldown='cofog1|cofog2')) h.assert_equal(response.status, '200 OK') result = json.loads(response.body) h.assert_equal(result['summary']['num_drilldowns'], 6) h.assert_equal(result['summary']['amount'], -371500000.0)
def test_browser_for_entity(self): h.skip_if_stubbed_solr() from openspending.model import Dataset, Entry dataset = Dataset(name='testdataset') Dataset.c.save(dataset, manipulate=True) dataset_ref_dict = dataset.to_ref_dict() entity = self._make_one(name="Test Entity", label="Test Entity Label") entity_ref_dict = entity.to_ref_dict() entry = {'name': 'Test Entry', 'label': 'Test Entry Label', 'from': entity_ref_dict, 'to': entity_ref_dict, 'amount': 10.0, 'dataset': dataset_ref_dict} Entry.c.save(entry) h.clean_and_reindex_solr() entity_url = url(controller='entity', id=str(entity['_id']), slug='test-entity-label', action='view') response = self.app.get(entity_url) h.assert_equal(response._status, '200 OK') h.assert_true('<b>1 entries</b> found.<br />' in response) h.assert_true('entries.json">' in response) h.assert_true('entries.csv">' in response)
def test_cut(self): response = self.app.get(url(controller='api2', action='aggregate', dataset='cra', cut='year:2009')) h.assert_equal(response.status, '200 OK') result = json.loads(response.body) h.assert_equal(result['summary']['num_drilldowns'], 7) h.assert_equal(result['summary']['amount'], 57300000.0)
def test_foi(self): t = model.Entry.find_one() response = self.app.get(url(controller='entry', action='view', id=str(t.id), name=t['name'])) # For now, just check we AREN'T showing the FOI screen on CRA pages. # TODO: more detailed testing. assert not 'Make an FOI request' in response
def test_dataset(self): response = self.app.get(url(controller='dataset', action='view', format='json', id=self.cra.name)) assert '"_id":' in response, response assert '"name": "cra"' in response, response
def test_aggregate(self): response = self.app.get(url(controller="api", action="aggregate", dataset="cra")) assert '"metadata": {' in response, response assert '"dataset": "cra"' in response, response assert '"include": []' in response, response assert '"dates":' in response, response assert '"axes": []' in response, response assert '"results": [' in response
def test_jsonp_mytax(self): # Copied from test_mytax. callback = randomjsonpcallback() u = url(controller="api", action="mytax", income=20000, callback=callback) print u response = self.app.get(u) assert '"tax": ' in response, response assert '"explanation": ' in response, response assert valid_jsonp(response, callback)
def test_entity_as_json(self): entity = self._make_one(name="Test Entity", label="Test Entity Label") response = self.app.get(url(controller='entity', id=str(entity['_id']), action='view', format='json')) h.assert_equal(response._status, '200 OK') h.assert_equal(response._headers['Content-Type'], 'application/json') h.assert_true('"name": "Test Entity",' in response._body, 'json fragment not found. got: %s' % response._body)
def test_view_entries_csv(self): classifier = self.db['classifier'].find_one({'taxonomy': 'cofog', 'name': '03'}) url_ = url(controller='classifier', action='entries', format='csv', taxonomy=classifier['taxonomy'], name=classifier['name']) result = self.app.get(url_) h.assert_equal(result.status, '200 OK') h.assert_equal(result.content_type, 'text/csv') h.assert_true(result.body.startswith('_id,amount,')) # csv headers
def test_aggregate_with_per_region(self): u = url( controller="api", action="aggregate", **{"dataset": "cra", "breakdown-region": "yes", "per-population2006": "region"} ) print u response = self.app.get(u) print response assert '"region"' in response, response assert '"ENGLAND_London"' in response, response assert "0.1" in response, response
def test_jsonp_aggregate(self): # Copied from test_aggregate_with_breakdown. callback = randomjsonpcallback() u = url( controller="api", callback=callback, action="aggregate", **{"dataset": "cra", "breakdown-region": "yes"} ) print u response = self.app.get(u) print response, str(response) assert '"region"' in response, response assert '"ENGLAND_London"' in response, response assert valid_jsonp(response, callback)
def test_404(self): response = self.app.get( url(controller='error_test', action='not_found'), status=404 ) assert "404 Not Found" in response, \ "'404 Not Found' not in response to request that should give a 404!" assert "OpenSpending" in response, \ "'OpenSpending' not in 404 page! Is this not a custom 404?" assert "Custom 404 error message" in response, \ "Custom error message was not passed through to 404 error page."
def test_common_pageview(self): h.skip_if_stubbed_solr() entity = self._make_one(name="Test Entity", label="Test Entity Label") response = self.app.get(url(controller='entity', id=str(entity['_id']), slug='test-entity-label', action='view')) h.assert_equal(response._status, '200 OK') h.assert_true('Test Entity Label' in response) json_name = '%s.json' % entity['_id'] h.assert_true(json_name in response, 'Entity json "%s" not found in output' % json_name)
def test_order(self): def unique(seq): result = [] for item in seq: if item not in result: result.append(item) return result response = self.app.get(url(controller='api2', action='aggregate', dataset='cra', order='year:asc')) h.assert_equal(response.status, '200 OK') result = json.loads(response.body) order = [cell['year'] for cell in result['drilldown']] h.assert_equal(unique(order), [2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010]) response = self.app.get(url(controller='api2', action='aggregate', dataset='cra', order='year:desc')) h.assert_equal(response.status, '200 OK') result = json.loads(response.body) order = [cell['year'] for cell in result['drilldown']] h.assert_equal(unique(order), [2010, 2009, 2008, 2007, 2006, 2005, 2004, 2003])
def test_view_entries_html(self): classifier = self.db['classifier'].find_one({'taxonomy': 'cofog', 'name': '03'}) url_ = url(controller='classifier', action='entries', format='html', taxonomy=classifier['taxonomy'], name=classifier['name']) result = self.app.get(url_) h.assert_equal(result.status, '200 OK') h.assert_equal(result.content_type, 'text/html') h.assert_true(('<h2 class="page-title">Public order and ' 'safety: Entries</h2>') in result) h.assert_equal(result.body.count('full entry'), 5)
def test_entry_custom_html(self): tpl = '<a href="/custom/path/%s">%s</a>' self.cra.entry_custom_html = tpl % ('${entry.id}', '${entry.name}') self.cra.save() t = model.Entry.find_one() response = self.app.get(url(controller='entry', action='view', id=str(t.id), name=t['name'])) assert tpl % (t.id, t.name) in response, \ 'Custom HTML not present in rendered page!'
def test_403(self): response = self.app.get( url(controller='error_test', action='not_authorised'), status=403 ) assert "403 Forbidden" in response, \ "'403 Not Found' not in response to request that should give a 403!" assert "OpenSpending" in response, \ "'OpenSpending' not in 403 page! Is this not a custom 403?" assert "Custom 403 error message" in response, \ "Custom error message was not passed through to 403 error page."
def test_aggregate(self): response = self.app.get(url(controller='api2', action='aggregate', dataset='cra')) h.assert_equal(response.status, '200 OK') h.assert_equal(response.content_type, 'application/json') result = json.loads(response.body) h.assert_equal(sorted(result.keys()), [u'drilldown', u'summary']) h.assert_equal(sorted(result['summary'].items()), [(u'amount', -371500000.0), (u'num_drilldowns', 36), (u'num_entries', 36), (u'page', 1), (u'pages', 1), (u'pagesize', 10000)])
def test_view_entries_json(self): classifier = self.db['classifier'].find_one({'taxonomy': 'cofog', 'name': '03'}) url_ = url(controller='classifier', action='entries', format='json', taxonomy=classifier['taxonomy'], name=classifier['name']) result = self.app.get(url_) h.assert_equal(result.status, '200 OK') h.assert_equal(result.content_type, 'application/json') json_data = json.loads(result.body) h.assert_equal(len(json_data['results']), 5)
def test_entry(self): example = model.Entry.find_one({ 'dataset.name': self.cra.name, 'from.name': 'Dept047' }) response = self.app.get(url(controller='entry', action='view', format='json', id=str(example.id))) assert '"_id":' in response, response assert '"cofog1":' in response, response assert '"from":' in response, response assert '"Dept047"' in response, response
def test_view_with_more_entities(self): # Test the view for an entry that has # entities for more dimensions than to and from from openspending.logic.entry import entitify_entry entry = model.Entry.find_one() # use the existing 'pog' dimensions and stuff a ref to an entity # in there. pog_entity = model.Entity(name='pog-entity', label='Test Pog Entity') pog_entity.save() entitify_entry(entry, pog_entity, 'pog') entry.save() response = self.app.get(url(controller='entry', action='view', id=str(entry.id), name=entry['name'])) assert 'Test Pog Entity' in response
def test_500(self): # NB: This test will fail if the tests are run in debug mode. Add the # following line to your test.ini under [app:main]: # # set debug = False response = self.app.get( url(controller='error_test', action='server_error'), status=500 ) assert "Server Error" in response, \ "'Server Error' not in response to request that should give a 500!" assert "OpenSpending" in response, \ "'OpenSpending' not in 500 page! Is this not a custom 500?" assert "Custom 500 error message" in response, \ "Custom error message was not passed through to 500 error page." assert "The Count" in response, \ "What have you done to The Count!? Put him back this instant!"
def test_mytax(self): u = url(controller="api", action="mytax", income=20000) print u response = self.app.get(u) assert '"tax": ' in response, response assert '"explanation": ' in response, response
def test_aggregate_with_breakdown(self): u = url(controller="api", action="aggregate", **{"dataset": "cra", "breakdown-region": "yes"}) response = self.app.get(u) assert '"region"' in response, response assert '"ENGLAND_London"' in response, response
def test_search_03_jsonpify(self): callback = "mycallback" response = self.app.get(url(controller="api", action="search", q="children", callback=callback)) assert response.body.startswith('%s({"responseHeader"' % callback), response.body
def test_search_02_query(self): response = self.app.get(url(controller="api", action="search", q="Children")) out = json.loads(str(response.body))["response"] assert out["numFound"] == 7, out["numFound"] exp_entity = "Department for Children, Schools and Families" assert out["docs"][0]["from.label"] == exp_entity, out["docs"][0]
def test_index(self): response = self.app.get(url(controller='rest', action='index')) for word in ['dataset', 'entry']: assert word in response, response
def test_search_01_no_query(self): response = self.app.get(url(controller="api", action="search")) out = json.loads(str(response.body))["response"] assert out["numFound"] == 36, out["numFound"] assert out["docs"][0]["dataset"] == "cra", out
def test_view(self): t = model.Entry.find_one() response = self.app.get(url(controller='entry', action='view', id=str(t.id), name=t['name'])) assert 'cra' in response
def test_index(self): response = self.app.get(url(controller='home', action='index')) assert 'OpenSpending' in response