def test_can_create_unique_key(self): test_question_name = "test" exp_hash = hashlib.md5(test_question_name.encode('utf-8')).hexdigest() got_hash = Packager.generate_key(test_question_name) self.assertEqual(got_hash, exp_hash, "hash string not the same") exp_hash2 = hashlib.md5(str(test_question_name + "_1").encode('utf-8') ).hexdigest() got_hash2 = Packager.generate_key_with_versioning( test_question_name, 1) self.assertEqual(got_hash2, exp_hash2, "hash string not the same") self.assertNotEqual(got_hash2, exp_hash, "versioned hash string should be" + " different from default hash string")
def test_bundle_code_and_test_then_execute(self): self.maxDiff = None # uncomment this to let unittest print unlimited stacktraces # self.maxDiff = None test_question_name = "foo" with open(self.get_file('code'), 'r') as code, \ open(self.get_file('tests'), 'r') as test: p = PythonPackager(self.packager_config) bundle = p.bundle(test_question_name, code.read(), test.read()) code_file = bundle.code_file_path test_file = bundle.test_file_path self.assertTrue(os.path.exists(code_file)) self.assertTrue(os.path.exists(test_file)) with open(test_file, 'r') as test_out, \ open(self.get_file('expected_test_str'), 'r') as expected_test, \ open(code_file, 'r') as code_out, \ open(self.get_file('expected_code_str'), 'r') as expected_code: # cache it to use later expected_test_str = expected_test.read() expected_code_str = expected_code.read() got_test_str = test_out.read() got_code_str = code_out.read() self.assertEqual(''.join(got_test_str.split()), ''.join(expected_test_str.split()), msg='got {} not equal to {}' .format(got_test_str, expected_test_str)) self.assertEqual(''.join(got_code_str.split()), ''.join(expected_code_str.split()), msg='got {} not equal to {}' .format(len(got_code_str), len(expected_code_str)),) exp_test_cases = yaml.load(expected_test_str) got_test_cases = Packager.parse_testcases(bundle) self.assertEqual(got_test_cases, exp_test_cases) got_test_output = p.execute(bundle) exp_test_output = { "testSimple": {"success": True, "message": None}, "testMultiSimple": {"success": True, "message": None} } self.assertEqual(got_test_output, exp_test_output)