def test_using_lazy_path(self): dataset = [] for i in range(10): dataset.append({"file_name": LazyPath(partial(_a_slow_func, i))}) dataset = DatasetFromList(dataset) for i in range(10): path = dataset[i]["file_name"] self.assertTrue(isinstance(path, LazyPath)) self.assertEqual(os.fspath(path), _a_slow_func(i))
def test_materialize(self) -> None: f = MagicMock(return_value="test") x = LazyPath(f) f.assert_not_called() p = os.fspath(x) f.assert_called() self.assertEqual(p, "test") p = os.fspath(x) # should only be called once f.assert_called_once() self.assertEqual(p, "test")
def test_getitem(self) -> None: x = LazyPath(lambda: "abc") with self.assertRaises(TypeError): x[0] _ = os.fspath(x) self.assertEqual(x[0], "a")
def test_PathManager(self) -> None: x = LazyPath(lambda: "./") output = self._pathmgr.ls(x) # pyre-ignore output_gt = self._pathmgr.ls("./") self.assertEqual(sorted(output), sorted(output_gt))
def test_getattr(self) -> None: x = LazyPath(lambda: "abc") with self.assertRaises(AttributeError): x.startswith("ab") _ = os.fspath(x) self.assertTrue(x.startswith("ab"))
def test_join(self) -> None: f = MagicMock(return_value="test") x = LazyPath(f) p = os.path.join(x, "a.txt") f.assert_called_once() self.assertEqual(p, "test/a.txt")