Best Python code snippet using autotest_python
unittests.py
Source:unittests.py  
...44		self.tests = []45		self.except_tests = []46		self.previous_result_of_ctor_on_storage = None47	48	def add_test(self, args, results, raw=False):49		if raw:50			expected = results51		else:52			expected = []53			for er in results:54				expected += utils.pack(er, 0x20)55		args = self.prepare_args(args)56		self.tests.append((args, expected))57	58	def prepare_args(self, args):59		raw = []60		raw += utils.pack(args[0], 4)61		for a in args[1:]:62			raw += utils.pack(a, 32)63		return raw64	65	def add_except_test(self, args, exceptions):66		args = self.prepare_args(args)67		self.except_tests.append((args, exceptions))68	69	def ctor_sanity_checks(self, interp, result):70		# calling the constructor should yield the deployed bytecode71		assert (result.bytes() == [ord(c) for c in self.deployed_bytecode])72		feedback(True)73		# the constructor should have the same effect on storage every time74		stor = {k: v.bytes() for k, v in interp.storage.items()}75		if self.previous_result_of_ctor_on_storage is None:	76			self.previous_result_of_ctor_on_storage = stor77		else:78			assert (stor == self.previous_result_of_ctor_on_storage)79	80	def do_run_tests(self, contract):81		interp = interpreter.make_interpreter(contract)82		if self.is_deployment_contract:83			result = interp.call(self.ctor_args)84			self.ctor_sanity_checks(interp, result)85			return86		# initialize storage etc. by calling the constructor87		if isinstance(contract, Contract) and self.deployment_contract:88			interp.call_ctor(self.deployment_contract, self.ctor_args)89		if isinstance(contract, absyn.Contract) and self.deployment_ast:90			interp.call_ctor(self.deployment_ast, self.ctor_args)91		if self.step_limit:92			interp.step_limit = self.step_limit93		for args, expected in self.tests:94			value = interp.call(args)95			actual = value.bytes()96			if actual == expected:97				feedback(True)98			else:99				feedback(False)100				print("Expected: %s" % str(expected))101				print("Got: %s" % str(actual))102				raise UnitTestFailedException()103		for args, expected_exceptions in self.except_tests:104			try:105				interp.call(args)106			except Exception, e:107				if e.__class__ in expected_exceptions:108					feedback(True)109				else:110					feedback(False)111					print("Got a wrong exception type:")112					print(e.__class__)113					raise UnitTestFailedException()114	115	def run(self):116		sys.stdout.write(self.filename + ": ")117		sys.stdout.flush()118		d = decompiler.Decompiler()119		d.set_optimization_hook(self.do_run_tests)120		contract, ast, code = d.decompile_raw(self.bytecode)121		feedback(True) # it decompiled OK122		if self.is_deployment_contract:123			self.is_deployment_contract = False124			self.deployment_contract = contract125			self.deployment_ast = ast126			contract, ast, code = d.decompile_raw(self.deployed_bytecode)127			feedback(True) # it decompiled OK128			sys.stdout.write(" (%d functions)" % len(contract.functions))129		print("")130def pack_string_ref(s):131	vs = []132	vs += interpreter.Value(0x20).bytes() # mem ref: next word.133	vs += interpreter.Value(len(s)).bytes() # string length134	for c in s:135		vs.append(ord(c))136	while len(vs) % 0x20 != 0:137		vs.append(0)138	return vs139# for printing our unit tests so we can use it in our report..140#total_num_tests = 0141#class Tester:142#	def __init__(self, contract_name, _, step_limit=None):143#		self.cn = contract_name144#		self.num_tests = 0145#	def new_test(self):146#		global total_num_tests147#		total_num_tests += 1148#		self.num_tests += 1149#	def add_test(self, _, __, raw=None):150#		self.new_test()151#	def add_except_test(self, _, __):152#		self.new_test()153#	def run(self):154#		if self.num_tests != 0:155#			print("%s & %d & description \\\\" % (self.cn, self.num_tests))156		157def run_all_tests():158	tester = Tester("Minimal.json", [])159	tester.add_test([0xc2985578], [0x123])160	tester.add_except_test([0xdeadc0de], 161						   [interpreter.RevertException,162						    interpreter.AssertionFailureException])163	tester.run()164	Tester("eval1.bc", []).run()165	Tester("eval2.bc", []).run()166	Tester("eval3.bc", []).run()167	tester = Tester("ArgOrder.json", [])168	tester.add_test([0x13d1aa2e, 1, 2], [2, 6])169	tester.run()170	tester = Tester("Multicall.json", [])171	tester.add_test([0xb3de648b, 0], [30])172	tester.add_test([0xb3de648b, 1], [40])173	tester.add_test([0xb3de648b, 2], [50])174	tester.add_test([0xb3de648b, 3], [60])175	tester.add_test([0xcb97492a, 0], [1])176	tester.add_test([0xcb97492a, 1], [15])177	tester.add_test([0xcb97492a, 2], [29])178	tester.add_test([0xcb97492a, 3], [43])179	tester.run()180	# TODO re-enable (and update to new values)181	#tester = Tester("SmallExample.json", [])182	#tester.add_test([0x8a9d0e05, 0], [0])183	#tester.add_test([0x8a9d0e05, 1], [2])184	#tester.add_test([0x8a9d0e05, 2], [4])185	#tester.run()186	tester = Tester("Multiargs.json", [])187	tester.add_test([0xbf06dbf1, 0x1, 0x2, 0x3], [0x1*0x2*0x3*0x11])188	tester.run()189	190	tester = Tester("Multiret.json", [])191	tester.add_test([0xb3de648b, 0x1], [0x1*0x11, 0x1*0x22])192	tester.add_test([0xb3de648b, 0x2], [0x2*0x11, 0x2*0x22])193	tester.add_test([0xb3de648b, 0x0], [0, 0])194	tester.run()195	tester = Tester("FourSimple.json", [])196	tester.add_test([0x6482e626, 0], [0]) # d197	tester.add_test([0x6482e626, 1], [1]) # d198	tester.add_test([0xc3da42b8], [0x11*0x22, 1])199	tester.run()200	tester = Tester("Mapping.json", [])201	tester.add_test([0x9507d39a, 0], [0])202	tester.add_test([0x2f30c6f6, 0, 0xc0ffee], []) # set m[0]=n[0]=0xc0ffee203	tester.add_test([0x9507d39a, 0], [0xc0ffee])204	tester.add_test([0x9507d39a, 1], [0])205	tester.add_test([0x2f30c6f6, 0, 0xdedede], [])206	tester.add_test([0x9507d39a, 0], [0xdedede])207	tester.add_test([0x2f30c6f6, 101, 0xfff], [])208	tester.add_test([0x9507d39a, 101], [0xfff])209	tester.run()210	# 0x76febb7e is the getter, 0xcf0d6774 is the setter211	tester = Tester("Array.json", [])212	tester.add_test([0x76febb7e, 9], [0])213	tester.add_test([0x76febb7e, 0], [0])214	tester.add_except_test([0x76febb7e, 10], 215						   [interpreter.RevertException,216						    interpreter.AssertionFailureException])217	tester.add_test([0x76febb7e, 1], [0])218	tester.add_test([0xcf0d6774, 123], [])219	tester.add_test([0x76febb7e, 0], [123])220	tester.add_test([0xcf0d6774, 321], [])221	tester.add_test([0x76febb7e, 0], [123])222	tester.add_test([0x76febb7e, 1], [321])223	tester.run() 224	tester = Tester("String.json", [])225	tester.add_test([0x1c008df9, 1], pack_string_ref("one"), raw=True)226	tester.add_test([0x1c008df9, 2], pack_string_ref("two"), raw=True)227	tester.add_test([0x1c008df9, 3], pack_string_ref("foo"), raw=True)228	tester.add_test(229		[230			0xbe161a2a, 231			1,  # x232			0x60,  # memref: s. Why not 0x64? No clue.233			0xa0,  # memref: r234			# s:235			1, # length236			0x41 << (256-8), # "A"237			# r:238			1, # length239			0x42 << (256-8), # "B"240		], 241		pack_string_ref("A"), 242		raw=True243	)244	tester.add_test(245		[246			0xbe161a2a, 247			1,  # x248			0x60,  # memref: s. Why not 0x64? No clue.249			0xa0,  # memref: r250			# s:251			1, # length252			0x42 << (256-8), # "A"253			# r:254			1, # length255			0x42 << (256-8), # "B"256		], 257		pack_string_ref("[::]URL:;._.=//**-+thisisAstringThatContaINsManyInvalidOPC0des!![]/\\()##%&/(-<-,"), 258		raw=True259	)260	tester.run()261	tester = Tester("Endless.json", [])262	tester.add_test([0x4c970b2f, 0x11], [368])263	tester.add_test([0x4c970b2f, 0x33], [464])264	tester.add_test([0x4c970b2f, 0x66], [63240])265	tester.add_test([0x4c970b2f, 0x77], [589])266	tester.add_except_test([0x4c970b2f, 0], [interpreter.OutOfGasException])267	tester.run()268	tester = Tester("Loop.json", [])269	tester.add_except_test([0xc2985578], [interpreter.OutOfGasException])270	tester.run()271	tester = Tester("NestedLoops.json", [])272	tester.add_test([0xc2985578], [1920])273	tester.run()274	tester = Tester("PostTestedLoop.json", [])275	tester.add_test([0x4c970b2f, 1], [307])276	tester.add_test([0x4c970b2f, 2], [308])277	tester.add_test([0x4c970b2f, 0x23], [307])278	tester.add_test([0x4c970b2f, 0x24], [291])279	tester.run()280	tester = Tester("SmallTypes.json", [])281	tester.add_test([0x1c008df9, 0], [0])282	tester.add_test([0x1c008df9, 3], [9])283	tester.add_test([0x1c008df9, -1], [0])284	tester.add_test([0x1c008df9, 50], [-106])285	tester.run()286	tester = Tester("SmallTypes2.json", [])287	tester.add_test([0x7877b803, 1], [0x11])288	tester.add_test([0x7877b803, 50], [-106])289	tester.run()290	tester = Tester("GT.json", [])291	tester.add_test([0xb3de648b, 0x12], [0x44])292	tester.add_test([0xb3de648b, 0x11], [0x11])293	tester.run()294	tester = Tester("Log.json", [])295	tester.add_test([0x26121ff0], [])296	tester.run()297	tester = Tester("Neg.json", [])298	tester.add_test([0xb3de648b, (2**256)-1], [0])299	tester.add_test([0xb3de648b, 0], [-1])300	tester.add_test([0xb3de648b, 1], [-2])301	tester.run()302	tester = Tester("Storage.json", [])303	tester.add_test([0xb3de648b, 0x123], [0x123])304	tester.add_test([0xb3de648b, 1], [1])305	tester.add_test([0xb3de648b, 0], [0x1234]) # note: this changes storage306	tester.add_test([0xb3de648b, 0], [0])307	tester.add_test([0xb3de648b, 1], [0x1234])308	tester.run()309	tester = Tester("NonCom.json", [])310	tester.add_test([0x29688a80, 2], [2]) # &2311	tester.add_test([0x29688a80, 1], [0]) # &2312	tester.add_test([0x29688a80, 0], [0]) # &2313	tester.add_test([0x29688a80, 3], [2]) # &2314	tester.add_test([0x7ece3246, 3], [9]) # **2315	tester.add_test([0x7ece3246, 2], [4]) # **2316	tester.add_test([0xb3de648b, 2], [0]) # /3317	tester.add_test([0xb3de648b, 3], [1]) # /3318	tester.add_test([0xb3de648b, 5], [1]) # /3319	tester.add_test([0xb3de648b, 6], [2]) # /3320	tester.add_test([0xcb97492a, 0], [0]) # %3321	tester.add_test([0xcb97492a, 1], [1]) # %3322	tester.add_test([0xcb97492a, 2], [2]) # %3323	tester.add_test([0xcb97492a, 3], [0]) # %3324	tester.add_test([0xe420264a, 0xff], [0xee]) # &0xee325	tester.add_test([0xe420264a, 0x12], [0x02]) # &0xee326	tester.add_test([0xe420264a, 0x32], [0x22]) # &0xee327	tester.run()328	tester = Tester("TailCall.json", [])329	tester.add_test([0xb3de648b, 0], [4])330	tester.add_test([0xb3de648b, 1], [6])331	tester.add_test([0xb3de648b, 2], [8])332	tester.add_test([0xcb97492a, 0], [3])333	tester.add_test([0xcb97492a, 1], [4])334	tester.run()335	tester = Tester("NestedIfElse.json", [])336	tester.add_test([0x13d1aa2e, 1, 1], [2])337	tester.add_test([0x13d1aa2e, 1, 2], [3])338	tester.add_test([0x13d1aa2e, 2, 1], [4])339	tester.add_test([0x13d1aa2e, 2, 2], [5])340	tester.run()341	tester = Tester("IfElseSame.json", [])342	tester.add_test([0xb3de648b, 0], [2])343	tester.add_test([0xb3de648b, 1], [1])344	tester.add_test([0xb3de648b, 2], [2])345	tester.run()346	tester = Tester("TryToBreak.json", [], step_limit=300)347	tester.add_test([0x29688a80, 3], [15])348	tester.add_test([0x29688a80, 0], [2])349	tester.add_test([0x29688a80, 4], [12])350	tester.add_test([0x443ddba2, 42], [1])351	tester.add_test([0x443ddba2, 43], [2])352	tester.add_test([0x54bb1361, 0], [12])353	tester.add_test([0x54bb1361, 1], [3])354	tester.add_test([0x54bb1361, 2], [4])355	tester.add_test([0x54bb1361, 3], [6])356	tester.add_test([0x54bb1361, 4], [8])357	tester.add_test([0x54bb1361, 5], [12])358	tester.add_test([0xb582ec5f], [])359	tester.add_test([0xcb97492a, 0], [0])360	tester.add_test([0xcb97492a, 2], [2])361	tester.add_test([0xcb97492a, 6], [6])362	tester.add_except_test([0xcb97492a, 1], [interpreter.OutOfGasException])363	tester.add_except_test([0xcb97492a, 4], [interpreter.OutOfGasException])364	tester.add_except_test([0xcb97492a, 8], [interpreter.OutOfGasException])365	tester.add_test([0xe420264a, 0], [-8])366	tester.add_test([0xe420264a, 1], [12])367	tester.add_test([0xe420264a, 2], [34])368	tester.add_test([0xe420264a, 3], [78])369	tester.add_test([0xe420264a, 4], [-8])370	tester.run()371	tester = Tester("BlackjackTipJar.json", [])372	# TODO add tests.373	tester.run()374	Tester("mystery.bc", []).run()375	Tester("Bytes.json", []).run()376	Tester("Struct.json", []).run()377	#Tester("etherscan1.bc", []).run()378	Tester("etherscan2.bc", []).run()379	Tester("misc.bc", []).run()380	#tester = Tester("Exchange.json", 381	#	utils.pack(0x34767f3c519f361c5ecf46ebfc08981c629d381, 32))382	#tester.run()383	######### Slow tests below #######...testSchemas.py
Source:testSchemas.py  
...50		parsed = etree.parse(handler)51		#Close schema file52		handler.close()53		return parsed54	def add_test(self , test_name , xml_path , error_class , parsed_xml = None):55		"""56		Add test case to object.57		test_name - Way of identifiying the test58		xml_path - Path to xml test file59		error_class - What sort of error that is going to be thrown. If error_class == None, it is assumed that the test will pass without raising exceptions.60		parsed_xml - Add the etree of the XML if available.61		"""62		test_set = (test_name , xml_path , error_class , parsed_xml)63		self.__test_set_list.append(test_set)64	def parse_and_add_directory(self, list_of_root_tags , directory):65		"""66		Parses through directory and all subdirectories and adds tests to object test lists67		list_of_root_tags - list of root tags to check for68		directory - directory to look in69		"""70		#Check if directory exists and list_of_root_tags isn't empty71		if len(list_of_root_tags) == 0:72			raise Exception("{} : List of root tags empty in parse_and_add_directory!".format(self.__schema_name))73		if not os.path.isdir(directory):74			raise Exception("{} : Directory {} does not exist in parse_and_add_directory!".format(self.__schema_name , directory))75		for subdir, dirs, files in os.walk(directory):76			for file in files:77				if file.upper().endswith(".XML"):78					try:79						new_path = os.path.join(subdir, file)80						parsed = self.__get_parsed_relaxng(new_path)81						root_tag = parsed.getroot().tag82						if root_tag in list_of_root_tags:83							self.add_test("Path Added: " + file , new_path , None , parsed_xml = parsed)84					except:85						pass86	def run_all_tests(self):87		"""88		Runs all the tests consecutivley.89		"""90		for index in range(len(self.__test_set_list)):91			self.run_test(index)92	def get_test_amount(self):93		"""94		Returns the amount of tests in the object.95		"""96		return len(self.__test_set_list)97	def run_test(self, index):98		"""99		Runs test of index100		"""101		if index >= len(self.__test_set_list):102			raise Exception("Illegal index was accessed")103		test_set = self.__test_set_list[index]104		xml_parsed = test_set[3]105		if not xml_parsed:106			self.__validate_file(test_set[1] , "XML")107			xml_file_handler = file(test_set[1] , 'r')108			xml_parsed = etree.parse(xml_file_handler)109			xml_file_handler.close()110		if test_set[2]:111			with pytest.raises(test_set[2]) as excinfo:112				self.__compiled.assertTrue(xml_parsed)113				if excinfo:114					print("Schema " + self.__schema_name + " failed validating the current file.")115					print("\n")116					print(test_set[0] + " raised the wrong exception or passed, when fail was expected (Exception " + str(test_set[2]) +  ".")117					print("File path - " + test_set[1])118					print(excinfo)119					print("\n")120					raise121		else:122			try:123				self.__compiled.assertTrue(xml_parsed)124			except:125				print("Schema " + self.__schema_name + " failed validating the current file.")126				print("\n")127				print(test_set[0] + " raised an exception but was supposed to pass.")128				print("File path - " + test_set[1])129				print("\n")130				raise131	def print_header(self):132		"""133		Prints a header string for a schema_test object.134		"""135		print(("\nTesting {} - {}\n".format(self.__schema_name , self.__schema_path)))136def setup():137	"""138	Sets up and returns test_list, which is a set of schema_test objects.139	"""140	test_list = []141	#Create schema object142	topology_test = schema_test("Topology" , "ISF/topology_schema.rng")143	component_test = schema_test("Component" , "ISF/component_schema.rng")144	command_test = schema_test("Command" , "ISF/command_schema.rng")145	parameter_test = schema_test("Parameter" , "ISF/parameters_schema.rng")146	channel_test = schema_test("Channel" , "ISF/channel_schema.rng")147	interface_test = schema_test("Interface" , "ISF/interface_schema.rng")148	serializable_test = schema_test("Serializable" , "ISF/serializable_schema.rng")149	event_test = schema_test("Event" , "ISF/event_schema.rng")150	#Declare schema tests151	channel_test.add_test("All working" , "sample_XML_files/channel/allWorking.xml" , None)152	channel_test.add_test("High Orange string instead of number" , "sample_XML_files/channel/colorString.xml" , AssertionError)153	channel_test.add_test("Missing comments" , "sample_XML_files/channel/missingComments.xml" , AssertionError)154	channel_test.add_test("Missing data type" , "sample_XML_files/channel/missingDataType.xml" , AssertionError)155	channel_test.add_test("Missing enum" , "sample_XML_files/channel/missingEnum.xml" , AssertionError)156	command_test.add_test("All working" , "sample_XML_files/command/allWorking.xml" , None)157	command_test.add_test("Command size is negative" , "sample_XML_files/command/negativeCommandSize.xml" , AssertionError)158	command_test.add_test("Enum missing when type ENUM is specified" , "sample_XML_files/command/missingEnum.xml" , AssertionError)159	command_test.add_test("Kind not sync nor async" , "sample_XML_files/command/kindMixed.xml" , AssertionError)160	command_test.add_test("String size not defined" , "sample_XML_files/command/noStringSize.xml" , AssertionError)161	component_test.add_test("Base all working" , "sample_XML_files/component/baseAllWorking.xml" , None)162	component_test.add_test("Complex all working" , "sample_XML_files/component/complexAllWorking.xml" , None)163	component_test.add_test("No ports" , "sample_XML_files/component/noPorts.xml" , AssertionError)164	component_test.add_test("<Interface> tag instead of <internal_interface> tag" , "sample_XML_files/component/interfaceOnly.xml" , AssertionError)165	event_test.add_test("All working" , "sample_XML_files/event/allWorking.xml" , None)166	event_test.add_test("Event throttle negative" , "sample_XML_files/event/negativeThrottle.xml" , AssertionError)167	event_test.add_test("Formot string missing" , "sample_XML_files/event/missingFormatString.xml" , AssertionError)168	event_test.add_test("Severity not valid" , "sample_XML_files/event/unknownSeverity.xml" , AssertionError)169	interface_test.add_test("All working" , "sample_XML_files/interface/allWorking.xml" , None)170	interface_test.add_test("Multiple return tags" , "sample_XML_files/interface/multipleReturns.xml" , AssertionError)171	interface_test.add_test("No ENUM in return type ENUM" , "sample_XML_files/interface/noEnumInReturn.xml" , AssertionError)172	interface_test.add_test("No return tags" , "sample_XML_files/interface/noReturns.xml" , None)173	interface_test.add_test("Priority attribute is a string" , "sample_XML_files/interface/stringPriority.xml" , AssertionError)174	parameter_test.add_test("All working" , "sample_XML_files/parameter/allWorking.xml" , None)175	parameter_test.add_test("Float in native integer type default attribute" , "sample_XML_files/parameter/floatInInt.xml" , AssertionError)176	parameter_test.add_test("Negative in unsigned 8 bit default attribute" , "sample_XML_files/parameter/negativeInUnsigned.xml" , AssertionError)177	parameter_test.add_test("No size attribute for string type attribute" , "sample_XML_files/parameter/noStringSize.xml" , AssertionError)178	parameter_test.add_test("String in 32 bit float attribute" , "sample_XML_files/parameter/stringInFloat.xml" , AssertionError)179	serializable_test.add_test("All working" , "sample_XML_files/serializable/allWorking.xml" , None)180	serializable_test.add_test("Multiple members tags" , "sample_XML_files/serializable/multipleMembers.xml" , AssertionError)181	serializable_test.add_test("No members tag" , "sample_XML_files/serializable/noMembers.xml" , AssertionError)182	serializable_test.add_test("No name in root" , "sample_XML_files/serializable/noName.xml" , AssertionError)183	serializable_test.add_test("No type in member" , "sample_XML_files/serializable/noType.xml" , AssertionError)184	topology_test.add_test("All working" , "sample_XML_files/topology/allWorking.xml" , None)185	topology_test.add_test("Negative connection number." , "sample_XML_files/topology/negativeConnectionNumber.xml" , AssertionError)186	topology_test.add_test("No Imports" , "sample_XML_files/topology/noImports.xml" , None)187	topology_test.add_test("No connections made" , "sample_XML_files/topology/noConnections.xml" , AssertionError)188	topology_test.add_test("No instances" , "sample_XML_files/topology/noInstances.xml" , AssertionError)189	topology_test.add_test("No types" , "sample_XML_files/topology/noTypes.xml" , AssertionError)190	#Add more schema tests191	channel_test.parse_and_add_directory(["telemetry"] , '../test')192	command_test.parse_and_add_directory(["commands"] , '../test')193	component_test.parse_and_add_directory(["component"] , '../test')194	event_test.parse_and_add_directory(["events"] , '../test')195	interface_test.parse_and_add_directory(["interface" , "port"] , '../test')196	parameter_test.parse_and_add_directory(["parameters"] , '../test')197	serializable_test.parse_and_add_directory(["serializable"] , '../test')198	topology_test.parse_and_add_directory(["deployment" , "assembly"] , '../test')199	#Add schemas to test_list200	test_list.append(topology_test)201	test_list.append(component_test)202	test_list.append(command_test)203	test_list.append(parameter_test)...assess_results.py
Source:assess_results.py  
...9debug = False10print_only = True11def to_dict(obj):12    return json.loads(json.dumps(obj, default=lambda o: o.__dict__))13def add_test(test_list, current_test, current_weights_object):14    test_list.append({"test": current_test, "calculated_weight": current_test.get_weight(current_weights_object)})15    return test_list16def get_changed_weight(total_score):17    if total_score >= 64000:18        change = 619    elif total_score >= 32000:20        change = 521    elif total_score >= 16000:22        change = 423    elif total_score >= 4000:24        change = 325    elif total_score >= 1000:26        change = 227    elif total_score >= 0:28        change = 129    else:30        change = 031    return change32def get_new_score(current_match, calculated_weight, current_winner, current_test):33    test_winner = current_test.calculate_winner(current_match)34    test_score = current_test.get_base_score(match)35    test_weight = current_test.get_weight(current_weights)36    if test_winner == current_winner:37        if test_weight is None:38            total_score = 039        else:40            total_score = test_score * test_weight41    else:42        total_score = 043    if calculated_weight is None:44        return 10045    if current_match["prediction_correct"] is True:46        if current_winner == current_match["prediction"]:47            if debug:48                print("adding " + current_test.weight_name + " weight based on " + str(total_score) + ", before: " + str(calculated_weight))49            calculated_weight += get_changed_weight(total_score)50            if debug:51                print("after " + str(calculated_weight))52        else:53            if debug:54                print("subtracting " + current_test.weight_name + " weight based on " + str(total_score) + ", before: " + str(calculated_weight))55            calculated_weight -= get_changed_weight(total_score)56            if debug:57                print("after " + str(calculated_weight))58    else:59        if current_winner == current_match["prediction"]:60            if debug:61                print("subtracting " + current_test.weight_name + " weight based on " + str(total_score) + ", before: " + str(calculated_weight))62            calculated_weight -= get_changed_weight(total_score)63            if debug:64                print("after " + str(calculated_weight))65        else:66            if debug:67                print("adding " + current_test.weight_name + " weight based on " + str(total_score) + ", before: " + str(calculated_weight))68            calculated_weight += get_changed_weight(total_score)69            if debug:70                print("after " + str(calculated_weight))71    return calculated_weight72client = mongo_client.MongoClient()73matches_doc = client.get_matches_document()74current_weights_doc = client.get_weights_document()75current_weights = current_weights_doc.find_one(sort=[("current_time", pymongo.DESCENDING)])76query = {"result_assessed": False, "weights_id": current_weights["_id"]}77count = matches_doc.count_documents(query)78no_assessment = matches_doc.find(query)79if count == 0:80    print("No unassessed matches for current weights entry: " + str(current_weights["_id"]))81    exit()82tests_and_weights = []83head_to_head = head_to_head.HeadToHead()84tests_and_weights = add_test(tests_and_weights, head_to_head, current_weights)85rank_difference = rank_difference.RankDifference()86tests_and_weights = add_test(tests_and_weights, rank_difference, current_weights)87# maps88ancient_played = ancient_played.AncientPlayed()89tests_and_weights = add_test(tests_and_weights, ancient_played, current_weights)90dust2_played = dust2_played.Dust2Played()91tests_and_weights = add_test(tests_and_weights, dust2_played, current_weights)92inferno_played = inferno_played.InfernoPlayed()93tests_and_weights = add_test(tests_and_weights, inferno_played, current_weights)94mirage_played = mirage_played.MiragePlayed()95tests_and_weights = add_test(tests_and_weights, mirage_played, current_weights)96nuke_played = nuke_played.NukePlayed()97tests_and_weights = add_test(tests_and_weights, nuke_played, current_weights)98overpass_played = overpass_played.OverpassPlayed()99tests_and_weights = add_test(tests_and_weights, overpass_played, current_weights)100vertigo_played = vertigo_played.VertigoPlayed()101tests_and_weights = add_test(tests_and_weights, vertigo_played, current_weights)102ancient_won = ancient_won.AncientWon()103tests_and_weights = add_test(tests_and_weights, ancient_won, current_weights)104dust2_won = dust2_won.Dust2Won()105tests_and_weights = add_test(tests_and_weights, dust2_won, current_weights)106inferno_won = inferno_won.InfernoWon()107tests_and_weights = add_test(tests_and_weights, inferno_won, current_weights)108mirage_won = mirage_won.MirageWon()109tests_and_weights = add_test(tests_and_weights, mirage_won, current_weights)110nuke_won = nuke_won.NukeWon()111tests_and_weights = add_test(tests_and_weights, nuke_won, current_weights)112overpass_won = overpass_won.OverpassWon()113tests_and_weights = add_test(tests_and_weights, overpass_won, current_weights)114vertigo_won = vertigo_won.VertigoWon()115tests_and_weights = add_test(tests_and_weights, vertigo_won, current_weights)116# history117maps_won = maps_won.MapsWon()118tests_and_weights = add_test(tests_and_weights, maps_won, current_weights)119maps_lost = maps_lost.MapsLost()120tests_and_weights = add_test(tests_and_weights, maps_lost, current_weights)121match_win_percentage = match_win_percentage.MatchesWinPercentage()122tests_and_weights = add_test(tests_and_weights, match_win_percentage, current_weights)123matches_won = matches_won.MatchesWon()124tests_and_weights = add_test(tests_and_weights, matches_won, current_weights)125matches_lost = matches_lost.MatchesLost()126tests_and_weights = add_test(tests_and_weights, matches_lost, current_weights)127for match in no_assessment:128    for test_and_weight in tests_and_weights:129        test = test_and_weight["test"]130        old_weight = test_and_weight["calculated_weight"]131        winner = test.calculate_winner(match)132        new_weight = get_new_score(match, old_weight, winner, test)133        test_and_weight["calculated_weight"] = new_weight134    if not print_only:135        matches_doc.update_one({"_id": match["_id"]}, {"$set": {"result_assessed": True}})136new_weights = weights_class.Weights()137for test_and_weight in tests_and_weights:138    new_weights.set_weight(test_and_weight["test"], test_and_weight["calculated_weight"])139print(to_dict(new_weights))140if not print_only:...homework_tests.py
Source:homework_tests.py  
...4t = tester.HomeworkTester()5"""Modify this file with your tests.6The test is already filled out with some basic tests.7Basically, your main usage is:8	t.add_test("command to execute 1", "expected output as a regex string")9	t.add_test("command to execute 2", "expected output as a regex string")10	...11	t.add_test("command to execute 3", "expected output as a regex string")12	t.run()13	t.print_results()14	t.reset()15"""16##################### Basic Executables #########################17# ls should not be found18t.add_test("ls", GENERAL_ERROR)19# But /bin/echo should work20t.add_test("/bin/echo hello world", "hello world")21t.run()22t.print_results()23t.reset()24############################# Builtins ##########################25# Test that cd works26t.add_test("cd /tmp", "")27t.add_test("/bin/pwd", "/tmp")28t.add_test("cd /var", "")29t.add_test("/bin/pwd", "/var")30t.add_test("cd", GENERAL_ERROR)31t.add_test("cd /bin /var", "")32t.add_test("pwd", "/bin")33t.run()34t.print_results()35t.reset()36# Test that history works as expected37t.add_test("history", "0 history")38t.add_test("history -c", "")39t.add_test("    abc   abc   ", GENERAL_ERROR)40t.add_test("def", GENERAL_ERROR)41expected_output = [42    "0     abc   abc   |0 abc   abc   ",43    "1 def",44    "2 history",45]46t.add_test("history", "\n".join(expected_output))47t.add_test("history -c", "")48t.add_test("history", "0 history")49t.add_test("history blahblahblah", GENERAL_ERROR)50expected_output = [51        "0 history",52        "1 history blahblahblah",53        "2 history",54]55t.add_test("history", "\n".join(expected_output))56t.add_test("/bin/echo hello", "hello")57expected_output.extend(["3 /bin/echo hello", "4 history 0"])58t.add_test("history 0", "\n".join(expected_output))59expected_output.append("5 history")60t.add_test("history", "\n".join(expected_output))61t.add_test("history -c 0", "")62t.add_test("history", "0 history")63t.add_test("history 000", "\n".join(["0 history", "1 history 000"]))64t.add_test("history -c", "")65t.add_test("cd / | /bin/pwd", "/")66t.add_test("history 000 -c", "/")67t.add_test("history +0", GENERAL_ERROR)68t.add_test("history -c", "")69t.add_test("history blabla -c", GENERAL_ERROR)70t.add_test("history", "\n".join(["0 history blabla -c", "1 history"]))71t.run()72t.print_results()73t.reset()74############################# Pipes #############################75t.add_test("/bin/echo hello world | /bin/grep hello", "hello world")76t.add_test("/bin/echo blah          |/usr/bin/cut -b 3,4", "ah")77t.add_test("/bin/echo blah|/usr/bin/cut -b 3,4", "ah")78t.run()79t.print_results()80t.reset()81t.add_test("/bin/echo hello world", "hello world")82t.add_test("/bin/ls|history 0 | /usr/bin/wc -w", "2")83t.add_test("cd /", "")84t.add_test("/bin/ls|/bin/pwd", "/")85t.add_test("/bin/ls|/usr/bin/wc -w", "24")86t.add_test("/bin/ls|cd /", "")87t.add_test("cd /|/usr/bin/wc -w", "0")88t.run()89t.print_results()90t.reset()91t.add_test("history | /usr/bin/wc -w", "5")92t.add_test("cd /tmp | /bin/pwd", "/tmp")93t.run()94t.print_results()95t.reset()96############ Pipes with `balabla` commands ###############97t.add_test("cd /bin | blabla | cd ..", GENERAL_ERROR)98t.add_test("pwd", GENERAL_ERROR)99t.add_test("bin/pwd", "/")100t.run()101t.print_results()102t.reset()103###### History Loop ######104t.add_test("history 0", GENERAL_ERROR)105t.add_test("history -c", "")106t.add_test("history 1", GENERAL_ERROR)107t.add_test("history 0", GENERAL_ERROR)108t.run()109t.print_results()110t.reset()111#### History Loop with Pipe #####112t.add_test("cd /tmp |history 0 | cd ..", GENERAL_ERROR)113t.add_test("/bin/pwd", "/")114t.add_test("cd /var | history 0| cd /bin", GENERAL_ERROR)115t.add_test("pwd", "/bin")116t.add_test("history -c", "")117t.add_test("cd /var | history 2| cd /bin", GENERAL_ERROR)118t.add_test("pwd", "/bin")119t.add_test("cd /tmp|history 0|cd /usr", GENERAL_ERROR)120t.add_test("../bin/pwd", "/usr")121t.add_test("cd /bin | history 1 | cd /usr | history 0", GENERAL_ERROR)122t.add_test("cd /bin | history 2 | cd /usr | history 0", "\n".join([GENERAL_ERROR, GENERAL_ERROR]))123t.run()124t.print_results()...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!!
