11
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
+ import os
15
+
16
+ import pytest
14
17
15
18
import cirq
16
19
import cirq_google as cg
@@ -47,6 +50,46 @@ def test_egr_filesystem_record_repr():
47
50
cg_assert_equivalent_repr (egr_fs_record )
48
51
49
52
53
+ def test_egr_filesystem_record_from_json (tmpdir ):
54
+ run_id = 'my-run-id'
55
+ egr_fs_record = cg .ExecutableGroupResultFilesystemRecord (
56
+ runtime_configuration_path = 'RuntimeConfiguration.json.gz' ,
57
+ shared_runtime_info_path = 'SharedRuntimeInfo.jzon.gz' ,
58
+ executable_result_paths = [
59
+ 'ExecutableResult.1.json.gz' ,
60
+ 'ExecutableResult.2.json.gz' ,
61
+ ],
62
+ run_id = run_id ,
63
+ )
64
+
65
+ # Test 1: normal
66
+ os .makedirs (f'{ tmpdir } /{ run_id } ' )
67
+ cirq .to_json_gzip (
68
+ egr_fs_record , f'{ tmpdir } /{ run_id } /ExecutableGroupResultFilesystemRecord.json.gz'
69
+ )
70
+ egr_fs_record2 = cg .ExecutableGroupResultFilesystemRecord .from_json (
71
+ run_id = run_id , base_data_dir = tmpdir
72
+ )
73
+ assert egr_fs_record == egr_fs_record2
74
+
75
+ # Test 2: bad object type
76
+ cirq .to_json_gzip (
77
+ cirq .Circuit (), f'{ tmpdir } /{ run_id } /ExecutableGroupResultFilesystemRecord.json.gz'
78
+ )
79
+ with pytest .raises (ValueError , match = r'.*not an `ExecutableGroupFilesystemRecord`.' ):
80
+ cg .ExecutableGroupResultFilesystemRecord .from_json (run_id = run_id , base_data_dir = tmpdir )
81
+
82
+ # Test 3: Mismatched run id
83
+ os .makedirs (f'{ tmpdir } /questionable_run_id' )
84
+ cirq .to_json_gzip (
85
+ egr_fs_record , f'{ tmpdir } /questionable_run_id/ExecutableGroupResultFilesystemRecord.json.gz'
86
+ )
87
+ with pytest .raises (ValueError , match = r'.*does not match the provided run_id' ):
88
+ cg .ExecutableGroupResultFilesystemRecord .from_json (
89
+ run_id = 'questionable_run_id' , base_data_dir = tmpdir
90
+ )
91
+
92
+
50
93
def test_filesystem_saver (tmpdir , patch_cirq_default_resolvers ):
51
94
assert patch_cirq_default_resolvers
52
95
run_id = 'asdf'
@@ -56,11 +99,13 @@ def test_filesystem_saver(tmpdir, patch_cirq_default_resolvers):
56
99
shared_rt_info = cg .SharedRuntimeInfo (run_id = run_id )
57
100
fs_saver .initialize (rt_config , shared_rt_info = shared_rt_info )
58
101
102
+ # Test 1: assert fs_saver.initialize() has worked.
59
103
rt_config2 = cirq .read_json_gzip (f'{ tmpdir } /{ run_id } /QuantumRuntimeConfiguration.json.gz' )
60
104
shared_rt_info2 = cirq .read_json_gzip (f'{ tmpdir } /{ run_id } /SharedRuntimeInfo.json.gz' )
61
105
assert rt_config == rt_config2
62
106
assert shared_rt_info == shared_rt_info2
63
107
108
+ # Test 2: assert `consume_result()` works.
64
109
# you shouldn't actually mutate run_id in the shared runtime info, but we want to test
65
110
# updating the shared rt info object:
66
111
shared_rt_info .run_id = 'updated_run_id'
@@ -76,6 +121,7 @@ def test_filesystem_saver(tmpdir, patch_cirq_default_resolvers):
76
121
assert shared_rt_info == shared_rt_info3
77
122
assert exe_result == exe_result3
78
123
124
+ # Test 3: assert loading egr_record works.
79
125
egr_record : cg .ExecutableGroupResultFilesystemRecord = cirq .read_json_gzip (
80
126
f'{ fs_saver .data_dir } /ExecutableGroupResultFilesystemRecord.json.gz'
81
127
)
0 commit comments