def test_set_new_lb_section_for_large_files(self, mock_labbook): x, y, lb = mock_labbook assert FileOperations.is_set_untracked(labbook=lb, section='input') is False FileOperations.set_untracked(labbook=lb, section='input') assert FileOperations.is_set_untracked(labbook=lb, section='input') is True assert FileOperations.is_set_untracked(labbook=lb, section='code') is False # 1 - Ensure there are no untracked changes after the set operation s = lb.git.status() for key in s.keys(): assert not s[key] # 2 - Add a file to the input directory using the old-fashioned add file op. with open('/tmp/unittestfile', 'w') as f: f.write('------------------------\n') r = FileOperations.put_file(lb, section="input", src_file=f.name, dst_path='') assert os.path.isfile( os.path.join(lb.root_dir, 'input', 'unittestfile')) # 3 - Make sure the new file exists but is not tracked (i.e., the git commit is the same) s = lb.git.status() for key in s.keys(): assert not s[key]
def mutate_and_process_upload(cls, info, upload_file_path, upload_filename, **kwargs): if not upload_file_path: logger.error('No file uploaded') raise ValueError('No file uploaded') owner = kwargs.get('owner') labbook_name = kwargs.get('labbook_name') section = kwargs.get('section') transaction_id = kwargs.get('transaction_id') file_path = kwargs.get('file_path') try: username = get_logged_in_username() lb = InventoryManager().load_labbook(username, owner, labbook_name, author=get_logged_in_author()) dst_path = os.path.join(os.path.dirname(file_path), upload_filename) with lb.lock(): fops = FileOperations.put_file(labbook=lb, section=section, src_file=upload_file_path, dst_path=dst_path, txid=transaction_id) finally: try: logger.debug(f"Removing temp file {upload_file_path}") os.remove(upload_file_path) except FileNotFoundError: pass # Create data to populate edge create_data = { 'owner': owner, 'name': labbook_name, 'section': section, 'key': fops['key'], '_file_info': fops } # TODO: Fix cursor implementation..this currently doesn't make sense when adding edges without a refresh cursor = base64.b64encode(f"{0}".encode('utf-8')) return AddLabbookFile(new_labbook_file_edge=LabbookFileConnection.Edge( node=LabbookFile(**create_data), cursor=cursor))
def test_with_the_whole_suite_of_file_operations_on_an_UNTRACKED_labbook( self, mock_labbook): x, y, lb = mock_labbook hash_0 = lb.git.commit_hash FileOperations.set_untracked(labbook=lb, section='input') hash_1 = lb.git.commit_hash assert hash_0 != hash_1 with open('/tmp/unittestfile', 'wb') as f: f.write('àbčdęfghįjkłmñöpqrštūvwxÿż0123456789'.encode('utf-8')) assert not os.path.exists( os.path.join(lb.root_dir, 'input', 'unittestfile')) r = FileOperations.put_file(lb, section="input", src_file=f.name, dst_path='') assert os.path.exists( os.path.join(lb.root_dir, 'input', 'unittestfile')) hash_2 = lb.git.commit_hash FileOperations.delete_files(lb, section='input', relative_paths=['unittestfile']) hash_3 = lb.git.commit_hash target_path = os.path.join(lb.root_dir, 'input', 'unittestfile') assert not os.path.exists(target_path) assert lb.is_repo_clean # Hash_2 == hash_3 because we delete a file in an UNTRACKED section assert hash_2 == hash_3 FileOperations.makedir(lb, 'input/sample-untracked-dir/nested-dir') hash_4 = lb.git.commit_hash assert hash_3 == hash_4 with open('/tmp/unittestfile', 'wb') as f: f.write('aaaaaæ'.encode('utf-8')) FileOperations.put_file(lb, section='input', src_file=f.name, dst_path='sample-untracked-dir/nested-dir') hash_5 = lb.git.commit_hash assert hash_4 == hash_5 FileOperations.move_file( lb, section='input', src_rel_path='sample-untracked-dir/nested-dir/unittestfile', dst_rel_path='unittestfile') assert not os.path.exists( os.path.join(lb.root_dir, 'input', 'sample-untracked-dir/nested-dir/unittestfile')) assert os.path.exists( os.path.join(lb.root_dir, 'input', 'unittestfile')) hash_6 = lb.git.commit_hash assert hash_5 == hash_6 FileOperations.delete_files( lb, section='input', relative_paths=['sample-untracked-dir/nested-dir']) hash_7 = lb.git.commit_hash assert hash_6 == hash_7