def testSaveLoad(self): with tempfile.TemporaryFile(suffix=".qgraph") as tmpFile: self.qGraph.save(tmpFile) tmpFile.seek(0) restore = QuantumGraph.load(tmpFile, self.universe) self.assertEqual(self.qGraph, restore) # Load in just one node tmpFile.seek(0) nodeId = [n.nodeId for n in self.qGraph][0] restoreSub = QuantumGraph.load(tmpFile, self.universe, nodes=(nodeId,)) self.assertEqual(len(restoreSub), 1) self.assertEqual(list(restoreSub)[0], restore.getQuantumNodeByNodeId(nodeId))
def testSaveLoad(self): with tempfile.TemporaryFile(suffix='.qgraph') as tmpFile: self.qGraph.save(tmpFile) tmpFile.seek(0) restore = QuantumGraph.load(tmpFile, self.universe) self._cleanGraphs(self.qGraph, restore) self.assertEqual(self.qGraph, restore) # Load in just one node tmpFile.seek(0) restoreSub = QuantumGraph.load(tmpFile, self.universe, nodes=(0, )) self.assertEqual(len(restoreSub), 1) self.assertEqual( list(restoreSub)[0], restore.getQuantumNodeByNodeId(NodeId(0, restore._buildId)))
def test_datastore_records(self): """Test for generating datastore records.""" with temporaryDirectory() as root: # need FileDatastore for this tests butler, qgraph1 = simpleQGraph.makeSimpleQGraph( root=root, inMemory=False, makeDatastoreRecords=True) # save and reload buffer = io.BytesIO() qgraph1.save(buffer) buffer.seek(0) qgraph2 = QuantumGraph.load(buffer, universe=butler.dimensions) del buffer for qgraph in (qgraph1, qgraph2): self.assertEqual(len(qgraph), 5) for i, qnode in enumerate(qgraph): quantum = qnode.quantum self.assertIsNotNone(quantum.datastore_records) # only the first quantum has a pre-existing input if i == 0: datastore_name = "FileDatastore@<butlerRoot>" self.assertEqual(set(quantum.datastore_records.keys()), {datastore_name}) records_data = quantum.datastore_records[ datastore_name] records = dict(records_data.records) self.assertEqual(len(records), 1) _, records = records.popitem() records = records["file_datastore_records"] self.assertEqual( [record.path for record in records], [ "test/add_dataset0/add_dataset0_INSTR_det0_test.pickle" ], ) else: self.assertEqual(quantum.datastore_records, {})