Best Python code snippet using pytractor_python
test_wordFrequencyHandler.py
Source:test_wordFrequencyHandler.py
1from environment.EnvironmentConstants import EnvironmentVariables as ev2ev()3import os4import pytest5from extractor.WordFrequencyHandler import WordFrequencyHandler, __WordFrequency__, __WordFrequenctEncoder__6import json7xfail = pytest.mark.xfail8class Test:9 10 def test_do_word_count_for_article_will_process_correct_amount_of_articles(self):11 # Setup12 handler = WordFrequencyHandler()13 test_title = 'title_key'14 test_content = 'This is awesome content from an article'15 test_extracted_list = ['FromHere', 'ViaThis', 'ToThere']16 # Confirm clear17 assert len(handler.word_frequencies_ready_for_sending) == 018 assert len(handler.tf[test_title]) == 019 handler.do_word_count_for_article(test_title, test_content, test_extracted_list)20 assert len(handler.word_frequencies_ready_for_sending) == 121 handler.do_word_count_for_article(test_title, test_content, test_extracted_list)22 handler.do_word_count_for_article(test_title, test_content, test_extracted_list)23 handler.do_word_count_for_article(test_title, test_content, test_extracted_list)24 handler.do_word_count_for_article(test_title, test_content, test_extracted_list)25 assert len(handler.word_frequencies_ready_for_sending) == 526#27 def test___concatenate_extracted_from__empty_list(self):28 # Setup29 test_extracted_list = []30 handler = WordFrequencyHandler()31 expected = ''32 actual = handler.__concatenate_extracted_from__(test_extracted_list)33 assert actual == expected34 def test___concatenate_extracted_from__can_contain_single_entry(self):35 # Setup36 test_extracted_list = ['FromHere']37 handler = WordFrequencyHandler()38 expected = 'FromHere'39 actual = handler.__concatenate_extracted_from__(test_extracted_list)40 assert actual == expected41 def test___concatenate_extracted_from__can_contain_multiple_entries(self):42 # Setup43 test_extracted_list = ['FromHere', 'ViaThis', 'ToThere']44 handler = WordFrequencyHandler()45 expected = 'FromHere,ViaThis,ToThere'46 actual = handler.__concatenate_extracted_from__(test_extracted_list)47 assert actual == expected48#49 def test___convert_to_word_frequency_JSON_object___produces_json_object(self):50 # Setup51 handler = WordFrequencyHandler()52 test_title = 'title_key'53 test_content = 'This is awesome content'54 test_extract_file = 'Extracted from here'55 distinct_word_count = len(set(test_content.lower().split(' ')))56 handler.tf.process(test_title, test_content)57 handler.currentKey = test_title58 assert len(handler.word_frequencies_ready_for_sending) == 059 handler.__convert_to_word_frequency_JSON_object__(test_title, test_extract_file)60 assert len(handler.word_frequencies_ready_for_sending) == 161 json_string = handler.word_frequencies_ready_for_sending[0]62 63 json_object = json.loads(json_string)64 assert json_object['articletitle'] == test_title65 assert json_object['totalwordsinarticle'] == distinct_word_count66#67 def test___reset__no_hard_reset_should_reset_wordcount_but_not_ready_for_sending(self):68 # Setup69 handler = WordFrequencyHandler()70 test_title = 'title_key'71 test_content = 'This is awesome content'72 distinct_word_count = len(set(test_content.lower().split(' ')))73 handler.word_frequencies_ready_for_sending.append(test_title)74 # Confirm empty75 assert len(handler.tf[test_title]) == 076 handler.tf.process(test_title, test_content)77 assert len(handler.tf[test_title]) == distinct_word_count78 assert len(handler.word_frequencies_ready_for_sending) > 079 handler.__reset__()80 # Confirm empty81 assert len(handler.tf[test_title]) == 082 assert len(handler.word_frequencies_ready_for_sending) > 083 def test___reset__hard_reset_sets_length_to_zero(self):84 handler = WordFrequencyHandler()85 test_title = 'title_key'86 test_content = 'This is awesome content'87 distinct_word_count = len(set(test_content.lower().split(' ')))88 handler.word_frequencies_ready_for_sending.append(test_title)89 # Confirm empty90 assert len(handler.tf[test_title]) == 091 handler.tf.process(test_title, test_content)92 assert len(handler.tf[test_title]) == distinct_word_count93 assert len(handler.word_frequencies_ready_for_sending) > 094 handler.__reset__(True)95 # Confirm empty96 assert len(handler.tf[test_title]) == 097 assert len(handler.word_frequencies_ready_for_sending) == 098#99 def test___create_file_back_up__can_craete_backup_file_with_content(self):100 # Setup101 error_dir = ev.instance.get_value(ev.instance.ERROR_DIRECTORY, './')102 test_file = 'thisIsSpecificForTest__create_file_back_up__.json'103 handler = WordFrequencyHandler()104 handler.__create_file_back_up__(test_file, error_dir)105 abs_path = os.path.abspath(error_dir + handler.back_up_file_prefix + test_file)106 assert os.path.exists(abs_path)107 with open(abs_path, 'r', encoding='utf-8') as file:108 content = file.readlines()109 assert '{\n' in content110 assert '\t\"back_up\": [\n' in content111 assert '\t]\n' in content112 assert '}' in content113 # Clean up...
test_reco.py
Source:test_reco.py
1from re import S2from params import *3import unittest4import os5from dotenv import dotenv_values6from shutil import rmtree7from main import *8temp = dotenv_values(".env")9class TestValidateEnv(unittest.TestCase):10 def test_aa_normal(self):11 code, name, distance = main_reco(TEST_IMGS, RECO_ID, test_title="test_normal")12 self.assertEqual(code, 200)13 self.assertEqual(name, NAME)14 self.assertIsInstance(distance, float)15 def test_bb_hard_to_verify(self):16 code, name, distance = main_reco(HARD_TO_VERIFY, RECO_ID, test_title="test_hard_to_verify", skip_verify=True)17 self.assertEqual(code, 134)18 self.assertEqual(name, NAME)19 self.assertIsInstance(distance, float)20 def test_cc_not_in_db(self):21 code, name, distance = main_reco(IMG_NOT_IN_DB, RECO_ID, test_title="test_not_in_db")22 self.assertTrue(code == 100 or code == 442 or code == 134)23 def test_dd_no_db_search(self):24 code, name, distance = main_reco(IMG_NOT_IN_DB, RECO_ID, test_title="test_no_db_search", skip_db_search=True)25 self.assertEqual(code, 500)26 self.assertTrue(name is None or not name)27 self.assertTrue(distance is None or not distance)28 def test_ee_both_true(self):29 code, name, distance = main_reco(IMG_NOT_IN_DB, RECO_ID, test_title="test_no_db_search", skip_db_search=True, skip_verify=True)30 self.assertEqual(code, 143)31 self.assertIsNone(name)32 self.assertIsNone(distance)33 34 def test_ff_bad_id(self):35 code, name, distance = main_reco(TEST_IMGS, RECO_ID + "24214", test_title="test_bad_id")36 self.assertEqual(code, 400)37 self.assertIsNone(name)38 self.assertIsNone(distance)39 40 def test_dd_empty_list(self):41 code, name, distance = main_reco([], RECO_ID, test_title="test_empty_list")42 self.assertEqual(code, 111)43 self.assertIsNone(name)44 self.assertIsNone(distance)45 def test_dd_non_existent_file(self):46 code, name, distance = main_reco(DB_NON_EXISTENT_MIX, RECO_ID, test_title="test_non_existent_file")47 self.assertEqual(code, 159)48 self.assertIsNone(name)49 self.assertIsNone(distance)50 def test_dd_no_face(self):51 code, name, distance = main_reco(IMG_NO_FACE, RECO_ID, test_title="test_no_face")52 self.assertEqual(code, 630)53 self.assertIsNone(name)54 self.assertIsNone(distance)55 def test_dd_face_spoof(self):56 code, name, distance = main_reco(IMGS_SPOOF, RECO_ID, test_title="test_face_spoof")57 self.assertEqual(code, 560)58 self.assertIsNone(name)59 self.assertIsNone(distance)60 def test_dd_face_multi(self):61 code, name, distance = main_reco(MULTI_IMG_TEST, RECO_ID, test_title="test_face_multi")62 self.assertTrue(code == 200 or code == 134)63 self.assertEqual(name, NAME)64 self.assertIsInstance(distance, float)65if __name__ == "__main__":66 unittest.main()...
question_to_test.py
Source:question_to_test.py
1def question_title_to_test_info(question_title):2 letters = question_title.lower()3 test_title = test_title_from_question_title(letters)4 question_number = question_number_from_question_title(letters)5 return test_title, question_number6def test_title_from_question_title(letters):7 test_title = ''8 if letters[3] == 'm':9 test_title += "midterm"10 else:11 test_title += "final"12 test_title += '_'13 if letters[0] == 'w':14 test_title += "winter"15 else:16 test_title += "fall"17 test_title += '_'18 test_title += "20" + letters[1] + letters[2]19 return test_title20def question_number_from_question_title(letters):21 question_number = ''22 if letters[4] == '0':23 question_number = letters[5]24 else:25 question_number = letters[4] + letters[5]...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!