Prevent image save counter collisions
This commit is contained in:
@@ -11,6 +11,8 @@ class FakeImage:
|
||||
|
||||
def save(self, path, *args, **kwargs):
|
||||
self.saved_paths.append((path, args, kwargs))
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
open(path, "wb").close()
|
||||
|
||||
def convert(self, _mode):
|
||||
return self
|
||||
@@ -19,8 +21,23 @@ class FakeImage:
|
||||
class FakeBatch:
|
||||
shape = (1, 6, 8, 3)
|
||||
|
||||
def cpu(self):
|
||||
return self
|
||||
|
||||
def numpy(self):
|
||||
return self
|
||||
|
||||
def clip(self, _low, _high):
|
||||
return self
|
||||
|
||||
def __mul__(self, _value):
|
||||
return self
|
||||
|
||||
def astype(self, _dtype):
|
||||
return self
|
||||
|
||||
def __getitem__(self, _index):
|
||||
return types.SimpleNamespace(cpu=lambda: self, numpy=lambda: self, astype=lambda _dtype: self)
|
||||
return self
|
||||
|
||||
|
||||
class DumasImageNodeTests(unittest.TestCase):
|
||||
@@ -67,6 +84,44 @@ class DumasImageNodeTests(unittest.TestCase):
|
||||
("image", "mask", "width", "height", "filename", "index", "total"),
|
||||
)
|
||||
|
||||
def test_counter_uses_highest_existing_file(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
for name in ("image_001.png", "image_004.png", "image_002.png.jpg"):
|
||||
open(os.path.join(directory, name), "wb").close()
|
||||
|
||||
self.assertEqual(
|
||||
self.nodes._next_counter_for_relative_path(directory, "image_%counter%.png"),
|
||||
5,
|
||||
)
|
||||
|
||||
def test_save_batch_assigns_distinct_counter_names(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
FakeImage.saved_paths.clear()
|
||||
node = self.nodes.DumasSaveImageNode()
|
||||
node.save_images(
|
||||
[FakeBatch(), FakeBatch()],
|
||||
directory,
|
||||
"batch_%counter%",
|
||||
"png",
|
||||
95,
|
||||
False,
|
||||
True,
|
||||
)
|
||||
node.save_images(
|
||||
[FakeBatch(), FakeBatch()],
|
||||
directory,
|
||||
"batch_%counter%",
|
||||
"png",
|
||||
95,
|
||||
False,
|
||||
True,
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
[os.path.basename(path) for path, _args, _kwargs in FakeImage.saved_paths[-2:]],
|
||||
["batch_003.png", "batch_004.png"],
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user