def testJsonFileExistsSoCreateShouldFail(self): json_file_name = 'boije_collection_test.json' create_boije_directory = getOrCreateBoijeFolder(DESTINATION_DIRECTORY, BOIJE_DIRECTORY_NAME) create_json_file = createJsonFile(json_file_name, create_boije_directory) create_json_file_again = createJsonFile(json_file_name, create_boije_directory) self.assertEqual(0, create_json_file_again)
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 testCreateIndexFile(self): json_file_name = 'boije_collection_test.json' create_boije_directory = getOrCreateBoijeFolder(DESTINATION_DIRECTORY, BOIJE_DIRECTORY_NAME) #this test will check to see if a function exists to create a json index file file_path = os.path.join(self.directory_path, json_file_name) #file returns path or 0 json_file_created = createJsonFile(json_file_name, create_boije_directory) self.assertEqual(file_path, json_file_created)
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)
def testSaveIndexToJSONAndReadIt(self): #checks to see if an index that is converted to a dict can be written into a file with json format index_to_check = 'c' create_boije_directory = getOrCreateBoijeFolder(DESTINATION_DIRECTORY, BOIJE_DIRECTORY_NAME) 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) #Our function will use a try/except block, that will will return a 1 or a 0 depending on success/failure dictionary_to_json = convertIndexToJson(dictionary_of_values, json_file_path) self.assertEqual(1, dictionary_to_json) json_file_size = os.path.getsize(json_file_path) json_file_exists = os.path.exists(json_file_path) self.assertTrue(json_file_exists) self.assertGreater(json_file_size, 0) #now we have a file that has something written to it. #We must now check to see if when we read it, it will equate to dictionary_of_values convert_json_to_dict = convertJsonToDict(json_file_path) self.assertEqual(convert_json_to_dict, dictionary_of_values)