def test_add_single_key_without_existed_movie(self): with self.instance.test_request_context() as request: u = UserFactory() self.db.session.commit() user_id = u.id key = Watchlist.make_key(user_id=u.id, movie_key='movie:tt0167260:imdb') add_to_watchlist(keys=(key,)) watchlist = self.db.session.query(Watchlist).filter(Watchlist.user_id == user_id).all() self.assertEqual(len(watchlist), 1) movies = self.db.session.query(Movie).all() self.assertEqual(len(movies), 1)
def test_add_multiple_keys_with_one_person_and_not_existed_movies(self): with self.instance.test_request_context() as request: u = UserFactory() self.db.session.commit() user_id = u.id keys = ['watchlist:1:movie:tt0167260:imdb', 'watchlist:1:movie:tt1229238:imdb', 'watchlist:1:movie:tt1272878:imdb'] add_to_watchlist(keys=keys) watchlist = self.db.session.query(Watchlist).filter(Watchlist.user_id == user_id).all() self.assertEqual(len(watchlist), len(keys)) movies = self.db.session.query(Movie).all() self.assertEqual(len(movies), len(keys))
def test_add_already_existed_key(self): provider = 'imdb' with self.instance.test_request_context() as request: u = UserFactory() m = MovieFactory() self.db.session.commit() user_id = u.id mid = getattr(m, '%s_id' % provider) movie_key = Movie.make_key(provider=provider, id=mid) w = Watchlist(user_id=u.id, movie_id=m.id) self.db.session.add(w) self.db.session.commit() key = Watchlist.make_key(user_id=u.id, movie_key=movie_key) add_to_watchlist(keys=(key,)) watchlist = self.db.session.query(Watchlist).filter(Watchlist.user_id == user_id).all() self.assertEqual(len(watchlist), 1)
def test_add_multiple_keys_with_various_persons_and_existed_movies(self): with self.instance.test_request_context() as request: u1 = UserFactory() u2 = UserFactory() self.db.session.commit() user1_id = u1.id user2_id = u2.id keys = ['watchlist:1:movie:tt0167260:imdb', 'watchlist:1:movie:tt1229238:imdb', 'watchlist:2:movie:tt1272878:imdb'] add_to_watchlist(keys=keys) movies = self.db.session.query(Movie).all() self.assertEqual(len(movies), len(keys)) u1_movies = self.db.session.query(Watchlist).filter(Watchlist.user_id == user1_id).all() self.assertEqual(len(u1_movies), 2) u2_movies = self.db.session.query(Watchlist).filter(Watchlist.user_id == user2_id).all() self.assertEqual(len(u2_movies), 1)