def test_results_sql(storage_socket, session, molecules_H4O2, kw_fixtures): """ Handling results throught the ME classes """ assert session.query(ResultORM).count() == 0 assert len(molecules_H4O2) == 2 assert len(kw_fixtures) == 1 page1 = { "procedure": "single", "molecule": molecules_H4O2[0], "method": "m1", "basis": "b1", "keywords": None, "program": "p1", "driver": "energy", "status": "COMPLETE", "protocols": {}, } page2 = { "procedure": "single", "molecule": molecules_H4O2[1], "method": "m2", "basis": "b1", "keywords": kw_fixtures[0], "program": "p1", "driver": "energy", "status": "COMPLETE", "protocols": {}, } result = ResultORM(**page1) session.add(result) session.commit() # IMPORTANT: To be able to access lazy loading children use joinedload ret = session.query(ResultORM).options( joinedload("molecule_obj")).filter_by(method="m1").first() assert ret.molecule_obj.molecular_formula == "H4O2" # Accessing the keywords_obj will issue a DB access assert ret.keywords_obj == None result2 = ResultORM(**page2) session.add(result2) session.commit() ret = session.query(ResultORM).options( joinedload("molecule_obj")).filter_by(method="m2").first() assert ret.molecule_obj.molecular_formula == "H4O2" assert ret.method == "m2" # clean up session_delete_all(session, ResultORM)
def test_optimization_procedure(storage_socket, session, molecules_H4O2): """ Optimization procedure """ assert session.query(OptimizationProcedureORM).count() == 0 # assert Keywords.objects().count() == 0 data1 = { "procedure": "optimization", "initial_molecule": molecules_H4O2[0], "keywords": None, "program": "p7", "qc_spec": { "basis": "b1", "program": "p1", "method": "m1", "driver": "energy" }, "status": "COMPLETE", "protocols": {}, } result1 = { "procedure": "single", "molecule": molecules_H4O2[0], "method": "m1", "basis": "b1", "keywords": None, "program": "p1", "driver": "energy", "status": "COMPLETE", "protocols": {}, } procedure = OptimizationProcedureORM(**data1) session.add(procedure) session.commit() proc = session.query(OptimizationProcedureORM).options( joinedload("initial_molecule_obj")).first() assert proc.initial_molecule_obj.molecular_formula == "H4O2" assert proc.procedure == "optimization" # add a trajectory result result = ResultORM(**result1) session.add(result) session.commit() assert result.id # link result to the trajectory proc.trajectory_obj = [Trajectory(opt_id=proc.id, result_id=result.id)] session.commit() proc = session.query(OptimizationProcedureORM).options( joinedload("trajectory_obj")).first() assert proc.trajectory_obj # clean up session_delete_all(session, ResultORM) session_delete_all(session, OptimizationProcedureORM)
def test_add_task_queue(storage_socket, session, molecules_H4O2): """ Simple test of adding a task using the SQL classes in QCFractal, tasks should be added using storage_socket """ assert session.query(TaskQueueORM).count() == 0 # TaskQueueORM.objects().delete() page1 = { "procedure": "single", "molecule": molecules_H4O2[0], "method": "m1", "basis": "b1", "keywords": None, "program": "p1", "driver": "energy", "protocols": {}, } # add a task that reference results result = ResultORM(**page1) session.add(result) session.commit() task = TaskQueueORM(base_result_obj=result, spec={"something": True}) session.add(task) session.commit() ret = session.query(TaskQueueORM) assert ret.count() == 1 task = ret.first() assert task.status == "WAITING" assert task.base_result_obj.status == "INCOMPLETE" # cleanup session_delete_all(session, TaskQueueORM) session_delete_all(session, ResultORM)
def test_results_pagination(storage_socket, session, molecules_H4O2, kw_fixtures): """ Test results pagination """ assert session.query(ResultORM).count() == 0 result_template = { "molecule": molecules_H4O2[0], "method": "m1", "basis": "b1", "keywords": kw_fixtures[0], "program": "p1", "driver": "energy", } # Save ~ 1 msec/doc in ME, 0.5 msec/doc in SQL # ------------------------------------------ t1 = time() total_results = 1000 first_half = int(total_results / 2) limit = 100 skip = 50 for i in range(first_half): result_template['basis'] = str(i) r = ResultORM(**result_template) session.add(r) result_template['method'] = 'm2' for i in range(first_half, total_results): result_template['basis'] = str(i) r = ResultORM(**result_template) session.add(r) session.commit() # must commit outside the loop, 10 times faster total_time = (time() - t1) * 1000 / total_results print('Inserted {} results in {:.2f} msec / doc'.format( total_results, total_time)) # query (~ 0.13 msec/doc) in ME, and ~0.02 msec/doc in SQL # ---------------------------------------- t1 = time() ret1 = session.query(ResultORM).filter_by(method='m1') ret2 = session.query(ResultORM).filter_by(method='m2').limit( limit) #.offset(skip) data1 = [d.to_dict() for d in ret1] data2 = [d.to_dict() for d in ret2] # count is total, but actual data size is the limit assert ret1.count() == first_half assert len(data1) == first_half # assert ret2.count() == total_results - first_half # assert len(ret2) == limit # assert len(data2) == limit # # assert int(data2[0]['basis']) == first_half + skip # # # get the last page when with fewer than limit are remaining # ret = session.query(ResultORM).filter_by(method='m1').limit(limit).offset(int(first_half - limit / 2)) # assert len(ret) == limit / 2 total_time = (time() - t1) * 1000 / total_results print('Query {} results in {:.3f} msec /doc'.format( total_results, total_time)) # cleanup session_delete_all(session, ResultORM)