def test_minimum_phone_numbers_used(self):
     c1 = Channel("c1")
     c2 = Channel("c2")
     phone_numbers = list()
     channels = [c1, c2]
     followings = [Following("u1", "c1"), Following("u2", "c2")]
     phone_numbers = c1.broadcast(phone_numbers, channels, followings)
     self.assertEqual(len(phone_numbers), 1)
     phone_numbers = c2.broadcast(phone_numbers, channels, followings)
     self.assertEqual(len(phone_numbers), 1)
 def test_is_collision_false(self):
     channels = [Channel("c1", "123"), Channel("c2"), Channel("c3", "123")]
     followers = [
         Following("u1", "c1"),
         Following("u3", "c3"),
         Following("u2", "c2"),
         Following("u4", "c2")
     ]
     collision = is_collision("123", "c2", channels, followers)
     self.assertEqual(collision, False)
 def test_assign_number_to_channel(self):
     c1 = Channel("c1")
     phone_numbers = list()
     channels = [c1]
     followings = [Following("u1", "c1")]
     self.assertIsNone(c1.phone_number)
     c1.assign_phone_number(phone_numbers, channels, followings)
     self.assertIsNotNone(c1.phone_number)
 def test_broadcast(self):
     c1 = Channel("c1")
     phone_numbers = list()
     channels = [c1]
     followings = [Following("u1", "c1")]
     self.assertIsNone(c1.phone_number)
     c1.broadcast(phone_numbers, channels, followings)
     self.assertIsNotNone(c1.phone_number)
    def test_follow_channel_without_collision(self):
        c1 = Channel("c1")
        c2 = Channel("c2")
        u2 = User("u2")
        phone_numbers = list()
        channels = [c1, c2]
        followings = [Following("u1", "c1")]
        phone_numbers = c1.broadcast(phone_numbers, channels, followings)
        phone_numbers = c2.broadcast(phone_numbers, channels, followings)

        u2.follow_channel("c2", channels, phone_numbers, followings)
        self.assertEqual(c1.phone_number, c2.phone_number)
Esempio n. 6
0
    def follow_channel(self, channel_uid, channel_list, phone_numbers, followers):
        # Check if user is already following the channel
        if channel_uid in [i.channel_uid for i in followers if i.user_uid == self.uid]:
            return
        # If not add to followers list
        followers.append(Following(self.uid, channel_uid))

        # get all of current user's channels
        user_channels_uid = [i.channel_uid for i in followers if i.user_uid == self.uid]
        user_channels = [i for i in channel_list if i.channel_uid in user_channels_uid]

        # check if there is a collision while following and handle
        curr_channel = [channel for channel in channel_list if channel.channel_uid == channel_uid][0]
        if curr_channel.phone_number is not None:
            if utils.is_collision(curr_channel.phone_number, channel_uid, channel_list, followers):
                # check least impacted channel among all of current user's channels
                channel_to_change = utils.least_impacted_channel(user_channels)
                phone_numbers = channel_to_change.assign_phone_number(phone_numbers, channel_list, followers)
        return followers, phone_numbers
Esempio n. 7
0
# Testing slightly complex test cases

from src.Following import Following
from src.User import User
from src.Channel import Channel

channels = [Channel("c1"), Channel("c2"), Channel("c3")]
users = [User("u1"), User("u2"), User("u3"), User("u4")]
followings = [
    Following("u1", "c1"),
    Following("u3", "c3"),
    Following("u2", "c2"),
    Following("u4", "c2")
]
c1 = channels[0]
c2 = channels[1]
c3 = channels[2]

u1 = users[0]
u2 = users[1]
u3 = users[2]
u4 = users[3]

phone_numbers = list()

phone_numbers = c1.broadcast(phone_numbers, channels, followings)
phone_numbers = c3.broadcast(phone_numbers, channels, followings)
phone_numbers = c1.broadcast(phone_numbers, channels, followings)
phone_numbers = c2.broadcast(phone_numbers, channels, followings)
phone_numbers = c2.broadcast(phone_numbers, channels, followings)
phone_numbers = c2.broadcast(phone_numbers, channels, followings)