How to use test_text_content method in Playwright Python

Best Python code snippet using playwright-python

evaluate_route.py

Source:evaluate_route.py Github

copy

Full Screen

1from flask import render_template, redirect, request, url_for, session2from werkzeug.utils import secure_filename3from opennlu import app4from opennlu.services import rasa_service 5from rasa.nlu.model import Interpreter6import os7import json8910DATA_FOLDER = os.path.join('opennlu/data','data')11app.config['DATA_FOLDER'] = DATA_FOLDER12PLOTS_FOLDER = os.path.join('opennlu/static', 'plots') # for file check13app.config['PLOTS_FOLDER'] = PLOTS_FOLDER14PLOT_FOLDER = os.path.join('/static', 'plots') # for image openning (relative path)15app.config['PLOT_FOLDER'] = PLOT_FOLDER1617PT_CACHE_FOLDER = os.path.join(os.getcwd(),'cached_data')1819#Redirect according to current model type20@app.route('/evaluate', methods = ['GET', 'POST'])21def evaluate():22 if 'model_type' in session:23 if session['model_type'] == 'rasa':24 return redirect(url_for('evaluate_rasa'))25 elif session['model_type'] == 'pytorch':26 return redirect(url_for('evaluate_pytorch'))27 else: # session['model_type'] == 'tensorflow'28 return redirect(url_for('evaluate_tensorflow'))29 else: #no models trained yet30 return redirect(url_for('model',fail=True))313233#Evaluate & Test interface for rasa model34@app.route('/evaluate/rasa', methods = ['GET', 'POST'])35def evaluate_rasa():36 if request.method == 'POST':37 #Interpret single message38 if 'interpret' in request.form:39 #Get rasa model40 rasa = app.config['RASA'].get_model(session['model_name'])41 #Perform interpretation42 session['msg'] = request.form['msg']43 result = rasa.results(session['msg'])44 session['results'] = json.dumps(result, indent=2)45 session['intent_name'] = result['intent']['name'] #extract info from nested dict46 session['intent_score'] = result['intent']['confidence']47 entities = result['entities']48 if len(entities) != 0:49 session['entity_names'] = [item['value'] for idx, item in enumerate(entities)] #convert dict to list50 session['entity_types'] = [item['entity'] for idx, item in enumerate(entities)]51 session['entity_scores'] = [item['confidence'] for idx, item in enumerate(entities)]52 session['entity_extractors'] = [item['extractor'] for idx, item in enumerate(entities)]53 else: #no entities found54 session['entity_names'] = ['-nil-']55 session['entity_types'] = ['-nil-']56 session['entity_scores'] = ['-']57 session['entity_extractors'] = ['-nil-']58 #Test data set59 else:60 test_data = request.files['testfile']61 test_filename = os.path.join(app.config['DATA_FOLDER'], test_data.filename)62 test_data.save(os.path.join(app.config['DATA_FOLDER'],secure_filename(test_data.filename))) #store in data folder63 with open(test_filename, "r") as f:64 session['test_data'] = f.read()65 session['test_name'] = test_filename66 #Get rasa model67 rasa = app.config['RASA'].get_model(session['model_name'])68 #Peform evaluation & get results69 [session['int_metrics'], session['ent_metrics'], session['int_results'], session['ent_results']] = rasa.evaluate_metrics(test_filename) 70 session['list_size'] = len(session['int_results'][0]) 71 #Plot confusion matrix and histogram72 basename = os.path.basename(session['test_name'])73 cf_name = session['model_name'] + '_' + os.path.splitext(basename)[0] + '_cf.png'74 hist_name = session['model_name'] + '_' + os.path.splitext(basename)[0] + '_hist.png'75 session['cf_path'] = os.path.join(app.config['PLOTS_FOLDER'], cf_name)76 session['hist_path'] = os.path.join(app.config['PLOTS_FOLDER'], hist_name)77 if os.path.isfile(session['cf_path']):78 os.remove(session['cf_path'])79 if os.path.isfile(session['hist_path']): 80 os.remove(session['hist_path']) 81 rasa.compute_confusion_matrix(session['cf_path']) #hist and cf stored in static/plots folder82 rasa.compute_histogram(session['hist_path'])83 [intent_report, entity_report] = rasa.evaluation_get_individual_report()84 session['intent_report'] = json.dumps(intent_report, indent=2)85 session['entity_report'] = json.dumps(entity_report, indent=2)8687 #Pre-process: copy data from previous load (if any)88 if 'msg' not in session:89 session['msg'] = ""90 if 'intent_name' not in session:91 session['intent_name'] = ""92 session['intent_score'] = ""93 session['entity_names'] = ""94 session['entity_types'] = ""95 session['entity_scores'] = ""96 session['entity_extractors'] = ""97 session['results'] = ""98 99 #Dictionary to pass to view for single msg interpretation100 msg_chunk = {101 'intent_name':session['intent_name'],102 'intent_score':session['intent_score'],103 'entity_names':session['entity_names'],104 'entity_types':session['entity_types'],105 'entity_scores':session['entity_scores'],106 'entity_extractors':session['entity_extractors'],107 'results':session['results'],108 'msg':session['msg']109 }110 #Dictionary to pass to view for testing111 if 'test_data' not in session:112 test_chunk = {113 }114 else:115 test_chunk = {116 'int_metrics':session['int_metrics'],117 'ent_metrics':session['ent_metrics'],118 'int_results':session['int_results'],119 'ent_results':session['ent_results'],120 'list_size':session['list_size'],121 'hist_path':os.path.join(app.config['PLOT_FOLDER'], hist_name),122 'cf_path':os.path.join(app.config['PLOT_FOLDER'], cf_name),123 'int_report':session['intent_report'],124 'ent_report':session['entity_report']125 }126 return render_template('/evaluate/rasa.html', **msg_chunk, **test_chunk)127 else:128 #Clear session variables (when loading url directly)129 msg_list = ['msg', 'intent_name', 'entity_names', 'intent_score', 'entity_scores', 'entity_types', 'entity_extractors', 'results']130 for key in msg_list:131 if key in session:132 session.pop(key)133134 test_list = ['test_data', 'test_name', 'int_metrics', 'ent_metrics', 'int_results', 'ent_results', 'list_size', 'hist_path', 'cf_path', 'intent_report', 'entity_report']135 for key in test_list:136 if key in session:137 session.pop(key)138139 #Check if model is loaded140 if app.config['RASA'].size() == 0:141 return redirect(url_for('model',fail=True))142 else:143 return render_template('/evaluate/rasa.html')144145146#Evaluate & Test interface for pytorch model147@app.route('/evaluate/pytorch', methods = ['GET', 'POST'])148def evaluate_pytorch():149 if request.method == 'POST':150 #Interpret single message151 if 'interpret' in request.form:152 #Get pytorch model153 pytorch = app.config['PT'].get_model(session['model_name'])154 #Perform interpretation155 session['msg'] = request.form['msg']156 [intent, slot, confid_score] = pytorch.predict(session['msg'])157 session['intent_msg'] = intent158 session['slot_msg'] = slot159 session['score'] = str(confid_score)160 else:161 #download multiple files from the folder162 list_folder = request.files.getlist('test_folder') #list()163 #check if folder contains correct files164 file_check = {'label':0, 'seq.in':0, 'seq.out':0}165 for file in list_folder:166 if os.path.basename(file.filename) in file_check:167 file_check[os.path.basename(file.filename)] = file_check[os.path.basename(file.filename)] + 1 168 if 0 in file_check.values(): #check if filenames meet requirement169 fail = True170 fail_message = 'Files uploaded do not match filename requirements. Please check if your label, text sequence and BIO-tag sequence files are named as label, seq.in and seq.out respectively for system to recognise.'171 return redirect(url_for('evaluate_pytorch',fail=fail,fail_message=fail_message))172 elif not all([False for value in file_check.values() if value>1]): #invalid data folder: contains more than one of each label,seq.in,seq.out files173 fail = True174 fail_message = 'Invalid folder selected! Folder contains more than required number of files (3). Please select the direct parent data folder with only one instance of label, seq.in and seq.out file.'175 return redirect(url_for('evaluate_pytorch',fail=fail,fail_message=fail_message))176 else: 177 # extract data from files178 for file in list_folder:179 if os.path.basename(file.filename) == 'label':180 file.seek(0)181 test_label_name = file.filename182 test_label_content = file.read().decode("utf-8")183 elif os.path.basename(file.filename) == 'seq.in':184 file.seek(0)185 test_text_name = file.filename186 test_text_content = file.read().decode("utf-8")187 elif os.path.basename(file.filename) == 'seq.out':188 file.seek(0)189 test_tags_name = file.filename190 test_tags_content = file.read().decode("utf-8")191192 # check if file content satisfy requirements193 len_label = len(test_label_content.splitlines())194 text_ex = test_text_content.splitlines()195 len_text = len(text_ex)196 tags_ex = test_tags_content.splitlines()197 len_tags = len(tags_ex)198 #check if no. of training examples tally199 if ((len_label != len_text) or (len_label != len_tags)): 200 fail = True201 fail_message = 'Number of training examples do not match across the 3 files. Please navigate to edit data page for correction.'202 return redirect(url_for('evaluate_pytorch',fail=fail,fail_message=fail_message))203 #check for each example if token count tally204 for text, tags in zip(text_ex,tags_ex):205 if len(text.split()) != len(tags.split()):206 fail = True207 fail_message = 'Number of word tokens do not match number of tags in BIO-tag sequence. Please navigate to edit data page for correction'208 return redirect(url_for('evaluate_pytorch',fail=fail,fail_message=fail_message))209210 # data safe to save211 test_folder_path = os.path.join(app.config['DATA_FOLDER'],os.path.dirname(test_label_name))212 213 if not os.path.exists(test_folder_path): 214 os.makedirs(test_folder_path)215 label_file = os.path.join(test_folder_path,'label')216 with open(label_file, "w") as f: 217 f.write(test_label_content)218 text_file = os.path.join(test_folder_path,'seq.in')219 with open(text_file, "w") as f: 220 f.write(test_text_content)221 tags_file = os.path.join(test_folder_path,'seq.out')222 with open(tags_file, "w") as f: 223 f.write(test_tags_content)224225 session['test_data'] = test_text_content.splitlines()226227 # copy data to cached folder228 from shutil import copyfile229 new_location = os.path.join(PT_CACHE_FOLDER,session['model_name'])230231 if not os.path.exists(new_location):232 os.makedirs(new_location) # create (task) folder233 os.makedirs(os.path.join(new_location,'test')) # create testing folder234 elif not os.path.exists(os.path.join(new_location,'test')):235 os.makedirs(os.path.join(new_location,'test')) # create testing folder236237 copyfile(os.path.join(test_folder_path,'label'),os.path.join(os.path.join(new_location,'test'),'label'))238 copyfile(os.path.join(test_folder_path,'seq.in'),os.path.join(os.path.join(new_location,'test'),'seq.in'))239 copyfile(os.path.join(test_folder_path,'seq.out'),os.path.join(os.path.join(new_location,'test'),'seq.out')) 240241 # Evaluation:242 #get pytorch model243 pytorch = app.config['PT'].get_model(session['model_name'])244 #peform evaluation & get results245 [session['metrics'], session['int_pred'], session['int_true'], session['slot_pred'], session['slot_true'], confid_score] = pytorch.evaluate()246 ### metrics : loss, intent_accuracy, intent_precision, intent_recall, intent_f1, slot_accuracy, slot_precision, slot_recall, slot_f1, sementic_frame_acc247 session['confid_score'] = [str(score) for score in confid_score]248 [intent_report, slot_report] = pytorch.evaluation_get_individual_report()249 session['intent_report'] = json.dumps(intent_report, indent=2)250 session['slot_report'] = json.dumps(slot_report, indent=2)251 session['list_size'] = len(session['int_true']) 252 #plot confusion matrix and histogram253 basename = os.path.basename(test_folder_path)254 cf_name = session['model_name'] + '_' + basename + '_cf.png'255 hist_name = session['model_name'] + '_' + basename + '_hist.png'256 session['cf_path'] = os.path.join(app.config['PLOTS_FOLDER'], cf_name)257 session['hist_path'] = os.path.join(app.config['PLOTS_FOLDER'], hist_name)258 if os.path.isfile(session['cf_path']):259 os.remove(session['cf_path'])260 if os.path.isfile(session['hist_path']): 261 os.remove(session['hist_path']) 262 pytorch.compute_confusion_matrix(session['cf_path']) #cf and hist stored in static/plots folder263 pytorch.compute_histogram(session['hist_path'])264265 #Pre-process: copy data from previous load (if any)266 if 'msg' not in session:267 session['msg'] = ""268 if 'intent_msg' not in session:269 session['intent_msg'] = ""270 session['slot_msg'] = ""271 session['score'] = ""272 273 #Dictionary to pass to view for single msg interpretation274 msg_chunk = {275 'intent_msg':session['intent_msg'],276 'slot_msg':session['slot_msg'],277 'msg':session['msg'],278 'score':session['score']279 }280 #Dictionary to pass to view for testing281 if 'test_data' not in session:282 test_chunk = {283 }284 else:285 test_chunk = {286 'test_data': session['test_data'],287 'metrics': session['metrics'],288 'int_pred': session['int_pred'], 289 'int_true': session['int_true'], 290 'slot_pred': session['slot_pred'], 291 'slot_true': session['slot_true'],292 'list_size': session['list_size'],293 'cf_path': os.path.join(app.config['PLOT_FOLDER'], cf_name), 294 'hist_path': os.path.join(app.config['PLOT_FOLDER'], hist_name), 295 'int_report': session['intent_report'],296 'slot_report': session['slot_report'],297 'confid_score': session['confid_score']298 }299 return render_template('/evaluate/pytorch.html', **msg_chunk, **test_chunk)300301 else:302 #Clear session variables (when loading url directly)303 msg_list = ['msg', 'intent_msg', 'slot_msg', 'score']304 for key in msg_list:305 if key in session:306 session.pop(key)307308 test_list = ['test_data', 'metrics', 'int_pred', 'int_true', 'slot_pred', 'slot_true', 'list_size', 'cf_path', 'hist_path', 'intent_report', 'slot_report', 'confid_score']309 for key in test_list:310 if key in session:311 session.pop(key)312 313 #Check if model is loaded314 if app.config['PT'].size() == 0:315 return redirect(url_for('model',fail=True))316 else:317 if 'fail' in request.args:318 fail = request.args['fail']319 fail_message = request.args['fail_message']320 return render_template('/evaluate/pytorch.html',fail=fail,fail_message=fail_message)321 else: 322 return render_template('/evaluate/pytorch.html')323324325#Evaluate & Test interface for tensorflow model326@app.route('/evaluate/tensorflow', methods = ['GET', 'POST'])327def evaluate_tensorflow():328 if request.method == 'POST':329 #Interpret single message330 if 'interpret' in request.form:331 #Get tensorflow model332 tensorflow = app.config['TF'].get_model(session['model_name'])333 #Perform interpretation334 session['msg'] = request.form['msg']335 response = tensorflow.predict(session['msg'])336337 session['intent_msg'] = response['intent']['name']338 session['slot_msg'] = response['slots']339 session['score'] = response['intent']['confidence']340341 else:342 #download multiple files from the folder343 list_folder = request.files.getlist('test_folder') #list()344 #check if folder contains correct files345 file_check = {'label':0, 'seq.in':0, 'seq.out':0}346 for file in list_folder:347 if os.path.basename(file.filename) in file_check:348 file_check[os.path.basename(file.filename)] = file_check[os.path.basename(file.filename)] + 1 349 if 0 in file_check.values(): #check if filenames meet requirement350 fail = True351 fail_message = 'Files uploaded do not match filename requirements. Please check if your label, text sequence and BIO-tag sequence files are named as label, seq.in and seq.out respectively for system to recognise.'352 return redirect(url_for('tensorflow',fail=fail,fail_message=fail_message))353 elif not all([False for value in file_check.values() if value>1]): #invalid data folder: contains more than one of each label,seq.in,seq.out files354 fail = True355 fail_message = 'Invalid folder selected! Folder contains more than required number of files (3). Please select the direct parent data folder with only one instance of label, seq.in and seq.out file.'356 return redirect(url_for('tensorflow',fail=fail,fail_message=fail_message))357 else: 358 # extract data from files359 for file in list_folder:360 if os.path.basename(file.filename) == 'label':361 file.seek(0)362 test_label_name = file.filename363 test_label_content = file.read().decode("utf-8")364 elif os.path.basename(file.filename) == 'seq.in':365 file.seek(0)366 test_text_name = file.filename367 test_text_content = file.read().decode("utf-8")368 elif os.path.basename(file.filename) == 'seq.out':369 file.seek(0)370 test_tags_name = file.filename371 test_tags_content = file.read().decode("utf-8")372373 # check if file content satisfy requirements374 len_label = len(test_label_content.splitlines())375 text_ex = test_text_content.splitlines()376 len_text = len(text_ex)377 tags_ex = test_tags_content.splitlines()378 len_tags = len(tags_ex)379 #check if no. of training examples tally380 if ((len_label != len_text) or (len_label != len_tags)): 381 fail = True382 fail_message = 'Number of training examples do not match across the 3 files. Please navigate to edit data page for correction.'383 return redirect(url_for('tensorflow',fail=fail,fail_message=fail_message))384 #check for each example if token count tally385 for text, tags in zip(text_ex,tags_ex):386 if len(text.split()) != len(tags.split()):387 fail = True388 fail_message = 'Number of word tokens do not match number of tags in BIO-tag sequence. Please navigate to edit data page for correction'389 return redirect(url_for('tensorflow',fail=fail,fail_message=fail_message))390391 # data safe to save392 test_folder_path = os.path.join(app.config['DATA_FOLDER'],os.path.dirname(test_label_name))393 394 if not os.path.exists(test_folder_path): 395 os.makedirs(test_folder_path)396 label_file = os.path.join(test_folder_path,'label')397 with open(label_file, "w") as f: 398 f.write(test_label_content)399 text_file = os.path.join(test_folder_path,'seq.in')400 with open(text_file, "w") as f: 401 f.write(test_text_content)402 tags_file = os.path.join(test_folder_path,'seq.out')403 with open(tags_file, "w") as f: 404 f.write(test_tags_content)405406 test_data = test_text_content.splitlines()407 session['test_data'] = test_folder_path408409 # Evaluation:410 #get tensorflow model411 tensorflow = app.config['TF'].get_model(session['model_name'])412 #peform evaluation & get results413 [metrics, predicted_intents, true_intents, predicted_tags, true_tags, confid_score] = tensorflow.evaluate(test_folder_path)414 ### metrics : intent_accuracy, intent_precision, intent_recall, intent_f1, slot_accuracy, slot_precision, slot_recall, slot_f1415 confid_score = [str(score) for score in confid_score]416 417 [intent_report, slot_report] = tensorflow.evaluation_get_individual_report()418 session['intent_report'] = json.dumps(intent_report, indent=2)419 session['slot_report'] = json.dumps(slot_report, indent=2)420 421 session['list_size'] = len(true_intents) 422 #plot confusion matrix & histogram423 basename = os.path.basename(test_folder_path)424 cf_name = session['model_name'] + '_' + basename + '_cf.png'425 hist_name = session['model_name'] + '_' + basename + '_hist.png'426 session['cf_path'] = os.path.join(app.config['PLOTS_FOLDER'], cf_name)427 session['hist_path'] = os.path.join(app.config['PLOTS_FOLDER'], hist_name)428 if os.path.isfile(session['cf_path']):429 os.remove(session['cf_path'])430 if os.path.isfile(session['hist_path']): 431 os.remove(session['hist_path']) 432 tensorflow.compute_confusion_matrix(session['cf_path']) #cf and hist stored in static/plots folder433 tensorflow.compute_histogram(session['hist_path'])434435 #Pre-process: copy data from previous load (if any)436 if 'msg' not in session:437 session['msg'] = ""438 if 'intent_msg' not in session:439 session['intent_msg'] = ""440 session['slot_msg'] = ""441 session['score'] = ""442 443 #Dictionary to pass to view for single msg interpretation444 msg_chunk = {445 'intent_msg':session['intent_msg'],446 'slot_msg':session['slot_msg'],447 'msg':session['msg'],448 'score':session['score']449 }450 #Dictionary to pass to view for testing451 if 'test_data' not in session:452 test_chunk = {453 }454 else:455 test_chunk = {456 'test_data': test_data,457 'metrics': metrics,458 'int_pred': predicted_intents, 459 'int_true': true_intents, 460 'slot_pred': predicted_tags, 461 'slot_true': true_tags,462 'list_size': session['list_size'],463 'cf_path': os.path.join(app.config['PLOT_FOLDER'], cf_name), 464 'hist_path': os.path.join(app.config['PLOT_FOLDER'], hist_name), 465 'int_report': session['intent_report'],466 'slot_report': session['slot_report'],467 'confid_score': confid_score468 }469 return render_template('/evaluate/tensorflow.html', **msg_chunk, **test_chunk)470 471 else:472 #Clear session variables (when loading url directly)473 msg_list = ['msg', 'intent_msg', 'slot_msg', 'score']474 for key in msg_list:475 if key in session:476 session.pop(key)477478 test_list = ['test_data', 'intent_report', 'slot_report', 'list_size', 'cf_path', 'hist_path']479 for key in test_list:480 if key in session:481 session.pop(key)482483 #Check if model is loaded484 if app.config['TF'].size() == 0:485 return redirect(url_for('model',fail=True))486 else:487 if 'fail' in request.args:488 fail = request.args['fail']489 fail_message = request.args['fail_message']490 return render_template('/evaluate/tensorflow.html',fail=fail,fail_message=fail_message)491 else: 492 return render_template('/evaluate/tensorflow.html')493 ...

Full Screen

Full Screen

test_gmail_client.py

Source:test_gmail_client.py Github

copy

Full Screen

1# coding=utf-82"""3Test Suit for gmail_client.py module4"""5import os6import base647import unittest8from unittest import mock9from ..magen_gmail_client_api import gmail_client10from ..magen_gmail_client_api import config11class TestGmailClient(unittest.TestCase):12 def setUp(self):13 self.test_creds_dir = os.getcwd() + '/.credentials'14 self.env_mock_data = {15 config.GMAIL_CLIENT_ID: 'non-existing-client_id',16 config.GMAIL_CLIENT_SECRET: 'non-existing-client_secret'17 }18 def tearDown(self):19 if os.path.exists(self.test_creds_dir):20 for filename in os.listdir(self.test_creds_dir):21 os.remove(os.path.join(self.test_creds_dir, filename))22 os.rmdir(self.test_creds_dir)23 def test_get_credentials_json(self):24 """25 Test Generation of Credentials for Gmail API based on Environment26 - gmail_client.get_credentials_env() method27 """28 # Generated data with correct environments29 with mock.patch('os.environ', self.env_mock_data):30 credentials_data = gmail_client.get_credentials_env()31 self.assertIn(self.env_mock_data[config.GMAIL_CLIENT_ID], str(credentials_data))32 self.assertIn(self.env_mock_data[config.GMAIL_CLIENT_SECRET], str(credentials_data))33 # KeyError is generated if environments are not set34 with mock.patch('os.environ', {}):35 self.assertRaises(KeyError, gmail_client.get_credentials_env)36 def test_credentials_user_path(self):37 """ Test Generation of directory and correct path creation - gmail_client.credentials_user_path() method """38 with mock.patch('os.path.expanduser') as home_dir_mock:39 home_dir_mock.return_value = os.getcwd()+'/' # mocking home_dir to tests dir40 # Verify directory does not exists41 self.assertFalse(os.path.exists(self.test_creds_dir))42 # Creating directory43 created_path = gmail_client.credentials_user_path()44 # Verify directory got created45 self.assertTrue(os.path.exists(self.test_creds_dir))46 # Verify path is correctly constructed47 self.assertIn('json', created_path)48 # Calling function again returns the same result49 self.assertEqual(created_path, gmail_client.credentials_user_path())50 @mock.patch('os.path.expanduser')51 @mock.patch('oauth2client.tools.run_flow')52 @mock.patch('oauth2client.client.OAuth2WebServerFlow')53 @mock.patch('oauth2client.file.Storage')54 def test_gmail_credentials(self, store_mock, oauth_mock, run_flow_mock, os_home_dir_mock):55 """ Test Gmail oAuth Credentials generation - gmail_client.gmail_credentials() method """56 os_home_dir_mock.return_value = os.getcwd() + '/' # mocking home_dir to tests dir57 # mocking `credentials.invalid` of Gmail API58 store_mock.return_value = mock.Mock()59 store_mock.return_value.get.return_value.invalid = False60 result = gmail_client.gmail_credentials()61 self.assertFalse(result.invalid) # mock returned unchanged62 store_mock.assert_called_once()63 store_mock.return_value.get.return_value = None64 # Verify correctness of the workflow65 with mock.patch('os.environ', new=self.env_mock_data):66 got_credentials = gmail_client.get_credentials_env()67 gmail_client.gmail_credentials()68 oauth_mock.assert_called_once_with(scope=config.SCOPES, **got_credentials['installed'])69 run_flow_mock.assert_called_once_with(oauth_mock.return_value, store_mock.return_value)70 # KeyError should be thrown71 with mock.patch('os.environ', new={}):72 self.assertIsNone(gmail_client.gmail_credentials()) # on KeyError returns None73 @mock.patch('os.path.expanduser')74 def test_cleanup_cache(self, home_dir_mock):75 """ Test cleanup cache data - gmail_client.cleanup_cache() method """76 test_filenames = ['magen-gmail_test', 'test', 'test.json', 'gmail.test']77 home_dir_mock.return_value = os.getcwd() + '/' # mocking home_dir to tests dir78 # if folder does not exist - plain return79 gmail_client.cleanup_cache()80 creds_path = os.path.dirname(gmail_client.credentials_user_path()) # creating folder81 # add some tests file to the test directory82 for filename in test_filenames:83 with open(os.path.join(creds_path, filename), '+w') as new_file:84 new_file.write('test') # write some test data85 gmail_client.cleanup_cache()86 # Assertions87 home_dir_mock.assert_called()88 self.assertTrue(os.path.exists(creds_path))89 self.assertNotIn(config.GMAIL_FILES_PREFIX, os.listdir(creds_path))90 self.assertEqual(len(os.listdir(creds_path)), 3)91 def test_create_message(self):92 """ Test message create - gmail_client.create_messsage() method """93 # Prepare94 test_html_content = '''<p>Welcome! Thanks for signing up. Please follow this link to activate your account:</p>95 <p><a href="some_url">some_url</a></p>96 <br>97 <p>Cheers!</p>'''98 test_text_content = 'Please Confirm your email'99 test_sender = 'sender@test.test'100 test_to = 'to@test.test'101 test_subject = 'test'102 # creating email from plain text only103 result_msg = gmail_client.create_message(104 sender=test_sender,105 to=test_to,106 subject=test_subject,107 text_part=test_text_content108 )109 # format required by gmail API110 self.assertIn('raw', result_msg)111 msg_bytes = result_msg['raw'].encode()112 msg_string = base64.urlsafe_b64decode(msg_bytes).decode('utf-8')113 self.assertIn(test_sender, msg_string)114 self.assertIn(test_to, msg_string)115 self.assertIn(test_subject, msg_string)116 self.assertIn(test_text_content, msg_string)117 # creating email from plain text and html content118 result_msg = gmail_client.create_message(119 sender=test_sender,120 to=test_to,121 subject=test_subject,122 text_part=test_text_content,123 html_part=test_html_content124 )125 # format required by gmail API126 self.assertIn('raw', result_msg)127 msg_bytes = result_msg['raw'].encode()128 msg_string = base64.urlsafe_b64decode(msg_bytes).decode('utf-8')129 self.assertIn(test_sender, msg_string)130 self.assertIn(test_to, msg_string)131 self.assertIn(test_subject, msg_string)132 self.assertIn(test_text_content, msg_string)133 self.assertIn(test_html_content, msg_string)134 def test_send_message(self):135 """ Test sending of a message using gmail API """136 test_message = dict(id='test_id')137 gmail_service = mock.Mock()138 gmail_service.users.return_value.messages.return_value.send.return_value.execute.return_value = test_message139 result = gmail_client.send_message(gmail_service, 'here goes email object')140 self.assertIn('id', result)141if __name__ == '__main__':...

Full Screen

Full Screen

test_renderer.py

Source:test_renderer.py Github

copy

Full Screen

1from md2cf.confluence_renderer import ConfluenceTag, ConfluenceRenderer2def test_add_namespace():3 assert ConfluenceTag.add_namespace("tagname", "namespace") == "namespace:tagname"4def test_tag_append():5 tag = ConfluenceTag("irrelevant")6 other_tag = ConfluenceTag("alsoirrelevant")7 tag.append(other_tag)8 assert tag.children == [other_tag]9def test_tag_render():10 test_tag_type = "structured-macro"11 test_tag_markup = "<ac:structured-macro></ac:structured-macro>\n"12 tag = ConfluenceTag(test_tag_type)13 output = tag.render()14 assert output == test_tag_markup15def test_tag_render_with_text():16 test_tag_type = "structured-macro"17 test_text_content = "This is some text"18 test_tag_markup = "<ac:structured-macro>This is some text</ac:structured-macro>\n"19 tag = ConfluenceTag(test_tag_type, text=test_text_content)20 output = tag.render()21 assert output == test_tag_markup22def test_tag_render_with_cdata_text():23 test_tag_type = "structured-macro"24 test_text_content = "This is some text\nwith newlines"25 test_tag_markup = "<ac:structured-macro><![CDATA[This is some text\nwith newlines]]></ac:structured-macro>\n"26 tag = ConfluenceTag(test_tag_type, text=test_text_content, cdata=True)27 output = tag.render()28 assert output == test_tag_markup29def test_tag_render_with_attribute():30 test_tag_type = "structured-macro"31 test_tag_attrib = {"name": "code"}32 test_tag_markup = '<ac:structured-macro ac:name="code"></ac:structured-macro>\n'33 tag = ConfluenceTag(test_tag_type, attrib=test_tag_attrib)34 output = tag.render()35 assert output == test_tag_markup36def test_tag_render_with_multiple_attributes():37 test_tag_type = "structured-macro"38 test_tag_attrib = {"name": "code", "foo": "bar"}39 test_tag_markup = (40 '<ac:structured-macro ac:foo="bar" ac:name="code"></ac:structured-macro>\n'41 )42 tag = ConfluenceTag(test_tag_type, attrib=test_tag_attrib)43 output = tag.render()44 assert output == test_tag_markup45def test_tag_render_with_child():46 test_tag_type = "structured-macro"47 test_other_tag_type = "unstructured-macro"48 test_tag_markup = "<ac:structured-macro><ac:unstructured-macro></ac:unstructured-macro>\n</ac:structured-macro>\n"49 tag = ConfluenceTag(test_tag_type)50 child_tag = ConfluenceTag(test_other_tag_type)51 tag.children = [child_tag]52 output = tag.render()53 assert output == test_tag_markup54def test_tag_render_with_child_and_text():55 test_tag_type = "structured-macro"56 test_tag_text = "This is some text"57 test_other_tag_type = "unstructured-macro"58 test_tag_markup = "<ac:structured-macro><ac:unstructured-macro></ac:unstructured-macro>\nThis is some text</ac:structured-macro>\n"59 tag = ConfluenceTag(test_tag_type, text=test_tag_text)60 child_tag = ConfluenceTag(test_other_tag_type)61 tag.children = [child_tag]62 output = tag.render()63 assert output == test_tag_markup64def test_renderer_block_code():65 test_code = "this is a piece of code"66 test_markup = (67 '<ac:structured-macro ac:name="code"><ac:parameter ac:name="linenumbers">true</ac:parameter>\n'68 "<ac:plain-text-body><![CDATA[this is a piece of code]]></ac:plain-text-body>\n"69 "</ac:structured-macro>\n"70 )71 renderer = ConfluenceRenderer()72 assert renderer.block_code(test_code) == test_markup73def test_renderer_block_code_with_language():74 test_code = "this is a piece of code"75 test_language = "whitespace"76 test_markup = (77 '<ac:structured-macro ac:name="code"><ac:parameter ac:name="language">whitespace</ac:parameter>\n'78 '<ac:parameter ac:name="linenumbers">true</ac:parameter>\n'79 "<ac:plain-text-body><![CDATA[this is a piece of code]]></ac:plain-text-body>\n"80 "</ac:structured-macro>\n"81 )82 renderer = ConfluenceRenderer()83 assert renderer.block_code(test_code, lang=test_language) == test_markup84def test_renderer_header_sets_title():85 test_header = "this is a header"86 renderer = ConfluenceRenderer()87 renderer.header(test_header, 1)88 assert renderer.title == test_header89def test_renderer_header_lower_level_does_not_set_title():90 test_header = "this is a header"91 renderer = ConfluenceRenderer()92 renderer.header(test_header, 2)93 assert renderer.title is None94def test_renderer_header_later_level_sets_title():95 test_lower_header = "this is a lower header"96 test_header = "this is a header"97 renderer = ConfluenceRenderer()98 renderer.header(test_lower_header, 2)99 renderer.header(test_header, 1)100 assert renderer.title is test_header101def test_renderer_header_only_sets_first_title():102 test_header = "this is a header"103 test_second_header = "this is another header"104 renderer = ConfluenceRenderer()105 renderer.header(test_header, 1)106 renderer.header(test_second_header, 1)107 assert renderer.title is test_header108def test_renderer_image_external():109 test_image_src = "http://example.com/image.jpg"110 test_image_markup = (111 '<ac:image ac:alt=""><ri:url ri:value="{}"></ri:url>\n'112 "</ac:image>\n".format(test_image_src)113 )114 renderer = ConfluenceRenderer()115 assert renderer.image(test_image_src, "", "") == test_image_markup116 assert not renderer.attachments117def test_renderer_image_external_alt_and_title():118 test_image_src = "http://example.com/image.jpg"119 test_image_alt = "alt text"120 test_image_title = "title"121 test_image_markup = (122 '<ac:image ac:alt="{}" ac:title="{}"><ri:url ri:value="{}"></ri:url>\n'123 "</ac:image>\n".format(test_image_alt, test_image_title, test_image_src)124 )125 renderer = ConfluenceRenderer()126 assert (127 renderer.image(test_image_src, test_image_title, test_image_alt)128 == test_image_markup129 )130def test_renderer_image_internal_absolute():131 test_image_file = "image.jpg"132 test_image_src = "/home/test/images/" + test_image_file133 test_image_markup = (134 '<ac:image ac:alt=""><ri:attachment ri:filename="{}"></ri:attachment>\n'135 "</ac:image>\n".format(test_image_file)136 )137 renderer = ConfluenceRenderer()138 assert renderer.image(test_image_src, "", "") == test_image_markup139 assert renderer.attachments == [test_image_src]140def test_renderer_image_internal_relative():141 test_image_file = "image.jpg"142 test_image_src = "test/images/" + test_image_file143 test_image_markup = (144 '<ac:image ac:alt=""><ri:attachment ri:filename="{}"></ri:attachment>\n'145 "</ac:image>\n".format(test_image_file)146 )147 renderer = ConfluenceRenderer()148 assert renderer.image(test_image_src, "", "") == test_image_markup...

Full Screen

Full Screen

test_backups.py

Source:test_backups.py Github

copy

Full Screen

1import os2import sys3from pathlib import Path4from .testing_utility_functions import BACKUP_DEST_DIR, FAKE_HOME_DIR, DOTFILES, setup_dirs_and_env_vars_and_create_config, clean_up_dirs_and_env_vars5sys.path.insert(0, "../shallow_backup")6from shallow_backup.backup import backup_dotfiles7from shallow_backup.utils import safe_mkdir8from shallow_backup.config import get_config, write_config9TEST_TEXT_CONTENT = 'THIS IS TEST CONTENT FOR THE DOTFILES'10class TestBackupMethods:11 """Test the backup methods."""12 @staticmethod13 def setup_method():14 setup_dirs_and_env_vars_and_create_config()15 # Create all dotfiles and dotfolders16 for file in DOTFILES:17 if not file.endswith("/"):18 print(f"Creating file: {file}")19 os.makedirs(Path(file).parent, exist_ok=True)20 with open(file, "w+") as f:21 f.write(TEST_TEXT_CONTENT)22 else:23 directory = file24 print(f"Creating dir: {directory}")25 safe_mkdir(directory)26 for file_2 in ["test1", "test2"]:27 with open(os.path.join(directory, file_2), "w+") as f:28 f.write(TEST_TEXT_CONTENT)29 @staticmethod30 def teardown_method():31 clean_up_dirs_and_env_vars()32 def test_backup_dotfiles(self):33 """Test backing up dotfiles and dotfolders."""34 backup_dest_path = os.path.join(BACKUP_DEST_DIR, "dotfiles")35 backup_dotfiles(backup_dest_path, dry_run=False, home_path=FAKE_HOME_DIR, skip=True)36 assert os.path.isdir(backup_dest_path)37 for path in DOTFILES:38 print(f"\nBACKUP DESTINATION DIRECTORY: ({backup_dest_path}) CONTENTS:", os.listdir(backup_dest_path), "")39 print(path + " should already be backed up.")40 backed_up_dot = os.path.join(backup_dest_path, path.replace(FAKE_HOME_DIR + "/", ""))41 print(f"Backed up dot: {backed_up_dot}\n")42 assert os.path.isfile(backed_up_dot) or os.path.isdir(backed_up_dot)43 def test_conditions(self):44 """Test backing up files based on conditions"""45 # Set false backup condition of all files.46 config = get_config()47 print(config["dotfiles"])48 for dot, _ in config["dotfiles"].items():49 config["dotfiles"][dot]["backup_condition"] = "[[ $(uname -s) == 'Made Up OS' ]]"50 write_config(config)51 backup_dest_path = os.path.join(BACKUP_DEST_DIR, "dotfiles")52 backup_dotfiles(backup_dest_path, dry_run=False, home_path=FAKE_HOME_DIR, skip=True)53 assert os.path.isdir(backup_dest_path)54 for path in DOTFILES:55 print(f"\nBACKUP DESTINATION DIRECTORY: ({backup_dest_path}) CONTENTS:", os.listdir(backup_dest_path), "")56 print(path + " should not be backed up.")57 backed_up_dot = os.path.join(backup_dest_path, path.replace(FAKE_HOME_DIR + "/", ""))...

Full Screen

Full Screen

Playwright tutorial

LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.

Chapters:

  1. What is Playwright : Playwright is comparatively new but has gained good popularity. Get to know some history of the Playwright with some interesting facts connected with it.
  2. How To Install Playwright : Learn in detail about what basic configuration and dependencies are required for installing Playwright and run a test. Get a step-by-step direction for installing the Playwright automation framework.
  3. Playwright Futuristic Features: Launched in 2020, Playwright gained huge popularity quickly because of some obliging features such as Playwright Test Generator and Inspector, Playwright Reporter, Playwright auto-waiting mechanism and etc. Read up on those features to master Playwright testing.
  4. What is Component Testing: Component testing in Playwright is a unique feature that allows a tester to test a single component of a web application without integrating them with other elements. Learn how to perform Component testing on the Playwright automation framework.
  5. Inputs And Buttons In Playwright: Every website has Input boxes and buttons; learn about testing inputs and buttons with different scenarios and examples.
  6. Functions and Selectors in Playwright: Learn how to launch the Chromium browser with Playwright. Also, gain a better understanding of some important functions like “BrowserContext,” which allows you to run multiple browser sessions, and “newPage” which interacts with a page.
  7. Handling Alerts and Dropdowns in Playwright : Playwright interact with different types of alerts and pop-ups, such as simple, confirmation, and prompt, and different types of dropdowns, such as single selector and multi-selector get your hands-on with handling alerts and dropdown in Playright testing.
  8. Playwright vs Puppeteer: Get to know about the difference between two testing frameworks and how they are different than one another, which browsers they support, and what features they provide.
  9. Run Playwright Tests on LambdaTest: Playwright testing with LambdaTest leverages test performance to the utmost. You can run multiple Playwright tests in Parallel with the LammbdaTest test cloud. Get a step-by-step guide to run your Playwright test on the LambdaTest platform.
  10. Playwright Python Tutorial: Playwright automation framework support all major languages such as Python, JavaScript, TypeScript, .NET and etc. However, there are various advantages to Python end-to-end testing with Playwright because of its versatile utility. Get the hang of Playwright python testing with this chapter.
  11. Playwright End To End Testing Tutorial: Get your hands on with Playwright end-to-end testing and learn to use some exciting features such as TraceViewer, Debugging, Networking, Component testing, Visual testing, and many more.
  12. Playwright Video Tutorial: Watch the video tutorials on Playwright testing from experts and get a consecutive in-depth explanation of Playwright automation testing.

Run Playwright Python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful