Start process for uploading files
This commit is contained in:
parent
853d2ecf69
commit
05211354ea
27
jardin.py
27
jardin.py
|
@ -1,6 +1,8 @@
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
import os, sys
|
import re
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
import bs4
|
import bs4
|
||||||
|
|
||||||
|
@ -52,13 +54,26 @@ def get_local_filenames(directory):
|
||||||
return os.listdir(directory)
|
return os.listdir(directory)
|
||||||
|
|
||||||
def get_missing_filenames(localFiles, gardenFiles):
|
def get_missing_filenames(localFiles, gardenFiles):
|
||||||
missingFilenames = []
|
missingFiles = []
|
||||||
|
|
||||||
for localFile in localFiles:
|
for localFile in localFiles:
|
||||||
if localFile not in gardenFiles:
|
if localFile not in gardenFiles:
|
||||||
missingFilenames.append(localFile)
|
missingFiles.append(localFile)
|
||||||
|
|
||||||
return missingFilenames
|
return missingFiles
|
||||||
|
|
||||||
|
def get_user_id(browser):
|
||||||
|
url = browser.get_current_url()
|
||||||
|
userId = re.search("[0-9a-f]{24}", url)[0]
|
||||||
|
return userId
|
||||||
|
|
||||||
|
def upload_file(browser, userId, file):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def upload_files(browser, missingFiles):
|
||||||
|
userId = get_user_id(browser)
|
||||||
|
for file in missingFiles:
|
||||||
|
upload_file(browser, userId, file)
|
||||||
|
|
||||||
def main(email, password, directory):
|
def main(email, password, directory):
|
||||||
browser = initialize_webdriver()
|
browser = initialize_webdriver()
|
||||||
|
@ -67,8 +82,8 @@ def main(email, password, directory):
|
||||||
click_go_to_your_garden(browser)
|
click_go_to_your_garden(browser)
|
||||||
garden_filenames = get_garden_filenames(browser)
|
garden_filenames = get_garden_filenames(browser)
|
||||||
local_filenames = get_local_filenames(directory)
|
local_filenames = get_local_filenames(directory)
|
||||||
get_missing_filenames(local_filenames, garden_filenames)
|
missing_files = get_missing_filenames(local_filenames, garden_filenames)
|
||||||
# For each file in the second list, go through the file upload process
|
upload_files(browser, missing_files)
|
||||||
#browser.close()
|
#browser.close()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -133,6 +133,48 @@ class TestJardin(unittest.TestCase):
|
||||||
|
|
||||||
self.assertEqual(missingFilenames, ["file1.jpg", "file3.jpg", "file5.jpg"])
|
self.assertEqual(missingFilenames, ["file1.jpg", "file3.jpg", "file5.jpg"])
|
||||||
|
|
||||||
|
@patch("jardin.webdriver.Firefox")
|
||||||
|
def test_get_user_id_calls_get_current_url(self, mockFirefox):
|
||||||
|
mockUserId = "73927475b259be67f8cea98f"
|
||||||
|
mockUrl = f"https://filegarden.com/users/{mockUserId}/garden/#"
|
||||||
|
mockFirefox.get_current_url.return_value = mockUrl
|
||||||
|
|
||||||
|
jardin.get_user_id(mockFirefox)
|
||||||
|
|
||||||
|
mockFirefox.get_current_url.assert_called_once()
|
||||||
|
|
||||||
|
@patch("jardin.webdriver.Firefox")
|
||||||
|
def test_get_user_id_returns_user_id_from_url(self, mockFirefox):
|
||||||
|
mockUserId = "73927475b259be67f8cea98f"
|
||||||
|
mockUrl = f"https://filegarden.com/users/{mockUserId}/garden/#"
|
||||||
|
mockFirefox.get_current_url.return_value = mockUrl
|
||||||
|
|
||||||
|
userId = jardin.get_user_id(mockFirefox)
|
||||||
|
|
||||||
|
self.assertEqual(userId, mockUserId)
|
||||||
|
|
||||||
|
@patch("jardin.get_user_id")
|
||||||
|
@patch("jardin.webdriver.Firefox")
|
||||||
|
def test_upload_files_calls_get_user_id(self, mockFirefox, mockGetUserId):
|
||||||
|
jardin.upload_files(mockFirefox, [])
|
||||||
|
|
||||||
|
mockGetUserId.assert_called_once_with(mockFirefox)
|
||||||
|
|
||||||
|
@patch("jardin.get_user_id")
|
||||||
|
@patch("jardin.upload_file")
|
||||||
|
@patch("jardin.webdriver.Firefox")
|
||||||
|
def test_upload_files_calls_upload_file_once_per_file(self, mockFirefox, mockUploadFile, mockGetUserId):
|
||||||
|
mockGetUserId.return_value = "userid"
|
||||||
|
mockFiles = ["file1.jpg", "file2.jpg", "file3.jpg"]
|
||||||
|
|
||||||
|
jardin.upload_files(mockFirefox, mockFiles)
|
||||||
|
|
||||||
|
mockUploadFile.assert_has_calls([
|
||||||
|
call(mockFirefox, "userid", "file1.jpg"),
|
||||||
|
call(mockFirefox, "userid", "file2.jpg"),
|
||||||
|
call(mockFirefox, "userid", "file3.jpg")
|
||||||
|
])
|
||||||
|
|
||||||
@patch("jardin.initialize_webdriver")
|
@patch("jardin.initialize_webdriver")
|
||||||
@patch("jardin.navigate_to_filegarden")
|
@patch("jardin.navigate_to_filegarden")
|
||||||
@patch("jardin.login_with_password")
|
@patch("jardin.login_with_password")
|
||||||
|
@ -140,7 +182,9 @@ class TestJardin(unittest.TestCase):
|
||||||
@patch("jardin.get_garden_filenames")
|
@patch("jardin.get_garden_filenames")
|
||||||
@patch("jardin.get_local_filenames")
|
@patch("jardin.get_local_filenames")
|
||||||
@patch("jardin.get_missing_filenames")
|
@patch("jardin.get_missing_filenames")
|
||||||
|
@patch("jardin.upload_files")
|
||||||
def test_main_calls_methods_in_correct_order(self,
|
def test_main_calls_methods_in_correct_order(self,
|
||||||
|
mockUploadFiles,
|
||||||
mockGetMissingFilenames,
|
mockGetMissingFilenames,
|
||||||
mockGetLocalFilenames,
|
mockGetLocalFilenames,
|
||||||
mockGetGardenFilenames,
|
mockGetGardenFilenames,
|
||||||
|
@ -161,7 +205,7 @@ class TestJardin(unittest.TestCase):
|
||||||
mockGetGardenFilenames.assert_called_once_with(mockInitializeWebdriver.return_value)
|
mockGetGardenFilenames.assert_called_once_with(mockInitializeWebdriver.return_value)
|
||||||
mockGetLocalFilenames.assert_called_once_with(mockDirectory)
|
mockGetLocalFilenames.assert_called_once_with(mockDirectory)
|
||||||
mockGetMissingFilenames.assert_called_once_with(mockGetLocalFilenames.return_value, mockGetGardenFilenames.return_value)
|
mockGetMissingFilenames.assert_called_once_with(mockGetLocalFilenames.return_value, mockGetGardenFilenames.return_value)
|
||||||
|
mockUploadFiles.assert_called_once_with(mockInitializeWebdriver.return_value, mockGetMissingFilenames.return_value)
|
||||||
#mockInitializeWebdriver.return_value.close.assert_called_once()
|
#mockInitializeWebdriver.return_value.close.assert_called_once()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue