File size: 1,917 Bytes
5e4510c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# tests/test_database_cleanup.py

import os
import shutil
import tempfile
import time
import unittest

from openevolve.config import DatabaseConfig
from openevolve.database import Program, ProgramDatabase


class TestArtifactCleanup(unittest.TestCase):

    def setUp(self):
        self.temp_dir = tempfile.mkdtemp()
        self.db_path = os.path.join(self.temp_dir, "db")
        os.makedirs(self.db_path, exist_ok=True)

    def tearDown(self):
        shutil.rmtree(self.temp_dir)

    def test_artifact_cleanup(self):
        # 1. Configure the database for cleanup
        config = DatabaseConfig(
            db_path=self.db_path, cleanup_old_artifacts=True, artifact_retention_days=1
        )
        db = ProgramDatabase(config)

        # 2. Create dummy artifact directories
        artifacts_path = os.path.join(self.db_path, "artifacts")
        os.makedirs(artifacts_path, exist_ok=True)

        dir_to_keep = os.path.join(artifacts_path, "artifact_to_keep")
        dir_to_delete = os.path.join(artifacts_path, "artifact_to_delete")

        os.makedirs(dir_to_keep, exist_ok=True)
        os.makedirs(dir_to_delete, exist_ok=True)

        # 3. Set the modification time of one directory to be old
        old_time = time.time() - (2 * 24 * 60 * 60)  # 2 days ago
        os.utime(dir_to_delete, (old_time, old_time))

        self.assertTrue(os.path.exists(dir_to_keep))
        self.assertTrue(os.path.exists(dir_to_delete))

        # 4. Call the save method, which should trigger the cleanup
        db.save()

        # 5. Assert that the old directory was deleted and the new one was kept
        self.assertTrue(
            os.path.exists(dir_to_keep), "New artifact directory should not be deleted."
        )
        self.assertFalse(
            os.path.exists(dir_to_delete), "Old artifact directory should have been deleted."
        )


if __name__ == "__main__":
    unittest.main()