def test_ls_two_folders_with_same_name(monkeypatch, initial_pwd_root): path = 'thefolder' named_path1 = ['/', 'theitem'] id_path1 = ['root', 'theitemid1'] id_path2 = ['root', 'theitemid2'] named_path3 = ['/', 'theitem', 'img1.jpg'] id_path3 = ['root', 'theitemid1', 'img1jpgid'] named_path4 = ['/', 'theitem', 'img2.jpg'] id_path4 = ['root', 'theitemid2', 'img2jpgid'] contents_by_name = [ [ gditem.GDItem.folder(named_path1, id_path1), gditem.GDItem.folder(named_path1, id_path2), ], ] contents_by_folder = [ [gditem.GDItem(named_path3, id_path3, 'image/jpeg')], [gditem.GDItem(named_path4, id_path4, 'image/jpeg')], ] expected_items = [ gditem.GDItem(named_path3, id_path3, 'image/jpeg'), gditem.GDItem(named_path4, id_path4, 'image/jpeg'), ] fake_get_items_by_name = utiltests.build_mock_get(contents_by_name) fake_get_items_by_folder = utiltests.build_mock_get(contents_by_folder) monkeypatch.setattr(gdcore, 'get_items_by_name', fake_get_items_by_name) monkeypatch.setattr(gdcore, 'get_items_by_folder', fake_get_items_by_folder) items = gdcli_ls.get_files(path) assert set(items) == set(expected_items)
def test_ls_path_to_folder(monkeypatch, initial_pwd_root): path = 'folder1' named_path1 = ['/', 'folder1'] id_path1 = ['root', 'folder1id'] named_path2 = ['/', 'folder1', 'img.jpg'] id_path2 = ['root', 'folder1id', 'imgjpgid1'] named_path3 = ['/', 'folder1', 'folder2'] id_path3 = ['root', 'folder1id', 'folder2id'] contents_by_name = [ [gditem.GDItem.folder(named_path1, id_path1)], ] contents_by_folder = [[ gditem.GDItem(named_path2, id_path2, 'image/jpeg'), gditem.GDItem.folder(named_path3, id_path3) ]] expected_items = [ gditem.GDItem(named_path2, id_path2, 'image/jpeg'), gditem.GDItem.folder(named_path3, id_path3) ] fake_get_items_by_name = utiltests.build_mock_get(contents_by_name) fake_get_items_by_folder = utiltests.build_mock_get(contents_by_folder) monkeypatch.setattr(gdcore, 'get_items_by_name', fake_get_items_by_name) monkeypatch.setattr(gdcore, 'get_items_by_folder', fake_get_items_by_folder) items = gdcli_ls.get_files(path) assert items == expected_items
def test_ls_absolute_path_to_non_folder_file(monkeypatch, initial_pwd_root): path = '/img.jpg' named_path1 = ['/', 'img.jpg'] id_path1 = ['root', 'imgjpgid'] contents_by_name = [[gditem.GDItem(named_path1, id_path1, 'image/jpeg')]] expected_items = [gditem.GDItem(named_path1, id_path1, 'image/jpeg')] fake_get_items_by_name = utiltests.build_mock_get(contents_by_name) monkeypatch.setattr(gdcore, 'get_items_by_name', fake_get_items_by_name) items = gdcli_ls.get_files(path) assert items == expected_items
def test_path_slash_existent_existent_file(monkeypatch, initial_pwd_root): path = '/folder1/file2.jpg' named_path1 = ['/', 'folder1'] id_path1 = ['root', 'folder1id'] named_path2 = ['/', 'folder1', 'file2.jpg'] id_path2 = ['root', 'folder1id', 'file2jpgid'] contents = [ [gditem.GDItem.folder(named_path1, id_path1)], [gditem.GDItem.folder(named_path2, id_path2)], ] contents = [[gditem.GDItem.folder(named_path1, id_path1)], [gditem.GDItem(named_path2, id_path2, 'image/jpeg')]] expected_item = gditem.GDItem(named_path2, id_path2, 'image/jpeg') assert_expected_results(contents, path, [expected_item], monkeypatch)
def _process_path_items(former_remaining_path_items, partial_id_path, partial_named_path, final_path): """ given a list of items to process, and the partial id_path it tries to complete remaining_path_items to construct matching GDItem @return list[GDItem]: the list of items found in GD matching the final path """ found = [] remaining_path_items = former_remaining_path_items[:] current = remaining_path_items.pop(0) folder = gditem.GDItem.folder(partial_named_path, partial_id_path) gd_items = gdcore.get_items_by_name(current, folder=folder) for gdi in gd_items: id_path = gdi['idPath'] named_path = gdi['namedPath'] if remaining_path_items: if not gdi.is_folder(): continue found += _process_path_items(remaining_path_items, id_path, named_path, final_path) else: mime_type = gdi['mimeType'] new_item = gditem.GDItem(named_path, id_path, mime_type) found.append(new_item) return found
def test_path_more_than_one_file_with_same_name_in_same_folder(monkeypatch): path = '/folder1/img.jpg' named_path1 = ['/', 'folder1'] id_path1 = ['root', 'folder1id'] named_path2 = ['/', 'folder1', 'img.jpg'] id_path2 = ['root', 'folder1id', 'imgjpgid1'] id_path3 = ['root', 'folder1id', 'imgjpgid2'] contents = [[gditem.GDItem.folder(named_path1, id_path1)], [ gditem.GDItem(named_path2, id_path2, 'image/jpeg'), gditem.GDItem(named_path2, id_path3, 'image/jpeg') ]] expected_items = [ gditem.GDItem(named_path2, id_path2, 'image/jpeg'), gditem.GDItem(named_path2, id_path3, 'image/jpeg') ] assert_expected_results(contents, path, expected_items, monkeypatch)
def test_print_item_when_unknown_type(): named_path = ['/', 'one', 'item'] id_path = ['root', 'oneid', 'itemid'] item = gditem.GDItem(named_path, id_path, 'application/vnd.google-apps.unknown') expected = '/one/item' got = gdcli_ls.item_to_str(item) assert got == expected
def test_path_a_file_and_a_folder_with_same_name_is_same_folder(monkeypatch): path = '/folder1/theitem' named_path1 = ['/', 'folder1'] id_path1 = ['root', 'folder1id'] named_path2 = ['/', 'folder1', 'theitem'] id_path2 = ['root', 'folder1id', 'theitemid1'] id_path3 = ['root', 'folder1id', 'theitemid2'] contents = [[gditem.GDItem.folder(named_path1, id_path1)], [ gditem.GDItem(named_path2, id_path2, 'image/jpeg'), gditem.GDItem.folder(named_path2, id_path3), ]] expected_items = [ gditem.GDItem(named_path2, id_path2, 'image/jpeg'), gditem.GDItem.folder(named_path2, id_path3), ] assert_expected_results(contents, path, expected_items, monkeypatch)
def test_path_two_folders_with_same_name_one_with_required_file_and_the_other_without( monkeypatch): path = '/folder1/img1.jpg' named_path1 = ['/', 'folder1'] id_path1 = ['root', 'folder1id1'] id_path2 = ['root', 'folder1id2'] named_path3 = ['/', 'folder1', 'img.jpg'] id_path3 = ['root', 'folder1id', 'imgjpgid'] contents = [ [ gditem.GDItem.folder(named_path1, id_path1), gditem.GDItem.folder(named_path1, id_path2), ], [gditem.GDItem(named_path3, id_path3, 'image/jpeg')], [], ] expected_items = [gditem.GDItem(named_path3, id_path3, 'image/jpeg')] assert_expected_results(contents, path, expected_items, monkeypatch)
def test_path_same_name_step_one_a_file_the_other_a_folder_containing_expected( monkeypatch): path = '/theitem/img.jpg' named_path1 = ['/', 'theitem'] id_path1 = ['root', 'theitemid1'] id_path2 = ['root', 'theitemid2'] named_path3 = ['/', 'theitem', 'img.jpg'] id_path3 = ['root', 'theitemid1', 'imgjpgid'] contents = [[ gditem.GDItem(named_path1, id_path1, 'image/jpeg'), gditem.GDItem.folder(named_path1, id_path2), ], [ gditem.GDItem(named_path3, id_path3, 'image/jpeg'), ]] expected_items = [ gditem.GDItem(named_path3, id_path3, 'image/jpeg'), ] assert_expected_results(contents, path, expected_items, monkeypatch)
def test_proper_item_file(): named_path = ['/', 'a', 'proper', 'file.jpg'] id_path = ['root', 'aid', 'properid', 'fileid'] mime_type = 'application/img.jpeg' item = gditem.GDItem(named_path, id_path, mime_type) assert item['name'] == 'file.jpg' assert item['id'] == 'fileid' assert item['mimeType'] == 'application/img.jpeg' assert item['namedPath'] == named_path assert item['idPath'] == id_path assert not item.is_root() assert not item.is_folder()
def test_proper_item_folder(): named_path = ['/', 'a', 'proper', 'path'] id_path = ['root', 'aid', 'properid', 'pathid'] mime_type = 'application/vnd.google-apps.folder' item = gditem.GDItem(named_path, id_path, mime_type) assert item['name'] == 'path' assert item['id'] == 'pathid' assert item['mimeType'] == 'application/vnd.google-apps.folder' assert item['namedPath'] == named_path assert item['idPath'] == id_path assert not item.is_root() assert item.is_folder()
def test_init_root(): named_path = ['/'] id_path = ['root'] mime_type = 'application/vnd.google-apps.folder' item = gditem.GDItem(named_path, id_path, mime_type) assert item['name'] == '/' assert item['id'] == 'root' assert item['mimeType'] == 'application/vnd.google-apps.folder' assert item['namedPath'] == ['/'] assert item['idPath'] == ['root'] assert item.is_root() assert item.is_folder()
def _gdcontents_to_gditem(contents, folder): """ given a response of a Google Drive query, it return the list of GDItem contained in the 'files' entry if present @param contents: a dict as return by a query to Google Driver @param folder: a GDItem such that folder.is_folder() == True @return list[GDItem] """ items = [] for raw_item in contents.get('files', []): name = raw_item['name'] item = gditem.GDItem(folder['namedPath'] + [name], folder['idPath'] + [raw_item['id']], raw_item['mimeType']) items.append(item) return items
def test_non_normalitze_path_with_dot(): named_path = ['/', 'a', '.', 'path'] id_path = ['root', 'aid', 'dotid', 'pathid'] mime_type = 'application/vnd.google-apps.folder' with pytest.raises(AssertionError): gditem.GDItem(named_path, id_path, mime_type)
def test_non_absolute_path(): named_path = ['a', 'relative', 'path'] id_path = ['aid', 'relativeid', 'pathid'] mime_type = 'application/vnd.google-apps.folder' with pytest.raises(AssertionError): gditem.GDItem(named_path, id_path, mime_type)
def test_named_path_with_two_root(): named_path = ['/', 'one', '/thisisvalid!', 'two'] id_path = ['root', 'oneid', 'root', 'two'] mime_type = 'application/vnd.google-apps.folder' with pytest.raises(AssertionError): gditem.GDItem(named_path, id_path, mime_type)
def test_path_existent_file_as_folder(monkeypatch, initial_pwd_root): path = '/img.jpg/anything' named_path2 = ['/', 'folder1', 'file2.jpg'] id_path2 = ['root', 'folder1id', 'file2jpgid'] contents = [[gditem.GDItem(named_path2, id_path2, 'image/jpeg')]] assert_expected_results(contents, path, [], monkeypatch)