1+ import collections
2+ import functools
13import os
24import tempfile
35from unittest .mock import MagicMock , patch
46import pytest
57
6-
78import openpathsampling as paths
89from openpathsampling .tests .test_helpers import make_1d_traj
910
@@ -64,7 +65,7 @@ def __call__(self, snap):
6465 return snap .xyz [0 ][0 ]
6566
6667 self .cv = paths .FunctionCV ("test" , RunOnceFunction ())
67- traj = paths . tests . test_helpers . make_1d_traj ([2 , 1 ])
68+ traj = make_1d_traj ([2 , 1 ])
6869 self .snap = traj [0 ]
6970 self .other_snap = traj [1 ]
7071
@@ -75,9 +76,54 @@ def test_precompute_cvs(self):
7576 assert recalced == 2
7677 assert self .cv .diskcache_enabled is True
7778
78- def test_precompute_cvs_and_inputs (self ):
79- pytest .skip ()
79+ @pytest .mark .parametrize ('cvs' , [['test' ], None ])
80+ def test_precompute_cvs_and_inputs (self , cvs ):
81+ with tempfile .TemporaryDirectory () as tmpdir :
82+ storage = paths .Storage (os .path .join (tmpdir , "test.nc" ),
83+ mode = 'w' )
84+ traj = make_1d_traj (list (range (10 )))
85+ cv = paths .FunctionCV ("test" , lambda s : s .xyz [0 ][0 ])
86+ storage .save (traj )
87+ storage .save (cv )
88+
89+ if cvs is not None :
90+ cvs = [storage .cvs [cv ] for cv in cvs ]
91+
92+ precompute_func , blocks = precompute_cvs_func_and_inputs (
93+ input_storage = storage ,
94+ cvs = cvs ,
95+ blocksize = 2
96+ )
97+ assert len (blocks ) == 5
98+ for block in blocks :
99+ assert len (block ) == 2
100+
101+ # smoke test: only effect should be caching results
102+ precompute_func (blocks [0 ])
80103
81104
82105def test_rewrite_file ():
83- pytest .skip ()
106+ # making a mock for storage instead of actually testing integration
107+ class FakeStore (object ):
108+ def __init__ (self ):
109+ self ._stores = collections .defaultdict (list )
110+
111+ def store (self , obj , store_name ):
112+ self ._stores [store_name ].append (obj )
113+
114+ stage_names = ['foo' , 'bar' ]
115+ storage = FakeStore ()
116+ store_funcs = {
117+ name : functools .partial (storage .store , store_name = name )
118+ for name in stage_names
119+ }
120+ stage_mapping = {
121+ 'foo' : (store_funcs ['foo' ], [0 , 1 , 2 ]),
122+ 'bar' : (store_funcs ['bar' ], [[3 ], [4 ], [5 ]])
123+ }
124+ silent_tqdm = lambda x , desc = None , leave = True : x
125+ with patch ('paths_cli.file_copying.tqdm' , silent_tqdm ):
126+ rewrite_file (stage_names , stage_mapping )
127+
128+ assert storage ._stores ['foo' ] == [0 , 1 , 2 ]
129+ assert storage ._stores ['bar' ] == [[3 ], [4 ], [5 ]]
0 commit comments