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 approved_search_terms(): try: terms = searchcloud.get_approved(model.Session) if terms: return searchcloud.approved_to_json(terms) except sqlalchemy.exc.ProgrammingError: log.error('Could not retrieve search cloud results from database. ' 'Do the tables exist? Rolling back the session.') model.Session.rollback()
def test_09_json_parsing_and_errors(self): # Post the wrong data and check we get an error res = self.app.post( '/searchcloud/save', status=200, extra_environ={'REMOTE_USER': '******'}, params={'searchcloud': expected_json} ) self.assert_equal('Error Accepting JSON File' in res.body, True) # Check it hasn't changed the approved data self.assert_equal(searchcloud.get_approved(model.Session), [[u'Environment', 3L], [u"Water<>&\"{}'", 2L], [u'Tax\u6c49\u5b57\u6f22\u5b57', 1L]]) # Post an empty list to empty the table res = self.app.post( '/searchcloud/save', status=200, extra_environ={'REMOTE_USER': '******'}, params={'searchcloud': '[]'} ) model.Session.commit() self.assert_equal('Search Cloud Successfully Updated' in res.body, True) # The approved data table should be empty self.assert_equal(searchcloud.get_approved(model.Session), [])
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, )