def test_create_file_collection_by_dictionary(self): file_collection = self.dal.file_collection.create( FileCollection(self.file_collection_input_dict)) assert file_collection.id assert file_collection.model_id == self.file_collection_input_dict[ 'model_id'] assert file_collection.driver_type == self.file_collection_input_dict[ 'driver_type'] assert file_collection.filehash == self.file_collection_input_dict[ 'filehash'] assert file_collection.path == self.file_collection_input_dict['path'] assert file_collection.created_at assert file_collection.updated_at file_collection_2 = self.dal.file_collection.create( FileCollection(self.file_collection_input_dict)) assert file_collection_2.id != file_collection.id test_file_collection_input_dict = self.file_collection_input_dict.copy( ) test_file_collection_input_dict['id'] = "file_collection_id" file_collection_3 = self.dal.file_collection.create( FileCollection(test_file_collection_input_dict)) assert file_collection_3.id == test_file_collection_input_dict['id']
def test_query_file_collections_multiple(self): file_collection_1 = self.dal.file_collection.create( FileCollection(self.file_collection_input_dict)) file_collection_2 = self.dal.file_collection.create( FileCollection(self.file_collection_input_dict)) file_collection_3 = self.dal.file_collection.create( FileCollection(self.file_collection_input_dict)) results = self.dal.file_collection.query({}, sort_key="created_at", sort_order="ascending") assert len(results) == 3 assert results[0].created_at == file_collection_1.created_at assert results[1].created_at == file_collection_2.created_at results = self.dal.file_collection.query({}, sort_key="created_at", sort_order="descending") assert len(results) == 3 assert results[0].created_at == file_collection_3.created_at assert results[1].created_at == file_collection_2.created_at # Wrong order being passed in failed = False try: _ = self.dal.file_collection.query({}, sort_key='created_at', sort_order='wrong_order') except InvalidArgumentType: failed = True assert failed # Wrong key and order being passed in failed = False try: _ = self.dal.file_collection.query({}, sort_key='wrong_key', sort_order='wrong_order') except InvalidArgumentType: failed = True assert failed # wrong key and right order being passed in expected_items = self.dal.file_collection.query({}, sort_key='created_at', sort_order='ascending') items = self.dal.file_collection.query({}, sort_key='wrong_key', sort_order='ascending') expected_ids = [item.id for item in expected_items] ids = [item.id for item in items] assert set(expected_ids) == set(ids)
def test_get_by_shortened_id_file_collection(self): file_collection = self.dal.file_collection.create( FileCollection(self.file_collection_input_dict)) result = self.dal.file_collection.get_by_shortened_id( file_collection.id[:10]) assert file_collection.id == result.id
def test_init_with_id(self): self.input_dict['id'] = "test" file_collection_entity = FileCollection(self.input_dict) for k, v in self.input_dict.items(): assert getattr(file_collection_entity, k) == v assert file_collection_entity.created_at assert file_collection_entity.updated_at
def test_init_no_id(self): file_collection_entity = FileCollection(self.input_dict) for k, v in self.input_dict.items(): assert getattr(file_collection_entity, k) == v assert file_collection_entity.id == None assert file_collection_entity.created_at assert file_collection_entity.updated_at
def test_query_file_collections_range_query(self): _ = self.dal.file_collection.create( FileCollection(self.file_collection_input_dict)) _ = self.dal.file_collection.create( FileCollection(self.file_collection_input_dict)) _ = self.dal.file_collection.create( FileCollection(self.file_collection_input_dict)) file_collections = self.dal.file_collection.query( {}, sort_key="created_at", sort_order="descending") result = self.dal.file_collection.query({ "created_at": { "$lt": file_collections[1].created_at.strftime( '%Y-%m-%dT%H:%M:%S.%fZ') } }) assert len(file_collections) == 3 assert len(result) == 1
def create(self, paths): """Create a FileCollection Parameters ---------- paths : list list of absolute or relative filepaths and/or dirpaths to collect with destination names (e.g. "/path/to/file>hello", "/path/to/file2", "/path/to/dir>newdir") Returns ------- FileCollection an object representing the collection of files Raises ------ RequiredArgumentMissing if any arguments needed for FileCollection are not provided """ # TODO: Validate Inputs create_dict = { "model_id": self.model.id, } # Populate a path list from the user inputs compatible with the file driver # a. add in user given paths as is if they exist (already within paths) # b. if there are NO paths found from input AND project files directory if not paths and os.path.isdir(self.file_driver.files_directory): paths.extend([ os.path.join(self.file_driver.files_directory, filepath) for filepath in list_all_filepaths( self.file_driver.files_directory) ]) # Parse paths to create collection and add in filehash create_dict['filehash'], create_dict['file_path_map'], create_dict['directory_path_map'] =\ self.file_driver.create_collection(paths) # If file collection with filehash exists, return it results = self.dal.file_collection.query({ "filehash": create_dict['filehash'] }) if results: return results[0] # Add in path of the collection created above create_dict['path'] = self.file_driver.get_relative_collection_path( create_dict['filehash']) # Add in driver_type of the relative collection path create_dict['driver_type'] = self.file_driver.type # Create file collection and return return self.dal.file_collection.create(FileCollection(create_dict))
def test_delete_file_collection(self): file_collection = self.dal.file_collection.create( FileCollection(self.file_collection_input_dict)) self.dal.file_collection.delete(file_collection.id) deleted = False try: self.dal.file_collection.get_by_id(file_collection.id) except EntityNotFound: deleted = True assert deleted
def test_get_by_id_file_collection_new_driver_instance(self): file_collection = self.dal.file_collection.create( FileCollection(self.file_collection_input_dict)) # create new dal with new driver instance (success) new_driver_instance = BlitzDBDALDriver("file", self.temp_dir) new_dal_instance = LocalDAL(new_driver_instance) new_file_collection_1 = new_dal_instance.file_collection.get_by_id( file_collection.id) assert new_file_collection_1.id == file_collection.id # create new dal instance with same driver (success) new_dal_instance = LocalDAL(self.datadriver) new_file_collection_2 = new_dal_instance.file_collection.get_by_id( file_collection.id) assert new_file_collection_2.id == file_collection.id
def create(self, filepaths): """Create a FileCollection Parameters ---------- filepaths : list list of absolute filepaths to collect Returns ------- FileCollection an object representing the collection of files Raises ------ RequiredArgumentMissing if any arguments needed for FileCollection are not provided """ # TODO: Validate Inputs create_dict = { "model_id": self.model.id, } ## Required args for FileCollection entity required_args = ["filehash", "path", "driver_type"] for required_arg in required_args: if required_arg == "filehash": create_dict[required_arg] = \ self.file_driver.create_collection(filepaths) # If file collection with filehash exists, return it results = self.dal.file_collection.query({ "filehash": create_dict[required_arg] }) if results: return results[0] elif required_arg == "path": create_dict[required_arg] = \ self.file_driver.get_relative_collection_path(create_dict['filehash']) elif required_arg == "driver_type": create_dict[required_arg] = self.file_driver.type else: raise NotImplementedError() # Create file collection and return return self.dal.file_collection.create(FileCollection(create_dict))
def test_update_file_collection(self): file_collection = self.dal.file_collection.create( FileCollection(self.file_collection_input_dict)) # Update required and optional parameters updated_file_collection_input_dict = self.file_collection_input_dict.copy( ) updated_file_collection_input_dict['id'] = file_collection.id updated_file_collection_input_dict['driver_type'] = "new_driver" updated_file_collection_input_dict['created_at'] = datetime.utcnow() updated_file_collection = self.dal.file_collection.update( updated_file_collection_input_dict) assert file_collection.id == updated_file_collection.id assert file_collection.updated_at < updated_file_collection.updated_at assert updated_file_collection.driver_type == updated_file_collection_input_dict[ 'driver_type'] assert updated_file_collection.created_at == updated_file_collection_input_dict[ 'created_at']
def test_to_dictionary(self): file_collection_entity = FileCollection(self.input_dict) output_dict = file_collection_entity.to_dictionary() for k, v in output_dict.items(): assert v == getattr(file_collection_entity, k)
def test_eq(self): file_collection_entity_1 = FileCollection(self.input_dict) file_collection_entity_2 = FileCollection(self.input_dict) assert file_collection_entity_1 == file_collection_entity_2
def test_query_file_collections_basic(self): file_collection = self.dal.file_collection.create( FileCollection(self.file_collection_input_dict)) assert len(self.dal.file_collection.query({"id": file_collection.id})) == 1