File size: 14,348 Bytes
ea8c728
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
"""
Integration tests that validate existing examples work correctly.
These tests verify that evaluators, configs, and initial programs are properly set up.
"""

import importlib.util
import os
import sys
import tempfile
import shutil
import unittest
from pathlib import Path
from unittest.mock import patch

# Add project root to path
PROJECT_ROOT = Path(__file__).parent.parent.parent
sys.path.insert(0, str(PROJECT_ROOT))

from openevolve.config import Config, load_config
from openevolve.evaluator import Evaluator


class TestFunctionMinimizationExample(unittest.TestCase):
    """Integration tests for the function_minimization example"""

    EXAMPLE_DIR = PROJECT_ROOT / "examples" / "function_minimization"

    def test_config_loads(self):
        """Test that the config file loads without errors"""
        config_path = self.EXAMPLE_DIR / "config.yaml"
        if not config_path.exists():
            self.skipTest("function_minimization config not found")

        config = load_config(str(config_path))
        self.assertIsInstance(config, Config)
        self.assertGreater(config.max_iterations, 0)

    def test_initial_program_exists(self):
        """Test that the initial program file exists"""
        program_path = self.EXAMPLE_DIR / "initial_program.py"
        self.assertTrue(program_path.exists(), "initial_program.py should exist")

    def test_initial_program_has_evolve_block(self):
        """Test that the initial program has EVOLVE-BLOCK markers"""
        program_path = self.EXAMPLE_DIR / "initial_program.py"
        if not program_path.exists():
            self.skipTest("initial_program.py not found")

        content = program_path.read_text()
        self.assertIn("EVOLVE-BLOCK-START", content)
        self.assertIn("EVOLVE-BLOCK-END", content)

    def test_evaluator_exists(self):
        """Test that the evaluator file exists"""
        evaluator_path = self.EXAMPLE_DIR / "evaluator.py"
        self.assertTrue(evaluator_path.exists(), "evaluator.py should exist")

    def test_evaluator_has_evaluate_function(self):
        """Test that the evaluator has an evaluate function"""
        evaluator_path = self.EXAMPLE_DIR / "evaluator.py"
        if not evaluator_path.exists():
            self.skipTest("evaluator.py not found")

        spec = importlib.util.spec_from_file_location("evaluator", evaluator_path)
        evaluator_module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(evaluator_module)

        self.assertTrue(hasattr(evaluator_module, "evaluate"))
        self.assertTrue(callable(evaluator_module.evaluate))

    def test_evaluator_runs_on_initial_program(self):
        """Test that the evaluator can evaluate the initial program"""
        evaluator_path = self.EXAMPLE_DIR / "evaluator.py"
        program_path = self.EXAMPLE_DIR / "initial_program.py"

        if not evaluator_path.exists() or not program_path.exists():
            self.skipTest("Example files not found")

        # Load evaluator
        spec = importlib.util.spec_from_file_location("evaluator", evaluator_path)
        evaluator_module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(evaluator_module)

        # Run evaluation
        result = evaluator_module.evaluate(str(program_path))

        # Check result structure
        if hasattr(result, 'metrics'):
            # EvaluationResult object
            metrics = result.metrics
        else:
            # Dictionary
            metrics = result

        self.assertIn("combined_score", metrics)
        self.assertIsInstance(metrics["combined_score"], (int, float))


class TestCirclePackingExample(unittest.TestCase):
    """Integration tests for the circle_packing example"""

    EXAMPLE_DIR = PROJECT_ROOT / "examples" / "circle_packing"

    def test_config_loads(self):
        """Test that config files load without errors"""
        for config_name in ["config_phase_1.yaml", "config_phase_2.yaml"]:
            config_path = self.EXAMPLE_DIR / config_name
            if config_path.exists():
                config = load_config(str(config_path))
                self.assertIsInstance(config, Config)

    def test_evaluator_exists(self):
        """Test that evaluator exists"""
        evaluator_path = self.EXAMPLE_DIR / "evaluator.py"
        self.assertTrue(evaluator_path.exists(), "evaluator.py should exist")

    def test_evaluator_has_evaluate_function(self):
        """Test that the evaluator has required functions"""
        evaluator_path = self.EXAMPLE_DIR / "evaluator.py"
        if not evaluator_path.exists():
            self.skipTest("evaluator.py not found")

        spec = importlib.util.spec_from_file_location("evaluator", evaluator_path)
        evaluator_module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(evaluator_module)

        self.assertTrue(hasattr(evaluator_module, "evaluate"))


class TestSignalProcessingExample(unittest.TestCase):
    """Integration tests for the signal_processing example"""

    EXAMPLE_DIR = PROJECT_ROOT / "examples" / "signal_processing"

    def test_config_loads(self):
        """Test that the config file loads"""
        config_path = self.EXAMPLE_DIR / "config.yaml"
        if not config_path.exists():
            self.skipTest("signal_processing config not found")

        config = load_config(str(config_path))
        self.assertIsInstance(config, Config)

    def test_evaluator_exists(self):
        """Test that evaluator exists"""
        evaluator_path = self.EXAMPLE_DIR / "evaluator.py"
        if not evaluator_path.exists():
            self.skipTest("evaluator.py not found")
        self.assertTrue(evaluator_path.exists())


class TestEvaluatorIntegration(unittest.TestCase):
    """Integration tests for the Evaluator class with real examples"""

    def test_evaluator_loads_function_minimization(self):
        """Test that Evaluator can load the function_minimization evaluator"""
        evaluator_path = PROJECT_ROOT / "examples" / "function_minimization" / "evaluator.py"
        if not evaluator_path.exists():
            self.skipTest("function_minimization evaluator not found")

        from openevolve.config import EvaluatorConfig
        config = EvaluatorConfig(timeout=30, cascade_evaluation=True)

        evaluator = Evaluator(config, str(evaluator_path))
        self.assertIsNotNone(evaluator.evaluate_function)
        self.assertTrue(callable(evaluator.evaluate_function))

    def test_evaluator_module_has_cascade_functions(self):
        """Test that function_minimization evaluator has cascade functions"""
        evaluator_path = PROJECT_ROOT / "examples" / "function_minimization" / "evaluator.py"
        if not evaluator_path.exists():
            self.skipTest("function_minimization evaluator not found")

        # Load the module directly to check for cascade functions
        spec = importlib.util.spec_from_file_location("evaluator", evaluator_path)
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)

        # function_minimization has evaluate_stage1 and evaluate_stage2
        self.assertTrue(hasattr(module, "evaluate_stage1"))
        self.assertTrue(hasattr(module, "evaluate_stage2"))
        self.assertTrue(callable(module.evaluate_stage1))
        self.assertTrue(callable(module.evaluate_stage2))


class TestConfigIntegration(unittest.TestCase):
    """Integration tests for config loading across examples"""

    @patch.dict(os.environ, {"ANTHROPIC_API_KEY": "test-key"})
    def test_all_example_configs_load(self):
        """Test that all example config files can be loaded"""
        examples_dir = PROJECT_ROOT / "examples"
        failed_configs = []

        for config_path in examples_dir.rglob("*config*.yaml"):
            try:
                config = load_config(str(config_path))
                self.assertIsInstance(config, Config)
            except Exception as e:
                failed_configs.append((str(config_path), str(e)))

        if failed_configs:
            failure_msg = "\n".join([f"{path}: {error}" for path, error in failed_configs])
            self.fail(f"Failed to load configs:\n{failure_msg}")

    def test_config_has_required_sections(self):
        """Test that loaded configs have required sections"""
        config_path = PROJECT_ROOT / "examples" / "function_minimization" / "config.yaml"
        if not config_path.exists():
            self.skipTest("function_minimization config not found")

        config = load_config(str(config_path))

        # Check required sections
        self.assertIsNotNone(config.llm)
        self.assertIsNotNone(config.database)
        self.assertIsNotNone(config.evaluator)
        self.assertIsNotNone(config.prompt)


class TestEndToEndWithMockedLLM(unittest.TestCase):
    """End-to-end tests with mocked LLM responses"""

    def setUp(self):
        """Set up test fixtures"""
        self.temp_dir = tempfile.mkdtemp()
        self.example_dir = PROJECT_ROOT / "examples" / "function_minimization"

    def tearDown(self):
        """Clean up"""
        shutil.rmtree(self.temp_dir, ignore_errors=True)

    def test_database_stores_and_retrieves_programs(self):
        """Test that the database can store and retrieve programs"""
        from openevolve.database import ProgramDatabase, Program, DatabaseConfig

        config = DatabaseConfig(population_size=100)
        db = ProgramDatabase(config)

        # Add a program
        program = Program(
            id="test_prog_1",
            code="def test(): return 42",
            generation=0,
            metrics={"combined_score": 0.5},
        )
        db.add(program)

        # Retrieve it
        retrieved = db.programs.get("test_prog_1")
        self.assertIsNotNone(retrieved)
        self.assertEqual(retrieved.code, "def test(): return 42")

    def test_program_evolution_tracking(self):
        """Test that program generations are tracked correctly"""
        from openevolve.database import ProgramDatabase, Program, DatabaseConfig

        config = DatabaseConfig(population_size=100)
        db = ProgramDatabase(config)

        # Add parent program
        parent = Program(
            id="parent_1",
            code="def test(): return 1",
            generation=0,
            metrics={"combined_score": 0.3},
        )
        db.add(parent)

        # Add child program
        child = Program(
            id="child_1",
            code="def test(): return 2",
            generation=1,
            parent_id="parent_1",
            metrics={"combined_score": 0.5},
        )
        db.add(child)

        # Verify relationships
        self.assertEqual(db.programs["child_1"].parent_id, "parent_1")
        self.assertEqual(db.programs["child_1"].generation, 1)

    def test_evaluator_returns_evaluation_result(self):
        """Test that evaluators return proper EvaluationResult objects"""
        from openevolve.evaluation_result import EvaluationResult

        evaluator_path = self.example_dir / "evaluator.py"
        program_path = self.example_dir / "initial_program.py"

        if not evaluator_path.exists() or not program_path.exists():
            self.skipTest("Example files not found")

        spec = importlib.util.spec_from_file_location("evaluator", evaluator_path)
        evaluator_module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(evaluator_module)

        result = evaluator_module.evaluate(str(program_path))

        # Should be an EvaluationResult or dict with metrics
        if isinstance(result, EvaluationResult):
            self.assertIn("combined_score", result.metrics)
        else:
            self.assertIn("combined_score", result)


class TestExampleStructure(unittest.TestCase):
    """Tests to verify example directory structure is correct"""

    def test_examples_have_required_files(self):
        """Test that examples have the minimum required files"""
        examples_dir = PROJECT_ROOT / "examples"

        # These examples should have at least a config and evaluator
        required_examples = [
            "function_minimization",
            "circle_packing",
        ]

        for example_name in required_examples:
            example_dir = examples_dir / example_name
            if not example_dir.exists():
                continue

            # Check for config
            config_files = list(example_dir.glob("*config*.yaml"))
            self.assertGreater(
                len(config_files), 0,
                f"{example_name} should have at least one config file"
            )

            # Check for evaluator
            evaluator_path = example_dir / "evaluator.py"
            self.assertTrue(
                evaluator_path.exists(),
                f"{example_name} should have evaluator.py"
            )

    def test_evaluators_are_importable(self):
        """Test that all evaluators can be imported without errors"""
        examples_dir = PROJECT_ROOT / "examples"
        failed_imports = []

        for evaluator_path in examples_dir.rglob("evaluator.py"):
            try:
                spec = importlib.util.spec_from_file_location(
                    f"evaluator_{evaluator_path.parent.name}",
                    evaluator_path
                )
                module = importlib.util.module_from_spec(spec)
                spec.loader.exec_module(module)

                # Verify evaluate function exists
                if not hasattr(module, "evaluate"):
                    failed_imports.append(
                        (str(evaluator_path), "Missing evaluate function")
                    )
            except Exception as e:
                failed_imports.append((str(evaluator_path), str(e)))

        if failed_imports:
            # Only fail if critical examples fail
            critical_failures = [
                f for f in failed_imports
                if "function_minimization" in f[0] or "circle_packing" in f[0]
            ]
            if critical_failures:
                failure_msg = "\n".join(
                    [f"{path}: {error}" for path, error in critical_failures]
                )
                self.fail(f"Critical evaluators failed to import:\n{failure_msg}")


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