def testMakeGraphFromPickle(self): """Tests for CmdLineFwk.makeGraph method. Only most trivial case is tested that does not do actual graph building. """ fwk = CmdLineFwk() taskFactory = TaskFactoryMock() with makeTmpFile() as tmpname: # make empty graph and store it in a file qgraph = QuantumGraph() with open(tmpname, "wb") as pickleFile: pickle.dump(qgraph, pickleFile) args = _makeArgs(qgraph=tmpname) with self.assertWarnsRegex(UserWarning, "QuantumGraph is empty"): # this also tests that warning is generated for empty graph qgraph = fwk.makeGraph(None, taskFactory, args) self.assertIsInstance(qgraph, QuantumGraph) self.assertEqual(len(qgraph), 0) # pickle with wrong object type with open(tmpname, "wb") as pickleFile: pickle.dump({}, pickleFile) args = _makeArgs(qgraph=tmpname) with self.assertRaises(TypeError): fwk.makeGraph(None, taskFactory, args)
def testSubgraph(self): """Test successfull execution of trivial quantum graph. """ nQuanta = 5 nNodes = 2 butler, qgraph = makeSimpleQGraph(nQuanta, root=self.root) # Select first two nodes for execution. This depends on node ordering # which I assume is the same as execution order. nodeIds = [node.nodeId.number for node in qgraph] nodeIds = nodeIds[:nNodes] self.assertEqual(len(qgraph.taskGraph), 5) self.assertEqual(len(qgraph), nQuanta) with makeTmpFile(suffix=".qgraph") as tmpname, makeSQLiteRegistry( ) as registryConfig: with open(tmpname, "wb") as saveFile: qgraph.save(saveFile) args = _makeArgs(qgraph=tmpname, qgraph_node_id=nodeIds, registryConfig=registryConfig) fwk = CmdLineFwk() # load graph, should only read a subset qgraph = fwk.makeGraph(pipeline=None, args=args) self.assertEqual(len(qgraph), nNodes)
def testMakeGraphFromSave(self): """Tests for CmdLineFwk.makeGraph method. Only most trivial case is tested that does not do actual graph building. """ fwk = CmdLineFwk() with makeTmpFile(suffix=".qgraph") as tmpname, makeSQLiteRegistry( ) as registryConfig: # make non-empty graph and store it in a file qgraph = _makeQGraph() with open(tmpname, "wb") as saveFile: qgraph.save(saveFile) args = _makeArgs(qgraph=tmpname, registryConfig=registryConfig) qgraph = fwk.makeGraph(None, args) self.assertIsInstance(qgraph, QuantumGraph) self.assertEqual(len(qgraph), 1) # will fail if graph id does not match args = _makeArgs(qgraph=tmpname, qgraph_id="R2-D2 is that you?", registryConfig=registryConfig) with self.assertRaisesRegex(ValueError, "graphID does not match"): fwk.makeGraph(None, args) # save with wrong object type with open(tmpname, "wb") as saveFile: pickle.dump({}, saveFile) args = _makeArgs(qgraph=tmpname, registryConfig=registryConfig) with self.assertRaises(ValueError): fwk.makeGraph(None, args) # reading empty graph from pickle should work but makeGraph() # will return None and make a warning qgraph = QuantumGraph(dict()) with open(tmpname, "wb") as saveFile: qgraph.save(saveFile) args = _makeArgs(qgraph=tmpname, registryConfig=registryConfig) with self.assertWarnsRegex(UserWarning, "QuantumGraph is empty"): # this also tests that warning is generated for empty graph qgraph = fwk.makeGraph(None, args) self.assertIs(qgraph, None)