def test_06_searchcloud_displays_when_approved_data_present(self): #Put some data into the cloud searchcloud.generate_unapproved_list(model.Session, days=30) latest_rows = searchcloud.get_latest(model.Session) self.assert_equal(latest_rows, [[u'Health', 3L], [u"Water<>&\"{}'", 2L], [u'Tax\u6c49\u5b57\u6f22\u5b57', 1L]]) searchcloud.update_approved(model.Session, latest_rows) approved_rows = searchcloud.get_approved(model.Session) self.assert_equal(latest_rows, approved_rows) #We'll use this test data later, so save it. model.Session.commit() # Test that the JSON text is what we expect. self.assert_equal( expected_json, searchcloud.approved_to_json(approved_rows) ) # Now we should get a cloud home_url = tests.url_for('home') res = self.app.get(home_url) self.assert_equal( 'var word_array = '+(cgi.escape(expected_json)) in res, True, ) self.assert_equal('Popular terms' in res, True) self.assert_equal('jqcloud-1.0.4.min.js' in res, True) self.assert_equal('jqcloud.css' in res, True) self.assert_equal('<div id="searchcloud"' in res, True)
def download(self): self._sysadmin_or_abort() data = searchcloud.get_latest(model.Session) # We don't know when the script is run, so let's assume just # after midnight and use today's date date = datetime.datetime.now().strftime('%Y-%m-%d') response.charset = 'utf8' response.content_type = 'application/json' response.headers['Content-Disposition'] = \ 'attachment; filename="ecodp-searchcloud-latest-%s.json"' % date return json.dumps(data, indent=4)
def test_08_updating_latest_table(self): # Check the sysadmin exists sysadmin = model.User.by_name(u'testsysadmin') self.assert_equal(sysadmin is not None, True) # Check that they can access the two versions of the index page for url in ['/searchcloud', '/searchcloud/']: res = self.app.get(url, status=200, extra_environ={'REMOTE_USER': '******'}) self.assert_equal('/searchcloud/download' in res, True) self.assert_equal('/searchcloud/upload' in res, True) # Download a file res = self.app.get('/searchcloud/download', status=200, extra_environ={'REMOTE_USER': '******'}) self.assert_equal(res.header_dict['Content-Disposition'.lower()].startswith('attachment'), True) self.assert_equal(res.header_dict['Content-Type'.lower()], 'application/json; charset=utf8') expected_results = json.dumps( [ [ "Health", 3 ], [ "Water<>&\"{}'", 2 ], [ u"Tax\u6c49\u5b57\u6f22\u5b57", 1 ] ], indent=4 ) latest_rows = searchcloud.get_latest(model.Session) self.assert_equal(expected_results, res.body) self.assert_equal(json.loads(res.body), latest_rows) # Check you get an upload form res = self.app.get('/searchcloud/upload', status=200, extra_environ={'REMOTE_USER': '******'}) self.assert_equal('type="file"' in res.body, True) self.assert_equal('enctype="multipart/form-data"' in res.body, True) # Check you get a preview res = self.app.post( '/searchcloud/upload', status=200, extra_environ={'REMOTE_USER': '******'}, upload_files=[('searchcloud', 'test.json', expected_results.replace('Health', 'Environment'))] ) self.assert_equal(cgi.escape(expected_json.replace('Health', 'Environment')) in res.body, True) # Check you get a saved message res = self.app.post( '/searchcloud/save', status=200, extra_environ={'REMOTE_USER': '******'}, params={'searchcloud': expected_results.replace('Health', 'Environment')} ) self.assert_equal('Search Cloud Successfully Updated' in res.body, True) model.Session.commit() # Check the new data is in the approved table self.assert_equal(searchcloud.get_approved(model.Session), [[u'Environment', 3L], [u"Water<>&\"{}'", 2L], [u'Tax\u6c49\u5b57\u6f22\u5b57', 1L]]) # Finally test that the new data is on the homepage home_url = tests.url_for('home') res = self.app.get(home_url) self.assert_equal( 'var word_array = '+(cgi.escape(expected_json.replace('Health', 'Environment'))) in res, True, )