bastisawesome / guessinggame_ttv

A Twitch bot to play a word guessing game
BSD 3-Clause "New" or "Revised" License
0 stars 1 forks source link

Database Manager Add_X Functions Should Validate Amounts #23

Closed bastisawesome closed 1 year ago

bastisawesome commented 1 year ago

In line with #21, tests should also be updated to show what happens in this situation.

The database manager currently assumes that when calling add_X (e.g. add_tokens) the amount passed is positive. This is not guaranteed at all, however, and in the event of passing in negative numbers could break the implied constraint that both tokens and score should be positive integers. There are two major flaws with the current logic:

  1. The amounts are never validated as positive
  2. The amounts are never validated as integers

The first issue should be made a non-issue, and in fact it would be possible to have the remove_X functions just call back to add_X. The add_X logic would then account for negatives and adjust the result as necessary. This means the remove_X functions will have minimal logic and act primarily as a wrapper function to the equivalent add_X. Example:

def remove_tokens(self, username: str, amount: int) -> None:
    neg_amount: int = -amount  # Convert amount into a negative number
    self.add_tokens(username, amount)

This simplifies the codebase greatly and ensures that only one test needs to be made for remove_X functions, to ensure that they properly adjust the amount and call the add_X functions.

The second issue is more complicated and could be resolved in a number of ways:

bastisawesome commented 1 year ago

I'm a dumbass and missed the most basic solution: using Python's built-in casting functions. While this method is not necessarily ideal, in that it relies on the behaviour of the built-in casting, it doesn't require an explicit bias for what happens the the numbers. So either developer bias on how to handle the numbers, or implementation bias on how to handle the numbers.