<commit_before>from __future__ import unicode_literals
import random
import datetime
from decimal import Decimal

import psycopg2
import pytz
from nose.tools import assert_raises

from gittip.testing import Harness
from gittip.models import Participant, Tip
from gittip.participant import Participant as OldParticipant


class Tests(Harness):

    def random_restricted_username(self):
        """helper method to chooses a restricted username for testing """
        from gittip import RESTRICTED_USERNAMES
        random_item = random.choice(RESTRICTED_USERNAMES)
        while random_item.startswith('%'):
            random_item = random.choice(RESTRICTED_USERNAMES)
        return random_item

    def setUp(self):
        super(Harness, self).setUp()
        self.participant = Participant(username='user1') # Our protagonist
        self.session.add(self.participant)
        self.session.commit()


    def test_claiming_participant(self):
        expected = now = datetime.datetime.now(pytz.utc)
        self.participant.set_as_claimed(claimed_at=now)
        actual = self.participant.claimed_time
        assert actual == expected, actual

    def test_changing_username_successfully(self):
        self.participant.change_username('user2')
        actual = Participant.query.get('user2')
        assert self.participant == actual, actual

    def test_changing_username_to_too_long(self):
        with assert_raises(Participant.UsernameTooLong):
            self.participant.change_username('123456789012345678901234567890123')

    def test_changing_username_to_already_taken(self):
        self.session.add(Participant(username='user2'))
        self.session.commit()
        with assert_raises(Participant.UsernameAlreadyTaken):
            self.participant.change_username('user2')

    def test_changing_username_to_invalid_characters(self):
        with assert_raises(Participant.UsernameContainsInvalidCharacters):
            self.participant.change_username(u"\u2603") # Snowman

    def test_changing_username_to_restricted_name(self):
        with assert_raises(Participant.UsernameIsRestricted):
            self.participant.change_username(self.random_restricted_username())

    def test_getting_tips_actually_made(self):
        expected = Decimal('1.00')
        self.session.add(Participant(username='user2'))
        self.session.add(Tip(tipper='user1', tippee='user2', amount=expected,
                             ctime=datetime.datetime.now(pytz.utc)))
        self.session.commit()
        actual = self.participant.get_tip_to('user2')
        assert actual == expected, actual

    def test_getting_tips_not_made(self):
        expected = Decimal('0.00')
        self.session.add(Participant(username='user2'))
        self.session.commit()
        actual = self.participant.get_tip_to('user2')
        assert actual == expected, actual


    # set_tip_to - stt

    def test_stt_sets_tip_to(self):
        alice = self.make_participant('alice', last_bill_result='')
        self.make_participant('bob')
        alice.set_tip_to('bob', '1.00')

        actual = alice.get_tip_to('bob')
        assert actual == Decimal('1.00'), actual

    def test_stt_returns_a_Decimal(self):
        alice = self.make_participant('alice', last_bill_result='')
        self.make_participant('bob')
        actual = alice.set_tip_to('bob', '1.00')<commit_msg>Add at least one test for id; #287<commit_after>from __future__ import unicode_literals
import random
import datetime
from decimal import Decimal

import psycopg2
import pytz
from nose.tools import assert_raises

from gittip.testing import Harness
from gittip.models import Participant, Tip
from gittip.participant import Participant as OldParticipant


class Tests(Harness):

    def random_restricted_username(self):
        """helper method to chooses a restricted username for testing """
        from gittip import RESTRICTED_USERNAMES
        random_item = random.choice(RESTRICTED_USERNAMES)
        while random_item.startswith('%'):
            random_item = random.choice(RESTRICTED_USERNAMES)
        return random_item

    def setUp(self):
        super(Harness, self).setUp()
        self.participant = Participant(username='user1') # Our protagonist
        self.session.add(self.participant)
        self.session.commit()


    def test_claiming_participant(self):
        expected = now = datetime.datetime.now(pytz.utc)
        self.participant.set_as_claimed(claimed_at=now)
        actual = self.participant.claimed_time
        assert actual == expected, actual

    def test_changing_username_successfully(self):
        self.participant.change_username('user2')
        actual = Participant.query.get('user2')
        assert self.participant == actual, actual

    def test_changing_username_to_too_long(self):
        with assert_raises(Participant.UsernameTooLong):
            self.participant.change_username('123456789012345678901234567890123')

    def test_changing_username_to_already_taken(self):
        self.session.add(Participant(username='user2'))
        self.session.commit()
        with assert_raises(Participant.UsernameAlreadyTaken):
            self.participant.change_username('user2')

    def test_changing_username_to_invalid_characters(self):
        with assert_raises(Participant.UsernameContainsInvalidCharacters):
            self.participant.change_username(u"\u2603") # Snowman

    def test_changing_username_to_restricted_name(self):
        with assert_raises(Participant.UsernameIsRestricted):
            self.participant.change_username(self.random_restricted_username())

    def test_getting_tips_actually_made(self):
        expected = Decimal('1.00')
        self.session.add(Participant(username='user2'))
        self.session.add(Tip(tipper='user1', tippee='user2', amount=expected,
                             ctime=datetime.datetime.now(pytz.utc)))
        self.session.commit()
        actual = self.participant.get_tip_to('user2')
        assert actual == expected, actual

    def test_getting_tips_not_made(self):
        expected = Decimal('0.00')
        self.session.add(Participant(username='user2'))
        self.session.commit()
        actual = self.participant.get_tip_to('user2')
        assert actual == expected, actual


    # id

    def test_participant_gets_a_long_id(self):
        actual = type(self.make_participant('alice').id)
        assert actual == long, actual


    # set_tip_to - stt

    def test_stt_sets_tip_to(self):
        alice = self.make_participant('alice', last_bill_result='')
        self.make_participant('bob')
        alice.set_tip_to('bob', '1.00')

        actual = alice.get_tip_to('bob')
        assert actual == Decimal('1.00'), actual

    def test_stt_returns_a_Decimal(self):
        alice = self.make_participant('alice', last_bill_result='')
        self.make_participant('bob')
        actual = alice.set_tip_to('bob', '1.00')
{'fraction_non_alphanumeric': 0.09695570624729476, 'fraction_numerical': 0.01890059154523157, 'mean_word_length': 3.4521515735388566, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7368511', 'n_tokens_mistral': 2219, 'n_tokens_neox': 2011, 'n_words': 349}