diff --git a/datastore/Nachet/__init__.py b/datastore/Nachet/__init__.py index 0c342cf8..b459bf28 100644 --- a/datastore/Nachet/__init__.py +++ b/datastore/Nachet/__init__.py @@ -631,6 +631,46 @@ async def get_seed_info(cursor): seed_dict["seeds"].append({"seed_id": seed_id, "seed_name": seed_name}) return seed_dict +async def get_pictures_inferences(cursor, user_id: str, picture_set_id: str): + """ + Retrieves inferences (if exist) of each picture in the given picture_set + + Args: + cursor: The cursor object to interact with the database. + user_id (str): id of the user + picture_set_id (str): id of the picture set + """ + try: + # Check if user exists + if not user.is_a_user_id(cursor=cursor, user_id=user_id): + raise user.UserNotFoundError( + f"User not found based on the given id: {user_id}" + ) + # Check if picture set exists + if not picture.is_a_picture_set_id(cursor, picture_set_id): + raise picture.PictureSetNotFoundError( + f"Picture set not found based on the given id: {picture_set_id}" + ) + # Check user is owner of the picture set + if picture.get_picture_set_owner_id(cursor, picture_set_id) != user_id: + raise UserNotOwnerError( + f"User can't delete this folder, user uuid :{user_id}, folder name : {picture_set_id}" + ) + result=[] + pictures = picture.get_picture_set_pictures(cursor, picture_set_id) + for pic in pictures: + picture_id = pic[0] + if picture.check_picture_inference_exist(cursor, picture_id): + inf = picture.get_picture_inference(cursor, picture_id) + result.append(inf) + print(result) + return result + except ( + user.UserNotFoundError, + picture.PictureSetNotFoundError, + UserNotOwnerError, + ) as e: + raise e async def delete_picture_set_with_archive( cursor, user_id, picture_set_id, container_client diff --git a/datastore/db/queries/picture/__init__.py b/datastore/db/queries/picture/__init__.py index 1ee30a52..99d4b731 100644 --- a/datastore/db/queries/picture/__init__.py +++ b/datastore/db/queries/picture/__init__.py @@ -380,6 +380,30 @@ def check_picture_inference_exist(cursor, picture_id: str): raise GetPictureError( f"Error: could not check if the picture {picture_id} has an existing inference") +def get_picture_inference(cursor, picture_id: str): + """ + This functions retrieve inference of a given picture + + Parameters: + - cursor (cursor): The cursor of the database. + - picture_id (str): The UUID of the picture. + """ + try : + query = """ + SELECT + * + FROM + inference + WHERE + picture_id = %s + """ + cursor.execute(query, (picture_id,)) + result = cursor.fetchone()[0] + return result + except Exception: + raise GetPictureError( + f"Error: could not get inference for the picture {picture_id}") + def change_picture_set_id(cursor, user_id, old_picture_set_id, new_picture_set_id): """ This function change picture_set_id of all pictures in a picture_set to a new one. diff --git a/tests/test_datastore.py b/tests/test_datastore.py index 9e3876f5..329e5bc8 100644 --- a/tests/test_datastore.py +++ b/tests/test_datastore.py @@ -12,6 +12,7 @@ import asyncio import datastore.db.__init__ as db import datastore.__init__ as datastore +import datastore.Nachet as nachet_datastore import datastore.db.metadata.validator as validator import datastore.db.queries.seed as seed_query from copy import deepcopy @@ -34,7 +35,7 @@ class test_ml_structure(unittest.TestCase): def setUp(self): with open("tests/ml_structure_exemple.json") as file: self.ml_dict= json.load(file) - self.con = db.connect_db(DB_CONNECTION_STRING) + self.con = db.connect_db(DB_CONNECTION_STRING,DB_SCHEMA) self.cursor = self.con.cursor() db.create_search_path(self.con, self.cursor,DB_SCHEMA) @@ -86,7 +87,7 @@ def test_get_ml_structure_eeror(self): class test_user(unittest.TestCase): def setUp(self): - self.con = db.connect_db(DB_CONNECTION_STRING) + self.con = db.connect_db(DB_CONNECTION_STRING,DB_SCHEMA) self.cursor = self.con.cursor() db.create_search_path(self.con, self.cursor,DB_SCHEMA) self.user_email="test@email" @@ -163,7 +164,7 @@ def test_get_user_container_client(self): class test_picture(unittest.TestCase): def setUp(self): - self.con = db.connect_db(DB_CONNECTION_STRING) + self.con = db.connect_db(DB_CONNECTION_STRING,DB_SCHEMA) self.cursor = self.con.cursor() db.create_search_path(self.con, self.cursor,DB_SCHEMA) self.connection_str=BLOB_CONNECTION_STRING @@ -293,7 +294,7 @@ def test_upload_pictures_error_seed_not_found(self): class test_feedback(unittest.TestCase): def setUp(self): - self.con = db.connect_db(DB_CONNECTION_STRING) + self.con = db.connect_db(DB_CONNECTION_STRING,DB_SCHEMA) self.cursor = self.con.cursor() db.create_search_path(self.con, self.cursor,DB_SCHEMA) self.connection_str=BLOB_CONNECTION_STRING @@ -487,7 +488,7 @@ def test_new_correction_inference_feedback_unknown_seed(self): class test_picture_set(unittest.TestCase): def setUp(self): - self.con = db.connect_db(DB_CONNECTION_STRING) + self.con = db.connect_db(DB_CONNECTION_STRING,DB_SCHEMA) self.cursor = self.con.cursor() db.create_search_path(self.con, self.cursor,DB_SCHEMA) self.connection_str=BLOB_CONNECTION_STRING @@ -507,13 +508,13 @@ def setUp(self): self.picture_set_id = asyncio.run(datastore.create_picture_set(self.cursor, self.container_client, 0, self.user_id, self.folder_name)) self.pictures_id =[] for i in range(3) : - picture_id = asyncio.run(datastore.upload_picture_unknown(self.cursor, self.user_id, self.pic_encoded,self.container_client, self.picture_set_id)) + picture_id = asyncio.run(nachet_datastore.upload_picture_unknown(self.cursor, self.user_id, self.pic_encoded,self.container_client, self.picture_set_id)) self.pictures_id.append(picture_id) with open("tests/inference_result.json") as file: self.inference= json.load(file) self.dev_user_id=datastore.user.get_user_id(self.cursor, os.environ["DEV_USER_EMAIL"]) - self.dev_container_client = asyncio.run(datastore.get_user_container_client(self.dev_user_id)) + self.dev_container_client = asyncio.run(datastore.get_user_container_client(self.dev_user_id, BLOB_CONNECTION_STRING)) def tearDown(self): self.con.rollback() @@ -527,8 +528,18 @@ def test_get_picture_sets_info(self) : picture_sets_info = asyncio.run(datastore.get_picture_sets_info(self.cursor, self.user_id)) self.assertEqual(len(picture_sets_info), 2) - self.assertEqual(picture_sets_info.get(str(self.picture_set_id))[1], 3) - self.assertEqual(picture_sets_info.get(str(self.picture_set_id))[0], self.folder_name) + for picture_set in picture_sets_info : + if picture_set["picture_set_id"] == self.picture_set_id : + self.assertEqual(picture_set["picture_set_id"], self.picture_set_id) + self.assertEqual(picture_set["folder_name"], self.folder_name) + self.assertEqual(picture_set["nb_pictures"], 3) + self.assertTrue("pictures" in picture_set) + ids = [pic["picture_id"] for pic in picture_set["pictures"]] + self.assertEqual(set(ids), set(self.pictures_id)) + for pic in picture_set["pictures"] : + pic["is_verified"] = False + pic["inference_exist"] = False + self.picture_set_id = asyncio.run(datastore.create_picture_set(self.cursor, self.container_client, 0, self.user_id, self.folder_name + "2")) @@ -761,7 +772,7 @@ def test_delete_picture_set_with_archive_error_default_folder(self): class test_analysis(unittest.TestCase): def setUp(self): - self.con = db.connect_db(DB_CONNECTION_STRING) + self.con = db.connect_db(DB_CONNECTION_STRING,DB_SCHEMA) self.cursor = self.con.cursor() db.create_search_path(self.con, self.cursor,DB_SCHEMA) self.connection_str=BLOB_CONNECTION_STRING