|
3 | 3 | import time
|
4 | 4 | import unittest
|
5 | 5 | from datetime import datetime, timedelta
|
| 6 | +from decimal import Decimal |
6 | 7 |
|
7 | 8 | import fsutil
|
8 | 9 |
|
@@ -666,6 +667,21 @@ def test_get_parent_dir(self):
|
666 | 667 | s = "/root/a/b/c/Document.txt"
|
667 | 668 | self.assertEqual(fsutil.get_parent_dir(s, 6), "/")
|
668 | 669 |
|
| 670 | + def test_get_unique_name(self): |
| 671 | + path = self.temp_path("a/b/c") |
| 672 | + fsutil.create_dir(path) |
| 673 | + name = fsutil.get_unique_name( |
| 674 | + path, |
| 675 | + prefix="custom-prefix", |
| 676 | + suffix="custom-suffix", |
| 677 | + extension="txt", |
| 678 | + separator="_", |
| 679 | + ) |
| 680 | + basename, extension = fsutil.split_filename(name) |
| 681 | + self.assertTrue(basename.startswith("custom-prefix_")) |
| 682 | + self.assertTrue(basename.endswith("_custom-suffix")) |
| 683 | + self.assertEqual(extension, "txt") |
| 684 | + |
669 | 685 | def test_is_dir(self):
|
670 | 686 | path = self.temp_path("a/b/")
|
671 | 687 | self.assertFalse(fsutil.is_dir(path))
|
@@ -1122,11 +1138,16 @@ def test_write_file(self):
|
1122 | 1138 | def test_write_file_json(self):
|
1123 | 1139 | path = self.temp_path("a/b/c.json")
|
1124 | 1140 | now = datetime.now()
|
1125 |
| - data = {"test": "Hello World", "test_datetime": now} |
| 1141 | + dec = Decimal("3.33") |
| 1142 | + data = { |
| 1143 | + "test": "Hello World", |
| 1144 | + "test_datetime": now, |
| 1145 | + "test_decimal": dec, |
| 1146 | + } |
1126 | 1147 | fsutil.write_file_json(self.temp_path("a/b/c.json"), data=data)
|
1127 | 1148 | self.assertEqual(
|
1128 | 1149 | fsutil.read_file(path),
|
1129 |
| - f"""{{"test": "Hello World", "test_datetime": "{now.isoformat()}"}}""", |
| 1150 | + f"""{{"test": "Hello World", "test_datetime": "{now.isoformat()}", "test_decimal": "{dec}"}}""", |
1130 | 1151 | )
|
1131 | 1152 |
|
1132 | 1153 | def test_write_file_with_append(self):
|
|
0 commit comments