Exemplo n.º 1
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
Exemplo n.º 2
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))
Exemplo n.º 3
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)
Exemplo n.º 4
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
Exemplo n.º 5
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
Exemplo n.º 6
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
Exemplo n.º 7
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)
Exemplo n.º 8
0
 def setUp(self):
     self._arithmetic = ArithmeticService()
     self._settings = Settings()
Exemplo n.º 9
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__())
Exemplo n.º 10
0
class TestSettings(unittest.TestCase):
    OPERANDS = [Settings.MULTIPLY_OPERAND1_MIN,
                Settings.MULTIPLY_OPERAND1_MAX,
                Settings.MULTIPLY_OPERAND2_MIN,
                Settings.MULTIPLY_OPERAND2_MAX,
                Settings.DIVIDE_OPERAND1_MIN,
                Settings.DIVIDE_OPERAND1_MAX,
                Settings.DIVIDE_OPERAND2_MIN,
                Settings.DIVIDE_OPERAND2_MAX,
                Settings.ADD_OPERAND1_MIN,
                Settings.ADD_OPERAND1_MAX,
                Settings.ADD_OPERAND2_MIN,
                Settings.ADD_OPERAND2_MAX,
                Settings.SUB_OPERAND1_MIN,
                Settings.SUB_OPERAND1_MAX,
                Settings.SUB_OPERAND2_MIN,
                Settings.SUB_OPERAND2_MAX]
    TIMELIMITS = [Settings.MULTIPLY_TIMELIMIT,
                  Settings.DIVIDE_TIMELIMIT,
                  Settings.ADD_TIMELIMIT,
                  Settings.SUB_TIMELIMIT,
                  Settings.RANDOM_TIMELIMIT]
    BOOLEANS = [Settings.MULTIPLY_TIMER,
                Settings.DIVIDE_TIMER,
                Settings.ADD_TIMER,
                Settings.SUB_TIMER,
                Settings.RANDOM_TIMER,
                Settings.RANDOM_USE_MULTIPLY,
                Settings.RANDOM_USE_DIVIDE,
                Settings.RANDOM_USE_ADD,
                Settings.RANDOM_USE_SUB,
                Settings.HIDE_KEYPAD]

    def setUp(self):
        self._settings = Settings()

    def test_operands_are_not_set_to_bad_values(self):
        bad_values = {}
        for operand_name in self.OPERANDS:
            bad_values[operand_name] = "-1"

        self._settings.set_settings_from_dict(bad_values)

        for operand_name in self.OPERANDS:
            self.assertNotEqual(self._settings.get_setting(operand_name), -1)

    def test_operands_are_set_to_correct_values(self):
        good_values = {}
        for operand_name in self.OPERANDS:
            good_values[operand_name] = str(OperandSettingValue.MIN_VALUE)

        self._settings.set_settings_from_dict(good_values)

        for operand_name in self.OPERANDS:
            self.assertEqual(self._settings.get_setting(
                operand_name), OperandSettingValue.MIN_VALUE)

    def test_timelimits_are_not_set_to_bad_values(self):
        bad_values = {}
        for timelimit_name in self.TIMELIMITS:
            bad_values[timelimit_name] = "-1"

        self._settings.set_settings_from_dict(bad_values)

        for timelimit_name in self.TIMELIMITS:
            self.assertNotEqual(self._settings.get_setting(timelimit_name), -1)

    def test_timelimits_are_set_to_correct_values(self):
        good_values = {}
        for timelimit_name in self.TIMELIMITS:
            good_values[timelimit_name] = str(TimelimitSettingValue.MIN_VALUE)

        self._settings.set_settings_from_dict(good_values)

        for timelimit_name in self.TIMELIMITS:
            self.assertEqual(self._settings.get_setting(
                timelimit_name), TimelimitSettingValue.MIN_VALUE)

    def test_booleans_are_not_set_to_bad_values(self):
        bad_values = {}
        for boolean_name in self.BOOLEANS:
            bad_values[boolean_name] = "-1"

        self._settings.set_settings_from_dict(bad_values)

        for boolean_name in self.BOOLEANS:
            self.assertNotEqual(self._settings.get_setting(boolean_name), -1)

    def test_booleans_are_set_to_correct_values(self):
        good_values = {}
        for boolean_name in self.BOOLEANS:
            good_values[boolean_name] = "0"

        self._settings.set_settings_from_dict(good_values)

        for boolean_name in self.BOOLEANS:
            self.assertEqual(self._settings.get_setting(boolean_name), 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)

    def test_accessing_non_existent_setting_returns_none(self):
        self.assertIsNone(self._settings.get_setting("no_such_name"))
Exemplo n.º 11
0
 def setUp(self):
     self._settings = Settings()
Exemplo n.º 12
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)
Exemplo n.º 13
0
    def get_settings_by_username(self, username):
        if username not in self._settings:
            self._settings[username] = (Settings(), 0)

        return self._settings[username]