1""" 2 Test Script for Lua Interpreter Flask server. 3""" 4 5import unittest 6import app 7from bs4 import BeautifulSoup 8 9 10class TestWebApp(unittest.TestCase): 11 12 def setUp(self): 13 """This method will be run before each of the test methods in the class.""" 14 self.client = app.app.test_client() 15 16 def test_home_page_render(self): 17 response = self.client.get('/') 18 self.assertIn('Lua Telemetry Interpreter', str(response.data)) 19 20 def test_get_published_data_file_names_and_content(self): 21 response = self.client.post('/get_published_data_file_names_and_content') 22 data = response.get_json()['file_names'] 23 self.assertCountEqual([ 24 'activity_foreground_state_changed', 'anr_occurred', 25 'app_start_memory_state_captured', 'app_crash_occurred', 26 'connectivity_publisher', 'memory_publisher', 'process_cpu_time', 27 'process_memory_snapshot', 'process_memory_state', 28 'vehicle_property_publisher', 'wtf_occurred' 29 ], data) 30 data = response.get_json()['memory_publisher'] 31 self.assertIn('"mem.timestamp_millis": 1664995933733', data) 32 33 def test_execute_script_output(self): 34 response = self.client.post( 35 '/execute_script', 36 data={ 37 'script': 38 'function test(data,state) ' \ 39 'tbl = {}; tbl[\'sessionId\'] = state.data + data.id;' \ 40 'on_metrics_report(tbl) end', 41 'function-name': 'test', 42 'published-data': '{"id": 2}', 43 'saved-state': '{"data": 5}' 44 }) 45 rendered_html = BeautifulSoup(response.data.decode('UTF-8'), 'html.parser') 46 self.assertIn('{<br/> "sessionId": 7<br/>}', 47 str(rendered_html.find(id='script-output'))) 48 49 def test_execute_script_loading_error(self): 50 response = self.client.post( 51 '/execute_script', 52 data={ 53 'script': 'function test(data, state) x == 1 end', 54 'function-name': 'test', 55 'published-data': "{}", 56 'saved-state': "{}" 57 }) 58 rendered_html = BeautifulSoup(response.data.decode('UTF-8'), 'html.parser') 59 span = rendered_html.find(id='script-output').find('span') 60 self.assertIn( 61 'Error encountered while loading the script. A possible cause could ' \ 62 'be syntax errors in the script.', 63 str(span)) 64 65 def test_execute_script_running_error(self): 66 response = self.client.post( 67 '/execute_script', 68 data={ 69 'script': 'function test(data, state) call() end', 70 'function-name': 'test', 71 'published-data': "{}", 72 'saved-state': "{}" 73 }) 74 rendered_html = BeautifulSoup(response.data.decode('UTF-8'), 'html.parser') 75 span = rendered_html.find(id='script-output').find('span') 76 self.assertIn('Error encountered while running the script.', str(span)) 77 78 def test_execute_script_running_error_with_stacktrace(self): 79 response = self.client.post( 80 '/execute_script', 81 data={ 82 'script': 'function func_1(data, state) func_2() end function func_2() func_3() end', 83 'function-name': 'func_1', 84 'published-data': "{}", 85 'saved-state': "{}" 86 }) 87 rendered_html = BeautifulSoup(response.data.decode('UTF-8'), 'html.parser') 88 span = rendered_html.find(id='script-output').find('span') 89 self.assertIn('Error encountered while running the script.', str(span)) 90 self.assertIn('func_3', str(span)) 91 self.assertIn('func_2', str(span)) 92 self.assertIn('func_1', str(span)) 93 94 def test_execute_script_saved_state_unchanged(self): 95 response = self.client.post( 96 '/execute_script', 97 data={ 98 'script': 'function test(data, state) log(2) end', 99 'function-name': 'test', 100 'published-data': "{}", 101 'saved-state': '{"test": "state"}' 102 }) 103 rendered_html = BeautifulSoup(response.data.decode('UTF-8'), 'html.parser') 104 span = rendered_html.find(id='saved-state-input').getText() 105 self.assertIn('{"test": "state"}', str(span)) 106 107 def test_execute_script_saved_state_changed(self): 108 response = self.client.post( 109 '/execute_script', 110 data={ 111 'script': 'function test(data, state) on_success(data) end', 112 'function-name': 'test', 113 'published-data': '{"test": "data"}', 114 'saved-state': '{"test": "state"}' 115 }) 116 rendered_html = BeautifulSoup(response.data.decode('UTF-8'), 'html.parser') 117 span = rendered_html.find(id='saved-state-input').getText() 118 self.assertIn('{\n "test": "data"\n}', str(span)) 119 120 def test_execute_script_faulty_published_data(self): 121 response = self.client.post('/execute_script', 122 data={ 123 'script': 'function test(data, state) end', 124 'function-name': 'test', 125 'published-data': "", 126 'saved-state': "{}" 127 }) 128 rendered_html = BeautifulSoup(response.data.decode('UTF-8'), 'html.parser') 129 span = rendered_html.find(id='script-output').find('span') 130 self.assertIn('Error from parsing published data', str(span)) 131 132 def test_execute_script_faulty_saved_state(self): 133 response = self.client.post('/execute_script', 134 data={ 135 'script': 'function test(data, state) end', 136 'function-name': 'test', 137 'published-data': "{}", 138 'saved-state': "f" 139 }) 140 rendered_html = BeautifulSoup(response.data.decode('UTF-8'), 'html.parser') 141 span = rendered_html.find(id='script-output').find('span') 142 self.assertIn('Error from parsing saved state', str(span)) 143 144 def test_execute_script_wrong_function(self): 145 response = self.client.post('/execute_script', 146 data={ 147 'script': 'function test(data, state) end', 148 'function-name': 'tes', 149 'published-data': "{}", 150 'saved-state': "{}" 151 }) 152 rendered_html = BeautifulSoup(response.data.decode('UTF-8'), 'html.parser') 153 span = rendered_html.find(id='script-output').find('span') 154 self.assertIn('Wrong function name.', str(span)) 155 156 def test_prettify_json_success(self): 157 self.assertEqual('{\n "test": 2\n}', app.prettify_json('{"test":2}')) 158 159 def test_prettify_json_failure(self): 160 self.assertEqual('not_a_json', 'not_a_json') 161 162 163if __name__ == '__main__': 164 unittest.main()