def test_routes_and_servers(self): ''' All servers in spec should be reachable ''' for host in self.servers: logger.info("Testing with host %s", host) for route in self.routes_with_args: try: r = requests.get(host + route, headers=self.headers, timeout=self.timeout) t = r.elapsed.total_seconds() try: self.assertEqual(r.status_code, 200) logger.info("Server at " + host + route + " responded in " + str(t) + " seconds, with a " + str(r.status_code) + " status code.") except: logger.error("Server at " + host + route + " responded in " + str(t) + " seconds, with a " + str(r.status_code) + " status code.") except requests.exceptions.ReadTimeout: logger.error("Server at " + host + route + " timed out after " + str(self.timeout) + " seconds.") except requests.exceptions.ConnectTimeout: logger.error("Server at " + host + route + " could not connect and timed out after " + str(self.timeout) + " seconds.")
def conditions_met(self, args, condition) -> bool: ''' A method for checking whether a particular post-processing rule should be applied. A condition consists of a key labelled "template_arg_key" and one of the following operations: - equal_to - starts_with - ends_with - contains - regex_contains ''' to_check = args[condition['template_arg_key']] if 'equal_to' in condition: return to_check == condition['equal_to'] if 'starts_with' in condition: return to_check.startswith(condition['starts_with']) if 'ends_with' in condition: return to_check.endswith(condition['ends_with']) if 'contains' in condition: return condition['contains'] in to_check if 'regex_contains' in condition: contains = re.compile(condition['contains']) return bool(re.search(contains, to_check)) logger.info( 'A post-processing condition was provided to the encoder that had an unrecognized operation.' ) return False
def test_all_tags(self): ''' Check if all tags are generated and otherwise produce 400. ''' logger.debug("Generating ALL tags, this might take a while...") red_verb_tag_len = len(self.red_verb_tags) * len( self.all_affopts) * len(self.all_pro_tags) blue_verb_tag_len = len(self.blue_verb_tags) * len( self.all_affopts) * len(self.all_pro_tags) purple_verb_tag_len = len(self.purple_verb_tags) * len( self.all_affopts) * len(self.valid_pro_combinations) logger.info("""There are a total of {total} possible tags, including {red} red verbs, {blue} blue verbs, and {purple} purple verbs""".format( total=str(red_verb_tag_len + blue_verb_tag_len + purple_verb_tag_len), red=str(red_verb_tag_len), blue=str(blue_verb_tag_len), purple=str(purple_verb_tag_len))) for arg in self.red_args: tag = self.return_tag(arg)[0]['fst'] try: self.assertTrue(self.valid_tag(tag)) except: self.fail("The following tag did not work: " + str(tag) + ". The args that generated it were: " + str(arg)) for arg in self.blue_args: tag = self.return_tag(arg)[0]['fst'] self.assertTrue(self.valid_tag(tag)) for arg in self.purple_args: tag = self.return_tag(arg)[0]['fst'] self.assertTrue(self.valid_tag(tag))
def test_error_response_code_404_with_non_existant_args(self): ''' Ensure all non-existant args return 404 ''' for ep in self.routes_only_args: if "/verb" in ep: rt = re.sub(self.arg_match, self.non_existant_tag, ep) try: r = requests.get(self.host + rt) self.assertEqual(r.status_code, 404) logger.info("Non-existant arg on route " + self.host + rt + " returned " + str(r.status_code)) except AssertionError: logger.error("Route " + self.host + rt + " returned " + str(r.status_code)) except: logger.error("Couldn't connect. Is flask running?") elif "/aff-option" in ep: rt = re.sub(self.arg_match, self.non_existant_tag, ep) try: r = requests.get(self.host + rt) self.assertEqual(r.status_code, 404) logger.info("Non-existant arg on route " + self.host + rt + " returned " + str(r.status_code)) except AssertionError: logger.error("Route " + self.host + rt + " returned " + str(r.status_code)) except: logger.error("Couldn't connect. Is flask running?") elif "/affix" in ep: rt = re.sub(self.arg_match, self.non_existant_tag, ep) try: r = requests.get(self.host + rt) self.assertEqual(r.status_code, 404) logger.info("Non-existant arg on route " + self.host + rt + " returned " + str(r.status_code)) except AssertionError: logger.error("Route " + self.host + rt + " returned " + str(r.status_code)) except: logger.error("Couldn't connect. Is flask running?") elif "/pronoun" in ep: rt = re.sub(self.arg_match, self.non_existant_tag, ep) try: r = requests.get(self.host + rt) self.assertEqual(r.status_code, 404) logger.info("Non-existant arg on route " + self.host + rt + " returned " + str(r.status_code)) except AssertionError: logger.error("Route " + self.host + rt + " returned " + str(r.status_code)) except: logger.error("Couldn't connect. Is flask running?") elif "/conjugations": rt = re.sub(self.arg_match, self.non_existant_tag, ep) try: r = requests.get(self.host + rt) self.assertEqual(r.status_code, 404) logger.info("Non-existant arg on route " + self.host + rt + " returned " + str(r.status_code)) except AssertionError: logger.error("Route " + self.host + rt + " returned " + str(r.status_code)) except: logger.error("Couldn't connect. Is flask running?") else: logger.warning("Route " + self.host + ep + " was registered, but not tested.")
def test_remove_long_stress(self): for pair in self.longstress: old = pair[0] expected = pair[1] new = self.util_cmp.remove_long_stress(pair[1]) msg = "test_remove_long_stress: old: " + old + \ " new: " + new + " (expected: " + expected + ")" logger.info(msg) if (new != expected): logger.error(msg) self.fail(msg)
def test_compare_ignore_stress(self): for triple in self.compare_allstress: s1 = triple[0] s2 = triple[1] ifSameExp = triple[2] ifSame = self.util_cmp.compare_ignore_stress(s1, s2) msg = "test_compare_ignore_stress: s1: " + s1 + ", s2: " + s2 + ", ifSameExp: " + str(ifSameExp) + ", " \ "ifSame: " + str(ifSame) if ifSame is ifSameExp: logger.info(msg) else: logger.error(msg) self.fail(msg)
def test_foma_access_Shell(self): foma_shell = None try: foma_shell = foma_access(self.path_to_model) except NullFomaTransducerException as e: logger.info("Failed to initialize foma-access", exc_info=True) fin = open(self.testFile, 'r') c = -1 for line in fin: c = c + 1 try: down_string = line.strip() logger.info("foma-shell: {0}: In: {1}".format(str(c), down_string)) res = foma_shell.down(down_string) for r in res: logger.info(" out: {0}".format(r)) logger.info("-") except Exception as e: logger.error("Error in TestBindingsFomaAccessPython: " + e) fin.close()
def test_wrong_person_with_command(self): ''' If anything other than 2nd person is used with command, response should be a 400 **this is unclear, and TBD. Currently returns nothing** ''' params = { 'verb': self.all_blue_verb_tags[0], 'aff-option': self.command_tag, 'patient': self.first_pers_tag } for host in self.servers: try: r = requests.get(host + self.route, params=params, headers=self.headers) self.assertEqual(r.status_code, 400) logger.info("Request to " + r.url + " returned " + str(r.status_code)) except Exception as e: logger.warning("Request to " + host + self.route + " returned " + str(e))
def test_response_code(self): ''' Ensure all routes return 200 ''' for rt in self.routes_no_fst_no_args: try: r = requests.get(self.host + rt) self.assertEqual(r.status_code, 200) logger.info("Route " + self.host + rt + " returned " + str(r.status_code)) except: logger.error("Couldn't connect. Is flask running?") for rt in self.routes_no_args: try: r = requests.get(self.host + rt) self.assertEqual(r.status_code, 200) logger.info("Route " + self.host + rt + " returned " + str(r.status_code)) except AssertionError: logger.error("Route " + self.host + rt + " returned " + str(r.status_code) + ". Is the FST running?") except: logger.error("Couldn't connect. Is flask running?")
def test_wrong_theta_role(self): ''' If agent combined with blue verb, or patient combined with red verb, response should be a 400 ''' blue_params = { 'verb': self.all_blue_verb_tags[0], 'aff-option': 'defpast', 'agent': self.first_pers_tag } red_params = { 'verb': self.all_red_verb_tags[0], 'aff-option': 'defpast', 'patient': self.first_pers_tag } for host in self.servers: try: r_blue = requests.get(host + self.route, params=blue_params, headers=self.headers) self.assertEqual(r_blue.status_code, 400) logger.info("Request to " + r_blue.url + " returned " + str(r_blue.status_code)) except Exception as e: logger.error("Request to " + host + self.route + f" returned {e}") try: r_red = requests.get(host + self.route, params=red_params, headers=self.headers) self.assertEqual(r_red.status_code, 400) logger.info("Request to " + r_red.url + " returned " + str(r_red.status_code)) except Exception as e: logger.error("Request to " + host + self.route + f" returned {e}")
def test_error_response_code_405_with_disallowed_methods(self): ''' Ensure all disallowed methods return 405s ''' for rt in self.routes_no_fst_no_args: try: r = requests.delete(self.host + rt) self.assertEqual(r.status_code, 405) logger.info("'DELETE' method on route " + self.host + rt + " returned " + str(r.status_code)) r = requests.put(self.host + rt) self.assertEqual(r.status_code, 405) logger.info("'PUT' method on route " + self.host + rt + " returned " + str(r.status_code)) r = requests.post(self.host + rt) self.assertEqual(r.status_code, 405) logger.info("'POST' method on route " + self.host + rt + " returned " + str(r.status_code)) except AssertionError: logger.warning("Route " + self.host + rt + " returned " + str(r.status_code)) except: logger.error("Couldn't connect. Is flask running?")
def test_TransducerDirectly(self): try: self.transducer = FST.load(self.path_to_model) except Exception as e: logger.error("Could not initialize a Foma FST. path_to_model: {}".format( self.path_to_model) + e) fin = open(self.testFile, 'r') c = -1 for line in fin: c = c + 1 try: down_string = line.strip() logger.info("Direct: {0}: In: {1}".format(str(c), down_string)) res = self.transducer.apply_down(down_string) for r in res: logger.info(" out: {0}".format(r)) logger.info("-") except Exception as e: logger.error("Error in TestBindingDirect: " + e) fin.close()
def test_response_code_with_args(self): ''' Ensure all args return 200 ''' for ep in self.routes_only_args: if "/verb" in ep: for x in self.all_verb_tags: rt = re.sub(self.arg_match, x, ep) try: r = requests.get(self.host + rt) self.assertEqual(r.status_code, 200) except AssertionError: logger.error("Route " + self.host + rt + " returned " + str(r.status_code) + ". Is the FST running?") except: logger.error("Couldn't connect. Is flask running?") logger.info("Successfully tested " + str(len(self.all_verb_tags)) + " verb resources at route " + self.host + ep + " .") elif "/aff-option" in ep: for x in self.all_affopt_tags: rt = re.sub(self.arg_match, x, ep) try: r = requests.get(self.host + rt) self.assertEqual(r.status_code, 200) except AssertionError: logger.error("Route " + self.host + rt + " returned " + str(r.status_code) + ". Is the FST running?") except: logger.error("Couldn't connect. Is flask running?") logger.info("Successfully tested " + str(len(self.all_affopt_tags)) + " affix option resources at route " + self.host + ep + ".") elif "/affix" in ep: for x in self.all_affix_tags: rt = re.sub(self.arg_match, x, ep) try: r = requests.get(self.host + rt) self.assertEqual(r.status_code, 200) except AssertionError: logger.error("Route " + self.host + rt + " returned " + str(r.status_code) + ". Is the FST running?") except: logger.error("Couldn't connect. Is flask running?") logger.info("Successfully tested " + str(len(self.all_affix_tags)) + " affix resources at route " + self.host + ep + ".") elif "/pronoun" in ep: for x in self.all_pronoun_tags: rt = re.sub(self.arg_match, x, ep) try: r = requests.get(self.host + rt) self.assertEqual(r.status_code, 200) except AssertionError: logger.error("Route " + self.host + rt + " returned " + str(r.status_code) + ". Is the FST running?") except: logger.error("Couldn't connect. Is flask running?") logger.info("Successfully tested " + str(len(self.all_pronoun_tags)) + " pronoun resources at route " + self.host + ep + ".") elif "/conjugations": logger.debug("Passing conjugations args for other test") pass else: logger.warning("Route " + ep + " was registered, but not tested.")