コード例 #1
0
class TestRoom(unittest.TestCase):
    def setUp(self):
        self.room = Room("Cover Your Ears!", 15, 10)
        self.room_small = Room("Tight Squeeze", 1, 15)

        self.song = Song("Hold a chicken in the air")
        self.song_not_fav = Song("Put that chair back")

        # fav_song should be song object, not just a string
        self.guest = Guest("Sid The Sloth", 33.25, "Hold a chicken in the air")
        # like...
        # self.guest = Guest("Sid The Sloth", 33.25, song)

        # fav_song should be song object, not just a string
        self.guest_no_money = Guest("Scrooge", 3.99, "Yeah, right!")
        # like...
        # self.guest_no_money = Guest("Scrooge", 3.99, song_not_fav)

    def test_room_has_name(self):
        self.assertEqual("Cover Your Ears!", self.room.name)

    def test_room_has_capacity(self):
        self.assertEqual(15, self.room.capacity)

    def test_room_has_entry_fee(self):
        self.assertEqual(10, self.room.entry_fee)

    def test_room_start_at_0(self):
        self.assertEqual(0, self.room.guest_count())

    def test_can_check_in_guest(self):
        self.room.check_in(self.guest)
        self.assertEqual(1, self.room.guest_count())

    def test_can_find_guess_by_name(self):
        # Add guest to room
        self.room.check_in(self.guest)

        # 'Extract' guest object from room, using their name
        guest = self.room.check_out("Sid The Sloth")
        self.assertEqual("Sid The Sloth", guest.name)

    def test_song_start_at_0(self):
        self.assertEqual(0, self.room.song_count())

    def test_can_add_song(self):
        self.room.add_song(self.song)
        self.assertEqual(1, self.room.song_count())

    def test_capacity_for_more_guests(self):
        self.assertEqual(True, self.room.check_capacity())

    def test_capacity_no_more_guests(self):
        self.room_small.check_in(self.guest)
        self.assertEqual(False, self.room_small.check_capacity())

    def test_guest_has_funds(self):
        self.assertEqual(True,
                         self.room_small.confirm_guest_has_funds(self.guest))

    def test_guest_has_not_funds(self):
        self.assertEqual(
            False, self.room.confirm_guest_has_funds(self.guest_no_money))

    def test_take_fee_from_guest(self):
        # Take fee from the guest
        self.room.take_fee_from_guest(self.guest)

        # The guest should now have 10 less than before
        self.assertEqual(23.25, self.guest.money)

    def test_take_no_fee_from_guest(self):
        # Try to take fee from the guest - this ones doesn't have sufficient funds
        self.room_small.take_fee_from_guest(self.guest_no_money)

        # The guest should still have the same amount of money as before.
        self.assertEqual(3.99, self.guest_no_money.money)

    def test_for_guest_fav_song(self):
        self.room.add_song(self.song)
        self.assertEqual("Whoo!",
                         self.room.look_for_guest_fav_song(self.guest))

    def test_for_no_guest_fav_song(self):
        self.room.add_song(self.song_not_fav)
        self.assertEqual(None, self.room.look_for_guest_fav_song(self.guest))
コード例 #2
0
ファイル: room_test.py プロジェクト: Stanhoucke/Karaoke_hw
class TestRoom(unittest.TestCase):

    def setUp(self):
        # Drinks
        self.drink_1 = Drink("Tennents", 3.50, True)
        self.drink_2 = Drink("Coca Cola", 2.00, False)

        # Songs
        self.song_1 = Song("Shake It Off", "Taylor Swift", 219)
        self.song_2 = Song("Sweet Caroline", "Neil Diamond", 201)
        self.song_3 = Song("YMCA", "Village People", 287)
        self.song_4 = Song("I'm Gonna Be (500 Miles)", "The Proclaimers", 217)

        # Guests
        self.guest_1 = Guest("Alice", 43, 30.00, self.song_3)
        self.guest_2 = Guest("Bob", 45, 60.00, self.song_3)
        self.guest_3 = Guest("Charlie", 21, 15.50, self.song_3)
        self.guest_4 = Guest("David", 17, 10.00, self.song_2)
        self.guest_5 = Guest("Emma", 24, 29.95, self.song_2)
        self.guest_6 = Guest("Freddie", 31, 5.00, self.song_1)

        group_1 = [self.guest_1, self.guest_2, self.guest_3, self.guest_4, self.guest_5]

        # Rooms
        self.room_1 = Room(1, 4)
        self.room_2 = Room(2, 5)

        self.room_2.guests = group_1

    # Class attribute tests
    def test_room_has_number(self):
        self.assertEqual(1, self.room_1.room_number)

    def test_room_has_empty_playlist(self):
        self.assertEqual(0, len(self.room_1.playlist))

    def test_room_has_no_guests(self):
        self.assertEqual(0, len(self.room_1.guests))

    # Methods that affect playlist
    def test_song_added_to_playlist(self):
        self.room_1.add_song(self.song_1)
        self.assertEqual(1, len(self.room_1.playlist))

    def test_add_song_3_times__returns_playlist_len_3(self):
        self.room_1.add_song(self.song_1)
        self.room_1.add_song(self.song_1)
        self.room_1.add_song(self.song_1)
        self.assertEqual(3, len(self.room_1.playlist))

    def test_favourite_song_on_playlist__returns_message(self):
        group_2 = [self.guest_1, self.guest_4, self.guest_6]
        self.room_1.guests = group_2
        self.room_1.add_song(self.song_1)
        self.assertEqual("Whoo!", self.room_1.favourite_song_on_playlist(self.guest_1))

    def test_favourite_song_on_playlist__returns_None(self):
        group_2 = [self.guest_1, self.guest_4, self.guest_6]
        self.room_1.guests = group_2
        self.room_1.add_song(self.song_4)
        self.assertIsNone(self.room_1.favourite_song_on_playlist(self.guest_1))

    def test_play_song__removes_played_song(self):
        playlist_1 = [self.song_1, self.song_2, self.song_3]
        self.room_1.playlist = playlist_1
        self.assertEqual("Now playing: Shake It Off by Taylor Swift", self.room_1.play_song(self.song_1))
        self.assertEqual(2, len(self.room_1.playlist))
    
    # Methods that affect number of guests in room
    def test_add_guest_to_room(self):
        self.room_1.add_guest(self.guest_1, self.room_1)
        self.assertEqual(1, len(self.room_1.guests))
    
    def test_add_guest_4_times__returns_guests_len_4(self):
        self.room_1.add_guest(self.guest_2, self.room_1)
        self.room_1.add_guest(self.guest_2, self.room_1)
        self.room_1.add_guest(self.guest_2, self.room_1)
        self.room_1.add_guest(self.guest_2, self.room_1)
        self.assertEqual(4, len(self.room_1.guests))
    
    def test_remove_guest_from_room(self):
        self.room_2.remove_guest(self.guest_3)
        self.assertEqual(4, len(self.room_2.guests))

    def test_remove_2_guests_from_room(self):
        self.room_2.remove_guest(self.guest_1)
        self.room_2.remove_guest(self.guest_5)
        self.assertEqual(3, len(self.room_2.guests))

    def test_add_guest__rejects_guest_cannot_afford_entry(self):
        self.room_1.add_guest(self.guest_1, self.room_1)
        self.room_1.add_guest(self.guest_6, self.room_1)
        self.assertEqual(1, len(self.room_1.guests))

    def test_add_guest__rejects_guest_room_at_capacity(self):
        self.room_2.add_guest(self.guest_1, self.room_2)
        self.assertEqual(5, len(self.room_2.guests))

    # Methods that check room capacity
    def test_check_capacity_for_under_capacity__returns_True(self):
        has_capacity = self.room_1.check_capacity(self.room_1)
        self.assertEqual(True, has_capacity)

    def test_check_capacity_for_at_capacity__returns_False(self):
        at_capacity = self.room_2.check_capacity(self.room_2)
        self.assertEqual(False, at_capacity)

    # Methods that affect tab
    def test_add_drink_to_tab__adds_drink_price_to_tab(self):
        self.room_1.add_drink_to_tab(self.guest_2, self.drink_1)
        self.assertEqual(3.50, self.room_1.tab)
        self.assertEqual(56.50, self.guest_2.wallet)

    def test_add_entry_fee_to_tab__adds_entry_fee_to_tab(self):
        self.room_1.add_entry_fee_to_tab(self.guest_2)
        self.assertEqual(9.95, self.room_1.tab)
        self.assertEqual(50.05, self.guest_2.wallet)

    def test_clear_room__removes_guests_playlist_tab(self):
        self.room_1.add_guest(self.guest_2, self.room_1)
        self.assertEqual(1, len(self.room_1.guests))
        self.assertEqual(9.95, self.room_1.tab)
        self.assertEqual(50.05, self.guest_2.wallet)

        self.room_1.add_song(self.song_1)
        self.assertEqual(1, len(self.room_1.playlist))

        self.room_1.clear_room(self.room_1)
        self.assertEqual(0, len(self.room_1.guests))
        self.assertEqual(0, len(self.room_1.playlist))
        self.assertEqual(0.00, self.room_1.tab)