Skip to content

Commit

Permalink
Issue #252: updated_at added to the Inspection model
Browse files Browse the repository at this point in the history
  • Loading branch information
Francois-Werbrouck committed Feb 26, 2025
1 parent 48b2751 commit 3027c83
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 35 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
**/__pycache__/**
.vscode/settings.json
.vscode/**
test/**
.env
0.1.3.json
ml_structure.json
.DS_Store
.venv
venv
.vscode/extensions.json
15 changes: 11 additions & 4 deletions fertiscan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,14 @@ def update_inspection(
):
raise inspection.InspectionUpdateError("The inspection you are trying to update is already verified")
else:
inspection.update_inspection(
updated_at =inspection.update_inspection(
cursor=cursor,
inspection_id=updated_data.inspection_id,
verified=updated_data.verified,
inspection_comment=updated_data.inspection_comment
)
updated_data.updated_at = updated_at
self.model.updated_at = updated_at # just in case
# --------
label_to_update = updated_data.product
label_id = updated_data.product.label_id
Expand Down Expand Up @@ -225,7 +227,7 @@ def update_inspection(
if main_org is None:
raise Warning("'Main contact organization information is required and was not found'")
else:
organization.upsert_organization(
organization_id = organization.upsert_organization(
cursor=cursor,
name = main_org.name,
website= main_org.website,
Expand All @@ -237,12 +239,13 @@ def update_inspection(
cursor=cursor,
name=fertilizer_name,
reg_number=registration_number_value,
org_owner_id=main_org.id,
org_owner_id=organization_id,
latest_inspection_id=updated_data.inspection_id
)
else:
updated_data.verified = False
# ------------
updated_data.updated_at = updated_at
return data_inspection.Inspection.model_validate(updated_data)

async def delete_inspection(
Expand Down Expand Up @@ -467,17 +470,21 @@ def new_inspection(cursor:Cursor, user_id:UUID, analysis_dict, container_id:UUID
)
if flag==True:
# We do this since we have no way of knowing who is the main contact
record.is_main_contact=True # We assume the first one is the main contact
flag=False

# Inspection
formatted_analysis.inspection_id = inspection.new_inspection(
inspection_id,upload_date = inspection.new_inspection(
cursor=cursor,
user_id=user_id,
picture_set_id=folder_id,
verified=False,
label_id=label_info_id,
container_id=container_id
)
formatted_analysis.inspection_id = inspection_id
formatted_analysis.upload_date = upload_date
formatted_analysis.updated_at = upload_date
analysis_db = data_inspection.Inspection.model_validate(formatted_analysis)
inspection.save_inspection_original_dataset(
cursor=cursor,
Expand Down
10 changes: 8 additions & 2 deletions fertiscan/db/metadata/inspection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ class Inspection(ValidatedModel):
ingredients: ValuesObjects
folder_id: UUID4
container_id: UUID4
upload_date: Optional[datetime] = None
updated_at: Optional[datetime] = None



Expand Down Expand Up @@ -355,7 +357,9 @@ def build_inspection_import(analysis_form: dict, user_id:UUID,folder_id:UUID,con
ingredients=ingredients,
folder_id=folder_id,
container_id=container_id,
inspection_comment= None
inspection_comment= None,
upload_date=None,
updated_at=None,
)
return inspection_formatted
except MetadataError:
Expand Down Expand Up @@ -441,7 +445,9 @@ def build_inspection_export(cursor, inspection_id) -> Inspection:
verified=db_inspection.verified,
ingredients=ingredients,
folder_id=folder_id,
container_id=container_id
container_id=container_id,
upload_date=db_inspection.upload_date,
updated_at=db_inspection.updated_at,
)
return inspection_formatted
except QueryError as e:
Expand Down
19 changes: 13 additions & 6 deletions fertiscan/db/queries/inspection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import json
from uuid import UUID
from datetime import datetime

from psycopg import Cursor
from psycopg.rows import dict_row
Expand All @@ -22,7 +23,7 @@


@handle_query_errors(InspectionCreationError)
def new_inspection(cursor: Cursor, user_id, picture_set_id, label_id,container_id,verified=False):
def new_inspection(cursor: Cursor, user_id, picture_set_id, label_id,container_id,verified=False)->tuple[UUID,datetime]:
"""
This function uploads a new inspection to the database.
Expand All @@ -47,11 +48,11 @@ def new_inspection(cursor: Cursor, user_id, picture_set_id, label_id,container_i
VALUES
(%s, %s, %s,%s,%s)
RETURNING
id
id, upload_date;
"""
cursor.execute(query, (user_id, picture_set_id, verified,label_id,container_id))
if result := cursor.fetchone():
return result[0]
return result
raise InspectionCreationError("Failed to create inspection. No data returned.")

def save_inspection_original_dataset(cursor: Cursor, inspection_id:UUID,og_data):
Expand Down Expand Up @@ -381,7 +382,7 @@ def update_inspection(
inspection_id: str | UUID,
verified: bool,
inspection_comment:str
):
)->datetime:
if verified:
query = """
UPDATE
Expand All @@ -392,7 +393,9 @@ def update_inspection(
inspection_comment = %s,
verified_date = CURRENT_TIMESTAMP
WHERE
id = %s;
id = %s
RETURNING
updated_at;
"""
else:
query = """
Expand All @@ -403,9 +406,13 @@ def update_inspection(
updated_at = CURRENT_TIMESTAMP,
inspection_comment = %s
WHERE
id = %s;
id = %s
RETURNING
updated_at;
"""
cursor.execute(query, (verified,inspection_comment,inspection_id))
return cursor.fetchone()[0]



@handle_query_errors(InspectionUpdateError)
Expand Down
2 changes: 1 addition & 1 deletion fertiscan/db/queries/organization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def upsert_organization(cursor: Cursor, name:str, website:str, phone_number: str
"""
cursor.execute(query, (name,))
res = cursor.fetchone()
if res[0] is None:
if res is None or res[0] is None:
id =new_organization(
cursor=cursor,
name=name,
Expand Down
49 changes: 41 additions & 8 deletions tests/fertiscan/db/test_inspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import os
import unittest

from datetime import datetime
from time import sleep

import datastore.db as db
from datastore import Role
from datastore.db.metadata import picture_set, validator
Expand Down Expand Up @@ -41,7 +44,7 @@ def tearDown(self):
db.end_query(self.con, self.cursor)

def test_new_inspection(self):
inspection_id = inspection.new_inspection(
inspection_id,upload_date = inspection.new_inspection(
cursor=self.cursor,
user_id=self.user_id,
picture_set_id=self.picture_set_id,
Expand All @@ -50,6 +53,7 @@ def test_new_inspection(self):
verified=False
)
self.assertTrue(validator.is_valid_uuid(inspection_id))
self.assertIsInstance(upload_date, datetime)

def test_is_inspection_verified(self):
inspection_id = inspection.new_inspection(
Expand All @@ -59,15 +63,15 @@ def test_is_inspection_verified(self):
label_id=None,
container_id=self.container_id,
verified=False
)
)[0]
inspection_id2 = inspection.new_inspection(
cursor=self.cursor,
user_id=self.user_id,
picture_set_id=self.picture_set_id,
label_id=None,
container_id=self.container_id,
verified=True
)
)[0]
self.assertFalse(inspection.is_inspection_verified(self.cursor, inspection_id))
self.assertTrue(inspection.is_inspection_verified(self.cursor, inspection_id2))

Expand All @@ -79,7 +83,7 @@ def test_get_inspection(self):
label_id=None,
container_id=self.container_id,
verified=False
)
)[0]
inspection_data = inspection.get_inspection(self.cursor, inspection_id)
self.assertEqual(inspection_data[0], False)
self.assertEqual(inspection_data[3], self.user_id)
Expand All @@ -93,15 +97,15 @@ def test_get_all_user_inspection(self):
label_id=None,
container_id=self.container_id,
verified=False
)
)[0]
inspection_id2 = inspection.new_inspection(
cursor=self.cursor,
user_id=self.user_id,
picture_set_id=self.picture_set_id,
label_id=None,
container_id=self.container_id,
verified=False
)
)[0]
inspection_data = inspection.get_all_user_inspection(self.cursor, self.user_id)
self.assertEqual(len(inspection_data), 2)
self.assertEqual(inspection_data[0][0], inspection_id)
Expand All @@ -115,15 +119,15 @@ def test_get_all_user_inspection_filter_verified(self):
label_id=None,
container_id=self.container_id,
verified=False
)
)[0]
inspection_id2 = inspection.new_inspection(
cursor=self.cursor,
user_id=self.user_id,
picture_set_id=self.picture_set_id,
label_id=None,
container_id=self.container_id,
verified=True
)
)[0]
inspection_data = inspection.get_all_user_inspection_filter_verified(
self.cursor, self.user_id, True
)
Expand Down Expand Up @@ -154,3 +158,32 @@ def test_get_all_user_inspection_filter_verified(self):
# self.assertEqual(len(inspection_data), 2)
# self.assertEqual(inspection_data[0][0], inspection_id)
# self.assertEqual(inspection_data[1][0], inspection_id2)

def test_update_inspection(self):
inspection_id,upload_date = inspection.new_inspection(
cursor=self.cursor,
user_id=self.user_id,
picture_set_id=self.picture_set_id,
label_id=None,
container_id=self.container_id,
verified=False,
)
self.con.commit() # need to commit to get the time change
sleep(2)
updated_at = inspection.update_inspection(
cursor=self.cursor,
inspection_id=inspection_id,
verified=True,
inspection_comment="Test comment"
)
inspection_data = inspection.get_inspection(self.cursor, inspection_id)
inspection.delete_inspection(self.cursor,inspection_id,self.user_id)
picture.delete_picture_set(self.cursor,self.picture_set_id)
container.delete_container(self.cursor,self.container_id)
user.delete_user(self.cursor, self.user_id)
self.con.commit()

self.assertTrue(inspection_data[0])
self.assertNotEqual(updated_at, upload_date)
self.assertEqual(updated_at, inspection_data[2])

Loading

0 comments on commit 3027c83

Please sign in to comment.