File size: 9,897 Bytes
d8d14f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from datetime import datetime

import pytest

from swarms.structs.base_structure import BaseStructure


class TestBaseStructure:
    def test_init(self):
        base_structure = BaseStructure(
            name="TestStructure",
            description="Test description",
            save_metadata=True,
            save_artifact_path="./test_artifacts",
            save_metadata_path="./test_metadata",
            save_error_path="./test_errors",
        )

        assert base_structure.name == "TestStructure"
        assert base_structure.description == "Test description"
        assert base_structure.save_metadata is True
        assert base_structure.save_artifact_path == "./test_artifacts"
        assert base_structure.save_metadata_path == "./test_metadata"
        assert base_structure.save_error_path == "./test_errors"

    def test_save_to_file_and_load_from_file(self, tmpdir):
        tmp_dir = tmpdir.mkdir("test_dir")
        file_path = os.path.join(tmp_dir, "test_file.json")

        data_to_save = {"key": "value"}
        base_structure = BaseStructure()

        base_structure.save_to_file(data_to_save, file_path)
        loaded_data = base_structure.load_from_file(file_path)

        assert loaded_data == data_to_save

    def test_save_metadata_and_load_metadata(self, tmpdir):
        tmp_dir = tmpdir.mkdir("test_dir")
        base_structure = BaseStructure(save_metadata_path=tmp_dir)

        metadata = {"name": "Test", "description": "Test metadata"}
        base_structure.save_metadata(metadata)
        loaded_metadata = base_structure.load_metadata()

        assert loaded_metadata == metadata

    def test_log_error(self, tmpdir):
        tmp_dir = tmpdir.mkdir("test_dir")
        base_structure = BaseStructure(save_error_path=tmp_dir)

        error_message = "Test error message"
        base_structure.log_error(error_message)

        log_file = os.path.join(tmp_dir, "TestStructure_errors.log")
        with open(log_file) as file:
            lines = file.readlines()
            assert len(lines) == 1
            assert lines[0] == f"{error_message}\n"

    def test_save_artifact_and_load_artifact(self, tmpdir):
        tmp_dir = tmpdir.mkdir("test_dir")
        base_structure = BaseStructure(save_artifact_path=tmp_dir)

        artifact = {"key": "value"}
        artifact_name = "test_artifact"
        base_structure.save_artifact(artifact, artifact_name)
        loaded_artifact = base_structure.load_artifact(artifact_name)

        assert loaded_artifact == artifact

    def test_current_timestamp(self):
        base_structure = BaseStructure()
        current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        timestamp = base_structure._current_timestamp()
        assert timestamp == current_time

    def test_log_event(self, tmpdir):
        tmp_dir = tmpdir.mkdir("test_dir")
        base_structure = BaseStructure(save_metadata_path=tmp_dir)

        event = "Test event"
        event_type = "INFO"
        base_structure.log_event(event, event_type)

        log_file = os.path.join(tmp_dir, "TestStructure_events.log")
        with open(log_file) as file:
            lines = file.readlines()
            assert len(lines) == 1
            assert (
                lines[0] == f"[{base_structure._current_timestamp()}]"
                f" [{event_type}] {event}\n"
            )

    @pytest.mark.asyncio
    async def test_run_async(self):
        base_structure = BaseStructure()

        async def async_function():
            return "Async Test Result"

        result = await base_structure.run_async(async_function)
        assert result == "Async Test Result"

    @pytest.mark.asyncio
    async def test_save_metadata_async(self, tmpdir):
        tmp_dir = tmpdir.mkdir("test_dir")
        base_structure = BaseStructure(save_metadata_path=tmp_dir)

        metadata = {"name": "Test", "description": "Test metadata"}
        await base_structure.save_metadata_async(metadata)
        loaded_metadata = base_structure.load_metadata()

        assert loaded_metadata == metadata

    @pytest.mark.asyncio
    async def test_log_error_async(self, tmpdir):
        tmp_dir = tmpdir.mkdir("test_dir")
        base_structure = BaseStructure(save_error_path=tmp_dir)

        error_message = "Test error message"
        await base_structure.log_error_async(error_message)

        log_file = os.path.join(tmp_dir, "TestStructure_errors.log")
        with open(log_file) as file:
            lines = file.readlines()
            assert len(lines) == 1
            assert lines[0] == f"{error_message}\n"

    @pytest.mark.asyncio
    async def test_save_artifact_async(self, tmpdir):
        tmp_dir = tmpdir.mkdir("test_dir")
        base_structure = BaseStructure(save_artifact_path=tmp_dir)

        artifact = {"key": "value"}
        artifact_name = "test_artifact"
        await base_structure.save_artifact_async(
            artifact, artifact_name
        )
        loaded_artifact = base_structure.load_artifact(artifact_name)

        assert loaded_artifact == artifact

    @pytest.mark.asyncio
    async def test_load_artifact_async(self, tmpdir):
        tmp_dir = tmpdir.mkdir("test_dir")
        base_structure = BaseStructure(save_artifact_path=tmp_dir)

        artifact = {"key": "value"}
        artifact_name = "test_artifact"
        base_structure.save_artifact(artifact, artifact_name)
        loaded_artifact = await base_structure.load_artifact_async(
            artifact_name
        )

        assert loaded_artifact == artifact

    @pytest.mark.asyncio
    async def test_log_event_async(self, tmpdir):
        tmp_dir = tmpdir.mkdir("test_dir")
        base_structure = BaseStructure(save_metadata_path=tmp_dir)

        event = "Test event"
        event_type = "INFO"
        await base_structure.log_event_async(event, event_type)

        log_file = os.path.join(tmp_dir, "TestStructure_events.log")
        with open(log_file) as file:
            lines = file.readlines()
            assert len(lines) == 1
            assert (
                lines[0] == f"[{base_structure._current_timestamp()}]"
                f" [{event_type}] {event}\n"
            )

    @pytest.mark.asyncio
    async def test_asave_to_file(self, tmpdir):
        tmp_dir = tmpdir.mkdir("test_dir")
        file_path = os.path.join(tmp_dir, "test_file.json")
        data_to_save = {"key": "value"}
        base_structure = BaseStructure()

        await base_structure.asave_to_file(data_to_save, file_path)
        loaded_data = base_structure.load_from_file(file_path)

        assert loaded_data == data_to_save

    @pytest.mark.asyncio
    async def test_aload_from_file(self, tmpdir):
        tmp_dir = tmpdir.mkdir("test_dir")
        file_path = os.path.join(tmp_dir, "test_file.json")
        data_to_save = {"key": "value"}
        base_structure = BaseStructure()
        base_structure.save_to_file(data_to_save, file_path)

        loaded_data = await base_structure.aload_from_file(file_path)
        assert loaded_data == data_to_save

    def test_run_in_thread(self):
        base_structure = BaseStructure()
        result = base_structure.run_in_thread(
            lambda: "Thread Test Result"
        )
        assert result.result() == "Thread Test Result"

    def test_save_and_decompress_data(self):
        base_structure = BaseStructure()
        data = {"key": "value"}
        compressed_data = base_structure.compress_data(data)
        decompressed_data = base_structure.decompres_data(
            compressed_data
        )
        assert decompressed_data == data

    def test_run_batched(self):
        base_structure = BaseStructure()

        def run_function(data):
            return f"Processed {data}"

        batched_data = list(range(10))
        result = base_structure.run_batched(
            batched_data, batch_size=5, func=run_function
        )

        expected_result = [
            f"Processed {data}" for data in batched_data
        ]
        assert result == expected_result

    def test_load_config(self, tmpdir):
        tmp_dir = tmpdir.mkdir("test_dir")
        config_file = os.path.join(tmp_dir, "config.json")
        config_data = {"key": "value"}
        base_structure = BaseStructure()

        base_structure.save_to_file(config_data, config_file)
        loaded_config = base_structure.load_config(config_file)

        assert loaded_config == config_data

    def test_backup_data(self, tmpdir):
        tmp_dir = tmpdir.mkdir("test_dir")
        base_structure = BaseStructure()
        data_to_backup = {"key": "value"}
        base_structure.backup_data(
            data_to_backup, backup_path=tmp_dir
        )
        backup_files = os.listdir(tmp_dir)

        assert len(backup_files) == 1
        loaded_data = base_structure.load_from_file(
            os.path.join(tmp_dir, backup_files[0])
        )
        assert loaded_data == data_to_backup

    def test_monitor_resources(self):
        base_structure = BaseStructure()
        base_structure.monitor_resources()

    def test_run_with_resources(self):
        base_structure = BaseStructure()

        def run_function():
            base_structure.monitor_resources()
            return "Resource Test Result"

        result = base_structure.run_with_resources(run_function)
        assert result == "Resource Test Result"

    def test_run_with_resources_batched(self):
        base_structure = BaseStructure()

        def run_function(data):
            base_structure.monitor_resources()
            return f"Processed {data}"

        batched_data = list(range(10))
        result = base_structure.run_with_resources_batched(
            batched_data, batch_size=5, func=run_function
        )

        expected_result = [
            f"Processed {data}" for data in batched_data
        ]
        assert result == expected_result