def test_get_all_event_joined_by_user_1(self): """ Test get_all_ongoing_event_by_user Case1: user has joined some events """ latitude = 40.730610 longitude = -73.935242 for i in range(5): latitude += 0.01 longitude += 0.01 time = datetime.strptime("2020-12-12 12:12:12", "%Y-%m-%d %H:%M:%S") tmp_event = Event(user=self.useremail, name="testevent" + str(i), address="address" + str(i), zipcode=10025, event_time=time, longitude=longitude, latitude=latitude) tmp_event.category = "test" tmp_event_id = Event.create_event(tmp_event) self.event_ids.append(tmp_event_id) join = Join(self.useremail, tmp_event_id) Join.create_join(join) if i % 2 == 0: like = Like(self.useremail, tmp_event_id) Like.create_like(like) result = Event.get_all_event_joined_by_user(self.useremail) for res in result: self.assertTrue(res.event_id in self.event_ids) self.assertEqual(5, len(result))
def test_get_join_by_user_2(self) -> None: """ test get_join_by_user: user doesn't exist """ Join.create_join(Join(self.user, self.event)) joins = Join.get_join_by_user('') events = [join.event for join in joins] self.assertListEqual(events, [])
def test_get_join_by_event_1(self) -> None: """ test get_join_by_event: event exists """ Join.create_join(Join(self.user, self.event)) joins = Join.get_join_by_event(self.event) users = [join.user for join in joins] self.assertListEqual(users, [self.user])
def test_get_join_by_event_2(self) -> None: """ test get_join_by_event: event doesn't exist """ Join.create_join(Join(self.user, self.event)) joins = Join.get_join_by_event('1') users = [join.user for join in joins] self.assertListEqual(users, [])
def test_get_join_by_user_1(self) -> None: """ test get_join_by_user: user exists """ Join.create_join(Join(self.user, self.event)) joins = Join.get_join_by_user(self.user) events = [join.event for join in joins] self.assertListEqual(events, [self.event])
def test_delete_join_4(self) -> None: """ test delete_join: user and event don’t exist """ Join.create_join(Join(self.user, self.event)) self.assertTrue(Join.user_is_attend(self.user, self.event)) Join.delete_join(Join('1', '1')) self.assertTrue(Join.user_is_attend(self.user, self.event))
def test_delete_join_3(self) -> None: """ test delete_join: user doesn’t exist, event exists """ Join.create_join(Join(self.user, self.event)) self.assertTrue(Join.user_is_attend(self.user, self.event)) Join.delete_join(Join('1', self.event)) self.assertTrue(Join.user_is_attend(self.user, self.event))
def get_all_ongoing_events(user: str): """ :return: An ongoing event list """ cnx = db_connector.get_connection() cursor = cnx.cursor() query = ("SELECT * FROM event " "where `time` >= now()") cursor.execute(query) events = [] for (eid, name, host, address, longi, lati, zipcode, event_time, description, image, num_likes, category) in cursor: new_event = Event(user=host, name=name, address=address, longitude=longi, latitude=lati, zipcode=zipcode, event_time=datetime.datetime.strptime( str(event_time), "%Y-%m-%d %H:%M:%S")) new_event.event_id = eid new_event.description = description new_event.image = image new_event.num_likes = num_likes new_event.category = category if user: new_event.liked = Like.exist(user, eid) new_event.attended = Join.user_is_attend(user=user, event=eid) else: new_event.liked = False new_event.attended = False new_event.comments = Comment.get_comment_by_event(eid) events.append(new_event) cursor.close() cnx.close() return events
def test_get_attendees_by_event_1(self): """ Test function get_attendees_by_event Event exist """ user = "******" event = create_event() event.category = "test" event_id = Event.create_event(event) Join.create_join(Join(user, event_id)) users = User.get_attendees_by_event(event_id) self.assertEqual(users[0].email, user) Join.delete_join(Join(user, event_id)) Event.delete_event_by_id(event_id)
def get_event_by_id(event_id: str, user=None): """ :param event_id: Id of the event :param user: user email :return: An event object whose id is the given event_id, and also indicates whether the user is attending the event """ cnx = db_connector.get_connection() cursor = cnx.cursor() query = ("SELECT * FROM `event` WHERE id='" + event_id + "'") cursor.execute(query) new_event = None for (eid, name, host, address, longitude, latitude, zipcode, event_time, description, image, num_likes, category) in cursor: new_event = Event(user=host, name=name, address=address, longitude=longitude, latitude=latitude, zipcode=zipcode, event_time=datetime.datetime.strptime( str(event_time), "%Y-%m-%d %H:%M:%S")) new_event.event_id = eid new_event.description = description new_event.image = image new_event.num_likes = num_likes new_event.category = category if user: new_event.liked = Like.exist(user, event_id) new_event.attended = Join.user_is_attend(user=user, event=event_id) else: new_event.liked = False new_event.attended = False new_event.comments = Comment.get_comment_by_event(event_id) cursor.close() cnx.close() return new_event
def get_all_event_liked_by_user(email: str): """ :param email: Email of user :return: A list of events which is liked by user. """ joins = Join.get_join_by_user(email) joined_events = set(join.event for join in joins) cnx = db_connector.get_connection() cursor = cnx.cursor() query = ("SELECT * FROM `event` WHERE `id` " "IN (SELECT `event` FROM `like` WHERE `user` = '" + email + "');") cursor.execute(query) events = [] for (event_id, name, host, address, longitude, latitude, zipcode, event_time, description, image, num_likes, category) in cursor: new_event = Event(user=host, name=name, address=address, longitude=longitude, latitude=latitude, zipcode=zipcode, event_time=datetime.datetime.strptime( str(event_time), "%Y-%m-%d %H:%M:%S")) new_event.event_id = event_id new_event.description = description new_event.image = image new_event.num_likes = num_likes new_event.liked = True new_event.category = category if event_id in joined_events: new_event.attended = True events.append(new_event) cursor.close() cnx.close() return events
def tearDown(self) -> None: Join.delete_join(Join(self.user, self.event)) Event.delete_event_by_id(self.event) User.delete_user_by_email(self.user)
def test_user_is_attend_2(self) -> None: """ test user_is_attend: user exists, event doesn’t exist """ Join.create_join(Join(self.user, self.event)) self.assertFalse(Join.user_is_attend(self.user, '1'))
def test_user_is_attend_1(self) -> None: """ test user_is_attend: user and event exist """ Join.create_join(Join(self.user, self.event)) self.assertTrue(Join.user_is_attend(self.user, self.event))
def test_user_is_attend_3(self) -> None: """ test user_is_attend: user doesn’t exist, event exists """ Join.create_join(Join(self.user, self.event)) self.assertFalse(Join.user_is_attend('1', self.event))
def test_user_is_attend_4(self) -> None: """ test user_is_attend: user and event don’t exist """ Join.create_join(Join(self.user, self.event)) self.assertFalse(Join.user_is_attend('1', '1'))
def test_create_join_2(self) -> None: """ test create_join: user exists, event doesn’t exist """ self.assertRaises(mysql.connector.Error, Join.create_join, Join(self.user, '1'))
def test_create_join_4(self) -> None: """ test create_join: user and event don’t exist """ self.assertRaises(mysql.connector.Error, Join.create_join, Join('1', '1'))
def test_create_join_3(self) -> None: """ test create_join: user doesn’t exist, event exists """ self.assertRaises(mysql.connector.Error, Join.create_join, Join('1', self.event))