Best Python code snippet using pytest-benchmark
test_bagit.py
Source:test_bagit.py
...13def setUpModule():14 ensure_tmpdir()15def tearDownModule():16 rmtmpdir()17def has_error(errs, label):18 return len([e for e in errs.failed() if e.label == label]) > 019 20class TestBagItValidator(test.TestCase):21 def setUp(self):22 self.tf = Tempfiles()23 self.bagdir = self.tf.track("mbag")24 shutil.copytree(bagdir, self.bagdir)25 26 self.bag = bag.NISTBag(self.bagdir)27 self.valid8 = val.BagItValidator()28 def tearDown(self):29 self.tf.clean()30 def test_all_test_methods(self):31 expect = ["test_"+m for m in32 "bagit_txt data_dir manifest tagmanifest baginfo fetch_txt".split()]33 expect.sort()34 meths = self.valid8.all_test_methods()35 meths.sort()36 self.assertEqual(meths, expect)37 def test_the_test_methods(self):38 all = ["test_"+m for m in39 "bagit_txt data_dir manifest tagmanifest baginfo fetch_txt".split()]40 all.sort()41 meths = self.valid8.the_test_methods()42 meths.sort()43 self.assertEqual(meths, all)44 45 config = {46 "skip_tests": ["test_"+m for m in "data_dir baginfo".split()]47 }48 self.valid8 = val.BagItValidator(config)49 expect = [t for t in all if t not in config['skip_tests']]50 expect.sort()51 meths = self.valid8.the_test_methods()52 meths.sort()53 self.assertEqual(meths, expect)54 config['include_tests']= ["test_"+m for m in "data_dir manifest".split()]55 self.valid8 = val.BagItValidator(config)56 expect = [t for t in all if t in config['include_tests']]57 expect.sort()58 meths = self.valid8.the_test_methods()59 meths.sort()60 self.assertEqual(meths, expect)61 def test_test_bagit_txt(self):62 errs = self.valid8.test_bagit_txt(self.bag)63 self.assertEqual(errs.count_failed(), 0,64 "False Positives: "+ str([str(e) for e in errs.failed()]))65 bagitf = os.path.join(self.bag.dir, "bagit.txt")66 with open(bagitf, 'w') as fd:67 print("BagIt-Version: 1.0", file=fd)68 errs = self.valid8.test_bagit_txt(self.bag)69 self.assertGreater(errs.count_failed(), 0)70 self.assertTrue(has_error(errs, "2.1.1-3"))71 self.assertTrue(has_error(errs, "2.1.1-4"))72 73 with open(bagitf, 'w') as fd:74 print("Tag-File-Character-Encoding: 1.0", file=fd)75 errs = self.valid8.test_bagit_txt(self.bag)76 self.assertGreater(len(errs.failed()), 0)77 self.assertTrue(has_error(errs, "2.1.1-2"))78 self.assertTrue(has_error(errs, "2.1.1-5"))79 os.remove(bagitf)80 errs = self.valid8.test_bagit_txt(self.bag)81 self.assertGreater(len(errs.failed()), 0)82 self.assertTrue(has_error(errs, "2.1.1-1"))83 def test_test_data_dir(self):84 errs = self.valid8.test_data_dir(self.bag)85 self.assertEqual(errs.failed(), [],86 "False Positives: "+ str([str(e) for e in errs.failed()]))87 shutil.rmtree(self.bag.data_dir)88 errs = self.valid8.test_data_dir(self.bag)89 self.assertGreater(len(errs.failed()), 0)90 self.assertTrue(has_error(errs, "2.1.2"))91 def test_test_manifest(self):92 errs = self.valid8.test_manifest(self.bag)93 self.assertEqual(errs.failed(), [],94 "False Positives: "+ str([str(e) for e in errs.failed()]))95 mf = os.path.join(self.bag.dir, "manifest-sha256.txt")96 with open(mf, 'a') as fd:97 print("sd9wh32 metadata/nerdm.json", file=fd)98 errs = self.valid8.test_manifest(self.bag)99 self.assertEqual(len(errs.failed()), 1)100 self.assertTrue(has_error(errs, "2.1.3-3"))101 with open(mf, 'a') as fd:102 print("blah", file=fd)103 print("a secret garden", file=fd)104 errs = self.valid8.test_manifest(self.bag)105 self.assertEqual(len(errs.failed()), 2)106 self.assertTrue(has_error(errs, "2.1.3-2"))107 os.remove(mf)108 errs = self.valid8.test_manifest(self.bag)109 self.assertEqual(len(errs.failed()), 1)110 self.assertTrue(has_error(errs, "2.1.3-1"))111 112 with open(os.path.join(bagdir, "manifest-sha256.txt")) as fd:113 lines = fd.readlines()114 lines.pop(0)115 line = lines.pop(0)116 lines.append("x9sx8lsd "+line.split()[1]+"\n")117 with open(mf, 'w') as fd:118 for line in lines:119 fd.write(line)120 print("sd8h2h20hgw data/trial3", file=fd)121 print("sd8h2h20hgw data/goober.txt", file=fd)122 errs = self.valid8.test_manifest(self.bag)123 self.assertEqual(len(errs.failed()), 4)124 self.assertTrue(has_error(errs, "2.1.3-7"))125 self.assertTrue(has_error(errs, "3-1-2"))126 self.assertTrue(has_error(errs, "2.1.3-4"))127 self.assertTrue(has_error(errs, "3-2-2"))128 self.valid8.cfg = {129 "test_manifest": {130 "check_checksums": False131 }132 }133 errs = self.valid8.test_manifest(self.bag)134 self.assertEqual(len(errs.failed()), 3)135 self.assertTrue(has_error(errs, "2.1.3-7"))136 self.assertTrue(has_error(errs, "3-1-2"))137 self.assertTrue(has_error(errs, "2.1.3-4"))138 139 def test_test_tagmanifest(self):140 errs = self.valid8.test_tagmanifest(self.bag)141 self.assertEqual(errs.failed(), [],142 "False Positives: "+ str([str(e) for e in errs.failed()]))143 mf = os.path.join(self.bag.dir, "tagmanifest-sha256.txt")144 with open(mf, 'a') as fd:145 print("blah", file=fd)146 print("a secret garden", file=fd)147 errs = self.valid8.test_tagmanifest(self.bag)148 self.assertEqual(len(errs.failed()), 2, "Unexpected # of errors: [\n " +149 "\n ".join([str(e) for e in errs.failed()]) + "\n]")150 self.assertTrue(has_error(errs, "2.1.3-2"))151 self.assertTrue(has_error(errs, "3-1-2"))152 os.remove(mf)153 errs = self.valid8.test_tagmanifest(self.bag)154 self.assertEqual(len(errs.failed()), 0, "Unexpected # of errors: [\n " +155 "\n ".join([str(e) for e in errs.failed()]) + "\n]")156 157 with open(os.path.join(bagdir, "manifest-sha256.txt")) as fd:158 lines = fd.readlines()159 lines.pop(0)160 line = lines.pop(0)161 lines.append("x9sx8lsd "+line.split()[1]+"\n")162 with open(mf, 'w') as fd:163 for line in lines:164 fd.write(line)165 print("sd8h2h20hgw data/trial3", file=fd)166 print("sd8h2h20hgw data/goober.txt", file=fd)167 errs = self.valid8.test_tagmanifest(self.bag)168 self.assertEqual(len(errs.failed()), 3, "Unexpected # of errors: [\n " +169 "\n ".join([str(e) for e in errs.failed()]) + "\n]")170 self.assertTrue(has_error(errs, "2.1.3-7"))171 self.assertTrue(has_error(errs, "3-1-2"))172 self.assertTrue(has_error(errs, "3-2-2"))173 self.valid8.cfg = {174 "test_manifest": {175 "check_checksums": False176 }177 }178 errs = self.valid8.test_manifest(self.bag)179 self.assertEqual(len(errs.failed()), 0)180 181 def test_test_baginfo(self):182 errs = self.valid8.test_baginfo(self.bag)183 self.assertEqual(errs.failed(), [],184 "False Positives: "+ str([str(e) for e in errs.failed()]))185 bif = os.path.join(self.bag.dir, "bag-info.txt")186 with open(bif, 'a') as fd:187 print("Incorrect", file=fd)188 errs = self.valid8.test_baginfo(self.bag)189 self.assertEqual(len(errs.failed()), 1)190 self.assertTrue(has_error(errs, "2.2.2-2"))191 self.assertEqual(errs.failed()[0].type, val.ERROR)192 193 os.remove(bif)194 errs = self.valid8.test_baginfo(self.bag)195 self.assertEqual(len(errs.failed()), 1)196 self.assertTrue(has_error(errs, "2.2.2-1"))197 self.assertEqual(errs.failed()[0].type, val.REC)198 199 with open(bif, 'w') as fd:200 print(" Incorrect", file=fd)201 print("name: value", file=fd)202 print("Bagging-Date: 2017", file=fd)203 print("Bag-Size: 2017", file=fd)204 errs = self.valid8.test_baginfo(self.bag)205 self.assertEqual(len(errs.failed()), 3, "Unexpected # of errors: [\n " +206 "\n ".join([str(e) for e in errs.failed()]) + "\n]")207 self.assertTrue(has_error(errs, "2.2.2-2"))208 self.assertTrue(has_error(errs, "2.2.2-Bagging-Date"))209 self.assertTrue(has_error(errs, "2.2.2-Bag-Size"))210 self.assertEqual(errs.failed()[0].type, val.ERROR)211 def test_test_fetch_txt(self):212 errs = self.valid8.test_fetch_txt(self.bag)213 self.assertEqual(errs.failed(), [],214 "False Positives: "+ str([str(e) for e in errs.failed()]))215 ff = os.path.join(self.bag.dir, "fetch.txt")216 with open(ff, 'a') as fd:217 print("Incorrect", file=fd)218 print("Incorrect file", file=fd)219 print("data/goober http://goober.net/get 20", file=fd)220 print("data/goober 20 http://goober.net/get", file=fd)221 222 errs = self.valid8.test_fetch_txt(self.bag)223 self.assertEqual(len(errs.failed()), 3, "Unexpected # of errors: [\n " +224 "\n ".join([str(e) for e in errs.failed()]) + "\n]")225 self.assertTrue(has_error(errs, "2.2.3-1"))226 self.assertTrue(has_error(errs, "2.2.3-2"))227 self.assertTrue(has_error(errs, "2.2.3-3"))228 self.assertEqual(errs.failed()[0].type, val.ERROR)229 os.remove(ff)230 errs = self.valid8.test_fetch_txt(self.bag)231 self.assertEqual(errs.failed(), [],232 "False Positives: "+ str([str(e) for e in errs.failed()]))233 def test_validate(self):234 errs = self.valid8.validate(self.bag)235 self.assertEqual(errs.failed(), [],236 "False Positives: "+ str([str(e) for e in errs.failed()]))237 # Mess up bag to see if all tests are getting run238 bagitf = os.path.join(self.bag.dir, "bagit.txt")239 with open(bagitf, 'w') as fd:240 print("BagIt-Version: 1.0", file=fd)241 shutil.rmtree(self.bag.data_dir)242 mf = os.path.join(self.bag.dir, "tagmanifest-sha256.txt")243 with open(mf, 'a') as fd:244 print("alskdf", file=fd)245 bif = os.path.join(self.bag.dir, "bag-info.txt")246 with open(bif, 'a') as fd:247 print("Incorrect", file=fd)248 ff = os.path.join(self.bag.dir, "fetch.txt")249 with open(ff, 'a') as fd:250 print("Incorrect", file=fd)251 252 errs = self.valid8.validate(self.bag)253 self.assertGreater(len(errs.failed()), 0)254 self.assertTrue(has_error(errs, "2.1.1-3"))255 self.assertTrue(has_error(errs, "2.1.1-4"))256 self.assertTrue(has_error(errs, "2.1.1-4"))257 self.assertTrue(has_error(errs, "2.2.3-1"))258 self.assertTrue(has_error(errs, "2.2.2-2"))259 self.assertTrue(has_error(errs, "2.1.2"))260 self.assertTrue(has_error(errs, "2.1.3-2"))261 self.assertTrue(has_error(errs, "3-1-2"))262 263 264 265class TestFmts(test.TestCase):266 def test_email(self):267 self.assertTrue(val._fmt_email("raymond.plante@nist.gov"))268 self.assertTrue(val._fmt_email("ray10171964@gmail.com"))269 self.assertFalse(val._fmt_email("rlp3@nist"))270 self.assertFalse(val._fmt_email("rlp3nist"))271 self.assertFalse(val._fmt_email("ra#964@gmail.com"))272 def test_date(self):273 self.assertTrue(val._fmt_date("2017-10-19"))274 self.assertTrue(val._fmt_date("1907-01-19"))275 self.assertTrue(val._fmt_date("2007-12-20"))...
admin.py
Source:admin.py
1from django.contrib import admin2from monitor.models import Metric, MetricDataInt, MetricDataChar,\3 MetricDataFloat, MetricDataDatetime, MetricDataJSON4# MetricDataTimeDelta, Source, Instrument5class MetricDataIntInline(admin.TabularInline):6 model = MetricDataInt7 fields = ('value', 'time', 'has_error', 'error_message',)8 readonly_fields = ('value', 'time', 'has_error', 'error_message',)9 ordering = ('-time', )10 extra = 011 def has_delete_permission(self, request, obj=None):12 return False13 def has_add_permission(self, request, obj=None):14 return False15class MetricDataFloatInline(admin.TabularInline):16 model = MetricDataFloat 17 fields = ('value', 'time', 'has_error', 'error_message', )18 readonly_fields = ('value', 'time', 'has_error', 'error_message', )19 ordering = ('-time', )20 extra = 021 def has_delete_permission(self, request, obj=None):22 return False23 def has_add_permission(self, request, obj=None):24 return False25class MetricDataCharInline(admin.TabularInline):26 model = MetricDataChar 27 fields = ('value', 'time', 'has_error', 'error_message', )28 readonly_fields = ('value', 'time', 'has_error', 'error_message', )29 ordering = ('-time', )30 extra = 031 def has_delete_permission(self, request, obj=None):32 return False33 def has_add_permission(self, request, obj=None):34 return False35class MetricDataDatetimeInline(admin.TabularInline):36 model = MetricDataDatetime 37 fields = ('value', 'time', 'has_error', 'error_message', )38 readonly_fields = ('value', 'time', 'has_error', 'error_message', )39 ordering = ('-time', )40 extra = 041 def has_delete_permission(self, request, obj=None):42 return False43 def has_add_permission(self, request, obj=None):44 return False45class MetricDataJSONInline(admin.TabularInline):46 model = MetricDataJSON47 fields = ('value', 'time', 'has_error', 'error_message', )48 readonly_fields = ('value', 'time', 'has_error', 'error_message', )49 ordering = ('-time', )50 extra = 051 def has_delete_permission(self, request, obj=None):52 return False53 def has_add_permission(self, request, obj=None):54 return False55class MetricAdmin(admin.ModelAdmin):56 def __init__(self, *args, **kwargs):57 super(MetricAdmin, self).__init__(*args, **kwargs)58 # select inlines dynamically here : HOW?59 60 fields = (61 ('name', 'owner', 'slug', ),62 ('doc', ),63 ('latest_value', 'latest_time', 'latest_tags', 'has_error',64 'error_message'),65 ('warning_if_no_value_after_seconds', ),66 ('unit', 'dashboard_display_option', ),67 ('value_type', ),68 ('alert_operator', 'alert_value', 'alert_triggered', ),69 ('expression_string', 'expression_evaluation'),70 ('dashboard_display_window_length_days', ),71 ('timestamp_modified', 'timestamp_created', ),72 )73 prepopulated_fields = {'slug': ('name', ), }74 75 search_fields = ('name', 'doc', 'latest_tags', )76 list_display = ( 'name', 'owner', 'dashboard_display_option', 'latest_value',77 'latest_time', 'has_error', 'alert_triggered')78 79 readonly_fields = ('alert_triggered', 'timestamp_modified',80 'timestamp_created', 'latest_time', 'latest_value', 'latest_tags',81 'has_error', 'error_message', 'expression_evaluation')82 83# inlines = (MetricDataIntInline, MetricDataFloatInline,84# MetricDataCharInline, MetricDataDatetimeInline,85# MetricDataJSONInline, )86 87admin.site.register(Metric, MetricAdmin)88class MetricDataIntAdmin(admin.ModelAdmin):89 fields = (90 ('metric', ), 91 ('value', 'time', ), 92 ('tags', ), 93 ('has_error', 'error_message', ), 94 )95 list_display = ('metric', 'value', 'time', 'has_error', )96 list_filter = ('has_error', 'metric', )97 readonly_fields = ('timestamp_modified', 'timestamp_created', )98 99admin.site.register(MetricDataInt, MetricDataIntAdmin)100class MetricDataFloatAdmin(admin.ModelAdmin):101 fields = (102 ('metric', ), 103 ('value', 'time', ), 104 ('tags', ), 105 ('has_error', 'error_message', ), 106 )107 list_display = ('metric', 'value', 'time', 'has_error', )108 list_filter = ('has_error', 'metric', )109 readonly_fields = ('timestamp_modified', 'timestamp_created', )110admin.site.register(MetricDataFloat, MetricDataFloatAdmin)111class MetricDataCharAdmin(admin.ModelAdmin):112 fields = (113 ('metric', ), 114 ('value', 'time', ), 115 ('tags', ), 116 ('has_error', 'error_message', ), 117 )118 list_display = ('metric', 'value', 'time', 'has_error', )119 list_filter = ('has_error', 'metric', )120 readonly_fields = ('timestamp_modified', 'timestamp_created', )121admin.site.register(MetricDataChar, MetricDataCharAdmin)122class MetricDataDatetimeAdmin(admin.ModelAdmin):123 fields = (124 ('metric', ), 125 ('value', 'time', ), 126 ('tags', ), 127 ('has_error', 'error_message', ), 128 )129 list_display = ('metric', 'value', 'time', 'has_error', )130 list_filter = ('has_error', 'metric', )131 readonly_fields = ('timestamp_modified', 'timestamp_created', )132admin.site.register(MetricDataDatetime, MetricDataDatetimeAdmin)133class MetricDataJSONAdmin(admin.ModelAdmin):134 fields = (135 ('metric', ), 136 ('value', 'time', ), 137 ('tags', ), 138 ('has_error', 'error_message', ), 139 )140 list_display = ('metric', 'value', 'time', 'has_error', )141 list_filter = ('has_error', 'metric', )142 readonly_fields = ('timestamp_modified', 'timestamp_created', )...
deltabar.py
Source:deltabar.py
1import ac2import os3import platform4import sys5import time6import traceback7# Nothing interesting here. See deltabar_lib/deltabar_lib.py8#9# This is the entry point for the Assetto Corsa API,10# we wrap the API calls with some error checking and farm it11# out to our actual implementation.12#13# This wrapper allows us to replace the deltabar_lib module14# with a new version while AC is running; AC will still call15# the endpoints here but we can switch over to the new module version.16has_error = False17logged_errors = []18def log_error():19 msg = 'Exception: {}\n{}'.format(time.asctime(), traceback.format_exc())20 ac.log(msg)21 ac.console(msg)22 logged_errors.append(msg)23def get_lib_dir():24 if platform.architecture()[0] == '64bit':25 return 'lib64'26 else:27 return 'lib'28# Fix import path for sim_info ctypes29lib_dir = 'apps/python/deltabar/{}'.format(get_lib_dir())30sys.path.insert(0, lib_dir)31os.environ['PATH'] += ';.'32class DeltaBarData:33 pass34# Important to do this before importing deltabar_lib.35deltabar_data = DeltaBarData()36try:37 from deltabar_lib.deltabar_lib import deltabar_app38except:39 log_error()40 raise41def acMain(ac_version):42 try:43 deltabar_data.app_id = ac.newApp('deltabar')44 ac.addRenderCallback(deltabar_data.app_id, onRender)45 return deltabar_app.acMain(ac_version)46 except:47 log_error()48 raise49def acUpdate(delta_t):50 global has_error51 if has_error:52 return53 try:54 deltabar_app.acUpdate(delta_t)55 except:56 log_error()57 has_error = True58def acShutdown():59 global has_error60 if has_error:61 return62 try:63 deltabar_app.acShutdown()64 except:65 log_error()66 has_error = True67def onRender(delta_t):68 global has_error69 if has_error:70 return71 try:72 deltabar_app.onRender(delta_t)73 except:74 log_error()75 has_error = True76def onClick(*args, **kwargs):77 global has_error78 if has_error:79 return80 try:81 deltabar_app.onClick()82 except:83 log_error()...
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!!