def test_when_all_query_params_are_bad(self): limit = 200 offset = "-30" filters = "illusion,Part_Time,full_time,-full_time" institution = "" length_of_course = "-2,8,twenty" countries = "bolivia,-england,england" subjects = 32 language = 'bad' validator = Validator( countries, filters, institution, length_of_course, subjects, limit, 100, offset, language ) expected_result = {} output_result, output_error_object = validator.validate() expected_error_object = [ { "error": "limit cannot exceed maximum value of 100", "error_values": [{"limit": 200}], }, { "error": "offset needs to be a positive number, offset cannot be lower than 0", "error_values": [{"offset": "-30"}], }, { "error": "use of the same filter option more than once", "error_values": [{"filters": "full_time"}], }, { "error": "invalid filters", "error_values": [{"filters": "illusion,Part_Time"}], }, { "error": "use of the same countries more than once", "error_values": [{"countries": "england"}], }, {"error": "invalid countries", "error_values": [{"countries": "bolivia"}]}, { "error": "length_of_course values needs to be a number", "error_values": [{"length_of_course": "twenty"}], }, { "error": "length_of_course values needs to be numbers between the range of 1 and 7", "error_values": [{"length_of_course": "-2,8"}], }, { 'error': 'missing value for mandatory field: institution', 'error_values': [{'institution': ''}], }, { 'error': 'value of language is not supported', 'error_values': [{'language': 'bad'}], } ] print(f"output: {output_error_object}") self.assertEqual(expected_result, output_result) self.assertEqual(expected_error_object, output_error_object)
def test_when_all_query_params_are_set(self): limit = 5 offset = "30" filters = "distance_learning,honours_award,-foundation_year,sandwich_year,-year_abroad,full_time" institution = "Coventry University" length_of_course = "3,4" countries = "england,wales" subjects = "CAH09-01-01,CAH09-01-02" language = "en" validator = Validator( countries, filters, institution, length_of_course, subjects, limit, 100, offset, language ) expected_result = { "countries": ["XF", "XI"], "distance_learning": True, "foundation_year": False, "full_time": True, "honours_award": True, "language": "en", "length_of_course": ["3", "4"], "limit": 5, "offset": 30, "sandwich_year": True, "subjects": ["CAH09-01-01", "CAH09-01-02"], "year_abroad": False, } output_result, output_error_object = validator.validate() self.assertEqual(expected_result, output_result) self.assertEqual([], output_error_object)
def test_when_scotland_is_selected_returns_no_error(self): countries = "scotland" validator = Validator(countries, "", "", "Coventry University", "", "0", 100, "0", "en") output_countries, output_error_object = validator.validate_countries() self.assertEqual(["XH"], output_countries) self.assertEqual([], output_error_object)
def test_when_multiple_countries_are_selected_returns_no_error(self): countries = "england,wales" validator = Validator(countries, "", "", "Coventry University", "", "0", 100, "0", "en") output_countries, output_error_object = validator.validate_countries() self.assertEqual(["XF", "XI"], output_countries) self.assertEqual([], output_error_object)
def test_when_multiple_valid_length_of_course_returns_no_error(self): length_of_course = "1,7" validator = Validator("", "", "Coventry University", length_of_course, "", "0", 100, "0", "en") output_lengths, output_error_object = validator.validate_length_of_course() self.assertEqual(["1", "7"], output_lengths) self.assertEqual([], output_error_object)
def test_when_empty_filters_returns_no_error(self): filters = "" validator = Validator("", filters, "", "Coventry University", "", "0", 100, "0", "en") output_filters, output_error_object = validator.validate_filters() self.assertEqual({}, output_filters) self.assertEqual([], output_error_object)
def test_when_empty_countries_returns_no_error(self): countries = "" validator = Validator(countries, "", "", "Coventry University", "", "0", 100, "0", "en") output_countries, output_error_object = validator.validate_countries() self.assertEqual([], output_countries) self.assertEqual([], output_error_object)
def test_when_a_country_is_selected_prefixed_with_hyphen_returns_no_error(self): countries = "-england" validator = Validator(countries, "", "", "Coventry University", "", "0", 100, "0", "en") output_countries, output_error_object = validator.validate_countries() self.assertEqual(["XG", "XH", "XI"], output_countries) self.assertEqual([], output_error_object)
def test_when_a_filter_is_selected_prefixed_with_hyphen_returns_no_error(self): filters = "-sandwich_year" validator = Validator("", filters, "Coventry University", "", "", "0", 100, "0", "en") output_filters, output_error_object = validator.validate_filters() expected_filters = {"sandwich_year": False} self.assertEqual(expected_filters, output_filters) self.assertEqual([], output_error_object)
def test_when_a_filter_is_selected_returns_no_error(self): filters = "distance_learning" validator = Validator("", filters, "Coventry University", "", "", "0", 100, "0", "en") output_filters, output_error_object = validator.validate_filters() expected_filters = {"distance_learning": True} self.assertEqual(expected_filters, output_filters) self.assertEqual([], output_error_object)
def test_when_offset_is_a_positive_number(self): offset = "20" validator = Validator("", "", "Coventry University", "", "", "0", 100, offset, "en") expected_result = 20 output_result, output_error_object = validator.validate_offset() self.assertEqual(expected_result, output_result) self.assertEqual([], output_error_object)
def test_when_limit_is_within_max_limit(self): limit = "10" max_limit = 100 validator = Validator("", "", "Coventry University", "", "", limit, max_limit, "0", "en") expected_result = 10 output_result, output_error_object = validator.validate_limit() self.assertEqual(expected_result, output_result) self.assertEqual([], output_error_object)
def test_when_there_are_duplicate_filters_returns_error(self): filters = "-distance_learning,distance_learning" validator = Validator("", filters, "", "Coventry University", "", "0", 100, "0", "en") output_filters, output_error_object = validator.validate_filters() expected_error_object = [ { "error": "use of the same filter option more than once", "error_values": [{"filters": "distance_learning"}], } ] self.assertEqual({}, output_filters) self.assertEqual(expected_error_object, output_error_object)
def test_when_length_of_course_is_above_minimum_number_of_years_returns_error(self): length_of_course = "0" validator = Validator("", "", "Coventry University", length_of_course, "", "0", 100, "0", "en") output_lengths, output_error_object = validator.validate_length_of_course() expected_error_object = [ { "error": "length_of_course values needs to be numbers between the range of 1 and 7", "error_values": [{"length_of_course": "0"}], } ] self.assertEqual([], output_lengths) self.assertEqual(expected_error_object, output_error_object)
def test_when_length_of_course_is_not_a_number_returns_error(self): length_of_course = "four,3" validator = Validator("", "", "Coventry University", length_of_course, "", "0", 100, "0", "en") output_lengths, output_error_object = validator.validate_length_of_course() expected_error_object = [ { "error": "length_of_course values needs to be a number", "error_values": [{"length_of_course": "four"}], } ] self.assertEqual([], output_lengths) self.assertEqual(expected_error_object, output_error_object)
def test_when_there_are_invalid_uk_countries_returns_error(self): countries = "bosnia,photosynthesis,england" validator = Validator(countries, "", "", "Coventry University", "", "0", 100, "0", "en") output_countries, output_error_object = validator.validate_countries() expected_error_object = [ { "error": "invalid countries", "error_values": [{"countries": "bosnia,photosynthesis"}], } ] self.assertEqual([], output_countries) self.assertEqual(expected_error_object, output_error_object)
def test_when_there_are_duplicate_countries_returns_error(self): countries = "england,-england" validator = Validator(countries, "", "", "Coventry University", "", "0", 100, "0", "en") output_countries, output_error_object = validator.validate_countries() expected_error_object = [ { "error": "use of the same countries more than once", "error_values": [{"countries": "england"}], } ] self.assertEqual([], output_countries) self.assertEqual(expected_error_object, output_error_object)
def test_when_there_are_invalid_filters_returns_error(self): filters = "salary,distance_learning,PaRt-Time" validator = Validator("", filters, "Coventry University", "", "", "0", 100, "0", "en") output_filters, output_error_object = validator.validate_filters() expected_error_object = [ { "error": "invalid filters", "error_values": [{"filters": "salary,PaRt-Time"}], } ] self.assertEqual({}, output_filters) self.assertEqual(expected_error_object, output_error_object)
def test_when_there_both_part_time_and_full_time_filters_set_returns_error(self): filters = "full_time,-part_time" validator = Validator("", filters, "Coventry University", "", "", "0", 100, "0", "en") output_filters, output_error_object = validator.validate_filters() expected_error_object = [ { "error": "cannot have both part_time and full_time filters set", "error_values": [{"filters": "part_time,full_time"}], } ] self.assertEqual({}, output_filters) self.assertEqual(expected_error_object, output_error_object)
def test_when_offset_is_a_negative_number(self): offset = "-20" validator = Validator("", "", "Coventry University", "", "", "0", 100, offset, "en") expected_result = -20 expected_error_object = [ { "error": "offset needs to be a positive number, offset cannot be lower than 0", "error_values": [{"offset": offset}], } ] output_result, output_error_object = validator.validate_offset() self.assertEqual(expected_result, output_result) self.assertEqual(expected_error_object, output_error_object)
def test_when_offset_is_not_a_number(self): offset = "twenty" validator = Validator("", "", "Coventry University", "", "", "0", 100, offset, "en") expected_result = 0 expected_error_object = [ { "error": "offset value needs to be a number", "error_values": [{"offset": offset}], } ] output_result, output_error_object = validator.validate_offset() self.assertEqual(expected_result, output_result) self.assertEqual(expected_error_object, output_error_object)
def test_when_limit_is_not_a_number(self): limit = "twenty" max_limit = 100 validator = Validator("", "", "Coventry University", "", "", limit, max_limit, "0", "en") expected_result = 0 expected_error_object = [ { "error": "limit value needs to be a number", "error_values": [{"limit": limit}], } ] output_result, output_error_object = validator.validate_limit() self.assertEqual(expected_result, output_result) self.assertEqual(expected_error_object, output_error_object)
def test_when_limit_is_a_negative_number(self): limit = "-20" max_limit = 100 validator = Validator("", "", "Coventry University", "", "", limit, max_limit, "0", "en") expected_result = -20 expected_error_object = [ { "error": "limit needs to be a positive number, limit cannot be lower than 0", "error_values": [{"limit": "-20"}], } ] output_result, output_error_object = validator.validate_limit() self.assertEqual(expected_result, output_result) self.assertEqual(expected_error_object, output_error_object)
def test_when_all_filters_are_selected_returns_no_error(self): filters = "-distance_learning,honours_award,foundation_year,sandwich_year,year_abroad,-full_time" validator = Validator("", filters, "Coventry University", "", "", "0", 100, "0", "en") output_filters, output_error_object = validator.validate_filters() expected_filters = { "distance_learning": False, "foundation_year": True, "full_time": False, "honours_award": True, "sandwich_year": True, "year_abroad": True, } self.assertEqual(expected_filters, output_filters) self.assertEqual([], output_error_object)
def test_when_institution_param_is_empty(self): limit = "10" max_limit = 100 validator = Validator("", "", "", "", "", limit, max_limit, "0", "en") expected_result = [] expected_error_object = [ { 'error': 'missing value for mandatory field: institution', 'error_values': [{'institution': ''}], } ] output_result, output_error_object = validator.validate_institution() self.assertEqual(expected_result, output_result) self.assertEqual(expected_error_object, output_error_object)
def test_when_limit_is_outside_max_limit(self): limit = "200" max_limit = 100 validator = Validator("", "", "Coventry University", "", "", limit, max_limit, "0", "en") expected_result = 200 expected_error_object = [ { "error": "limit cannot exceed maximum value of 100", "error_values": [{"limit": "200"}], } ] output_result, output_error_object = validator.validate_limit() self.assertEqual(expected_result, output_result) self.assertEqual(expected_error_object, output_error_object)