Best Python code snippet using tempest_python
testDocStore.py
Source:testDocStore.py
...75 self.assertEqual(self.store.path("123", "foo", ".bar"),76 self.datadir.replace("/", os.sep) + "\\foo\\123.bar")77 finally:78 os.sep = realsep79 def test_path_version(self):80 eq = self.assertEqual81 eq(self.store.path("123","foo", ".bar", version="42"),82 self.p("archive/foo/123/.versions/42.bar"))83 eq(self.store.path("123/a","foo", ".bar", version="42"),84 self.p("archive/foo/123/a/.versions/42.bar"))85 eq(self.store.path("123:a","foo", ".bar", version="42"),86 self.p("archive/foo/123/%3Aa/.versions/42.bar"))87 eq(self.store.path("123:a","foo", ".bar", version="42:1"),88 self.p("archive/foo/123/%3Aa/.versions/42/%3A1.bar"))89 self.store.storage_policy = "dir"90 eq(self.store.path("123","foo", ".bar", version="42"),91 self.p("archive/foo/123/.versions/42/index.bar"))92 eq(self.store.path("123/a","foo", ".bar", version="42"),93 self.p("archive/foo/123/a/.versions/42/index.bar"))94 eq(self.store.path("123:a","foo", ".bar", version="42"),95 self.p("archive/foo/123/%3Aa/.versions/42/index.bar"))96 eq(self.store.path("123:a","foo", ".bar", version="42:1"),97 self.p("archive/foo/123/%3Aa/.versions/42/%3A1/index.bar"))98 99 def test_path_attachment(self):100 eq = self.assertEqual101 repo = self.store # to shorten lines < 80 chars102 repo.storage_policy = "dir" # attachments require this103 eq(repo.path("123","foo", None, attachment="external.foo"),104 self.p("foo/123/external.foo"))105 eq(repo.path("123/a","foo", None, attachment="external.foo"),106 self.p("foo/123/a/external.foo"))107 eq(repo.path("123:a","foo", None, attachment="external.foo"),108 self.p("foo/123/%3Aa/external.foo"))109 110 with self.assertRaises(AttachmentNameError):111 repo.path("123:a","foo", None,112 attachment="invalid:attachment")113 with self.assertRaises(AttachmentNameError):114 repo.path("123:a","foo", None,115 attachment="invalid/attachment"), 116 repo.storage_policy = "file"117 with self.assertRaises(AttachmentPolicyError):118 repo.path("123:a","foo", None,119 attachment="external.foo"), 120 def test_path_version_attachment(self):121 eq = self.assertEqual122 self.store.storage_policy = "dir"123 eq(self.store.path("123","foo", None,124 version="42", attachment="external.foo"),125 self.p("archive/foo/123/.versions/42/external.foo"))126 eq(self.store.path("123/a","foo", None,127 version="42", attachment="external.foo"),128 self.p("archive/foo/123/a/.versions/42/external.foo"))129 eq(self.store.path("123:a","foo", None,130 version="42", attachment="external.foo"),131 self.p("archive/foo/123/%3Aa/.versions/42/external.foo"))132 133 134 def test_specific_path_methods(self):135 self.assertEqual(self.store.downloaded_path('123/a'),136 self.p("downloaded/123/a.html"))137 self.assertEqual(self.store.downloaded_path('123/a', version="1"),138 self.p("archive/downloaded/123/a/.versions/1.html"))139 self.assertEqual(self.store.parsed_path('123/a', version="1"),140 self.p("archive/parsed/123/a/.versions/1.xhtml"))141 self.assertEqual(self.store.generated_path('123/a', version="1"),142 self.p("archive/generated/123/a/.versions/1.html"))143 self.store.storage_policy = "dir"144 self.assertEqual(self.store.downloaded_path('123/a'),145 self.p("downloaded/123/a/index.html"))146 self.assertEqual(self.store.downloaded_path('123/a', version="1"),147 self.p("archive/downloaded/123/a/.versions/1/index.html"))148 self.assertEqual(self.store.parsed_path('123/a', version="1"),149 self.p("archive/parsed/123/a/.versions/1/index.xhtml"))150 self.assertEqual(self.store.generated_path('123/a', version="1"),151 self.p("archive/generated/123/a/.versions/1/index.html"))152 153 def test_basefile_to_pathfrag(self):154 self.assertEqual(self.store.basefile_to_pathfrag("123-a"), "123-a")155 self.assertEqual(self.store.basefile_to_pathfrag("123/a"), "123/a")156 self.assertEqual(self.store.basefile_to_pathfrag("123:a"), "123"+os.sep+"%3Aa")157 def test_pathfrag_to_basefile(self):158 self.assertEqual(self.store.pathfrag_to_basefile("123-a"), "123-a")159 self.assertEqual(self.store.pathfrag_to_basefile("123/a"), "123/a")160 self.assertEqual(self.store.pathfrag_to_basefile("123/%3Aa"), "123:a")161 try:162 # make sure the pathfrag method works as expected even when os.sep is not "/"163 realsep = os.sep164 os.sep = "\\"165 self.assertEqual(self.store.pathfrag_to_basefile("123\\a"), "123/a")166 finally:167 os.sep = realsep168 def test_list_basefiles_file(self):169 files = ["downloaded/123/a.html",170 "downloaded/123/b.html",171 "downloaded/124/a.html",172 "downloaded/124/b.html"]173 basefiles = ["124/b", "124/a", "123/b", "123/a"]174 for f in files:175 util.writefile(self.p(f),"Nonempty")176 self.assertEqual(list(self.store.list_basefiles_for("parse")),177 basefiles)178 def test_list_basefiles_parse_dir(self):179 files = ["downloaded/123/a/index.html",180 "downloaded/123/b/index.html",181 "downloaded/124/a/index.html",182 "downloaded/124/b/index.html"]183 basefiles = ["124/b", "124/a", "123/b", "123/a"]184 self.store.storage_policy = "dir"185 for f in files:186 p = self.p(f)187 util.writefile(p,"nonempty")188 self.assertEqual(list(self.store.list_basefiles_for("parse")),189 basefiles)190 def test_list_basefiles_generate_dir(self):191 files = ["parsed/123/a/index.xhtml",192 "parsed/123/b/index.xhtml",193 "parsed/124/a/index.xhtml",194 "parsed/124/b/index.xhtml"]195 basefiles = ["124/b", "124/a", "123/b", "123/a"]196 self.store.storage_policy = "dir"197 for f in files:198 util.writefile(self.p(f),"nonempty")199 self.assertEqual(list(self.store.list_basefiles_for("generate")),200 basefiles)201 def test_list_basefiles_postgenerate_file(self):202 files = ["generated/123/a.html",203 "generated/123/b.html",204 "generated/124/a.html",205 "generated/124/b.html"]206 basefiles = ["124/b", "124/a", "123/b", "123/a"]207 for f in files:208 util.writefile(self.p(f),"nonempty")209 self.assertEqual(list(self.store.list_basefiles_for("_postgenerate")),210 basefiles)211 def test_list_basefiles_invalid(self):212 with self.assertRaises(ValueError):213 list(self.store.list_basefiles_for("invalid_action"))214 def test_list_versions_file(self):215 files = ["archive/downloaded/123/a/.versions/1.html",216 "archive/downloaded/123/a/.versions/2.html",217 "archive/downloaded/123/a/.versions/2bis.html",218 "archive/downloaded/123/a/.versions/10.html"]219 versions = ["1","2", "2bis", "10"]220 for f in files:221 util.writefile(self.p(f),"nonempty")222 # list_versions(action, basefile)223 self.assertEqual(list(self.store.list_versions("123/a","downloaded")),224 versions)225 def test_list_versions_dir(self):226 files = ["archive/downloaded/123/a/.versions/1/index.html",227 "archive/downloaded/123/a/.versions/2/index.html",228 "archive/downloaded/123/a/.versions/2bis/index.html",229 "archive/downloaded/123/a/.versions/10/index.html"]230 basefiles = ['123/a']231 versions = ["1","2", "2bis", "10"]232 for f in files:233 util.writefile(self.p(f),"nonempty")234 self.store.storage_policy = "dir"235 self.assertEqual(list(self.store.list_versions("123/a", "downloaded")),236 versions)237 def test_list_complicated_versions(self):238 # the test here is that basefile + version might be ambigious239 # as to where to split unless we add the reserved .versions240 # directory241 a_files = ["archive/downloaded/123/.versions/a/27.html",242 "archive/downloaded/123/.versions/a/27/b"]243 b_files = ["archive/downloaded/123/b/.versions/27.html",244 "archive/downloaded/123/b/.versions/27/b.html"]245 a_versions = ["a/27", "a/27/b"]246 b_versions = ["27", "27/b"]247 for f in a_files + b_files:248 util.writefile(self.p(f),"nonempty")249 self.assertEqual(list(self.store.list_versions("123","downloaded")),250 a_versions)251 self.assertEqual(list(self.store.list_versions("123/b","downloaded")),252 b_versions)253 254 def test_list_attachments(self):255 self.store.storage_policy = "dir" # attachments require this256 files = ["downloaded/123/a/index.html",257 "downloaded/123/a/attachment.html",258 "downloaded/123/a/appendix.pdf",259 "downloaded/123/a/other.txt"]260 basefiles = ['123/a']261 attachments = ['appendix.pdf', 'attachment.html', 'other.txt']262 for f in files:263 util.writefile(self.p(f),"nonempty")264 # list_attachments(action, basefile, version=None)265 self.assertEqual(list(self.store.list_attachments("123/a", "downloaded")),266 attachments)267 def test_list_invalid_attachments(self):268 # test that files with an invalid suffix (in269 # store.invalid_suffixes) is not listed270 self.store.storage_policy = "dir" # attachments require this271 files = ["downloaded/123/a/index.html",272 "downloaded/123/a/index.invalid",273 "downloaded/123/a/other.invalid",274 "downloaded/123/a/other.txt"]275 basefiles = ['123/a']276 attachments = ['other.txt']277 for f in files:278 util.writefile(self.p(f),"nonempty")279 # list_attachments(action, basefile, version=None)280 self.assertEqual(list(self.store.list_attachments("123/a", "downloaded")),281 attachments)282 283 def test_list_attachments_version(self):284 self.store.storage_policy = "dir" # attachments require this285 files = ["archive/downloaded/123/a/.versions/1/index.html",286 "archive/downloaded/123/a/.versions/1/attachment.txt",287 "archive/downloaded/123/a/.versions/2/index.html",288 "archive/downloaded/123/a/.versions/2/attachment.txt",289 "archive/downloaded/123/a/.versions/2/other.txt"]290 basefiles = ['123/a']291 versions = ['1','2']292 attachments_1 = ['attachment.txt']293 attachments_2 = ['attachment.txt', 'other.txt']294 for f in files:295 util.writefile(self.p(f),"nonempty")296 self.assertEqual(list(self.store.list_attachments("123/a","downloaded",297 "1")),298 attachments_1)299 self.assertEqual(list(self.store.list_attachments("123/a","downloaded",300 "2")),301 attachments_2)302class Compression(unittest.TestCase):303 maxDiff = 2048304 compression = None305 expected_suffix = ""306 expected_mimetype = "text/plain"307 dummytext = """For applications that require data compression, the functions in this module allow compression and decompression, using the zlib library. The zlib library has its own home page at http://www.zlib.net. There are known incompatibilities between the Python module and versions of the zlib library earlier than 1.1.3; 1.1.3 has a security vulnerability, so we recommend using 1.1.4 or later.308zlibâs functions have many options and often need to be used in a particular order. This documentation doesnât attempt to cover all of the permutations; consult the zlib manual at http://www.zlib.net/manual.html for authoritative information.309For reading and writing .gz files see the gzip module.310"""311 312 def p(self,path):313 path = self.datadir+"/"+path314 return path.replace('/', '\\') if os.sep == '\\' else path315 def setUp(self):316 self.datadir = tempfile.mkdtemp()317 self.store = DocumentStore(self.datadir, compression=self.compression)318 def test_intermediate_path(self):319 self.assertEqual(self.p("intermediate/123/a.xml" + self.expected_suffix),320 self.store.intermediate_path('123/a'))321 def test_intermediate_path_selectsuffix(self):322 self.store.intermediate_suffixes = [".html", ".xhtml"]323 util.writefile(self.p("intermediate/123/a.html"), self.dummytext)324 self.assertEqual(self.p("intermediate/123/a.html") + self.expected_suffix,325 self.store.intermediate_path('123/a'))326 def test_open_intermediate_path(self):327 self.store.intermediate_suffixes = [".html", ".xhtml"]328 with self.store.open_intermediate("123/a", mode="w", suffix=".xhtml") as fp:329 fp.write(self.dummytext)330 filename = self.p("intermediate/123/a.xhtml" + self.expected_suffix)331 self.assertTrue(os.path.exists(filename))332 mimetype = util.runcmd("file -b --mime-type %s" % filename)[1]333 self.assertIn(mimetype.strip(), self.expected_mimetype)334 with self.store.open_intermediate("123/a") as fp:335 # note, open_intermediate should open the file with the336 # the .xhtml suffix automatically337 self.assertEqual(self.dummytext, fp.read())338class GzipCompression(Compression):339 compression = "gz"340 expected_suffix = ".gz"341 expected_mimetype = ("application/x-gzip", "application/gzip")342class Bzip2Compression(Compression):343 compression = "bz2"344 expected_suffix = ".bz2"345 expected_mimetype = ("application/x-bzip2",)346@unittest.skipIf(sys.version_info < (3, 0, 0), "LZMAFile not available in py2")347class XzCompression(Compression):348 compression = "xz"349 expected_suffix = ".xz"350 expected_mimetype = ("application/x-xz",)351 352class Needed(unittest.TestCase):353 def setUp(self):354 self.datadir = tempfile.mkdtemp()355 self.store = DocumentStore(self.datadir)356 def tearDown(self):357 shutil.rmtree(self.datadir)358 def create_file(self, path, timestampoffset=0, content="dummy"):359 util.ensure_dir(path)360 with open(path, "w") as fp:361 fp.write(content)362 if timestampoffset:363 os.utime(path, (time.time(), time.time() + timestampoffset))364 365 def test_parse_not_needed(self):366 self.create_file(self.store.downloaded_path("a"))367 self.create_file(self.store.parsed_path("a"), 3600)368 self.assertFalse(self.store.needed("a", "parse"))369 def test_parse_needed(self):370 self.create_file(self.store.downloaded_path("a"))371 res = self.store.needed("a", "parse")372 self.assertTrue(res)373 self.assertIn("outfile doesn't exist", res.reason)374 def test_parse_needed_outdated(self):375 self.create_file(self.store.downloaded_path("a"))376 self.create_file(self.store.parsed_path("a"), -3600)377 res = self.store.needed("a", "parse")378 self.assertTrue(res)379 self.assertIn("is newer than outfile", res.reason)380 def create_entry(self, basefile, timestampoffset=0):381 # create a entry file with indexed_{ft,ts,dep} set to the382 # current time with optional offset. Also383 # .status['generated']['date'], to test needed(...,384 # 'transformlinks')385 de = DocumentEntry(self.store.documententry_path(basefile))386 delta = timedelta(seconds=timestampoffset)387 ts = datetime.now() + delta388 de.indexed_ts = ts389 de.indexed_ft = ts390 de.indexed_dep = ts391 de.updated = ts392 de.status['generate'] = {'date': ts}393 de.save()394 def test_relate_not_needed(self):395 self.create_entry("a")396 self.create_file(self.store.distilled_path("a"), -3600)397 self.create_file(self.store.parsed_path("a"), -3600)398 self.create_file(self.store.dependencies_path("a"), -3600)399 self.assertFalse(self.store.needed("a", "relate"))400 def test_relate_needed_ts(self):401 self.create_entry("a", -3600)402 self.create_file(self.store.distilled_path("a"))403 self.create_file(self.store.parsed_path("a"), -7200)404 self.create_file(self.store.dependencies_path("a"), -7200)405 res = self.store.needed("a", "relate")406 self.assertTrue(res)407 self.assertIn("is newer than indexed_ts in documententry", res.reason)408 def test_relate_needed_ft(self):409 self.create_entry("a", -3600)410 self.create_file(self.store.distilled_path("a"), -7200)411 self.create_file(self.store.parsed_path("a"))412 self.create_file(self.store.dependencies_path("a"), -7200)413 res = self.store.needed("a", "relate")414 self.assertTrue(res)415 self.assertIn("is newer than indexed_ft in documententry", res.reason)416 def test_relate_needed_dep(self):417 self.create_entry("a", -3600)418 self.create_file(self.store.distilled_path("a"), -7200)419 self.create_file(self.store.parsed_path("a"), -7200)420 self.create_file(self.store.dependencies_path("a"))421 res = self.store.needed("a", "relate")422 self.assertTrue(res)423 self.assertIn("is newer than indexed_dep in documententry", res.reason)424 425 def test_generate_not_needed(self):426 self.create_file(self.store.parsed_path("a"))427 self.create_file(self.store.generated_path("a"), 3600)428 self.assertFalse(self.store.needed("a", "generate"))429 def test_generate_not_needed_404(self):430 self.create_file(self.store.parsed_path("a"))431 self.create_file(self.store.generated_path("a") + ".404", 3600)432 self.assertFalse(self.store.needed("a", "generate"))433 def test_generate_needed(self):434 self.create_file(self.store.parsed_path("a"))435 res = self.store.needed("a", "generate")436 self.assertTrue(res)437 self.assertIn("outfile doesn't exist", res.reason)438 def test_generate_needed_outdated(self):439 self.create_file(self.store.parsed_path("a"))440 self.create_file(self.store.generated_path("a"), -3600)441 res = self.store.needed("a", "generate")442 self.assertTrue(res)443 self.assertIn("is newer than outfile", res.reason)444 def test_generate_needed_outdated_dep(self):445 self.create_file(self.store.parsed_path("a"), -3600)446 self.create_file(self.store.generated_path("a"), -1800)447 dependency_path = self.store.datadir + os.sep + "blahonga.txt"448 self.create_file(self.store.dependencies_path("a"), -3600, dependency_path)449 # create a arbitrary dependency file which is newer than outfile450 self.create_file(dependency_path)451 res = self.store.needed("a", "generate")452 self.assertTrue(res)453 self.assertIn("is newer than outfile", res.reason)454 def test_transformlinks_needed(self):455 self.create_file(self.store.generated_path("a"))456 self.create_entry("a")457 res = self.store.needed("a", "transformlinks")458 self.assertTrue(res)459 self.assertIn("has not been modified after generate", res.reason)460 def test_transformlinks_not_needed(self):461 self.create_entry("a", -3600)462 self.create_file(self.store.generated_path("a"))463 self.assertFalse(self.store.needed("a", "transformlinks"))464class ZipArchive(Store):465 def setUp(self):466 super(ZipArchive, self).setUp()467 self.datadir = tempfile.mkdtemp()468 self.store = DocumentStore(self.datadir)469 self.store.archiving_policy = "zip"470 def test_archive_path(self):471 self.assertEqual(self.store.archive_path("123/a"),472 self.p("archive/123/a.zip"))473 def test_path_version(self):474 eq = self.assertEqual475 eq(self.p("archive/123.zip#foo/42.bar"),476 self.store.path("123","foo", ".bar", version="42"))477 eq(self.p("archive/123/a.zip#foo/42.bar"),478 self.store.path("123/a","foo", ".bar", version="42"))479 eq(self.p("archive/123/%3Aa.zip#foo/42.bar"),480 self.store.path("123:a","foo", ".bar", version="42"))481 eq(self.p("archive/123/%3Aa.zip#foo/42/%3A1.bar"),482 self.store.path("123:a","foo", ".bar", version="42:1"))483 self.store.storage_policy = "dir"484 eq(self.p("archive/123.zip#foo/42/index.bar"),485 self.store.path("123","foo", ".bar", version="42"))486 eq(self.p("archive/123/a.zip#foo/42/index.bar"),487 self.store.path("123/a","foo", ".bar", version="42"))...
test-loadable.py
Source:test-loadable.py
...46 def test_modules(self):47 modules = list(map(lambda a: a[0], db.execute("select name from loaded_modules").fetchall()))48 self.assertEqual(modules, MODULES)49 50 def test_path_version(self):51 with open("./VERSION") as f:52 version = f.read()53 54 self.assertEqual(db.execute("select path_version()").fetchone()[0], version)55 def test_path_debug(self):56 debug = db.execute("select path_debug()").fetchone()[0].split('\n')57 self.assertEqual(len(debug), 4)58 self.assertTrue(debug[0].startswith("Version: v"))59 self.assertTrue(debug[1].startswith("Date: "))60 self.assertTrue(debug[2].startswith("Source: "))61 self.assertTrue(debug[3].startswith("cwalk version:"))62 63 def test_path_absolute(self):64 path_absolute = lambda arg: db.execute("select path_absolute(?)", [arg]).fetchone()[0]...
test_api_version.py
Source:test_api_version.py
...182 self.assertRaises(gfapy.VersionError,183 gfapy.Line, "X\tVN:Z:1.0", version="gfa1")184 self.assertRaises(gfapy.VersionError,185 gfapy.line.CustomRecord, ["X","VN:Z:1.0"], version="gfa1")186 def test_path_version(self):187 string = "P\t1\tA+,B-\t*"188 self.assertEqual("gfa1", gfapy.Line(string).version)189 self.assertEqual("gfa1", gfapy.Line(string, version="gfa1").version)190 self.assertRaises(gfapy.VersionError, gfapy.Line, string, version="gfa2")191 string = "O\t1\tA+ B-"192 self.assertEqual("gfa2", gfapy.Line(string).version)193 self.assertEqual("gfa2", gfapy.Line(string, version="gfa2").version)194 self.assertRaises(gfapy.VersionError, gfapy.Line, string, version="gfa1")195 def test_set_version(self):196 string = "U\t1\tA B C"197 self.assertEqual("gfa2", gfapy.Line(string).version)198 self.assertEqual("gfa2", gfapy.Line(string, version="gfa2").version)199 self.assertRaises(gfapy.VersionError, gfapy.Line, string, version="gfa1")200 def test_unknown_record_version(self):...
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!!