def test_validate_email(self): try: from email import validate_email except ImportError: self.assertFalse(no_function_found("validate_email")) check = False tree = ast.parse(''.join(open("../email.py"))) for node in tree.body: if isinstance(node, ast.FunctionDef): if node.name == "validate_email": for nodelist in node.body: if hasattr(nodelist, 'targets'): i = 0 for target in nodelist.targets: if target.id == "pattern": self.assertEqual( "(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", nodelist.value.args[i].s) check = True break i = i + 1 if check: self.assertTrue(succeed("test cases are passed")) else: self.assertFalse(no_function_found("pattern keyword"))
def test_deposit_savings(self): try: from account import Savings sa = Savings("123") try: sa.deposit(1500) except: self.assertFalse(no_function_found("deposit")) self.assertEqual(1500, sa.balance) self.assertTrue(succeed("Savings.deposit")) except ImportError: self.assertFalse(no_function_found("Savings")) except AssertionError: self.assertFalse(incorrect_output())
def test_deposit_current(self): try: from account import Current ca = Current("456") try: ca.deposit(20000) except: self.assertFalse(no_function_found("Savings.deposit")) self.assertEqual(20000, ca.balance) self.assertTrue(succeed("Savings.deposit")) except ImportError: self.assertFalse(no_function_found("Current")) except AssertionError: self.assertFalse(incorrect_output())
def test_countWord(self): try: from count_word import countWord result = countWord('../files/testfile.txt', 'dummy') self.assertEqual(2, result) result1 = countWord('../files/testfile.txt', 'Lorem') self.assertEqual(2, result1) self.assertTrue(succeed("countWord")) except ImportError: self.assertFalse(no_function_found("countWord")) except AssertionError: self.assertFalse(incorrect_output()) except FileExistsError: self.assertFalse(no_function_found("file not found"))
def test_calculateAvgHwy(self): try: from hwy_mpg import calculateAvgHwy result = calculateAvgHwy("../files/mpg.csv") self.assertEqual(16.87878787878788, result[0][1]) self.assertEqual("pickup", result[0][0]) self.assertEqual(18.129032258064516, result[1][1]) self.assertEqual("suv", result[1][0]) self.assertEqual(22.363636363636363, result[2][1]) self.assertEqual("minivan", result[2][0]) self.assertEqual(24.8, result[3][1]) self.assertEqual("2seater", result[3][0]) self.assertEqual(27.29268292682927, result[4][1]) self.assertEqual("midsize", result[4][0]) self.assertEqual(28.142857142857142, result[5][1]) self.assertEqual("subcompact", result[5][0]) self.assertEqual(28.29787234042553, result[6][1]) self.assertEqual("compact", result[6][0]) self.assertTrue(succeed("calculateAvgHwy")) except ImportError: self.assertFalse(no_function_found("calculateAvgHwy")) except AssertionError: self.assertFalse(incorrect_output())
def test_pairSum(self): try: from pair_sum import pairSum result = pairSum([2, 5, 4, 3, 8, 7, 6, 1, 10, -1], 9) for item in result: self.assertEqual(9, (item[0] + item[1])) self.assertTrue(succeed("pairSum")) except ImportError: self.assertFalse(no_function_found("pairSum"))
def test_letterAndDigit(self): try: from letter_digit import letterAndDigit result = letterAndDigit('hello world! 123') self.assertEqual(3, result['DIGITS']) self.assertEqual(10, result['LETTERS']) self.assertTrue(succeed("letterAndDigit")) except ImportError: self.assertFalse(no_function_found("letterAndDigit")) except AssertionError: self.assertFalse(incorrect_output())
def test_widraw_current(self): try: from account import Current ca = Current("456") try: ca.deposit(20000) except: self.assertFalse(no_function_found("Current.deposit")) try: ca.withdraw(1) except: self.assertFalse(no_function_found("Current.widraw")) self.assertEqual(19998.9, ca.balance) self.assertTrue(succeed("Current.deposit")) except ImportError: self.assertFalse(no_function_found("Current")) except AssertionError: self.assertFalse(incorrect_output())
def test_validate_email(self): try: from build import validate_email except ImportError: self.assertFalse(no_function_found("validate_email")) check = False s = "*****@*****.**" solution = validate_email(s) self.assertNotEqual(None, solution) self.assertEqual(True, solution)
def test_even_numbers(self): try: from even_numbers import get_even_numbers except ImportError: self.assertFalse(no_function_found("get_even_numbers")) try: result = get_even_numbers(0) except ValueError: self.assertTrue("number should be less than 0 passed") result = get_even_numbers(10) self.assertEqual(2, result[0]) self.assertEqual(4, result[1]) self.assertEqual(6, result[2]) self.assertEqual(8, result[3]) self.assertTrue(succeed("all test cases passed for even numbers"))
def test_prime_numbers(self): try: from prime_numbers import get_prime_numbers except ImportError: self.assertFalse(no_function_found("get_prime_numbers")) result = get_prime_numbers(10) try: self.assertEqual(2, result[0]) self.assertEqual(3, result[1]) self.assertEqual(5, result[2]) self.assertEqual(7, result[3]) self.assertTrue(succeed("Test cases passed")) except: self.assertFalse( incorrect_output( "function should return array of prime numbers"))
def test_squareOfNumbers(self): try: from square_of_numbers import squareOfNumbers result = squareOfNumbers(8) if isinstance(result, dict): self.assertEqual(result[1], 1) self.assertEqual(result[2], 4) self.assertEqual(result[3], 9) self.assertEqual(result[4], 16) self.assertEqual(result[5], 25) self.assertEqual(result[6], 36) self.assertEqual(result[7], 49) self.assertEqual(result[8], 64) self.assertTrue(succeed("squareOfNumbers")) else: self.assertFalse(incorrect_output()) except ImportError: self.assertFalse(no_function_found("squareOfNumbers")) except AssertionError: self.assertFalse(incorrect_output())
def test_calculateAvgHwy(self): try: from hwy_mpg import calculateAvgHwy result = calculateAvgHwy( "./files/mpg.csv" ) # result => A dict object -> {'pickup':16.78, 'suv':'18.1290','minivan':22.363, ...} self.assertAlmostEqual(16.87878787878788, result["pickup"], places=3) self.assertAlmostEqual(18.129032258064516, result["suv"], places=3) self.assertAlmostEqual(22.363636363636363, result["minivan"], places=3) self.assertAlmostEqual(24.8, result["2seater"], places=3) self.assertAlmostEqual(27.29268292682927, result["midsize"], places=3) self.assertAlmostEqual(28.142857142857142, result["subcompact"], places=3) self.assertAlmostEqual(28.29787234042553, result["compact"], places=3) self.assertTrue(succeed("calculateAvgHwy")) except ImportError: self.assertFalse(no_function_found("calculateAvgHwy")) except AssertionError: self.assertFalse(incorrect_output())