File size: 9,332 Bytes
b3e25ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import unittest
import os
import sys
import json
from unittest.mock import patch, MagicMock

# Add parent directory to path for imports
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

# Import modules to test
from model_config import ModelConfigManager

class MockDB:
    """Mock database for testing."""
    
    def __init__(self):
        self.configs = {}
        
    def get_config(self, config_id):
        return self.configs.get(config_id)
        
    def add_config(self, config_id, config):
        self.configs[config_id] = config
        return config_id

class TestModelConfigManager(unittest.TestCase):
    """Test the ModelConfigManager class."""
    
    def setUp(self):
        """Set up test environment."""
        self.db = MockDB()
        self.config_dir = "test_model_configs"
        
        # Create test directory
        os.makedirs(self.config_dir, exist_ok=True)
        
        # Create a test instance with the test directory
        self.manager = ModelConfigManager(self.db)
        self.manager.config_dir = self.config_dir
        
    def tearDown(self):
        """Clean up after tests."""
        # Remove test files
        for filename in os.listdir(self.config_dir):
            file_path = os.path.join(self.config_dir, filename)
            if os.path.isfile(file_path):
                os.unlink(file_path)
        
        # Remove test directory
        os.rmdir(self.config_dir)
    
    def test_initialize_default_configs(self):
        """Test _initialize_default_configs method."""
        # Check that default configs are created
        self.manager._initialize_default_configs()
        
        # Verify files exist
        for model_type in self.manager.default_configs:
            config_path = os.path.join(self.config_dir, f"{model_type}.json")
            self.assertTrue(os.path.exists(config_path))
            
            # Verify content
            with open(config_path, "r") as f:
                config = json.load(f)
            
            self.assertEqual(config["name"], self.manager.default_configs[model_type]["name"])
            self.assertEqual(config["description"], self.manager.default_configs[model_type]["description"])
            self.assertEqual(config["parameters"], self.manager.default_configs[model_type]["parameters"])
    
    def test_get_available_configs(self):
        """Test get_available_configs method."""
        # Create test configs
        test_configs = {
            "test1": {
                "name": "Test Config 1",
                "description": "Test description 1",
                "parameters": {"temperature": 0.7}
            },
            "test2": {
                "name": "Test Config 2",
                "description": "Test description 2",
                "parameters": {"temperature": 0.8}
            }
        }
        
        for config_id, config in test_configs.items():
            config_path = os.path.join(self.config_dir, f"{config_id}.json")
            with open(config_path, "w") as f:
                json.dump(config, f)
        
        # Get configs
        configs = self.manager.get_available_configs()
        
        # Verify results
        self.assertEqual(len(configs), 2)
        
        # Check that IDs are added
        config_ids = [config["id"] for config in configs]
        self.assertIn("test1", config_ids)
        self.assertIn("test2", config_ids)
        
        # Check content
        for config in configs:
            test_config = test_configs[config["id"]]
            self.assertEqual(config["name"], test_config["name"])
            self.assertEqual(config["description"], test_config["description"])
            self.assertEqual(config["parameters"], test_config["parameters"])
    
    def test_get_config(self):
        """Test get_config method."""
        # Create test config
        config = {
            "name": "Test Config",
            "description": "Test description",
            "parameters": {"temperature": 0.7}
        }
        
        config_path = os.path.join(self.config_dir, "test.json")
        with open(config_path, "w") as f:
            json.dump(config, f)
        
        # Get config
        result = self.manager.get_config("test")
        
        # Verify result
        self.assertIsNotNone(result)
        self.assertEqual(result["id"], "test")
        self.assertEqual(result["name"], config["name"])
        self.assertEqual(result["description"], config["description"])
        self.assertEqual(result["parameters"], config["parameters"])
        
        # Test non-existent config
        result = self.manager.get_config("nonexistent")
        self.assertIsNone(result)
    
    def test_add_config(self):
        """Test add_config method."""
        # Add config
        name = "Test Config"
        description = "Test description"
        parameters = {"temperature": 0.7, "top_k": 50}
        
        config_id = self.manager.add_config(name, description, parameters)
        
        # Verify result
        self.assertIsNotNone(config_id)
        self.assertEqual(config_id, "test_config")
        
        # Verify file exists
        config_path = os.path.join(self.config_dir, f"{config_id}.json")
        self.assertTrue(os.path.exists(config_path))
        
        # Verify content
        with open(config_path, "r") as f:
            config = json.load(f)
        
        self.assertEqual(config["name"], name)
        self.assertEqual(config["description"], description)
        self.assertEqual(config["parameters"], parameters)
    
    def test_update_config(self):
        """Test update_config method."""
        # Create test config
        config = {
            "name": "Test Config",
            "description": "Test description",
            "parameters": {"temperature": 0.7}
        }
        
        config_path = os.path.join(self.config_dir, "test.json")
        with open(config_path, "w") as f:
            json.dump(config, f)
        
        # Update config
        new_name = "Updated Config"
        new_description = "Updated description"
        new_parameters = {"temperature": 0.8, "top_k": 60}
        
        success = self.manager.update_config("test", new_name, new_description, new_parameters)
        
        # Verify result
        self.assertTrue(success)
        
        # Verify content
        with open(config_path, "r") as f:
            updated_config = json.load(f)
        
        self.assertEqual(updated_config["name"], new_name)
        self.assertEqual(updated_config["description"], new_description)
        self.assertEqual(updated_config["parameters"], new_parameters)
        
        # Test updating non-existent config
        success = self.manager.update_config("nonexistent", "New Name", "New Description", {})
        self.assertFalse(success)
    
    def test_delete_config(self):
        """Test delete_config method."""
        # Create test config
        config = {
            "name": "Test Config",
            "description": "Test description",
            "parameters": {"temperature": 0.7}
        }
        
        config_path = os.path.join(self.config_dir, "test.json")
        with open(config_path, "w") as f:
            json.dump(config, f)
        
        # Delete config
        success = self.manager.delete_config("test")
        
        # Verify result
        self.assertTrue(success)
        self.assertFalse(os.path.exists(config_path))
        
        # Test deleting non-existent config
        success = self.manager.delete_config("nonexistent")
        self.assertFalse(success)
        
        # Test deleting default config
        for model_type in self.manager.default_configs:
            config_path = os.path.join(self.config_dir, f"{model_type}.json")
            with open(config_path, "w") as f:
                json.dump(self.manager.default_configs[model_type], f)
            
            success = self.manager.delete_config(model_type)
            self.assertFalse(success)
            self.assertTrue(os.path.exists(config_path))
    
    def test_apply_config_to_model_params(self):
        """Test apply_config_to_model_params method."""
        # Create test config
        config = {
            "name": "Test Config",
            "description": "Test description",
            "parameters": {
                "temperature": 0.7,
                "top_k": 50,
                "top_p": 0.9
            }
        }
        
        config_path = os.path.join(self.config_dir, "test.json")
        with open(config_path, "w") as f:
            json.dump(config, f)
        
        # Apply config
        model_params = {
            "temperature": 0.5,
            "max_length": 100
        }
        
        result = self.manager.apply_config_to_model_params(model_params, "test")
        
        # Verify result
        self.assertEqual(result["temperature"], 0.7)
        self.assertEqual(result["top_k"], 50)
        self.assertEqual(result["top_p"], 0.9)
        self.assertEqual(result["max_length"], 100)
        
        # Test with non-existent config
        result = self.manager.apply_config_to_model_params(model_params, "nonexistent")
        self.assertEqual(result, model_params)

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