def setUp(self): super(RenamerUtilityTests, self).setUp() score_1_path = "./Boije 1.pdf" score_2_path = "./Boije 2.pdf" json_file_path = os.path.join(os.getcwd(), JSON_FILE_NAME) if not os.path.exists(score_1_path): # Download this score score_1_url = "pdf/Boije%201.pdf" score_1 = getScorePDF(score_1_url) saveScorePDF(score_1, "Boije 1", ".") if not os.path.exists("./Boije 2.pdf"): #Download this score score_2_url = "pdf/Boije%202.pdf" score_2 = getScorePDF(score_2_url) saveScorePDF(score_2, "Boije 2", ".") if (not os.path.exists(json_file_path) or not os.path.getsize(json_file_path)): json_file_path = createJsonFile(JSON_FILE_NAME, os.getcwd()) dictionary_of_composers = dictionaryInit(json_file_path) update_json_file = updateJsonFile( dictionary_of_composers, json_file_path) shutil.copy(score_1_path, self.directory_path) shutil.copy(score_2_path, self.directory_path) shutil.copy(json_file_path, self.directory_path)
def testDownloadScore_tryDownloadAgain_ResultsInError(self): index_to_check = 'c' create_boije_directory = getOrCreateBoijeFolder(DESTINATION_DIRECTORY, BOIJE_DIRECTORY_NAME) composer = 'Carcassi_M' score = 'Op_1_3_Sonates' json_file_name = 'boije_collection_test.json' json_file_path = createJsonFile(json_file_name, create_boije_directory) link_to_check = boijeLink(index_to_check) soup = getIndexSoup(link_to_check) dictionary_of_values = convertIndexToDictionary(soup) dictionary_to_json = convertIndexToJson(dictionary_of_values, json_file_path) convert_json_to_dict = convertJsonToDict(json_file_path) score_dictionary = copy.deepcopy(convert_json_to_dict) score_attributes = convert_json_to_dict = score_dictionary.get(composer).get(score) download_file = downloadAndSaveScore(create_boije_directory, composer, score, score_attributes) self.assertTrue(download_file) #now to save json file and reload it. score_dictionary[composer][score][2] = download_file convert_dict_to_json = updateJsonFile(score_dictionary, json_file_path) convert_json_to_dict_again = convertJsonToDict(json_file_path) score_dictionary = copy.deepcopy(convert_json_to_dict_again) score_attributes = score_dictionary.get(composer).get(score) download_file = downloadAndSaveScore(create_boije_directory, composer, score, score_attributes) self.assertTrue(download_file)
def testDictionaryLoadsFromJson(self): index_to_check = 'c' boije_directory, json_file_path = boijeCollectionInit(DESTINATION_DIRECTORY, BOIJE_DIRECTORY_NAME) link_to_check = boijeLink(index_to_check) convert_index_to_dictionary = convertIndexToDictionary(getIndexSoup(link_to_check)) write_to_json_file = updateJsonFile(convert_index_to_dictionary, json_file_path) returned_dictionary = dictionaryInit(json_file_path) self.assertEqual(convert_index_to_dictionary, returned_dictionary)
def testReadJsonFile_modifyAnd_SaveIt(self): index_to_check = 'c' create_boije_directory = getOrCreateBoijeFolder(DESTINATION_DIRECTORY, BOIJE_DIRECTORY_NAME) composer = 'Carcassi_M' score = 'Op_1_3_Sonates' json_file_name = 'boije_collection_test.json' json_file_path = createJsonFile(json_file_name, create_boije_directory) link_to_check = boijeLink(index_to_check) soup = getIndexSoup(link_to_check) dictionary_of_values = convertIndexToDictionary(soup) dictionary_to_json = convertIndexToJson(dictionary_of_values, json_file_path) convert_json_to_dict = convertJsonToDict(json_file_path) score_dictionary = copy.deepcopy(convert_json_to_dict) #make a function that wraps a try/except block #this function will need to copy the dictionary, and depending on whether #a download succeeds. It will update the 'downloaded' field from the dict #it will then return the dict carcassi = score_dictionary.get(composer) score_attributes = carcassi.get(score) html = score_attributes[0] self.assertFalse(score_attributes[2]) #it will be better if this updating function takes in score attributes downloaded_file = downloadAndSaveScore(create_boije_directory, composer, score, score_attributes) self.assertTrue(downloaded_file) #set score_dictionary value to True score_dictionary[composer][score][2] = downloaded_file self.assertTrue(score_dictionary.get(composer).get(score)[0]) self.assertNotEqual(score_dictionary[composer][score][2], convert_json_to_dict[composer][score][2]) convert_dict_to_json = updateJsonFile(score_dictionary, json_file_path) convert_json_to_dict_again = convertJsonToDict(json_file_path) self.assertEqual(convert_json_to_dict_again, score_dictionary)