Ejemplo n.º 1
0
    def test_total_score() -> None:
        """Verify the functionality of KarmaItem.total_score"""
        item = KarmaItem("foobar", 10, 4)
        assert item.total_score == 6

        item = KarmaItem("foobar", 10, 20)
        assert item.total_score == -10
Ejemplo n.º 2
0
    def test_str() -> None:
        """Simple test to verify __str__ works"""
        item = KarmaItem("testName", 1, 1)
        assert str(
            item) == "testName has 1 plus and 1 minus for a total of 0 points."

        item = KarmaItem("foobar", 3, 2)
        assert str(
            item
        ) == "foobar has 3 pluses and 2 minuses for a total of 1 point."
Ejemplo n.º 3
0
    def decrement_karma(self, msg: dict) -> str:
        """Decrement karma for a passed item, and pass a corresponding message to the
        channel inside which the karma was bumped to be sent.

        Arguments:
        msg -- text containing a karma event

        Returns:
        A message to be sent back to the channel in which the karma event occurred.
        """
        self.logger.debug("Processing decrement message.")
        if self._check_for_self_bump(msg):
            self.logger.debug("Skipping self-decrement")
            return "Now, now.  Don't be so hard on yourself!"

        if self._check_for_url(msg):
            return None  # Fail silently... no need to respond to the user.

        item = self._clean_up_msg_text(msg)
        if not self.karma.get(item):
            self.karma[item] = KarmaItem(item)
        self.karma[item].minuses += 1
        snark = get_negative_message()
        total = self.karma[item].total_score
        self._save_karma_to_json_file()
        self.logger.debug("Got decrement for %s", item)
        return f"{snark} {item} now has {total} points."
Ejemplo n.º 4
0
    def increment_karma(self, msg: dict) -> str:
        """Increment karma for a passed item, and pass a corresponding message to the
        channel inside which the karma was bumped to be sent.

        Arguments:
        msg -- text containing a karma event

        Returns:
        The message to be sent back to the channel in which the karma event occurred.
        """
        self.logger.debug("Processing increment message.")
        if self._check_for_self_bump(msg):
            self.logger.debug("Skipping self-increment")
            return "Ahem, no self-karma please!"

        tail = f", thanks to {self.get_username_from_uid(msg['user'])}."

        item = self._clean_up_msg_text(msg)
        if not self.karma.get(item):
            self.karma[item] = KarmaItem(item)
        self.karma[item].pluses += 1
        snark = get_positive_message()
        total = self.karma[item].total_score
        self._save_karma_to_json_file()
        self.logger.debug("Got increment for %s", item)
        return f"{snark} {item} now has {total} points{tail}."
Ejemplo n.º 5
0
    def test_json_file_load_write(self, _) -> None:
        """Test the functionality of JSON file loading and saving for KarmaItem objects"""
        assert not os.path.exists(self.karma_file_path)
        bot = KarmaBot(token=os.environ.get("SLACK_BOT_TOKEN"), )
        assert os.path.exists(self.karma_file_path)
        assert not bot.karma

        with open(self.karma_file_path, "w", encoding="utf-8") as file_ptr:
            file_ptr.write('[{"name": "foobar", "pluses": 1, "minuses": 1}]')

        bot._load_karma_from_json_file()
        assert "foobar" in bot.karma
        assert bot.karma.get("foobar").pluses == 1
        assert bot.karma.get("foobar").minuses == 1

        bot.karma["baz"] = KarmaItem("baz", 10, 10)
        bot._save_karma_to_json_file()
        with open(self.karma_file_path, "r", encoding="utf-8") as file_ptr:
            lines = file_ptr.readline()
            assert "baz" in lines

        bot.karma = {}
        bot._load_karma_from_json_file()
        assert bot.karma.get("baz").pluses == 10
        assert bot.karma.get("baz").minuses == 10

        self.cleanup()
Ejemplo n.º 6
0
    def test_dict_to_karmaitem() -> None:
        """Verify that transforming a dict into a KarmaItem works"""
        karma_dict = {
            "name": "foobar",
            "pluses": 60,
            "minuses": 10,
        }
        karma = KarmaItem.dict_to_karmaitem(karma_dict)
        assert isinstance(karma, KarmaItem)
        assert karma.name == karma_dict["name"]
        assert karma.pluses == karma_dict["pluses"]
        assert karma.minuses == karma_dict["minuses"]

        input_dict = {
            "foo": "foo",
            "bar": "bar",
        }
        output = KarmaItem.dict_to_karmaitem(input_dict)
        assert output == input_dict
Ejemplo n.º 7
0
    def test_json() -> None:
        # Passing an object to KarmaItemEncoder which is not a KarmaItem should use the
        # parent JSONEncoder class instead.
        """Verify that JSON serialization works"""
        encoder = KarmaItemEncoder()
        result = encoder.default(KarmaItem("foobarbaz", 9001, 10))
        assert isinstance(result, dict)
        assert result == {"name": "foobarbaz", "pluses": 9001, "minuses": 10}

        # Pushing a dict through the KarmaItemEncoder should result in a TypeError being
        # thrown from the parent JSONEncoder class.
        failed = False
        try:
            encoder.default({"foo": "bar"})
        except TypeError:
            failed = True
        assert failed
Ejemplo n.º 8
0
 def test_repr() -> None:
     """Simple test to verify __repr__ works"""
     assert repr(KarmaItem("testName", 5,
                           6)) == "KarmaItem('testName', 5, 6)"