def test_unsafe_input(self): print("\n\n\n" + " Test if Unsafe Input Detected ".center(75, "#")) test_str = "().__class__.__blah__" target_str = "A" response = api.check(test_str, target_str) # Implicitly we're testing no unhandled exceptions. self.assertTrue("error" in response, 'Expected "error" in response!') print(" PASS ".center(75, "#"))
def test_invalid_test_str(self): print("\n\n\n" + " Test if Invalid Test Str Detected ".center(75, "#")) test_str = "A & B & " target_str = "A & B" response = api.check(test_str, target_str) # Implicitly we're testing no unhandled exceptions. self.assertTrue("error" in response, 'Expected "error" in response!') self.assertTrue("code" not in response, 'Expected "code" in response!') print(" PASS ".center(75, "#"))
def test_invalid_target(self): print("\n\n\n" + " Test if Invalid Target Detected ".center(75, "#")) test_str = "A & B" target_str = "A & B & " response = api.check(test_str, target_str) # Implicitly we're testing no unhandled exceptions. self.assertTrue("error" in response, 'Expected "error" in response!') self.assertTrue("code" in response, 'Expected "code" in response!') self.assertTrue(response["code"] == 400, 'Expected error "code" 400 in response, got "{}"!'.format(response["code"])) print(" PASS ".center(75, "#"))
def test_equivalence(self): print("\n\n\n" + " Test if Equivalence (==) Works ".center(75, "#")) test_str = "P == Q" target_str = "P == Q" symbols = None response = api.check(test_str, target_str, symbols=symbols) self.assertTrue("error" not in response, 'Unexpected "error" in response!') self.assertTrue("equal" in response, 'Key "equal" not in response!') self.assertTrue(response["equal"] == "true", 'Expected "equal" to be "true", got "{}"!'.format(response["equal"])) self.assertTrue("equality_type" in response, 'Key "equality_type" not in response!') self.assertTrue(response["equality_type"] in EQUALITY_TYPES, 'Unexpected "equality_type": "{}"!'.format(response["equality_type"])) self.assertTrue(response["equality_type"] == "exact", 'For these expressions, expected "equality_type" to be "exact", got "{}"!'.format(response["equality_type"])) print(" PASS ".center(75, "#"))
def test_xor(self): print("\n\n\n" + " Test if XOR (^) Works ".center(75, "#")) test_str = "P ^ Q" target_str = "(P & ~Q) | (Q & ~P)" symbols = None response = api.check(test_str, target_str, symbols=symbols) self.assertTrue("error" not in response, 'Unexpected "error" in response!') self.assertTrue("equal" in response, 'Key "equal" not in response!') self.assertTrue(response["equal"] == "true", 'Expected "equal" to be "true", got "{}"!'.format(response["equal"])) self.assertTrue("equality_type" in response, 'Key "equality_type" not in response!') self.assertTrue(response["equality_type"] in EQUALITY_TYPES, 'Unexpected "equality_type": "{}"!'.format(response["equality_type"])) self.assertTrue(response["equality_type"] == "symbolic", 'For these expressions, expected "equality_type" to be "symbolic", got "{}"!'.format(response["equality_type"])) print(" PASS ".center(75, "#"))
def test_missing_symbols(self): print("\n\n\n" + " Test if Missing Symbols Detected ".center(75, "#")) test_str = "A" target_str = "B" response = api.check(test_str, target_str, check_symbols=True) self.assertTrue("error" not in response, 'Unexpected "error" in response!') self.assertTrue("equal" in response, 'Key "equal" not in response!') self.assertTrue(response["equal"] == "false", 'Expected "equal" to be "false", got "{}"!'.format(response["equal"])) self.assertTrue("equality_type" in response, 'Key "equality_type" not in response!') self.assertTrue(response["equality_type"] in EQUALITY_TYPES, 'Unexpected "equality_type": "{}"!'.format(response["equality_type"])) self.assertTrue(response["equality_type"] == "symbolic", 'For these expressions, expected "equality_type" to be "exact", got "{}"!'.format(response["equality_type"])) self.assertTrue("incorrect_symbols" in response, 'Key "incorrect_symbols" not in response!') print(" PASS ".center(75, "#"))
def test_not_not(self): print("\n\n\n" + " Test NOT NOT Cancellation ".center(75, "#")) test_str = "~~A" target_str = "A" symbols = None response = api.check(test_str, target_str, symbols=symbols) self.assertTrue("error" not in response, 'Unexpected "error" in response!') self.assertTrue("equal" in response, 'Key "equal" not in response!') self.assertTrue(response["equal"] == "true", 'Expected "equal" to be "true", got "{}"!'.format(response["equal"])) self.assertTrue("equality_type" in response, 'Key "equality_type" not in response!') self.assertTrue(response["equality_type"] in EQUALITY_TYPES, 'Unexpected "equality_type": "{}"!'.format(response["equality_type"])) self.assertTrue(response["equality_type"] == "symbolic", 'For these expressions, expected "equality_type" to be "symbolic", got "{}"!'.format(response["equality_type"])) print(" PASS ".center(75, "#"))
def test_demorgan_or(self): print("\n\n\n" + " Test De Morgan's Law (OR) ".center(75, "#")) test_str = "~(A | B)" target_str = "~A & ~B" symbols = None response = api.check(test_str, target_str, symbols=symbols) self.assertTrue("error" not in response, 'Unexpected "error" in response!') self.assertTrue("equal" in response, 'Key "equal" not in response!') self.assertTrue(response["equal"] == "true", 'Expected "equal" to be "true", got "{}"!'.format(response["equal"])) self.assertTrue("equality_type" in response, 'Key "equality_type" not in response!') self.assertTrue(response["equality_type"] in EQUALITY_TYPES, 'Unexpected "equality_type": "{}"!'.format(response["equality_type"])) self.assertTrue(response["equality_type"] == "symbolic", 'For these expressions, expected "equality_type" to be "symbolic", got "{}"!'.format(response["equality_type"])) print(" PASS ".center(75, "#"))
def check_endpoint(): """Check the equivalence of two boolean logic expressions.""" body = request.get_json(force=True) if not (("test" in body) and ("target" in body)): print("=" * 50) print("ERROR: Ill-formed request!") print(body) print("=" * 50) abort(400) # Probably want to just abort with a '400 BAD REQUEST' target_str = body.get("target") test_str = body.get("test") description = body.get("description") if (target_str == "") or (test_str == ""): print("=" * 50) if description is not None: print(description) print("=" * 50) print("ERROR: Empty string in request!") print("Target: '{0}'\nTest: '{1}'".format(target_str, test_str)) print("=" * 50) abort(400) # Probably want to just abort with a '400 BAD REQUEST' check_symbols = str(body.get("check_symbols", "true")).lower() == "true" # To reduce computation issues on single-threaded server, institute a timeout # for requests. If it takes longer than this to process, return an error. # This cannot interrupt numpy's computation, so care must be taken in selecting # a value for MAX_REQUEST_COMPUTATION_TIME. try: with TimeoutProtection(MAX_REQUEST_COMPUTATION_TIME): response_dict = logic.check(test_str, target_str, check_symbols=check_symbols, description=description) return jsonify(**response_dict) except TimeoutException as e: print("ERROR: {} - Request took too long to process, aborting!".format( type(e).__name__)) print("=" * 50) error_dict = dict( target=target_str, test=test_str, error="Request took too long to process!", ) return jsonify(**error_dict)