def test_config_exists(tmp_path: pathlib.Path): """This test will tests if the config exists""" # GIVEN # STANDARD_CONFIG_DATA configpath = tmp_path / "reportdailyrc" # WHEN = expect a config file is created in tmp_path rd.create_config(STANDARD_CONFIG_DATA, configpath) # THEN config file existence is True assert os.path.exists(configpath) is True
def test_start_year_relation_namespace(tmp_path: pathlib, wrong_year, right_year, args_change): """ Check if the year can be changed by the cmd namespace. Test for check_start_year_relation_namespace() function. """ # GIVEN configpath = tmp_path / "reportdaily_year_namespace" args_change.year = wrong_year expected_year_after_change = "1000" data_after_change_dict = {'start_year': right_year} # WHEN # create config file and read it rd.create_config(STANDARD_CONFIG_DATA, configpath) config = configparser.ConfigParser() config.read(configpath) with open(configpath, 'r') as configfile: configs_before_change = configfile.read() def mock_input(txt): """ PATCH USER INPUT """ return right_year with patch.object(builtins, 'input', mock_input): rd.check_start_year_relation_namespace(args_change, configpath) # save the changed config config.read(configpath) with open(configpath, 'r') as configfile: config_after_change = configfile.read() # save all values configs_after_change_dict = dict(config.items("settings")) # read config values config = configparser.ConfigParser() config.read(configpath) # THEN # check if the file has changed assert configs_before_change != config_after_change # prove if the expected values are in our changed config assert data_after_change_dict.items() <= configs_after_change_dict.items() # check year is the same as change assert expected_year_after_change == config.get("settings", "start_year")
def test_change_config_namespace(args_ns, tmp_path: pathlib, wrong_duration, right_duration, count_teams): """This test will check if the change option works""" # GIVEN # STANDARD_CONFIG_DATA configpath = tmp_path / "reportdailyrc" args_ns.duration = wrong_duration args_ns.change = True data_after_change_dict = { "name": args_ns.name, "team": args_ns.team, "count_teams": count_teams, "team_number": args_ns.team_number, "start_year": args_ns.year, "duration": right_duration, } # WHEN # create config file and read it rd.create_config(STANDARD_CONFIG_DATA, configpath) config = configparser.ConfigParser() config.read(configpath) with open(configpath, 'r') as configfile: configs_before_change = configfile.read() def mock_input(txt): """ PATCH USER INPUT """ return right_duration with patch.object(builtins, 'input', mock_input): rd.namespace_config_change(args_ns, configpath) # save the changed config config.read(configpath) with open(configpath, 'r') as configfile: config_after_change = configfile.read() # save all values configs_after_change_dict = dict(config.items("settings")) # THEN # check if the file has changed assert configs_before_change != config_after_change # prove if the expected values are in our changed config assert data_after_change_dict.items() <= configs_after_change_dict.items()
def test_config_section_option_namespace(tmp_path: pathlib.Path): """This test will check the config file if the sections and options are correct""" # GIVEN # STANDARD_SECTIONS # STANDARD_OPTIONS configpath = tmp_path / "reportdailyrc" # WHEN rd.create_config(STANDARD_CONFIG_DATA, configpath) config = configparser.ConfigParser() config.read(configpath) # THEN for section in STANDARD_SECTIONS: assert config.has_section(section) for key in STANDARD_OPTIONS: assert config.has_option(section, key)
def test_config_real_section(tmp_path: pathlib): """This test will check if all the sections are correct""" # GIVEN std_keys = set(STANDARD_CONFIG) configpath = tmp_path / "reportdailyrc" # WHEN # create config rd.create_config(STANDARD_CONFIG_DATA, configpath) config = configparser.ConfigParser() config.read(configpath) real_keys = set(config.keys()) # need to delete default key. DEFAULT is created automatically by the configparser module. real_keys.remove("DEFAULT") # THEN assert real_keys == std_keys
def test_show_config(tmp_path: pathlib, capsys): """Test if awaited part of the output is in sdtout""" # GIVEN configpath = tmp_path / "reportdailyrc" awaited_part_of_output = "CONFIGURATION:" # WHEN # create a config file rd.create_config(STANDARD_CONFIG_DATA, configpath) config = configparser.ConfigParser() # show the config rd.show_config(configpath) # capture input with pystest (capsys) captured = capsys.readouterr() # THEN check the captured output if awaited part is in output assert awaited_part_of_output in captured.out
def test_wrong_input_change(args_change, tmp_path: pathlib): # GIVEN user_wrong_input = "TEST" user_right_input = "Name" user_changed_value = "TESTNAME" awaited_output = "No key in config found --> Try again" # path configpath = tmp_path / "reportdailyrc" # WHEN # create a config file rd.create_config(STANDARD_CONFIG_DATA, configpath) config = configparser.ConfigParser() config.read(configpath) # open config and save it for visibility with open(configpath, 'r') as configfile: configs_before_change = dict(config.items("settings")) # simultate input of user run_once = 0 def mock_input(txt): "This is our function that patches the builtin input function. " nonlocal run_once while True: if txt.lower().startswith("please enter") and run_once == 0: run_once += 1 return user_wrong_input if txt.lower().startswith("please enter") and run_once == 1: run_once += 1 return user_right_input if txt.lower().startswith("enter") and run_once == 2: run_once += 1 return user_changed_value break # patch the input with patch.object(builtins, 'input', mock_input): # run config input function rd.user_input_change(args_change, configpath) # THEN assert awaited_output in awaited_output
def test_change_by_input(args_change, tmp_path: pathlib.Path, user_category, user_change, user_key): """This test will simulate user input and check the changes""" # GIVEN # user_category, user_change, user_key configpath = tmp_path / "reportdailyrc" # WHEN # create a config file rd.create_config(STANDARD_CONFIG_DATA, configpath) config = configparser.ConfigParser() config.read(configpath) # open config and save it for visability with open(configpath, 'r') as configfile: # save dict before changes configs_before_change = dict(config.items("settings")) # patch your input test_value = 0 def mock_input(txt): "This is our function that patches the builtin input function. " nonlocal test_value if txt.lower().startswith("please"): test_value += 1 return user_category if txt.lower().startswith("enter"): test_value += 2 return user_change with patch.object(builtins, 'input', mock_input): rd.user_input_change(args_change, configpath) # read changed value config.read(configpath) configs_after_change = dict(config.items("settings")) # THEN assert configs_after_change.get(user_key) == user_change
def test_duration_relation(awaited_duration, input_duration, count_teams, tmp_path: pathlib.Path, args_change): """ Test the return value of duration by change """ # GIVEN # STANDARD_CONFIG_DATA configpath = tmp_path / "reportdaily_config" # function will be patched return_duration = "None" # WHEN # create config rd.create_config(STANDARD_CONFIG_DATA, configpath) # counter for input patch counter = 0 def mock_input(txt): "This is our function that patches the builtin input function. " nonlocal counter while (True): if txt.lower().strip().startswith("how long") and counter == 0: counter += 1 return input_duration elif txt.lower().strip().startswith("how") and counter == 1: counter += 1 return "3.0" elif counter == 3: break with patch.object(builtins, 'input', mock_input): return_duration = rd.check_duration_relation(args_change, configpath) # read config config = configparser.ConfigParser() config.read(configpath) # THEN assert awaited_duration == return_duration assert count_teams == config.get("settings", "count_teams")
def test_team_number_relation(mocker_check_duration, mocker_check_team, wrong_input1, answer_change_name, answer_duration_change, wrong_input2, team_number, tmp_path: pathlib, args_change): """ Check values of check_team_number_relation function. Simulate wrong user input , mixed with right answer. Assert value change is correct """ # GIVEN # STANDARD_CONFIG_DATA configpath = tmp_path / "reportdaily_team_number_input" expected_team_number = "6" # WHEN # create config file and read it rd.create_config(STANDARD_CONFIG_DATA, configpath) config = configparser.ConfigParser() config.read(configpath) with open(configpath, 'r') as configfile: configs_before_change = configfile.read() # patch the user_input_change function from reportdaily mocker_check_duration.return_value = True mocker_check_team.return_value = True # simulate user input counter = 0 def mock_input(txt): """ Patch user input for check_team_number_relatio """ nonlocal counter if txt.lower().strip().startswith("what team number"): return int(team_number) elif txt.lower().strip().startswith("did the name") and counter == 0: counter += 1 return wrong_input1 elif txt.lower().strip().startswith("did the name") and counter == 1: counter += 1 return answer_change_name elif txt.lower().strip().startswith( "did the duration") and counter == 2: counter += 1 return wrong_input2 elif txt.lower().strip().startswith( "did the duration") and counter == 3: counter += 1 return answer_duration_change else: print("No right string found") with patch.object(builtins, 'input', mock_input): rd.check_team_number_relation(args_change, configpath) # read config values config = configparser.ConfigParser() config.read(configpath) # THEN assert expected_team_number == config.get("settings", "team_number")
def test_create_config_user_input(args_ns_empty, tmp_path: pathlib, duration, count_teams, end_duration_education): # GIVEN name = "TESTNAME" team = "TESTTEAM" year = "2020" wrong_year = "wrong_year" # duration duration_right = "3.0" # count teams team_number = 4 wrong_team_number = 10 wrong_team_number_txt = "ewgwer" configpath = tmp_path / "reportdailyrc" # WHEN # patch the input of user count_of_questions_int = 0 count_of_questions_char = 0 count_of_questions_wrong_input = 0 count_of_questions_team_number = 0 def mock_input(txt): "This is our function that patches the builtin input function. " nonlocal count_of_questions_int nonlocal count_of_questions_char nonlocal count_of_questions_wrong_input nonlocal count_of_questions_team_number if txt.lower().strip().startswith("please enter your full name"): return name if txt.lower().strip().startswith( "in which" ): # Handle the wrong input, insert right input as next while True: if count_of_questions_wrong_input == 0: count_of_questions_wrong_input += 1 return wrong_year elif count_of_questions_wrong_input == 1: count_of_questions_wrong_input += 1 return year elif count_of_questions_wrong_input == 2: break if txt.lower().strip().startswith("how long is your apprenticeship?" ) and count_of_questions_int == 0: count_of_questions_int = 1 return duration # capture wrong input! elif txt.lower().strip().startswith("how long is your apprenticeship?" ) and count_of_questions_int == 1: count_of_questions_int = 2 return duration_right elif txt.lower().strip().startswith( "how long is your apprenticeship?") and duration == "y": count_of_questions_char = 1 return duration_right if txt.lower().strip().startswith("enter your current team name"): return team if txt.lower().strip().startswith("what team"): # capture wrong input, to high number and character while True: if count_of_questions_team_number == 0: count_of_questions_team_number += 1 return wrong_team_number elif count_of_questions_team_number == 1: count_of_questions_team_number += 1 return wrong_team_number_txt elif count_of_questions_team_number == 2: count_of_questions_team_number += 1 return team_number elif count_of_questions_team_number == 3: break return team_number with patch.object(builtins, 'input', mock_input): dict_values = rd.collect_config_data(args_ns_empty) # open configs rd.create_config(dict_values, configpath) config = configparser.ConfigParser() config.read(configpath) with open(configpath, 'r') as configfile: configs_after_creation = dict(config.items("settings")) # THEN assert configs_after_creation.get("name") == name assert configs_after_creation.get("team") == team assert configs_after_creation.get("team_number") == str(team_number) if count_of_questions_int == 1: assert configs_after_creation.get("duration") == duration elif count_of_questions_int == 2: assert configs_after_creation.get("duration") == duration_right elif count_of_questions_char == 1: assert configs_after_creation.get("duration") == duration_right assert configs_after_creation.get("count_teams") == count_teams assert configs_after_creation.get("start_year") == year assert configs_after_creation.get( "end_duration_education") == end_duration_education