def test_UserSettingsLoaded(self): ''' Make sure the user settings are properly loaded on the page and match the database ''' self.driver.get(PATH + '/settings') # Get the data from the user settings table. userSettings_table = UserSettings.query.all()[0] # Get the values from the page userSettings_page = UserSettings() userSettings_page.shock = self.driver.find_element_by_xpath( '//*[@id="shock"]').is_selected() userSettings_page.noise = self.driver.find_element_by_xpath( '//*[@id="noise"]').is_selected() userSettings_page.vibration = self.driver.find_element_by_xpath( '//*[@id="vibration"]').is_selected() userSettings_page.alertFrequency = self.driver.find_element_by_xpath( '//*[@id="alertFrequency"]').get_attribute("value") userSettings_page.drowsinessThreshold = self.driver.find_element_by_xpath( '//*[@id="drowsinessThreshold"]').get_attribute("value") # Check that both objects have the same values self.assertEqual(bool(userSettings_table.shock), userSettings_page.shock) self.assertEqual(bool(userSettings_table.noise), userSettings_page.noise) self.assertEqual(bool(userSettings_table.vibration), userSettings_page.vibration) self.assertEqual(userSettings_table.alertFrequency, int(userSettings_page.alertFrequency)) self.assertEqual(userSettings_table.drowsinessThreshold, float(userSettings_page.drowsinessThreshold))
def settings(): usersettings = UserSettings.query.all()[0] form = UserSettingsForm(form_name='User Settings') # Render Page if request.method == "GET": return render_template('settings.html', title='User Device Settings', form=form, usersetting=usersettings) # Submit Form and Make Changes to Local Database if request.method == 'POST': # Get the Results as a UserSettings object result = UserSettings() if request.form.get("shock") == 'y': result.shock = 1 else: result.shock = 0 if request.form.get("vibration") == 'y': result.vibration = 1 else: result.vibration = 0 if request.form.get("noise") == 'y': result.noise = 1 else: result.noise = 0 result.alertFrequency = int(request.form.get('alertFrequency')) result.drowsinessThreshold = float( request.form.get('drowsinessThreshold')) # # DEBUGGING: View Results # print(result) # settings from page # print(usersettings) # settings from database # oldSettings = usersettings # UPDATE values if result.shock != usersettings.shock: print("Updating shock in Database...") usersettings.shock = result.shock if result.vibration != usersettings.vibration: print("Updating vibration in Database...") usersettings.vibration = result.vibration if result.noise != usersettings.noise: print("Updating noise in Database...") usersettings.noise = result.noise if result.alertFrequency != usersettings.alertFrequency: print("Updating alertFrequency in Database...") usersettings.alertFrequency = result.alertFrequency if result.drowsinessThreshold != usersettings.drowsinessThreshold: print("Updating drowsinessThreshold in Database...") usersettings.drowsinessThreshold = result.drowsinessThreshold db.session.commit() # Commit the changes # # DEBUGGING: View Differences # print(UserSettings.query.all()[0]) # View the Database Data # print(oldSettings) # View the Old Settings usersettings_new = UserSettings.query.all()[0] form_updated = UserSettingsForm(form_name='User Settings') return render_template('settings.html', title='User Device Settings', form=form_updated, usersetting=usersettings_new)