-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtest_fixtures.py
More file actions
197 lines (159 loc) · 5.88 KB
/
test_fixtures.py
File metadata and controls
197 lines (159 loc) · 5.88 KB
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
"""Test fixtures and mock implementations for robodm testing."""
import os
import shutil
import tempfile
import time
from typing import Any, Dict, List, Optional, Union
from unittest.mock import MagicMock, Mock
import numpy as np
import pytest
from robodm import Trajectory
from robodm.trajectory_base import FileSystemInterface, TimeProvider
class MockFileSystem(FileSystemInterface):
"""Mock file system for testing."""
def __init__(self):
self.files = {}
self.directories = set()
def exists(self, path: str) -> bool:
return path in self.files or path in self.directories
def makedirs(self, path: str, exist_ok: bool = False) -> None:
if not exist_ok and path in self.directories:
raise FileExistsError(f"Directory {path} already exists")
self.directories.add(path)
def remove(self, path: str) -> None:
if path in self.files:
del self.files[path]
else:
raise FileNotFoundError(f"File {path} not found")
def rename(self, src: str, dst: str) -> None:
if src not in self.files:
raise FileNotFoundError(f"File {src} not found")
self.files[dst] = self.files[src]
del self.files[src]
def add_file(self, path: str, content: Any = None):
"""Add a file to the mock filesystem."""
self.files[path] = content
class MockTimeProvider(TimeProvider):
"""Mock time provider for deterministic testing."""
def __init__(self, initial_time: float = 0.0):
self._current_time = initial_time
self._time_calls = []
def time(self) -> float:
self._time_calls.append(self._current_time)
return self._current_time
def advance_time(self, seconds: float):
"""Advance the mock time."""
self._current_time += seconds
def set_time(self, time: float):
"""Set the current time."""
self._current_time = time
@property
def call_count(self) -> int:
return len(self._time_calls)
@pytest.fixture
def mock_filesystem():
"""Pytest fixture for mock filesystem."""
return MockFileSystem()
@pytest.fixture
def mock_time_provider():
"""Pytest fixture for mock time provider."""
return MockTimeProvider()
@pytest.fixture
def temp_dir():
"""Pytest fixture for temporary directory."""
temp_path = tempfile.mkdtemp()
yield temp_path
shutil.rmtree(temp_path, ignore_errors=True)
@pytest.fixture
def sample_trajectory_data():
"""Sample trajectory data for testing."""
return [
{
"observation": {
"image": np.random.randint(0,
255, (480, 640, 3),
dtype=np.uint8),
"joint_positions": np.random.random(7).astype(np.float32),
},
"action": np.random.random(7).astype(np.float32),
"reward": np.float32(1.0),
},
{
"observation": {
"image": np.random.randint(0,
255, (480, 640, 3),
dtype=np.uint8),
"joint_positions": np.random.random(7).astype(np.float32),
},
"action": np.random.random(7).astype(np.float32),
"reward": np.float32(0.5),
},
]
@pytest.fixture
def sample_dict_of_lists():
"""Sample dictionary of lists for testing."""
return {
"observation": {
"image": [
np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8),
np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8),
],
"joint_positions": [
np.random.random(7).astype(np.float32),
np.random.random(7).astype(np.float32),
],
},
"action": [
np.random.random(7).astype(np.float32),
np.random.random(7).astype(np.float32),
],
"reward": [np.float32(1.0), np.float32(0.5)],
}
@pytest.fixture
def large_sample_data():
"""Large sample data for benchmarking."""
num_samples = 100
return {
"observation/image": [
np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8)
for _ in range(num_samples)
],
"observation/joint_positions":
[np.random.random(7).astype(np.float32) for _ in range(num_samples)],
"action":
[np.random.random(7).astype(np.float32) for _ in range(num_samples)],
"reward": [np.float32(np.random.random()) for _ in range(num_samples)],
}
class BenchmarkDataset:
"""Helper class for creating benchmark datasets."""
@staticmethod
def create_vla_dataset(path: str,
data: Dict[str, List[Any]],
video_codec: str = "auto"):
"""Create a VLA dataset file for testing."""
traj = Trajectory.from_dict_of_lists(data,
path,
video_codec=video_codec)
return traj
@staticmethod
def create_hdf5_dataset(path: str, data: Dict[str, List[Any]]):
"""Create an HDF5 dataset file."""
import h5py
with h5py.File(path, "w") as f:
for key, values in data.items():
if isinstance(values[0], np.ndarray):
stacked_data = np.stack(values)
else:
stacked_data = np.array(values)
f.create_dataset(key,
data=stacked_data,
compression="gzip",
compression_opts=9)
@staticmethod
def get_file_size(path: str) -> int:
"""Get file size in bytes."""
return os.path.getsize(path)
@pytest.fixture
def benchmark_dataset():
"""Pytest fixture for benchmark dataset helper."""
return BenchmarkDataset()