Ejemplo n.º 1
0
    def test_settings_repository_can_create_new_settings_without_default_file(self):
        self.assertFalse(os.path.exists(self._new_test_default_path))
        no_default_repository = SettingsRepository(self._new_test_default_path,
                                                   Config.USER_CSV_PATH)
        settings = no_default_repository.get_settings_by_username(
            self.TEST_NEW_USER_NAME)

        self.assertEqual(Settings(), settings)
Ejemplo n.º 2
0
    def _create_default_settings(self):
        changed_default_setting = Settings.SUB_TIMELIMIT
        changed_default_value = 12345

        with open(Config.USER_DEFAULT_CSV_FILEPATH, mode="a") as settings_file:
            settings_file.write(
                f"{changed_default_setting},{changed_default_value}")

        settings = Settings()
        settings.set_setting(changed_default_setting, changed_default_value)

        return settings
Ejemplo n.º 3
0
    def test_operations_with_greater_operand_minimum_defaults_to_max(self):
        settings = Settings()
        settings.set_setting(settings.MULTIPLY_OPERAND1_MIN, 100)
        settings.set_setting(settings.MULTIPLY_OPERAND1_MAX, 9)
        settings.set_setting(settings.MULTIPLY_OPERAND2_MIN, 10)
        settings.set_setting(settings.MULTIPLY_OPERAND2_MAX, 9)
        self.assertEqual(100,
                         settings.get_setting(settings.MULTIPLY_OPERAND1_MIN))
        self.assertEqual(9,
                         settings.get_setting(settings.MULTIPLY_OPERAND1_MAX))

        operation = self._arithmetic.get_multiplication_question(settings)
        self.assertEqual("9 * 9", operation.__str__())
Ejemplo n.º 4
0
 def test_generated_random_question_when_none_are_selected(self):
     settings = Settings()
     settings.set_settings_from_dict({
         settings.RANDOM_USE_ADD: 0,
         settings.RANDOM_USE_DIVIDE: 0,
         settings.RANDOM_USE_MULTIPLY: 0,
         settings.RANDOM_USE_SUB: 0
     })
     operation = self._arithmetic.get_random_question(settings)
     self.assertTrue(
         isinstance(operation, MultiplicationOperation)
         or isinstance(operation, DivisionOperation)
         or isinstance(operation, AdditionOperation)
         or isinstance(operation, SubstractionOperation))
Ejemplo n.º 5
0
 def test_generated_random_question_is_within_setting_parameters(self):
     settings = Settings()
     settings.set_settings_from_dict({
         settings.RANDOM_USE_ADD: 1,
         settings.RANDOM_USE_DIVIDE: 0,
         settings.RANDOM_USE_MULTIPLY: 0,
         settings.RANDOM_USE_SUB: 0
     })
     operation = self._arithmetic.get_random_question(settings)
     parts = operation.__str__().split(" + ")
     op1_min = settings.get_setting(settings.ADD_OPERAND1_MIN)
     op1_max = settings.get_setting(settings.ADD_OPERAND1_MAX)
     op2_min = settings.get_setting(settings.ADD_OPERAND2_MIN)
     op2_max = settings.get_setting(settings.ADD_OPERAND2_MAX)
     self.assertTrue(op1_min <= int(parts[0]) <= op1_max)
     self.assertTrue(op2_min <= int(parts[1]) <= op2_max)
Ejemplo n.º 6
0
    def _get_default_settings(self):
        """Returns default user settings as defined by the default settings file.

        If the default settings file does not exist, the method creates a new one from
        the default values set in a new Settings-object.

        Returns:
            Settings: a default Settings-object.
        """
        settings = Settings()

        try:
            settings = self._read_settings_file(self._default_filepath)
        except FileNotFoundError:
            self._write_settings(self._default_filepath, settings)

        return settings
Ejemplo n.º 7
0
    def _read_settings_file(self, settings_filepath):
        """Tries to open and read the settings file named by settings_filepath.

        Args:
            settings_filepath (str): Filepath to the settings file.

        Returns:
            Settings: A Settings-object containing the file settings or a default one.
        """
        user_settings = Settings()

        try:
            with open(settings_filepath) as settings_file:
                reader = csv.reader(settings_file, delimiter=",")
                user_settings = self._parse_settings(reader)
        except PermissionError:
            print(f"Failed to read user settings: {settings_filepath}")

        return user_settings
Ejemplo n.º 8
0
    def _parse_settings(self, reader):
        """Parses a settings file using the given reader.

        Args:
            reader (csv.reader): A csv reader.

        Returns:
            Settings: Parsed settings or a newly created Settings-object.
        """
        settings = Settings()

        for line in reader:
            if len(line) != 2:
                continue
            try:
                setting = line[0]
                settings.parse_and_set_setting(setting, line[1])
            except ValueError:
                continue

        return settings
Ejemplo n.º 9
0
 def setUp(self):
     self._arithmetic = ArithmeticService()
     self._settings = Settings()
Ejemplo n.º 10
0
 def setUp(self):
     self._settings = Settings()
Ejemplo n.º 11
0
 def test_setting_non_existent_setting_name_changes_nothing(self):
     new_settings = Settings()
     new_settings.set_setting("no_such_name", 123)
     new_settings.parse_and_set_setting("no_such_name", "123")
     self.assertEqual(new_settings, self._settings)
Ejemplo n.º 12
0
    def get_settings_by_username(self, username):
        if username not in self._settings:
            self._settings[username] = (Settings(), 0)

        return self._settings[username]