Best Python code snippet using pytest-play_python
icare - Copy.py
Source:icare - Copy.py
...78 ## Return extract directory79 return extract_dir808182def get_file_contents(text_file):83 """84 Read csv85 """86 ##f = open(text_file, 'rt', encoding='utf-8-sig')87 f = codecs.open(text_file, 'r')88 try:89 reader = csv.DictReader(f, delimiter='|', quoting=csv.QUOTE_NONE)90 data = []91 for row in reader:92 data.append(row)9394 f.close()95 return data9697 except Exception as e:98 sys.stderr.write('[FILE_CONTENT] Error: %s \r\n' % e)99100101def get_connection():102 """103 Connection to MongoDB104 """105 conn = Connection(host=MONGO_HOST, port=MONGO_PORT)106 db = conn[MONGO_DB]107 db.authenticate(MONGO_USER, MONGO_PASSWORD)108 sys.stderr.write('[MONGO] Connected \r\n')109 return db110111112def main():113114 try:115 db = get_connection()116 117 zip_count = len(glob.glob1(ZIP_DIR, '*.zip'))118119 if zip_count:120 sys.stderr.write('Starting...\r\n')121 #Get file list122 zip_files = get_zip_file_list()123 #print(ZIP_DIR)124 for f in zip_files:125126 extracted_dir = do_extract(f)127128 try:129 #count file130 file_count = len(glob.glob1(extracted_dir, '*.txt'))131132 if file_count == 43:133 ## get files list134 text_files = glob.glob(os.path.join(extracted_dir, '*.txt'))135 for text_file in text_files:136 ##file_content = get_file_contents(text_file)137 text_file_name = os.path.basename(text_file).upper()138139 if text_file_name == 'PERSON.TXT':140 from icare.person import Person141142 data = get_file_contents(text_file)143 col = Person(db, data)144 col.do_import()145146 if text_file_name == 'ADDRESS.TXT':147 from icare.address import Address148149 data = get_file_contents(text_file)150 col = Address(db, data)151 col.do_import()152153 if text_file_name == 'DEATH.TXT':154 from icare.death import Death155156 data = get_file_contents(text_file)157 col = Death(db, data)158 col.do_import()159160 if text_file_name == 'CARD.TXT':161 from icare.card import Card162163 data = get_file_contents(text_file)164 col = Card(db, data)165 col.do_import()166167 if text_file_name == 'DRUGALLERGY.TXT':168 from icare.drugallergy import Drugallergy169170 data = get_file_contents(text_file)171 col = Drugallergy(db, data)172 col.do_import()173174 if text_file_name == 'HOME.TXT':175 from icare.home import Home176177 data = get_file_contents(text_file)178 col = Home(db, data)179 col.do_import()180181 if text_file_name == 'SERVICE.TXT':182 from icare.service import Service183184 data = get_file_contents(text_file)185 col = Service(db, data)186 col.do_import()187188 if text_file_name == 'APPOINTMENT.TXT':189 from icare.appointment import Appointment190191 data = get_file_contents(text_file)192 col = Appointment(db, data)193 col.do_import()194195 if text_file_name == 'ACCIDENT.TXT':196 from icare.accident import Accident197198 data = get_file_contents(text_file)199 col = Accident(db, data)200 col.do_import()201202 if text_file_name == 'DIAGNOSIS_OPD.TXT':203 from icare.diagnosis_opd import Diagnosis_opd204205 data = get_file_contents(text_file)206 col = Diagnosis_opd(db, data)207 col.do_import()208209 if text_file_name == 'PROCEDURE_OPD.TXT':210 from icare.procedure_opd import Procedure_opd211212 data = get_file_contents(text_file)213 col = Procedure_opd(db, data)214 col.do_import()215216 if text_file_name == 'DRUG_OPD.TXT':217 from icare.drug_opd import Drug_opd218219 data = get_file_contents(text_file)220 col = Drug_opd(db, data)221 col.do_import()222223 if text_file_name == 'CHARGE_OPD.TXT':224 from icare.charge_opd import Charge_opd225226 data = get_file_contents(text_file)227 col = Charge_opd(db, data)228 col.do_import()229230 if text_file_name == 'ADMISSION.TXT':231 from icare.admission import Admission232233 data = get_file_contents(text_file)234 col = Admission(db, data)235 col.do_import()236237 if text_file_name == 'DIAGNOSIS_IPD.TXT':238 from icare.diagnosis_ipd import Diagnosis_ipd239240 data = get_file_contents(text_file)241 col = Diagnosis_ipd(db, data)242 col.do_import()243244 if text_file_name == 'PROCEDURE_IPD.TXT':245 from icare.procedure_ipd import Procedure_ipd246247 data = get_file_contents(text_file)248 col = Procedure_ipd(db, data)249 col.do_import()250251 if text_file_name == 'DRUG_IPD.TXT':252 from icare.drug_ipd import Drug_ipd253254 data = get_file_contents(text_file)255 col = Drug_ipd(db, data)256 col.do_import()257258 if text_file_name == 'CHARGE_IPD.TXT':259 from icare.charge_ipd import Charge_ipd260261 data = get_file_contents(text_file)262 col = Charge_ipd(db, data)263 col.do_import()264265 if text_file_name == 'SURVEILLANCE.TXT':266 from icare.surveillance import Surveillance267268 data = get_file_contents(text_file)269 col = Surveillance(db, data)270 col.do_import()271272 if text_file_name == 'WOMEN.TXT':273 from icare.women import Women274275 data = get_file_contents(text_file)276 col = Women(db, data)277 col.do_import()278279 if text_file_name == 'FP.TXT':280 from icare.fp import Fp281282 data = get_file_contents(text_file)283 col = Fp(db, data)284 col.do_import()285286 if text_file_name == 'EPI.TXT':287 from icare.epi import Epi288289 data = get_file_contents(text_file)290 col = Epi(db, data)291 col.do_import()292293 if text_file_name == 'NUTRITION.TXT':294 from icare.nutrition import Nutrition295296 data = get_file_contents(text_file)297 col = Nutrition(db, data)298 col.do_import()299300 if text_file_name == 'PRENATAL.TXT':301 from icare.prenatal import Prenatal302303 data = get_file_contents(text_file)304 col = Prenatal(db, data)305 col.do_import()306307 if text_file_name == 'ANC.TXT':308 from icare.anc import Anc309310 data = get_file_contents(text_file)311 col = Anc(db, data)312 col.do_import()313314 if text_file_name == 'LABOR.TXT':315 from icare.labor import Labor316317 data = get_file_contents(text_file)318 col = Labor(db, data)319 col.do_import()320321 if text_file_name == 'POSTNATAL.TXT':322 from icare.postnatal import Postnatal323324 data = get_file_contents(text_file)325 col = Postnatal(db, data)326 col.do_import()327328 if text_file_name == 'NEWBORN.TXT':329 from icare.newborn import NewBorn330331 data = get_file_contents(text_file)332 col = NewBorn(db, data)333 col.do_import()334335 if text_file_name == 'NEWBORNCARE.TXT':336 from icare.newborncare import NewBornCare337338 data = get_file_contents(text_file)339 col = NewBornCare(db, data)340 col.do_import()341342 if text_file_name == 'DENTAL.TXT':343 from icare.dental import Dental344345 data = get_file_contents(text_file)346 col = Dental(db, data)347 col.do_import()348349 if text_file_name == 'SPECIALPP.TXT':350 from icare.specialpp import SpecialPP351352 data = get_file_contents(text_file)353 col = SpecialPP(db, data)354 col.do_import()355356 if text_file_name == 'NCDSCREEN.TXT':357 from icare.ncdscreen import NcdScreen358359 data = get_file_contents(text_file)360 col = NcdScreen(db, data)361 col.do_import()362363 if text_file_name == 'CHRONIC.TXT':364 from icare.chronic import Chronic365366 data = get_file_contents(text_file)367 col = Chronic(db, data)368 col.do_import()369370 if text_file_name == 'CHRONICFU.TXT':371 from icare.chronicfu import ChronicFu372373 data = get_file_contents(text_file)374 col = ChronicFu(db, data)375 col.do_import()376377 if text_file_name == 'LABFU.TXT':378 from icare.labfu import LabFu379380 data = get_file_contents(text_file)381 col = LabFu(db, data)382 col.do_import()383384 if text_file_name == 'COMMUNITY_SERVICE.TXT':385 from icare.community_service import CommunityService386387 data = get_file_contents(text_file)388 col = CommunityService(db, data)389 col.do_import()390391 if text_file_name == 'DISABILITY.TXT':392 from icare.disability import Disability393394 data = get_file_contents(text_file)395 col = Disability(db, data)396 col.do_import()397398 if text_file_name == 'ICF.TXT':399 from icare.icf import Icf400401 data = get_file_contents(text_file)402 col = Icf(db, data)403 col.do_import()404405 if text_file_name == 'REHABILITATION.TXT':406 from icare.rehabilitation import Rehabilitation407408 data = get_file_contents(text_file)409 col = Rehabilitation(db, data)410 col.do_import()411412 if text_file_name == 'VILLAGE.TXT':413 from icare.village import Village414415 data = get_file_contents(text_file)416 col = Village(db, data)417 col.do_import()418419 else:420 sys.stderr.write('[ERROR] File total invalid \r\n')421 #Remove extracted directory422 shutil.rmtree(extracted_dir)423424 except:425 shutil.rmtree(extracted_dir)426 sys.stderr.write('[ERROR]: file %s \r\n' % f)427 continue428429 print("End.\r\n")
...
test_puppetfile.py
Source:test_puppetfile.py
...20 github_repository = MagicMock()21 Puppetfile.from_github_repository(github_repository, 'env')22 github_repository.get_file_contents.assert_called_once_with(23 '/Puppetfile', ref='env')24 content = github_repository.get_file_contents()25 content.decoded_content.decode.assert_called_once_with('utf-8')26 @staticmethod27 def test_whith_no_puppetfile_in_github_repository():28 github_repository = MagicMock()29 github_repository.get_file_contents.side_effect = GithubException(30 404, 'file not found')31 with pytest.raises(PuppetfileNotFoundException):32 Puppetfile.from_github_repository(github_repository, 'env')33 @staticmethod34 def test_it_set_file_sha():35 github_repository = MagicMock()36 github_repository.get_file_contents().sha = 'shasha'37 puppetfile = Puppetfile.from_github_repository(github_repository,38 'env')39 assert puppetfile.sha == 'shasha'40 @staticmethod41 def test_it_returns_a_puppetfile_with_both_git_and_forge_modules():42 github_repository = MagicMock()43 content = github_repository.get_file_contents()44 content.decoded_content.decode.return_value = (45 "mod 'apache',\n"46 " :git => 'https://url/git/apache',\n"47 " :ref => 'ed19f'\n"48 "mod 'custommod',\n"49 " :git => 'https://url/git/custommod'\n"50 "mod 'puppetlabs/apache', '0.1.10'\n"51 "mod 'puppetlabs/vcsrepo', '0.2.10'"52 )53 forge_module_apache = ForgeModule('puppetlabs/apache', '0.1.10')54 forge_module_vcsrepo = ForgeModule('puppetlabs/vcsrepo', '0.2.10')55 git_module_custommod = GitModule('custommod',56 'https://url/git/custommod')57 puppetfile = Puppetfile.from_github_repository(github_repository,58 'env')59 assert forge_module_apache in puppetfile.forge_modules60 assert forge_module_vcsrepo in puppetfile.forge_modules61 assert GIT_MODULE_APACHE in puppetfile.git_modules62 assert git_module_custommod in puppetfile.git_modules63 @staticmethod64 def test_it_parse_modules_with_both_simple_and_double_quotes():65 github_repository = MagicMock()66 content = github_repository.get_file_contents()67 content.decoded_content.decode.return_value = (68 "mod \"custommod\",\n"69 " :git => \"https://url/git/custommod\"\n"70 "mod 'puppetlabs/apache', '0.1.10'\n"71 "mod \"puppetlabs/vcsrepo\", \"0.2.10\""72 )73 forge_module_apache = ForgeModule('puppetlabs/apache', '0.1.10')74 forge_module_vcsrepo = ForgeModule('puppetlabs/vcsrepo', '0.2.10')75 git_module_custommod = GitModule('custommod',76 'https://url/git/custommod')77 puppetfile = Puppetfile.from_github_repository(github_repository,78 'env')79 assert forge_module_apache in puppetfile.forge_modules80 assert forge_module_vcsrepo in puppetfile.forge_modules81 assert git_module_custommod in puppetfile.git_modules82class TestPuppetfileAddGitModule:83 @staticmethod84 def test_it_add_a_git_module_to_the_puppetfile():85 github_repository = MagicMock()86 content = github_repository.get_file_contents()87 content.decoded_content.decode.return_value = ('')88 puppetfile = Puppetfile(github_repository, 'env', sha='shasha',89 git_modules=[GIT_MODULE_NGINX])90 assert puppetfile.git_modules == [GIT_MODULE_NGINX]91 puppetfile.add_git_module('apache',92 'https://url/git/apache',93 reference_type='ref',94 reference='ed19f')95 assert GIT_MODULE_APACHE in puppetfile.git_modules96 github_repository.update_file.assert_called_once_with(97 "Puppetfile",98 "Puppetfile - Add git module apache",99 (f'{str(GIT_MODULE_NGINX)}'100 "mod 'apache',\n"101 " :git => 'https://url/git/apache',\n"102 " :ref => 'ed19f'\n"),103 "shasha")104 @staticmethod105 def test_it_cannot_add_an_existing_git_module():106 github_repository = MagicMock()107 content = github_repository.get_file_contents()108 content.decoded_content.decode.return_value = ('')109 puppetfile = Puppetfile(github_repository,110 'env',111 sha='shasha',112 git_modules=[GIT_MODULE_APACHE])113 with pytest.raises(ModuleAlreadyPresentException):114 puppetfile.add_git_module('apache', 'https://url/git/apache')115class TestPuppetfileAddForgeModule:116 @staticmethod117 def test_it_add_a_module_to_the_puppetfile():118 github_repository = MagicMock()119 content = github_repository.get_file_contents()120 content.decoded_content.decode.return_value = ('')121 puppetfile = Puppetfile(github_repository, 'env', sha='shasha',122 forge_modules=[FORGE_MODULE_NGINX])123 assert puppetfile.forge_modules == [FORGE_MODULE_NGINX]124 puppetfile.add_forge_module('puppetlabs/apache', '0.1.10')125 forge_module_apache = ForgeModule('puppetlabs/apache', '0.1.10')126 assert forge_module_apache in puppetfile.forge_modules127 github_repository.update_file.assert_called_once_with(128 "Puppetfile",129 "Puppetfile - Add forge module puppetlabs/apache",130 f"{str(FORGE_MODULE_NGINX)}mod 'puppetlabs/apache', '0.1.10'\n",131 "shasha")132 @staticmethod133 def test_it_cannot_add_an_existing_module():134 github_repository = MagicMock()135 content = github_repository.get_file_contents()136 content.decoded_content.decode.return_value = ('')137 forge_module_apache = ForgeModule('puppetlabs/apache')138 puppetfile = Puppetfile(github_repository,139 'env',140 sha='shasha',141 forge_modules=[forge_module_apache])142 with pytest.raises(ModuleAlreadyPresentException):143 puppetfile.add_forge_module('puppetlabs/apache')144class TestPuppetfileRemoveForgeModule:145 @staticmethod146 def test_it_remove_a_forge_module_from_the_puppetfile():147 github_repository = MagicMock()148 content = github_repository.get_file_contents()149 content.decoded_content.decode.return_value = ('')150 forge_module_apache = ForgeModule('puppetlabs/apache', '0.1.10')151 puppetfile = Puppetfile(github_repository,152 'env',153 sha='shasha',154 forge_modules=[forge_module_apache])155 assert forge_module_apache in puppetfile.forge_modules156 puppetfile.remove_forge_module('puppetlabs/apache')157 assert puppetfile.forge_modules == []158 github_repository.update_file.assert_called_once_with(159 "Puppetfile",160 "Puppetfile - Remove forge module puppetlabs/apache",161 "",162 "shasha")163 @staticmethod164 def test_it_cannot_remove_a_forge_module_not_present_in_the_puppetfile():165 github_repository = MagicMock()166 content = github_repository.get_file_contents()167 content.decoded_content.decode.return_value = ('')168 puppetfile = Puppetfile(github_repository, 'env', sha='shasha')169 assert puppetfile.forge_modules == []170 with pytest.raises(ModuleNotFoundException):171 puppetfile.remove_forge_module('puppetlabs/apache')172class TestPuppetfileRemoveGitModule:173 @staticmethod174 def test_it_remove_a_git_module_from_the_puppetfile():175 github_repository = MagicMock()176 content = github_repository.get_file_contents()177 content.decoded_content.decode.return_value = ('')178 puppetfile = Puppetfile(github_repository,179 'env',180 sha='shasha',181 git_modules=[GIT_MODULE_APACHE])182 assert GIT_MODULE_APACHE in puppetfile.git_modules183 puppetfile.remove_git_module('apache')184 assert puppetfile.git_modules == []185 github_repository.update_file.assert_called_once_with(186 "Puppetfile",187 "Puppetfile - Remove git module apache",188 "",189 "shasha")190 @staticmethod191 def test_it_cannot_remove_a_git_module_not_present_in_the_puppetfile():192 github_repository = MagicMock()193 content = github_repository.get_file_contents()194 content.decoded_content.decode.return_value = ('')195 puppetfile = Puppetfile(github_repository, 'env', sha='shasha')196 with pytest.raises(ModuleNotFoundException):197 puppetfile.remove_git_module('apache')198class TestPuppetfileUpdateGitModule:199 @staticmethod200 def test_it_update_a_git_module_in_the_puppetfile():201 github_repository = MagicMock()202 content = github_repository.get_file_contents()203 content.decoded_content.decode.return_value = ('')204 puppetfile = Puppetfile(github_repository,205 'env',206 sha='shasha',207 git_modules=[deepcopy(GIT_MODULE_APACHE)])208 assert puppetfile.git_modules[0].reference == 'ed19f'209 puppetfile.update_git_module('apache', 'a76f6fb')210 assert puppetfile.git_modules[0].reference == 'a76f6fb'211 github_repository.update_file.assert_called_once_with(212 "Puppetfile",213 "Puppetfile - Update git module apache from ed19f to a76f6fb",214 ("mod 'apache',\n"215 " :git => 'https://url/git/apache',\n"216 " :ref => 'a76f6fb'\n"),217 "shasha")218 @staticmethod219 def test_it_update_a_git_module_reference_and_reference_type():220 github_repository = MagicMock()221 content = github_repository.get_file_contents()222 content.decoded_content.decode.return_value = ('')223 puppetfile = Puppetfile(github_repository,224 'env',225 sha='shasha',226 git_modules=[deepcopy(GIT_MODULE_APACHE)])227 assert puppetfile.git_modules[0].reference == 'ed19f'228 puppetfile.update_git_module('apache',229 'master',230 reference_type='branch')231 assert puppetfile.git_modules[0].reference == 'master'232 assert puppetfile.git_modules[0].reference_type == 'branch'233 @staticmethod234 def test_it_cannot_update_a_missing_git_module():235 github_repository = MagicMock()236 content = github_repository.get_file_contents()237 content.decoded_content.decode.return_value = ('')238 puppetfile = Puppetfile(github_repository, 'env', sha='shasha')239 assert puppetfile.git_modules == []240 with pytest.raises(ModuleNotFoundException):241 puppetfile.update_git_module('apache', 'updatebranch')242class TestPuppetfileUpdateForgeModule:243 @staticmethod244 def test_it_update_a_module_in_the_puppetfile():245 github_repository = MagicMock()246 content = github_repository.get_file_contents()247 content.decoded_content.decode.return_value = ('')248 forge_module_apache = ForgeModule('puppetlabs/apache', '0.1.1')249 puppetfile = Puppetfile(github_repository,250 'env',251 sha='shasha',252 forge_modules=[forge_module_apache])253 assert puppetfile.forge_modules[0].version == '0.1.1'254 puppetfile.update_forge_module('puppetlabs/apache', version='0.1.2')255 assert puppetfile.forge_modules[0].version == '0.1.2'256 github_repository.update_file.assert_called_once_with(257 "Puppetfile",258 ("Puppetfile - Update forge module puppetlabs/apache "259 "from 0.1.1 to 0.1.2"),260 "mod 'puppetlabs/apache', '0.1.2'\n",261 "shasha")262 @staticmethod263 def test_it_cannot_update_a_missing_module():264 github_repository = MagicMock()265 content = github_repository.get_file_contents()266 content.decoded_content.decode.return_value = ('')267 puppetfile = Puppetfile(github_repository, 'env', sha='shasha')268 assert puppetfile.forge_modules == []269 with pytest.raises(ModuleNotFoundException):270 puppetfile.update_forge_module('puppetlabs/apache', '0.1.2')271class TestPuppetfileListModules:272 @staticmethod273 def test_it_returns_the_list_of_modules():274 github_repository = MagicMock()275 content = github_repository.get_file_contents()276 content.decoded_content.decode.return_value = ('')277 git_module_custommod = GitModule('custommod',278 'https://url/git/custommod')279 forge_module_apache = ForgeModule('puppetlabs/apache', '0.1.10')280 forge_module_vcsrepo = ForgeModule('puppetlabs/vcsrepo', '0.2.10')281 puppetfile = Puppetfile(github_repository,282 'env',283 sha='shasha',284 forge_modules=[forge_module_apache,285 forge_module_vcsrepo],286 git_modules=[git_module_custommod])287 assert sorted(puppetfile.list_modules()) == sorted([288 'custommod',289 'puppetlabs/apache',...
__init__.py
Source:__init__.py
...11 self.maxDiff = None12 self.syntax = plim.syntax.Mako()13 def tearDown(self):14 pass15 def get_file_contents(self, template_name):16 return codecs.open(os.path.join(self.templates_dir, template_name), 'r', 'utf-8').read()17 def check_relevant_chars(self, value1, value2):18 value1 = value1.strip().replace('\n\n\n\n', '\n\n').replace('\n\n\n', '\n\n').replace('\n\n', '\n')19 value2 = value2.strip().replace('\n\n\n\n', '\n\n').replace('\n\n\n', '\n\n').replace('\n\n', '\n')20 self.assertEqual(value1, value2)21class TestPreprocessorSyntax(TestCaseBase):22 def test_plim(self):23 cases = [24 'pipe',25 'plim_line',26 'if',27 'unless',28 'python',29 'for',30 'while',31 'until',32 'with',33 'try',34 'def_block',35 'style_script',36 'comment',37 'one_liners',38 'mako_text',39 'early_return',40 'call',41 'multiline_variable',42 'literal_one_liners',43 'no_filtering',44 'linebreak',45 'explicit_space',46 'unicode_attributes',47 'inline_conditions',48 'handlebars',49 ]50 for test_case in cases:51 source = self.get_file_contents(test_case + '_test.plim')52 result = self.get_file_contents(test_case + '_result.mako')53 data = plim.preprocessor(source)54 self.check_relevant_chars(data.strip(), result.strip())55 def test_dynamic_attributes(self):56 test_case = 'dynamic_attributes'57 source = self.get_file_contents(test_case + '_test.plim')58 result = self.get_file_contents(test_case + '_result.mako')59 data = plim.preprocessor(source)60 # normalize data61 data = data.replace("<a \n", "<a\n")62 # normalize for Test463 data = data.replace("\n \n", "\n\n")64 self.assertEqual(data.strip(), result.strip())65 def test_inline_loops(self):66 test_case = 'inline_loop'67 source = self.get_file_contents(test_case + '_test.plim')68 result = self.get_file_contents(test_case + '_result.mako')69 data = plim.preprocessor(source)70 self.assertEqual(data.strip(), result.strip())71 def test_embedded_markup(self):72 test_case = 'embedded'73 source = self.get_file_contents(test_case + '_test.plim')74 result = self.get_file_contents(test_case + '_result.mako')75 data = plim.preprocessor(source)76 # normalize result77 result = result.strip().replace('\n---\n', '')...
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!!