How to use _create_tmp_file method in avocado

Best Python code snippet using avocado_python

test_fileupload.py

Source:test_fileupload.py Github

copy

Full Screen

...21 DBSession.rollback()22 for id in self._tmp_files:23 if os.path.exists("/tmp/%s.txt" % id) is True:24 os.remove("/tmp/%s.txt" % id)25 def _create_tmp_file(self, id="120", content="FOOBAR"):26 self._tmp_files.append(id)27 tmp_file = open("/tmp/%s.txt" % id, "w")28 tmp_file.write(content)29 tmp_file.close()30 def _request(self, id, content):31 uploaded_file = StringIO()32 uploaded_file.write(content)33 post = {34 "filedata": type(35 "filedata", (object,), {"filename": "test.txt", "file": uploaded_file}36 )37 }38 matchdict = {"id": id}39 registry = type(40 "registry", (object,), {"settings": {"dms.storage.path": "./"}}41 )()42 return type(43 "request",44 (object,),45 {"POST": post, "matchdict": matchdict, "registry": registry},46 )()47 def _file(self, id="120", content="FOOBAR"):48 return FileUpload(self._request(id, content))49 def _event(self, version="1.0"):50 context = type("context", (object,), {"version": version})()51 return type("event", (object,), {"context": context})()52 def test_remove_file(self):53 self._create_tmp_file()54 self.assertTrue(os.path.exists("/tmp/120.txt"))55 remove_file("tmp_path", self._file(), None, test=None)56 self.assertFalse(os.path.exists("/tmp/120.txt"))57 def test_remove_file_validatorevent(self):58 self._create_tmp_file()59 event = ValidatorEvent(None, self._file())60 self.assertTrue(os.path.exists("/tmp/120.txt"))61 remove_file("tmp_path", event, None, test=None)62 self.assertFalse(os.path.exists("/tmp/120.txt"))63 def test_id(self):64 self.assertEqual(120, self._file().id)65 def test_filename(self):66 self.assertEqual("120.txt", self._file().filename)67 def test_basepath(self):68 path = get_blob_path(12345678901234)69 self.assertEqual("12/34/56/78/90/12", path)70 path = get_blob_path(3411)71 self.assertEqual("00/00/00/00/00/34", path)72 path = get_blob_path(3567)73 self.assertEqual("00/00/00/00/00/35", path)74 def test_filepath(self):75 self.assertEqual("./00/00/00/00/00/01/120.txt", self._file().filepath)76 def test_tmp_path(self):77 self.assertEqual("/tmp/120.txt", self._file().tmp_path)78 def test_size(self):79 self._create_tmp_file()80 self.assertTrue(os.path.exists(self._file().tmp_path))81 self.assertEqual(6, self._file().size)82 def test_data_cached(self):83 _file = self._file()84 _file._data = "test"85 self.assertEqual("test", _file.data)86 def test_data(self):87 record = File(88 id=120,89 external_id="CH-0001",90 client_id="CH",91 type="COUR_E",92 version=1,93 user="testuser",94 )95 metadata = {96 "filesize": 6,97 "type": "COUR_E",98 "client_id": "CH",99 "external_id": "CH-0001",100 "filemd5": "901890a8e9c8cf6d5a1a542b229febff",101 }102 record.file_metadata = metadata103 record.insert(flush=True)104 file_data = self._file().data105 self.assertEqual(120, file_data.id)106 def test_save_tmpfile(self):107 self.assertFalse(os.path.exists(self._file().tmp_path))108 self._file().save_tmpfile()109 self.assertTrue(os.path.exists(self._file().tmp_path))110 self.assertEqual("FOOBAR", open(self._file().tmp_path).read())111 def test_move(self):112 self._create_tmp_file()113 self.assertTrue(os.path.exists(self._file().tmp_path))114 self._file().move()115 self.assertTrue(os.path.exists(self._file().filepath))116 self.assertFalse(os.path.exists(self._file().tmp_path))117 def test_save_reference(self):118 record = File(119 id=120,120 external_id="CH-0001",121 client_id="CH",122 type="COUR_E",123 version=1,124 user="testuser",125 )126 metadata = {127 "filesize": 6,128 "type": "COUR_E",129 "client_id": "CH",130 "external_id": "CH-0001",131 "filemd5": "901890a8e9c8cf6d5a1a542b229febff",132 }133 record.file_metadata = metadata134 record.insert(flush=True)135 self._file().save_reference()136 record = File.first(id=120)137 self.assertEqual("./00/00/00/00/00/01/120.txt", record.filepath)138 def test_handle_exception(self):139 self._create_tmp_file()140 file_upload = self._file()141 file_upload._file.file = None142 self.assertTrue(os.path.exists(file_upload.tmp_path))143 self.assertRaises(AttributeError, file_upload.save_tmpfile)144 self.assertFalse(os.path.exists(file_upload.tmp_path))145 def test_validate_file_no_data(self):146 self._create_tmp_file()147 event = ValidatorEvent(None, self._file())148 self.assertRaisesRegexp(149 ValidationError, ".*no metadata.*", validate_file, event150 )151 def test_validate_file_already_exist(self):152 self._create_tmp_file()153 record = File(154 id=120,155 external_id="CH-0001",156 client_id="CH",157 type="COUR_E",158 version=1,159 user="testuser",160 filepath="/tmp/120.txt",161 )162 metadata = {163 "filesize": 6,164 "type": "COUR_E",165 "client_id": "CH",166 "external_id": "CH-0001",167 "filemd5": "95c72a49c488d59f60c022fcfecf4382",168 }169 record.file_metadata = metadata170 record.insert(flush=True)171 file_upload = self._file()172 file = open(self._file().filepath, "w")173 file.write("FOOBAR")174 file.close()175 event = ValidatorEvent(self._event(), file_upload)176 self.assertIsNone(validate_file(event))177 def test_validate_file_filesize_mismatch(self):178 self._create_tmp_file()179 record = File(180 id=120,181 external_id="CH-0001",182 client_id="CH",183 type="COUR_E",184 version=1,185 user="testuser",186 )187 metadata = {188 "filesize": 4,189 "type": "COUR_E",190 "client_id": "CH",191 "external_id": "CH-0001",192 "filemd5": "95c72a49c488d59f60c022fcfecf4382",193 }194 record.file_metadata = metadata195 record.insert(flush=True)196 event = ValidatorEvent(None, self._file())197 self.assertRaisesRegexp(198 ValidationError, ".*filesize does not match.*", validate_file, event199 )200 def test_validate_file_md5_mismatch(self):201 self._create_tmp_file()202 record = File(203 id=120,204 external_id="CH-0001",205 client_id="CH",206 type="COUR_E",207 version=1,208 user="testuser",209 )210 metadata = {211 "filesize": 6,212 "type": "COUR_E",213 "client_id": "CH",214 "external_id": "CH-0001",215 "filemd5": "95c72a49c488d59f60c022fcfecf4382XXX",216 }217 record.file_metadata = metadata218 record.insert(flush=True)219 event = ValidatorEvent(None, self._file())220 self.assertRaisesRegexp(221 ValidationError, ".*MD5 check: difference found.*", validate_file_11, event222 )223 def test_validate_file_md5_uppercase(self):224 self._create_tmp_file()225 record = File(226 id=120,227 external_id="CH-0001",228 client_id="CH",229 type="COUR_E",230 version=1,231 user="testuser",232 )233 metadata = {234 "filesize": 6,235 "type": "COUR_E",236 "client_id": "CH",237 "external_id": "CH-0001",238 "filemd5": "95C72A49C488D59F60C022FCFECF4382",239 }240 record.file_metadata = metadata241 record.insert(flush=True)242 event = ValidatorEvent(None, self._file())243 validate_file_11(event)244 def test_validate_file(self):245 self._create_tmp_file()246 record = File(247 id=120,248 external_id="CH-0001",249 client_id="CH",250 type="COUR_E",251 version=1,252 user="testuser",253 )254 metadata = {255 "filesize": 6,256 "type": "COUR_E",257 "client_id": "CH",258 "external_id": "CH-0001",259 "filemd5": "95c72a49c488d59f60c022fcfecf4382",260 }261 record.file_metadata = metadata262 record.insert(flush=True)263 event = ValidatorEvent(None, self._file())264 self.assertIsNone(validate_file_11(event))265 def test_validate_file_update(self):266 self._create_tmp_file()267 record = File(268 id=120,269 external_id="CH-0001",270 client_id="CH",271 type="COUR_E",272 version=1,273 user="testuser",274 )275 metadata = {276 "filesize": 6,277 "type": "COUR_E",278 "client_id": "CH",279 "external_id": "CH-0001",280 "filemd5": "95c72a49c488d59f60c022fcfecf4382",281 }282 record.file_metadata = metadata283 record.insert(flush=True)284 event = ValidatorEvent(None, self._file())285 validate_file_11(event)286 self._create_tmp_file(id="121", content="FOO")287 record = File(288 id=121,289 external_id="CH-0001",290 client_id="CH",291 type="COUR_E",292 version=2,293 user="testuser",294 )295 metadata = {296 "filesize": 3,297 "type": "COUR_E",298 "client_id": "CH",299 "external_id": "CH-0001",300 "filemd5": "901890a8e9c8cf6d5a1a542b229febff",...

Full Screen

Full Screen

test_web_app.py

Source:test_web_app.py Github

copy

Full Screen

...14 "uid": str(uuid.uuid4()),15 "contents": str(uuid.uuid4()),16 }17}18def _create_tmp_file(tmp_file, contents):19 """20 Create the given file, and it's base directory if necessary.21 Fill the file with the given contents.22 :param tmp_file: Name of the file to be created (including path).23 :type tmp_file: ``str``24 :param contents: Contents of the file to be created.25 :type contents: ``str``26 """27 base_dir = os.path.dirname(tmp_file)28 if not os.path.exists(base_dir):29 os.makedirs(base_dir)30 with open(tmp_file, "w") as tmp_file:31 tmp_file.write(str(contents))32def _create_tmp_static_files(base_dir):33 """34 Create fake static files to be sent by the Testplan web app.35 <STATIC_PATH>/testplan/build/index.html36 <STATIC_PATH>/testplan/build/static.file37 :param base_dir: The Testplan web app's STATIC_PATH directory.38 :type base_dir: ``str``39 """40 for report_type, report in list(STATIC_REPORTS.items()):41 index_file = os.path.join(base_dir, report_type, "build", "index.html")42 _create_tmp_file(tmp_file=index_file, contents=report["contents"])43 static_file = os.path.join(44 base_dir, report_type, "build", "static", "static.file"45 )46 _create_tmp_file(tmp_file=static_file, contents=report["contents"])47def _create_tmp_data_files(base_dir):48 """49 Create fake data files to be sent by the Testplan web app.50 <DATA_PATH>/reports/testplan_<UID>/report.json51 <DATA_PATH>/reports/monitor_<UID>/report.json52 :param base_dir: The Testplan web app's DATA_PATH directory.53 :type base_dir: ``str``54 """55 for report_type, report in list(DATA_REPORTS.items()):56 report_file = os.path.join(base_dir, report["report_name"])57 _create_tmp_file(tmp_file=report_file, contents=report["contents"])58 attachment_file = os.path.join(59 base_dir, defaults.ATTACHMENTS, "attached.file"60 )61 _create_tmp_file(62 tmp_file=attachment_file, contents=DATA_REPORTS["testplan"]["contents"]63 )64class TestStaticEndpoints(object):65 """66 Test the endpoints returning files from the STATIC_PATH directory.67 """68 def setup_method(self, _):69 """Create fake static files and a test client."""70 self.static_dir = tempfile.mkdtemp()71 _create_tmp_static_files(self.static_dir)72 tp_web_app.config["STATIC_PATH"] = self.static_dir73 tp_web_app.config["TESTPLAN_REPORT_NAME"] = "report.json"74 tp_web_app.config["TESTING"] = True75 tp_web_app.static_folder = os.path.abspath(...

Full Screen

Full Screen

stanford.py

Source:stanford.py Github

copy

Full Screen

...48 tag_prob.append(t[0])49 return tag_prob50 sentence = [(s, 'I') for s in sentence]51 _input = '\n'.join((' '.join(x) for x in sentence))52 _input_file_path = self._create_tmp_file(_input)53 cmd = [54 'edu.stanford.nlp.ie.crf.CRFClassifier',55 '-loadClassifier',56 self._stanford_model,57 '-testFile',58 _input_file_path,59 '-printprobs'60 ]61 return self.run(_input_file_path, cmd, parse_output)62 def _create_tmp_file(self, _input):63 encoding = self._encoding64 # Create a temporary input file65 _input_fh, _input_file_path = tempfile.mkstemp(text=True)66 # Write the actual sentences to the temporary input file67 _input_fh = os.fdopen(_input_fh, 'wb')68 if isinstance(_input, text_type) and encoding:69 _input = _input.encode(encoding)70 _input_fh.write(_input)71 _input_fh.close()72 return _input_file_path73 def probability_sent(self, sentences):74 # Parser75 def parse_output(text, sentences=None):76 probs = []77 for prob in text.split('<sentence')[1:]:78 probs.append(float(prob.split('prob=')[1].split('>')[0]))79 return probs80 _input = ''81 for sentence in sentences:82 _input += '\n\n'83 _input += '\n'.join([s + '\tI' for s in sentence])84 _input_file_path = self._create_tmp_file(_input)85 cmd = [86 'edu.stanford.nlp.ie.crf.CRFClassifier',87 '-loadClassifier',88 self._stanford_model,89 '-testFile',90 _input_file_path,91 '-kbest', '1'92 ]93 return self.run(_input_file_path, cmd, parse_output)94 def predict(self, sentences):95 # Parser96 def parse_output(text, sentences=None):97 if self._FORMAT == 'tsv':98 # Joint together to a big list99 tagged_sentences = []100 for tagged_sentence in text.strip().split('\n\n'):101 tagged_sentences.append(102 [sent.split('\t')[2] for sent in tagged_sentence.split('\n')])103 return tagged_sentences104 raise NotImplementedError105 _input = '\tI\n\n'.join(('\tI\n'.join(x) for x in sentences))106 _input += '\tI'107 _input_file_path = self._create_tmp_file(_input)108 cmd = [109 'edu.stanford.nlp.ie.crf.CRFClassifier',110 '-loadClassifier',111 self._stanford_model,112 '-testFile',113 _input_file_path,114 '-outputFormat',115 self._FORMAT,116 ]117 return self.run(_input_file_path, cmd, parse_output)118 def fit(self):119 cmd = [120 'edu.stanford.nlp.ie.crf.CRFClassifier',121 '-prop',...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado 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