Example #1
0
    def test_karma_calculation_bug(self):
        toms_karma = list([
            Karma(awarded_to_username="******",
                  reason="",
                  awarded_by_username="******",
                  awarded=datetime.now(),
                  karma_type=KarmaType.NEGGYNEG),
            Karma(awarded_to_username="******",
                  reason="",
                  awarded_by_username="******",
                  awarded=datetime.now(),
                  karma_type=KarmaType.NEGGYNEG),
            Karma(awarded_to_username="******",
                  reason="",
                  awarded_by_username="******",
                  awarded=datetime.now(),
                  karma_type=KarmaType.NEGGYNEG)
        ])

        for k in toms_karma:
            k.save()

        net_karma = Karma.get_current_net_karma_for_recipient("tomallen")

        self.assertEqual(-3, net_karma)
Example #2
0
    def test_get_net_karma_any_input(self):
        expected_reasons = {'reasonless': 10, 'reasoned': [Karma(), Karma()]}
        username = "******"
        with patch.object(Karma,
                          "get_current_karma_reasons_for_recipient",
                          return_value=expected_reasons) as test_method:
            c = GetCurrentKarmaReasonsCommand()
            actual_reasons = c.execute(username)

            self.assertEqual(expected_reasons, actual_reasons)
            test_method.assert_called_once_with(username)
Example #3
0
    def test_get_current_karma_with_expired(self):
        # Arrange
        newly_awarded = datetime.today()
        award_ages_ago = datetime.today() - timedelta(days=31)
        new_karma = Karma(awarded=newly_awarded)
        old_karma = Karma(awarded=award_ages_ago)
        karma_list = list([new_karma, old_karma])
        m = Member(karma_list)

        # Act
        current_karma = m.get_current_karma()

        # Assert
        self.assertEqual(1, current_karma)
Example #4
0
    def test_persistence(self):
        # Arrange
        Config().connect_to_db()
        awarded_to = "testUser1"
        awarded_by = "testUser2"
        test_reason = "testReason"
        test_type = KarmaType.POZZYPOZ
        k = Karma(awarded_to_username=awarded_to,
                  reason=test_reason,
                  awarded_by_username=awarded_by,
                  awarded=datetime.now(),
                  karma_type=str(test_type))

        # Act
        c = AddKarmaCommand()
        saved_karma = c.execute(k.awarded_to_username, k.awarded_by_username,
                                k.reason, k.karma_type)

        # Assert
        self.assertIsNotNone(saved_karma._id)
        self.assertEqual(k.awarded_by_username,
                         saved_karma.awarded_by_username)
        self.assertEqual(k.reason, saved_karma.reason)
        self.assertEqual(k.awarded_to_username,
                         saved_karma.awarded_to_username)
        self.assertEqual(k.karma_type, saved_karma.karma_type)
        self.assertIsNotNone(saved_karma.awarded)

        # Tear down
        saved_karma.delete()
Example #5
0
    def test_add_new_karma(self):
        # Arrange
        m = Member(default_karma_list())
        k = Karma(karma_type=KarmaType.POZZYPOZ)

        # Act
        m.add_karma(k)

        # Assert
        self.assertEqual(1, m.get_current_karma())
Example #6
0
    def execute(self, awarded_to, awarded_by, reason, karma_type):
        self.config.connect_to_db()
        k = Karma(awarded_to_username=awarded_to,
                  reason=reason,
                  awarded_by_username=awarded_by,
                  awarded=datetime.now(),
                  karma_type=str(karma_type))

        k.save()
        return k
Example #7
0
def create_karma(awarded_by_username=test_awarder,
                 awarded_to_username=test_recipient,
                 reason=Karma.default_reason,
                 awarded=datetime.today(),
                 karma_type=KarmaType.POZZYPOZ):
    k = Karma(awarded_by_username=awarded_by_username,
              awarded_to_username=awarded_to_username,
              reason=reason,
              awarded=awarded,
              karma_type=karma_type)
    return k
Example #8
0
 def test_negative_karma_can_be_awarded_by_others(self):
     awarded_to = "testUser1"
     awarded_by = "testUser2"
     test_reason = "testReason"
     test_awarded = datetime.today()
     test_type = str(KarmaType.NEGGYNEG)
     k = Karma(awarded_to_username=awarded_to,
               reason=test_reason,
               awarded_by_username=awarded_by,
               awarded=test_awarded,
               karma_type=test_type)
     k.save()
Example #9
0
    def test_get_karma_reasons_one_non_default(self):
        # Arrange
        karma_list = default_karma_list()
        karma_with_reason = Karma(reason="This is a silly reason")
        karma_list.append(karma_with_reason)
        m = Member(karma_list)

        # Act
        karma_reasons = m.get_karma_reasons()

        # Assert
        self.assertEqual(len(default_karma_list()), karma_reasons['reasonless'])
        self.assertEqual(list([karma_with_reason]), karma_reasons['reasoned'])
Example #10
0
    def test_positive_karma_cannot_be_self_awarded(self):
        awarded_to = "testUser1"
        awarded_by = "testUser1"
        test_reason = "testReason"
        test_awarded = datetime.today()
        test_type = str(KarmaType.POZZYPOZ)
        k = Karma(awarded_to_username=awarded_to,
                  reason=test_reason,
                  awarded_by_username=awarded_by,
                  awarded=test_awarded,
                  karma_type=test_type)

        self.assertRaises(ValidationError, k.save)
Example #11
0
    def test_karma_reasons_do_not_filter_on_substrings(self):
        # Arrange
        dan_karma = Karma(awarded_to_username="******",
                          reason="a reason",
                          awarded_by_username=test_awarder,
                          awarded=datetime.now(),
                          karma_type=KarmaType.POZZYPOZ)
        dan_karma.save()

        seniordaniel_karma = Karma(awarded_to_username="******",
                                   reason="a different reason",
                                   awarded_by_username=test_awarder,
                                   awarded=datetime.now(),
                                   karma_type=KarmaType.POZZYPOZ)
        seniordaniel_karma.save()

        expected_reasoned_karma = list([dan_karma])

        # Act
        dan_reasons = Karma.get_current_karma_reasons_for_recipient("dan")

        # Assert
        self.assertEqual(expected_reasoned_karma, dan_reasons["reasoned"])
Example #12
0
    def test_persistence(self):
        # Arrange
        awarded_to = "testUser1"
        awarded_by = "testUser2"
        test_reason = "testReason"
        test_awarded = datetime.today()
        test_type = str(KarmaType.POZZYPOZ)
        k = Karma(awarded_to_username=awarded_to,
                  reason=test_reason,
                  awarded_by_username=awarded_by,
                  awarded=test_awarded,
                  karma_type=test_type)

        # Act
        k.save()

        # Assert
        loaded_karma = Karma.objects.all()[0]
        self.assertEqual(k.awarded_to_username,
                         loaded_karma.awarded_to_username)
        self.assertEqual(k.reason, loaded_karma.reason)
        self.assertEqual(k.awarded_by_username,
                         loaded_karma.awarded_by_username)
        self.assertEqual(k.karma_type, loaded_karma.karma_type)
Example #13
0
    "user": "******"
}
valid_event_no_recipient = {
    "text": test_prefix + "reasons",
    "user": "******"
}
invalid_event = {'text': '_g test'}
fake_member = Member(username="******", slack_id="slack_id")
help_event = {"text": test_prefix + "reasons -?"}
command_result = {
    "reasonless":
    10,
    "reasoned":
    list([
        Karma(reason="reason1",
              karma_type=KarmaType.POZZYPOZ,
              awarded_by_username="******"),
        Karma(reason="reason2",
              karma_type=KarmaType.POZZYPOZ,
              awarded_by_username="******"),
        Karma(reason="reason3",
              karma_type=KarmaType.NEGGYNEG,
              awarded_by_username="******")
    ])
}
expected_response = "Recent Karma for recipient:" + os.linesep + \
                    "Recently awarded with no reason: 10." + os.linesep + \
                    '++ from user1 for "reason1"' + os.linesep + \
                    '++ from user2 for "reason2"' + os.linesep + \
                    '-- from user3 for "reason3"'
Example #14
0
def default_karma_list():
    """Returns a list of 4 karma entries, 2 negative, 2 positive"""
    return list([Karma(karma_type=KarmaType.NEGGYNEG), Karma(karma_type=KarmaType.NEGGYNEG),
                 Karma(karma_type=KarmaType.POZZYPOZ), Karma(karma_type=KarmaType.POZZYPOZ)])