Best Python code snippet using localstack_python
gfile_test.py
Source:gfile_test.py  
1# Copyright 2015 Google Inc. All Rights Reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7#     http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14# ==============================================================================15from __future__ import absolute_import16from __future__ import division17from __future__ import print_function18import os19import shutil20from tensorflow.python.platform.default import _gfile as gfile21from tensorflow.python.platform.default import _googletest as googletest22from tensorflow.python.platform.default import _logging as logging23class _BaseTest(object):24  @property25  def tmp(self):26    return self._tmp_dir27  def setUp(self):28    self._orig_dir = os.getcwd()29    self._tmp_dir = googletest.GetTempDir() + "/"30    try:31      os.makedirs(self._tmp_dir)32    except OSError:33      pass  # Directory already exists34  def tearDown(self):35    try:36      shutil.rmtree(self._tmp_dir)37    except OSError:38      logging.warn("[%s] Post-test directory cleanup failed: %s",39                   self, self._tmp_dir)40class _GFileBaseTest(_BaseTest):41  @property42  def gfile(self):43    raise NotImplementedError("Do not use _GFileBaseTest directly.")44  def testWith(self):45    with self.gfile(self.tmp + "test_with", "w") as fh:46      fh.write("hi")47    with self.gfile(self.tmp + "test_with", "r") as fh:48      self.assertEqual(fh.read(), "hi")49  def testSizeAndTellAndSeek(self):50    with self.gfile(self.tmp + "test_tell", "w") as fh:51      fh.write("".join(["0"] * 1000))52    with self.gfile(self.tmp + "test_tell", "r") as fh:53      self.assertEqual(1000, fh.Size())54      self.assertEqual(0, fh.tell())55      fh.seek(0, 2)56      self.assertEqual(1000, fh.tell())57      fh.seek(0)58      self.assertEqual(0, fh.tell())59  def testReadAndWritelines(self):60    with self.gfile(self.tmp + "test_writelines", "w") as fh:61      fh.writelines(["%d\n" % d for d in range(10)])62    with self.gfile(self.tmp + "test_writelines", "r") as fh:63      self.assertEqual(["%d\n" % x for x in range(10)], fh.readlines())64  def testWriteAndTruncate(self):65    with self.gfile(self.tmp + "test_truncate", "w") as fh:66      fh.write("ababab")67    with self.gfile(self.tmp + "test_truncate", "a+") as fh:68      fh.seek(0, 2)69      fh.write("hjhjhj")70    with self.gfile(self.tmp + "test_truncate", "a+") as fh:71      self.assertEqual(fh.Size(), 12)72      fh.truncate(6)73    with self.gfile(self.tmp + "test_truncate", "r") as fh:74      self.assertEqual(fh.read(), "ababab")75  def testErrors(self):76    self.assertRaises(77        IOError, lambda: self.gfile(self.tmp + "doesnt_exist", "r"))78    with self.gfile(self.tmp + "test_error", "w") as fh:79      # Raises FileError inside Google and ValueError outside, so we80      # can only test for Exception.81      self.assertRaises(Exception, lambda: fh.seek(-1))82    # test_error now exists, we can read from it:83    with self.gfile(self.tmp + "test_error", "r") as fh:84      self.assertRaises(IOError, lambda: fh.write("ack"))85    fh = self.gfile(self.tmp + "test_error", "w")86    self.assertFalse(fh.closed)87    fh.close()88    self.assertTrue(fh.closed)89    self.assertRaises(ValueError, lambda: fh.write("ack"))90  def testIteration(self):91    with self.gfile(self.tmp + "test_iter", "w") as fh:92      fh.writelines(["a\n", "b\n", "c\n"])93    with self.gfile(self.tmp + "test_iter", "r") as fh:94      lines = list(fh)95      self.assertEqual(["a\n", "b\n", "c\n"], lines)96class GFileTest(_GFileBaseTest, googletest.TestCase):97  @property98  def gfile(self):99    return gfile.GFile100class FastGFileTest(_GFileBaseTest, googletest.TestCase):101  @property102  def gfile(self):103    return gfile.FastGFile104class FunctionTests(_BaseTest, googletest.TestCase):105  def testExists(self):106    self.assertFalse(gfile.Exists(self.tmp + "test_exists"))107    with gfile.GFile(self.tmp + "test_exists", "w"):108      pass109    self.assertTrue(gfile.Exists(self.tmp + "test_exists"))110  def testMkDirsGlobAndRmDirs(self):111    self.assertFalse(gfile.Exists(self.tmp + "test_dir"))112    gfile.MkDir(self.tmp + "test_dir")113    self.assertTrue(gfile.Exists(self.tmp + "test_dir"))114    gfile.RmDir(self.tmp + "test_dir")115    self.assertFalse(gfile.Exists(self.tmp + "test_dir"))116    gfile.MakeDirs(self.tmp + "test_dir/blah0")117    gfile.MakeDirs(self.tmp + "test_dir/blah1")118    self.assertEqual([self.tmp + "test_dir/blah0", self.tmp + "test_dir/blah1"],119                     sorted(gfile.Glob(self.tmp + "test_dir/*")))120    gfile.DeleteRecursively(self.tmp + "test_dir")121    self.assertFalse(gfile.Exists(self.tmp + "test_dir"))122  def testErrors(self):123    self.assertRaises(124        OSError, lambda: gfile.RmDir(self.tmp + "dir_doesnt_exist"))125    self.assertRaises(126        OSError, lambda: gfile.Remove(self.tmp + "file_doesnt_exist"))127    gfile.MkDir(self.tmp + "error_dir")128    with gfile.GFile(self.tmp + "error_dir/file", "w"):129      pass  # Create file130    self.assertRaises(131        OSError, lambda: gfile.Remove(self.tmp + "error_dir"))132    self.assertRaises(133        OSError, lambda: gfile.RmDir(self.tmp + "error_dir"))134    self.assertTrue(gfile.Exists(self.tmp + "error_dir"))135    gfile.DeleteRecursively(self.tmp + "error_dir")136    self.assertFalse(gfile.Exists(self.tmp + "error_dir"))137if __name__ == "__main__":...testmysql.py
Source:testmysql.py  
...5from core.tools.tools import prn6class MysqlTest(unittest.TestCase):7    _dbconf = {'host': 'localhost', 'port': '3306', 'user': 'root', 'password': '1234', 'database': 'db_eduardoaf'}8    @decorator_warnings9    def test_truncate(self):10        diccfg = self._dbconf11        omysql = Mysql(diccfg)12        sql = "TRUNCATE TABLE imp_post"13        prn(sql,"test_truncate")14        omysql.execute(sql)15        omysql.commit().close()16        17    18    @decorator_warnings19    def test_insert(self):20        diccfg = self._dbconf21        omysql = Mysql(diccfg)22        sql = "INSERT INTO imp_post (publish_date, last_update, title, content, excerpt, id_status, slug) VALUES ( %s, %s, %s, %s, %s, %s, %s )"23        prn(sql,"test_insert")24        lastid = omysql.insert_tpl(sql,25            (26                "pd", 27                "lu", 28                'a','b','c','d','e',29            )30        )31        omysql.commit().close()32        self.assertGreater(lastid, 0, "assert insert")33    @decorator_warnings34    def test_select(self):35        diccfg = self._dbconf36        omysql = Mysql(diccfg)37        38        sql = "SELECT * FROM imp_post WHERE title='a'"39        prn(sql,"test_select")40        r = omysql.query(sql)41        prn(r, "result")42        omysql.close()43        ilen = 0 if r is None else len(r)44        self.assertGreater(ilen, 0, "assert select")45if __name__ == "__main__":46    # o = MysqlTest();o.test_truncate();o.test_insert();o.test_select()...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!!
