#Mocking calls to deepgram in python unit tests

1 messages · Page 1 of 1 (latest)

steep path
#

Apologies in advance for what may be a newbie python dev question

I have a function that creates a deepgram client, calls the transcribe_url function, and returns the json representation of the response (to_json). I am trying to use unittest mocks to patch the deepgram client and the transcribe_url function, but I'm having a hard time figuring out how to do this when the transcribe_url function is accessed in a chain of functions like deepgram.listen.prerecorded.v("1").transcribe_url.

Mocking the DeepgramClient is straightforward, but I'm not able to patch the transcribe_url.to_json response so I can assert it works. Is there a better way of setting up transcribe_url so its patchable, or am I missing some unittest mock syntax that would let me patch a deep call?

tender oarBOT
#

Thanks for asking your question. Please be sure to reply with as much detail as possible so we can assist you efficiently. Such as:

  • Provide the request_id if you've a question about a transcription response.
  • The options you used or the api.deepgram.com URL you sent your request to, including parameters.
  • Any code snippets you can include.
  • Any audio you can include, or if you can't share it here please email it to us at [email protected] and provide a link to this thread.
barren tundra
#

Instantiate a PreRecordedClient object

steep path
#

you mean in my function under test, change it around so it directly uses what the chain resolves to?

barren tundra
#

If you are just unit testing, yes. If you want to actually use the class, you can do either (off the tree or create an object of that class directly). If you do the latter, you need to understand the project in a little more detail.

#

You can just try an example in the examples folder if you are just trying to make use the transcribe_url function works though

#

It's a little odd to unit test a consuming libraries function because the project should do that

#

If you want to write a unit test for the project, go for it. Create a PR and we can review for acceptance

steep path
#

well i'm trying to unit test my project's function which basically does what the example shows, and some other stuff. i'm trying to test the other stuff

barren tundra
#

Then create a unit test for your function

steep path
#

perhaps i should put deepgram stuff into its own function that i mock and then unit test my function

#

right

barren tundra
#

No need to create a unit test for transcribe_url because if it doesn't work, then we guarantee that it will work

#

Yup

#

That's the normal way