1
chain.py
----------------------------------
from langchain.chains import GraphCypherQAChain
from langchain_openai import ChatOpenAI
from langchain_community.graphs import Neo4jGraph
from langchain.prompts.prompt import PromptTemplate

import os

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

graph = Neo4jGraph(
    url=os.getenv("NEO4J_URI"),
    username=os.getenv("NEO4J_USERNAME"),
    password=os.getenv("NEO4J_PASSWORD"),
    sanitize=True,
)

graph_chain = GraphCypherQAChain.from_llm(
    graph=graph,
    cypher_llm=ChatOpenAI(temperature=0, model="gpt-3.5-turbo"),
    qa_llm=ChatOpenAI(temperature=0, model="gpt-3.5-turbo-16k"),
    verbose=True,
    return_intermediate_steps=True,
    validate_cypher=True,
    top_k=100,
)

------------------------------------------------------
main.py
------------------------------------------------------
from chain import graph_chain

def generate(question: str) -> str:
  
    chain_result = graph_chain.invoke(
         {"query": question},
         return_only_outputs=False,
    )
    return chain_result['result']

I am trying to write unit test case for main.py.

test_main.py
----------------------------------------------
from main import generate

question = "mock_question"

class mock_GraphCypherQAChain:
    def invoke(cls, a=None, return_only_outputs=False):
        return {
            "result": "Some completion",
            "intermediate_steps": [{"query": question}],
        }

@mock.patch('main.graph_chain')
def test_generate_for_valid_chain_result(mock_graph_chain):
    mock_graph_chain.return_value = mock_GraphCypherQAChain()
    actual_output = "Some completion"
    
    test_output = generate(question)

    assert actual_output == test_output

But I am getting the test case failed as -

FAILED tests/test_main.py::test_generate_for_valid_chain_result - ValueError: Could not find result in chain_result

The mock mock_graph_chain.invoke() is returning a MagicMock object as below, instead of the dict given -

<MagicMock name='graph_chain.invoke()' id='139624876920784'>

How can I resolve this?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.