def test_not_a_dir(self): empty_file = open(Path(self.test_dir.name) / 'empty', 'a') with self.assertRaises(NotADirectoryError): Carousel(Path(empty_file.name)) empty_file.close()
def test_with_images(self): filenames = ["01.png", "01.xml", "03.jpg", "04.pdf", "foo"] for name in filenames: (Path(self.test_dir.name) / name).touch() specimen = Carousel(Path(self.test_dir.name)) # Verify that the image files have been picked-up by the object self.assertEqual({"01.png", "03.jpg"}, set(map(lambda p: p.name, specimen._image_files)))
def test_regular_iteration(self): filenames = ["01.png", "01.xml", "03.jpg"] fobs = [] for name in filenames: fobs.append(open(Path(self.test_dir.name) / name, 'a')) # Valid paths that should be produced by an iteration over the entire test directory expected_paths = { Path(self.test_dir.name) / "01.png", Path(self.test_dir.name) / "03.jpg" } specimen = Carousel(Path(self.test_dir.name)) # Perform forward iteration until StopIteration # This movement must uncover all the images full_scan_results = set() for _ in range(0, len(expected_paths)): self.assertTrue(specimen.has_next()) full_scan_results.add(specimen.next()) self.assertFalse(specimen.has_next()) self.assertRaises(StopIteration, lambda: specimen.next()) self.assertEqual(expected_paths, full_scan_results) # Perform reverse iteration until StopIteration # This movement must go back to the first item, not returning the current one again reverse_scan_results = set() self.assertTrue(specimen.has_prev()) reverse_scan_results.add(specimen.prev()) self.assertFalse(specimen.has_prev()) self.assertRaises(StopIteration, lambda: specimen.prev()) self.assertTrue(len(reverse_scan_results) > 0) self.assertTrue(expected_paths > reverse_scan_results) # Perform the movement once again and move forward # This movement must move again towards the last item, without returning the current one again forward_scan_results = set() self.assertTrue(specimen.has_next()) forward_scan_results.add(specimen.next()) self.assertFalse(specimen.has_next()) self.assertRaises(StopIteration, lambda: specimen.next()) self.assertTrue(len(forward_scan_results) > 0) self.assertTrue(expected_paths > forward_scan_results) self.assertNotEqual(reverse_scan_results, forward_scan_results) for file in fobs: file.close()
def __init__(self, context_dir: Path, filter_factory: Optional[FilterBuilder] = None): """ Instantiate a new view over the image/metadata file pairs at the specified path. The new view will have most of its data uninitialized, since to load them means to start scanning the contents. Therefore, before attempting to retrieve any data, call the `load_next()` method. Optionally, a `FilterBuilder` can be provided as a second argument, which will be used for obtaining image filters. :arg context_dir: path to the directory under which all operations will be performed :arg filter_factory: a filter builder providing filters for the new view :raise FileNotFoundError: when the path points to an invalid location :raise NotADirectoryException: when the path point to a file that is not a directory """ # If given a filter provider, use it to generate a set of filters and apply them on the carousel if filter_factory is not None: self._carousel = Carousel(context_dir, filter_factory.get_all_filters()) else: self._carousel = Carousel(context_dir)
def test_filter(self): filenames_meta = [("included.png", "included"), ("excluded.jpg", "excluded")] for name, meta in filenames_meta: img_path = (Path(self.test_dir.name) / name) img_path.touch() # Use the author field as the filtering target write_meta(ImageMetadata(uuid4(), name, meta, None, None, None), img_path) specimen = Carousel(Path(self.test_dir.name), [lambda meta: meta.author == "included"]) self.assertEqual([Path(self.test_dir.name) / "included.png"], specimen._image_files)
def test_file_deletion_after_creation(self): test_dir_path = Path(self.test_dir.name) first = "first.png" fo = open(test_dir_path / first, 'a') second = "second.png" so = open(test_dir_path / second, 'a') third = "third.png" to = open(test_dir_path / third, 'a') fourth = "fourth.png" yo = open(test_dir_path / fourth, 'a') specimen = Carousel(test_dir_path) fo.close() os.remove(test_dir_path / first) results = set() # Perform a full scan for _ in range(0, 3): results.add(specimen.next()) # 'First' shouldn't be among the results self.assertEqual( { test_dir_path / second, test_dir_path / third, test_dir_path / fourth }, results) self.assertFalse(specimen.has_next()) self.assertRaises(StopIteration, lambda: specimen.next()) so.close() os.remove(test_dir_path / second) to.close() os.remove(test_dir_path / third) # Iteration should now jump straight at the fourth element self.assertTrue(specimen.has_prev()) self.assertEqual(test_dir_path / fourth, specimen.prev()) self.assertFalse(specimen.has_prev()) self.assertRaises(StopIteration, lambda: specimen.prev()) yo.close() os.remove(test_dir_path / fourth) # Carousel should now be stuck self.assertFalse(specimen.has_next()) self.assertRaises(StopIteration, lambda: specimen.next()) self.assertFalse(specimen.has_prev()) self.assertRaises(StopIteration, lambda: specimen.prev())
def test_iteration_empty(self): specimen = Carousel(Path(self.test_dir.name)) self.assertRaises(StopIteration, lambda: specimen.prev()) self.assertRaises(StopIteration, lambda: specimen.next())
def test_nonexistent_directory(self): with self.assertRaises(FileNotFoundError): Carousel(Path(Path(self.test_dir.name) / 'nonexistent'))