def test__eq__(self): ca = CameraImage(path=Path("ca.JPG")) ca_bis = CameraImage(path=Path("ca.JPG")) cc = CameraImage(path=Path("cc.JPG")) assert ca == ca_bis, "Equal files" assert ca != cc, "Different files"
def test_get_timestamp_failed(self, pic_util_mock): path = Path("404.jpg") cam_pic = CameraImage(path=path) pic_util_mock.side_effect = FileNotFoundError with pytest.raises(FileNotFoundError): cam_pic.get_timestamp()
def test_fetchImages_all_inte(self, mock_logger, test_dir_env): fetcher = CameraImageFetcher(dcim_folder=Path("DCIM/")) res_cam = fetcher.fetch_images() expected_cam = [ CameraImage(path=Path("DCIM/100S3D_L/3D_L0001.JPG")), CameraImage(path=Path("DCIM/100S3D_L/3D_L0002.JPG")), CameraImage(path=Path("DCIM/101S3D_L/3D_L0003.JPG")), CameraImage(path=Path("DCIM/101S3D_L/3D_L0004.JPG")), CameraImage(path=Path("DCIM/101S3D_L/3D_L0000.JPG")) ] assert self.fake_env_patcher_mocks['dirs'].called, "Directories weren't fetch" assert res_cam == expected_cam, "Something went wrong"
def test_get_images(self, fetcher_test_env): lm = LotMaker(pictures_path=Path("picPath/"), rederbro_csv_path=None, nb_cams=2) img_set = lm.get_images(indexes=[0, 1]) expected_img_set = ImageSet(l={ 0: CameraImage(path=Path("picPath/APN0/DCIM/100S3D_L/3D_L0000.JPG")), 1: CameraImage(path=Path("picPath/APN1/DCIM/100S3D_L/3D_L0001.JPG")) }, number_of_pictures=2) assert img_set == expected_img_set, "Wrong image set"
def test_get_timestamp_ok(self, pic_util_mock): ts = 10 path = Path("toto.jpg") pic_util_mock.return_value = ts cam_pic = CameraImage(path=path) obtained_ts = cam_pic.get_timestamp() # calling more times cam_pic.get_timestamp() cam_pic.get_timestamp() assert len( pic_util_mock.call_args_list ) == 1, "Exif reader was called more than once (performance issu)" assert pic_util_mock.call_args_list[0] == call( path), "Exif reader was called with the wrong image path" assert obtained_ts == ts, "Wrong timestamp"
def test_fetch_images(self, mock_fetch_dir, mock_serie_check, mock_ordered_dir): dirs = [ Path("DCIM/100S3D_L/"), Path("DCIM/101S3D_L/")] dir_a_files = [ CameraImage(path=Path("DCIM/100S3D_L/3D_L0001.JPG")), CameraImage(path=Path("DCIM/100S3D_L/3D_L0002.JPG"))] dir_b_files = [ CameraImage(path=Path("DCIM/101S3D_L/3D_L0003.JPG")), CameraImage(path=Path("DCIM/101S3D_L/3D_L0004.JPG")), CameraImage(path=Path("DCIM/101S3D_L/3D_L0000.JPG"))] def fetch_dir(dcf_dir, start_index): if dcf_dir == Path("DCIM/100S3D_L/"): return (3, dir_a_files) if dcf_dir == Path("DCIM/101S3D_L/"): return (1, dir_b_files) return (1, []) mock_fetch_dir.side_effect = fetch_dir fetcher = object.__new__(CameraImageFetcher) fetcher.dcim_folder = MagicMock() fetcher.logger = MagicMock() mock_ordered_dir.return_value = dirs mock_serie_check.return_value = True res_img_cam = fetcher.fetch_images() expected_img_cam = [ CameraImage(path=Path("DCIM/100S3D_L/3D_L0001.JPG")), CameraImage(path=Path("DCIM/100S3D_L/3D_L0002.JPG")), CameraImage(path=Path("DCIM/101S3D_L/3D_L0003.JPG")), CameraImage(path=Path("DCIM/101S3D_L/3D_L0004.JPG")), CameraImage(path=Path("DCIM/101S3D_L/3D_L0000.JPG")) ] fetch_dcf_calls = [ call(dcf_dir=Path("DCIM/100S3D_L/"), start_index=1), call(dcf_dir=Path("DCIM/101S3D_L/"), start_index=3)] assert mock_ordered_dir.called, "Dir weren't ordered" assert mock_serie_check.call_args_list == [call(next_index=3, next_dcf_folder_path=Path("DCIM/101S3D_L/"))], "Check serie wasn't called" assert mock_fetch_dir.call_args_list == fetch_dcf_calls, "DCF fetch not called correctly" assert res_img_cam == expected_img_cam, "Fetcher isn't fetch images in correct order"
def test_get_images(self, mock_fetch_images): mock_fetch_images.return_value = [CameraImage(path=Path("DCIM/101S3D_L/3D_L0003.JPG"))] fetcher = object.__new__(CameraImageFetcher) fetcher._cache_camimg = None r = fetcher.get_images() fetcher.get_images() assert mock_fetch_images.call_count == 1, "Not lazy fetch called too much times" assert r == mock_fetch_images.return_value, "Result is incorrect"
def test___fetch_pic_files_from_dcf_dir(self, mock_path_exists, mock_name, mock_order_file): dcf_dir = Path("DCIM/101S3D_L/") files = [ Path("DCIM/101S3D_L/3D_L0000.JPG"), Path("DCIM/101S3D_L/3D_L0001.JPG"), Path("DCIM/101S3D_L/3D_L0010.JPG"), # should not be returned Path("DCIM/101S3D_L/3D_L0100.JPG"), Path("DCIM/101S3D_L/3D_L0101.JPG")] dcf_dir.files = MagicMock() dcf_dir.files.return_value = files def exists(p): return p in files def make_name_mocked(index): return Path("3D_L{}.JPG".format(str(index).zfill(4))) mock_order_file.return_value = files mock_name.side_effect = make_name_mocked mock_path_exists.side_effect = exists fetcher = object.__new__(CameraImageFetcher) # dcim_folder="DCIM" fetcher.logger = MagicMock() next_index, images = fetcher._fetch_pic_files_from_dcf_dir(dcf_dir=dcf_dir, start_index=100) assert dcf_dir.files.call_count == 1, "Files were listed more than once" assert mock_order_file.call_count == 1, "Files weren't ordered by index" expected_camimg = [ CameraImage(path=Path("DCIM/101S3D_L/3D_L0100.JPG")), CameraImage(path=Path("DCIM/101S3D_L/3D_L0101.JPG")), CameraImage(path=Path("DCIM/101S3D_L/3D_L0000.JPG")), CameraImage(path=Path("DCIM/101S3D_L/3D_L0001.JPG")) ] assert next_index == 2, "Wrong next index for search" assert len(images) == 4, "Should find 4 images" assert images == expected_camimg, "Wrong result in file order"
def _fetch_pic_files_from_dcf_dir( self, dcf_dir: Path, start_index: GORPRO_IMG_START_INDEX = int) -> (int, List[CameraImage]): """ Get pictures in a DCIM folder from a start index using DCF standard name convention. :param dcf_dir: Should be a directory which is under the DCIM directory (full path). :type dcf_dir: Path :param start_index: Index of the picture we will start at. Might not be 0 if we are continuing a serie from an other folder. :type start_index: int :return: The next index (to continue a serie in the next folder), the list of ordered CameraImage. :rtype: (next_index, pic_list) """ files = self._order_dcf_files(dcf_dir.files()) pic_files = [] # Files that should be returned in the correct order next_index = start_index if len(files) == 0: # No files nothing to do return (next_index, []) start_at_first_pic_index = self._make_dcf_pic_filename(index=next_index) == files[0].basename() while len(files) > 0: # since all files aren't treated next_file = dcf_dir / self._make_dcf_pic_filename(index=next_index) while next_file.exists(): pic_files.append(CameraImage(path=next_file)) files.remove(next_file) next_index += 1 next_file = dcf_dir / self._make_dcf_pic_filename(index=next_index) self.logger.debug("This file {} doesn't exists".format(next_file)) if len(files): # some files still need to be added if not start_at_first_pic_index: # we didn't start with the lower index in the current directory next_index = int(files[0].namebase[DCF_FILE_ALPHADIGIT_LEN:]) # starting over a new serie using the lower index in directory start_at_first_pic_index = True self.logger.debug("Some files missing resseting index to {} ".format(str(next_index))) continue else: # break in file indexing serie, shouldn't happened, will not treat orther files self.logger.error("Some files weren't added are they aren't part of the lower serie in the DCF directory :") self.logger.error("Latest added file is {} but file {} doesn't exists.".format(pic_files[-1], next_file)) files = [] return (next_index, pic_files)
def cam_img(p, ts): c = CameraImage(path=Path(p)) c._ts = ts return c
def test_get_last(self, mock_logger, test_dir_env): fetcher = CameraImageFetcher(dcim_folder=Path("DCIM/")) assert fetcher.get_last() == CameraImage(path=Path("DCIM/101S3D_L/3D_L0000.JPG"))
def test_get_pic(self, mock_logger, test_dir_env): fetcher = CameraImageFetcher(dcim_folder=Path("DCIM/")) assert fetcher.get_pic(index=1) == CameraImage(path=Path("DCIM/100S3D_L/3D_L0002.JPG")) assert fetcher.get_pic(index=30) is None