I am trying to write a test for class that makes a post request to an external API. I'm using the responses library to mock the external service after recording a real response (so this test is designed to run with either a real connection to the external service or without).
I'm having trouble making sure the mocked response is correct. The issue is that the method I'm testing doesn't return the actual response object--just a specific parameter of the json response. I'm wondering how to access the response object that's in a lower level of the call stack(? <<this might be the wrong terminology).
This is an excerpt of the test:
ls = Client(url=LABEL_STUDIO_URL, api_key=API_KEY)
ls.check_connection()
ls.delete_all_projects()
# Create a sample project to classify types of texts
project = ls.start_project(
title="Text Type Classifications",
label_config="""
<View>
<Text name="text" value="$text"/>
<View style="box-shadow: 2px 2px 5px #999;
padding: 20px; margin-top: 2em;
border-radius: 5px;">
<Header value="Choose text type"/>
<Choices name="type" toName="text"
choice="single" showInLine="true">
<Choice value="Title"/>
<Choice value="Narrative"/>
</Choices>
</View>
</View>
""",
)
label_studio_data = label_studio.stage_for_label_studio(elements)
imported_ids = project.import_tasks(label_studio_data)
The issue is that project.import_tasks returns response.json()['task_ids'], so I'm wondering how, from my test where I init a project, I can test more than just the task-ids from the response.
def import_tasks(self, tasks, preannotated_from_fields: List = None):
"""Import JSON-formatted labeling tasks. Tasks can be unlabeled or contain predictions.
Parameters
----------
tasks: list of dicts | dict | path to file
Tasks in <a href="https://labelstud.io/guide/tasks.html#Basic-Label-Studio-JSON-format">
Label Studio JSON format</a>
preannotated_from_fields: list of strings
Turns flat task JSON formatted like: `{"column1": value, "column2": value}` into Label Studio prediction
data format: `{"data": {"column1"..}, "predictions": [{..."column2"}]`
Useful when all your data is stored in tabular format with one column dedicated to model predictions.
Returns
-------
list of int
Imported task IDs
"""
params = {'return_task_ids': '1'}
if preannotated_from_fields:
params['preannotated_from_fields'] = ','.join(preannotated_from_fields)
if isinstance(tasks, (list, dict)):
response = self.make_request(
method='POST',
url=f'/api/projects/{self.id}/import',
json=tasks,
params=params,
)
elif isinstance(tasks, (str, Path)):
# try import from file
if not os.path.isfile(tasks):
raise LabelStudioException(f'Not found import tasks file {tasks}')
with open(tasks, mode='rb') as f:
response = self.make_request(
method='POST',
url=f'/api/projects/{self.id}/import',
files={'file': f},
params=params,
)
else:
raise TypeError(
f'Not supported type provided as "tasks" argument: {type(tasks)}'
)
return response.json()['task_ids']