def create_list(self, name, list_type): """ Create an SC list :param name: The name of the list to create :type name: str :param list_type: The type of list to create, eg Movie, Serie :type list_type: ListType :return: the SC path to access the list, created by SC using the name and a random id :rtype: ScList """ data = { "label": name, "subtype_id_related": list_type.value[0], "is_ordered": 0, "is_public": 0 } response = self.send_post(self._URL_ADD_LIST, data=data) return ScList(type=list_type, name=name, path=response.headers["Location"])
def test_remote_collection_creates_list_if_not_present( self, mock_add_movie, mock_find_product, mock_create_list, *mocks): user = User(email="*****@*****.**", password="******", username="******") mock_find_product.return_value = [ Product(title="SomeTitle", id="12345", type=ProductType.MOVIE), ] mock_create_list.return_value = ScList(name="a List", path="/liste/aList/123ListId", type=ListType.MOVIE) piece = FixtureList.piece_list[0] # WHEN remote = RemoteCollectionStore(user=user, movie_collection_title="A movie List") result = remote.add(piece) # THEN self.assertTrue(result) mock_create_list.assert_called_once_with(name="A movie List", list_type=ListType.MOVIE) mock_add_movie.assert_called_once_with(list_id="123ListId", product_id="12345")
def find_list(self, title, list_type=ListType.MOVIE): """ Look on SC for lists matching the given title in the given user lists :param title: the title of the list to find :type title: str :param list_type: the type of list to find (Serie/Movie) :type list_type: ListType :return: a list of ScList matching the title :rtype: generator(ScList) """ page = 1 list_paths = True while list_paths: url = self._build_list_search_url(page=page, list_type=list_type) response = self.send_post(url=url, data={"searchQuery": title}) list_paths = self.parse_html(response).xpath( "//a[@class='elth-thumbnail-title']") yield from [ ScList(type=list_type, name=l.attrib["title"], path=l.attrib["href"]) for l in list_paths ] page += 1
def test_remote_collection_adds_to_specified_list(self, mock_add_movie, mock_find_product, mock_find_list, *mocks): user = User(email="*****@*****.**", password="******", username="******") mock_find_product.return_value = [ Product(title="SomeTitle", id="12345", type=ProductType.MOVIE), ] mock_find_list.return_value = iter([ ScList(name="a List", path="/liste/aList/aListId", type=ListType.MOVIE) ]) piece = FixtureList.piece_list[0] # WHEN remote = RemoteCollectionStore(user=user) result = remote.add(piece) # THEN self.assertTrue(result) self.assertTrue(mock_find_list.called) mock_add_movie.assert_called_once_with(list_id="aListId", product_id="12345")
def test_should_construct_page_url(self): sclist = ScList(type=ListType.MOVIE, name="A name", path="/liste/a_name/1622651") self.assertEqual("/sc2/liste/1622651/page-1.ajax", sclist.page_url(index=1))
def test_compute_list_id_slash_start(self): sclist = ScList(type=ListType.MOVIE, name="A name", path="/liste/a_name/1624343") self.assertEqual("1624343", sclist.compute_list_id())
def test_delete_unknown_list_returns_true(self): sc_list = ScList(type=ListType.MOVIE, name="unknown", path="list/unknown/12345") self.assertTrue(self.service.delete_list(list_to_delete=sc_list))
class TestRemoteCollectionStore(RateItSevenTestCase): @patch.object(AuthService, 'is_authenticated', return_value=True) @patch.object(ListService, 'find_list', return_value=iter([ ScList(name="a List", path="/liste/aList/aListId", type=ListType.MOVIE) ])) @patch.object(ProductService, 'find_product') @patch.object(ListService, 'add_movie') def test_should_add_piece_to_sc_list(self, mock_add_movie, mock_find_product, *mocks): # GIVEN user = User(email="*****@*****.**", password="******", username="******") mock_find_product.return_value = [ Product(title="SomeTitle", id="12345", type=ProductType.MOVIE), ] remote = RemoteCollectionStore(user=user) piece_1 = FixtureList.piece_list[0] piece_2 = FixtureList.piece_list[1] # WHEN self.assertTrue(remote.add(piece_1)) self.assertTrue(remote.add(piece_2)) # THEN self.assertEqual(2, mock_add_movie.call_count) @patch.object(AuthService, 'is_authenticated', return_value=True) @patch.object(ListService, 'find_list', return_value=iter([ ScList(name="a List", path="/liste/aList/aListId", type=ListType.MOVIE) ])) @patch.object(ProductService, 'find_product') @patch.object(ListService, 'add_movie') def test_add_takes_first_matching_sc_product(self, mock_add_movie, mock_find_product, *mocks): user = User(email="*****@*****.**", password="******", username="******") mock_find_product.return_value = [ Product(title="SomeTitle", id="12345", type=ProductType.MOVIE), Product(title="SomeTitle2", id="67890", type=ProductType.MOVIE), ] remote = RemoteCollectionStore(user=user) piece = FixtureList.piece_list[0] # WHEN remote.add(piece) # THEN mock_add_movie.assert_called_once_with(list_id=mock.ANY, product_id="12345") @patch.object(AuthService, 'is_authenticated', return_value=True) @patch.object(ListService, 'find_list', return_value=iter([ ScList(name="a List", path="/liste/aList/aListId", type=ListType.MOVIE) ])) @patch.object(ProductService, 'find_product') @patch.object(ListService, 'add_movie') def test_add_cannot_find_piece_in_sc(self, mock_add_movie, mock_find_product, *mocks): user = User(email="*****@*****.**", password="******", username="******") mock_find_product.return_value = [] remote = RemoteCollectionStore(user=user) piece = FixtureList.piece_list[0] # WHEN result = remote.add(piece) # THEN self.assertFalse(result) self.assertFalse(mock_add_movie.called) @patch.object(AuthService, 'is_authenticated', return_value=True) @patch.object(ListService, 'find_list', return_value=iter([ ScList(name="a List", path="/liste/aList/aListId", type=ListType.MOVIE) ])) @patch.object(ProductService, 'find_product') @patch.object(ListService, 'add_movie') def test_add_fail_to_add_piece_to_sc(self, mock_add_movie, mock_find_product, *mocks): user = User(email="*****@*****.**", password="******", username="******") mock_find_product.return_value = [ Product(title="SomeTitle", id="12345", type=ProductType.MOVIE), ] mock_add_movie.return_value = None remote = RemoteCollectionStore(user=user) piece = FixtureList.piece_list[0] # WHEN result = remote.add(piece) # THEN self.assertFalse(result) self.assertTrue(mock_add_movie.called) def test_add_should_give_a_nice_description_to_piece(self): #@todo implement pass @patch.object(AuthService, 'is_authenticated', return_value=True) @patch.object(ListService, 'find_list') @patch.object(ProductService, 'find_product') @patch.object(ListService, 'add_movie') def test_remote_collection_adds_to_specified_list(self, mock_add_movie, mock_find_product, mock_find_list, *mocks): user = User(email="*****@*****.**", password="******", username="******") mock_find_product.return_value = [ Product(title="SomeTitle", id="12345", type=ProductType.MOVIE), ] mock_find_list.return_value = iter([ ScList(name="a List", path="/liste/aList/aListId", type=ListType.MOVIE) ]) piece = FixtureList.piece_list[0] # WHEN remote = RemoteCollectionStore(user=user) result = remote.add(piece) # THEN self.assertTrue(result) self.assertTrue(mock_find_list.called) mock_add_movie.assert_called_once_with(list_id="aListId", product_id="12345") @patch.object(AuthService, 'is_authenticated', return_value=True) @patch.object(ListService, 'find_list', return_value=iter([])) @patch.object(ListService, 'create_list') @patch.object(ProductService, 'find_product') @patch.object(ListService, 'add_movie') def test_remote_collection_creates_list_if_not_present( self, mock_add_movie, mock_find_product, mock_create_list, *mocks): user = User(email="*****@*****.**", password="******", username="******") mock_find_product.return_value = [ Product(title="SomeTitle", id="12345", type=ProductType.MOVIE), ] mock_create_list.return_value = ScList(name="a List", path="/liste/aList/123ListId", type=ListType.MOVIE) piece = FixtureList.piece_list[0] # WHEN remote = RemoteCollectionStore(user=user, movie_collection_title="A movie List") result = remote.add(piece) # THEN self.assertTrue(result) mock_create_list.assert_called_once_with(name="A movie List", list_type=ListType.MOVIE) mock_add_movie.assert_called_once_with(list_id="123ListId", product_id="12345") @patch.object(AuthService, 'is_authenticated', return_value=False) def test_should_not_allow_unauthentified_user_at_creation( self, mock_is_authentified): user = User(email="*****@*****.**", password="", username="******") with self.assertRaises(NotAuthenticatedError): RemoteCollectionStore(user=user) self.assertTrue(mock_is_authentified.called)