Coverage for test/test_utils/test_utils_input_validator.py: 98%

52 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-03-09 21:08 +0000

1"""This Module Tests the utility functions in the input_validator module.""" 

2import unittest 

3from datetime import datetime, date 

4from utils.input_validator import validate_date, validate_int, validate_bool, validate_array 

5 

6 

7class TestInputValidator(unittest.TestCase): 

8 """Tests for the input_validator module""" 

9 def test_validate_date_returns_date(self): 

10 """Test that the validate_date function returns a date object""" 

11 self.assertIsInstance(validate_date(datetime.now()), date) 

12 

13 def test_validate_date_returns_correct_date(self): 

14 """Test that the validate_date function returns the correct date""" 

15 self.assertEqual(validate_date(datetime.now()), datetime.now().date()) 

16 

17 def test_validate_date_returns_correct_date_from_string(self): 

18 """Test that the validate_date function returns the correct date from a string""" 

19 self.assertEqual(validate_date('2021-01-01'), date(2021, 1, 1)) 

20 

21 def test_validate_date_raises_error(self): 

22 """Test that the validate_date function raises an error for invalid input""" 

23 with self.assertRaises(TypeError): 

24 validate_date(123) 

25 

26 def test_validate_int_returns_int(self): 

27 """Test that the validate_int function returns an integer""" 

28 self.assertIsInstance(validate_int(123, 'test'), int) 

29 

30 def test_validate_int_returns_correct_int(self): 

31 """Test that the validate_int function returns the correct integer""" 

32 self.assertEqual(validate_int(123, 'test'), 123) 

33 

34 def test_validate_int_returns_correct_int_from_string(self): 

35 """Test that the validate_int function returns the correct integer from a string""" 

36 self.assertEqual(validate_int('123', 'test'), 123) 

37 

38 def test_validate_int_raises_error(self): 

39 """Test that the validate_int function raises an error for invalid input""" 

40 with self.assertRaises(TypeError): 

41 validate_int('abc', 'test') 

42 

43 def test_validate_bool_returns_bool(self): 

44 """Test that the validate_bool function returns a boolean""" 

45 self.assertIsInstance(validate_bool(True, 'test'), bool) 

46 

47 def test_validate_bool_returns_correct_bool(self): 

48 """Test that the validate_bool function returns the correct boolean""" 

49 self.assertEqual(validate_bool(True, 'test'), True) 

50 

51 def test_validate_bool_returns_correct_bool_from_string(self): 

52 """Test that the validate_bool function returns the correct boolean from a string""" 

53 self.assertEqual(validate_bool('true', 'test'), True) 

54 

55 def test_validate_bool_raises_error(self): 

56 """Test that the validate_bool function raises an error for invalid input""" 

57 with self.assertRaises(TypeError): 

58 validate_bool('abc', 'test') 

59 # test array validator 

60 def test_validate_array_returns_list(self): 

61 """Test that the validate_array function returns a list""" 

62 self.assertIsInstance(validate_array([1, 2, 3], 'test'), list) 

63 

64 def test_validate_array_returns_correct_list(self): 

65 """Test that the validate_array function returns the correct list""" 

66 self.assertEqual(validate_array([1, 2, 3], 'test'), [1, 2, 3]) 

67 

68 def test_validate_array_returns_correct_list_from_string(self): 

69 """Test that the validate_array function returns the correct list from a string""" 

70 self.assertEqual(validate_array('1,2,3', 'test'), [1, 2, 3]) 

71 

72 def test_validate_array_raises_error(self): 

73 """Test that the validate_array function raises an error for invalid input""" 

74 with self.assertRaises(TypeError): 

75 validate_array(123, 'test') 

76 

77 def test_validate_array_raises_error_for_invalid_string(self): 

78 """Test that the validate_array function raises an error for invalid string input""" 

79 with self.assertRaises(TypeError): 

80 validate_array('abc', 'test') 

81 

82 def test_validate_array_raises_error_for_invalid_list(self): 

83 """Test that the validate_array function raises an error for invalid list input""" 

84 invalid_input = {"not": "a list"} 

85 with self.assertRaises(TypeError): 

86 validate_array(invalid_input, "invalid_input") 

87 

88 def test_validate_array_raises_error_for_invalid_list_string(self): 

89 """Test that the validate_array function raises an error for invalid list string input""" 

90 with self.assertRaises(TypeError): 

91 validate_array('1,a,3', 'test') 

92 

93 

94if __name__ == '__main__': 

95 unittest.main()