Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #203: add inspector_id and relevant tests #209

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fertiscan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async def register_analysis(
)

# Register analysis in the database
formatted_analysis = data_inspection.build_inspection_import(analysis_dict)
formatted_analysis = data_inspection.build_inspection_import(analysis_dict,user_id)

analysis_db = inspection.new_inspection_with_label_info(
cursor, user_id, picture_set_id, formatted_analysis
Expand Down
5 changes: 4 additions & 1 deletion fertiscan/db/metadata/inspection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class DBInspection(ValidatedModel):

class Inspection(ValidatedModel):
inspection_id: Optional[str] = None
inspector_id: Optional[str] = None
inspection_comment: Optional[str] = None
verified: Optional[bool] = False
company: Optional[OrganizationInformation] = OrganizationInformation()
Expand All @@ -133,7 +134,7 @@ class Inspection(ValidatedModel):
guaranteed_analysis: GuaranteedAnalysis


def build_inspection_import(analysis_form: dict) -> str:
def build_inspection_import(analysis_form: dict,user_id) -> str:
"""
This funtion build an inspection json object from the pipeline of digitalization analysis.
This serves as the metadata for the inspection object in the database.
Expand Down Expand Up @@ -322,6 +323,7 @@ def build_inspection_import(analysis_form: dict) -> str:
)

inspection_formatted = Inspection(
inspector_id=str(user_id),
company=company,
manufacturer=manufacturer,
product=product,
Expand Down Expand Up @@ -377,6 +379,7 @@ def build_inspection_export(cursor, inspection_id, label_info_id) -> str:

inspection_formatted = Inspection(
inspection_id=str(inspection_id),
inspector_id=str(db_inspection.inspector_id),
inspection_comment=db_inspection.inspection_comment,
cautions=cautions,
company=company,
Expand Down
2 changes: 1 addition & 1 deletion fertiscan_pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "fertiscan_datastore"
version = "1.0.5"
version = "1.0.6"
authors = [
{ name="Francois Werbrouck", email="[email protected]" },
{ name="Kotchikpa Guy-Landry Allagbe" , email = "[email protected]"}
Expand Down
45 changes: 26 additions & 19 deletions tests/fertiscan/metadata/test_inspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ def setUp(self):
with open("tests/fertiscan/analyse.json") as f:
self.analyse = json.load(f)

self.formatted_analysis = metadata.build_inspection_import(self.analyse)

self.user_id = user.register_user(self.cursor, "test-email@email")

self.formatted_analysis = metadata.build_inspection_import(self.analyse,self.user_id)

self.picture_set_id = picture.new_picture_set(
self.cursor, json.dumps({}), self.user_id
)
Expand All @@ -45,7 +45,7 @@ def tearDown(self):
db.end_query(self.con, self.cursor)

def test_perfect_inspection(self):
formatted_analysis = metadata.build_inspection_import(self.analyse)
formatted_analysis = metadata.build_inspection_import(self.analyse,self.user_id)

inspection_dict = inspection.new_inspection_with_label_info(
self.cursor, self.user_id, self.picture_set_id, formatted_analysis
Expand All @@ -61,6 +61,7 @@ def test_perfect_inspection(self):
)
data = json.loads(data)
self.assertIsNotNone(data["inspection_id"])
self.assertEqual(inspection_dict["inspector_id"],data["inspector_id"])
self.assertEqual(inspection_dict["inspection_id"], data["inspection_id"])
self.assertEqual(inspection_dict["verified"], data["verified"])
self.assertDictEqual(inspection_dict["product"], data["product"])
Expand All @@ -81,7 +82,7 @@ def test_no_manufacturer(self):
self.analyse["manufacturer_phone_number"] = None
self.analyse["manufacturer_address"] = None

formatted_analysis = metadata.build_inspection_import(self.analyse)
formatted_analysis = metadata.build_inspection_import(self.analyse,self.user_id)

# print(formatted_analysis)
inspection_dict = inspection.new_inspection_with_label_info(
Expand All @@ -98,6 +99,7 @@ def test_no_manufacturer(self):
)
data = json.loads(data)
self.assertIsNotNone(data["inspection_id"])
self.assertEqual(inspection_dict["inspector_id"],data["inspector_id"])
self.assertEqual(inspection_dict["inspection_id"], data["inspection_id"])
self.assertEqual(inspection_dict["verified"], data["verified"])
self.assertDictEqual(inspection_dict["company"], data["company"])
Expand All @@ -110,7 +112,7 @@ def test_no_manufacturer(self):

def test_no_volume(self):
self.analyse["volume"] = None
formatted_analysis = metadata.build_inspection_import(self.analyse)
formatted_analysis = metadata.build_inspection_import(self.analyse,self.user_id)

# print(formatted_analysis)
inspection_dict = inspection.new_inspection_with_label_info(
Expand All @@ -127,6 +129,7 @@ def test_no_volume(self):
)
data = json.loads(data)
self.assertIsNotNone(data["inspection_id"])
self.assertEqual(inspection_dict["inspector_id"],data["inspector_id"])
self.assertEqual(inspection_dict["inspection_id"], data["inspection_id"])
self.assertEqual(inspection_dict["verified"], data["verified"])

Expand All @@ -139,7 +142,7 @@ def test_no_volume(self):

def test_no_weight(self):
self.analyse["weight"] = []
formatted_analysis = metadata.build_inspection_import(self.analyse)
formatted_analysis = metadata.build_inspection_import(self.analyse,self.user_id)

# print(formatted_analysis)
inspection_dict = inspection.new_inspection_with_label_info(
Expand All @@ -156,6 +159,7 @@ def test_no_weight(self):
)
data = json.loads(data)
self.assertIsNotNone(data["inspection_id"])
self.assertEqual(inspection_dict["inspector_id"],data["inspector_id"])
self.assertEqual(inspection_dict["inspection_id"], data["inspection_id"])
self.assertEqual(inspection_dict["verified"], data["verified"])

Expand All @@ -165,7 +169,7 @@ def test_no_weight(self):
def test_missing_sub_label(self):
self.analyse["instructions_en"] = []
self.analyse["instructions_fr"] = []
formatted_analysis = metadata.build_inspection_import(self.analyse)
formatted_analysis = metadata.build_inspection_import(self.analyse,self.user_id)

# print(formatted_analysis)
inspection_dict = inspection.new_inspection_with_label_info(
Expand All @@ -182,6 +186,7 @@ def test_missing_sub_label(self):
)
data = json.loads(data)
self.assertIsNotNone(data["inspection_id"])
self.assertEqual(inspection_dict["inspector_id"],data["inspector_id"])
self.assertEqual(inspection_dict["inspection_id"], data["inspection_id"])
self.assertEqual(inspection_dict["verified"], data["verified"])

Expand All @@ -196,7 +201,8 @@ def test_unequal_sub_label_lengths_and_order(self):
expected_instructions_fr = ["un", "deux"]
self.analyse["instructions_en"] = expected_instructions_en
self.analyse["instructions_fr"] = expected_instructions_fr
formatted_analysis = metadata.build_inspection_import(self.analyse)

formatted_analysis = metadata.build_inspection_import(self.analyse,self.user_id)

inspection_dict = inspection.new_inspection_with_label_info(
self.cursor, self.user_id, self.picture_set_id, formatted_analysis
Expand Down Expand Up @@ -324,7 +330,7 @@ def test_missing_guaranteed_analysis(self):
"title": None,
"nutrients": [],
}
formatted_analysis = metadata.build_inspection_import(self.analyse)
formatted_analysis = metadata.build_inspection_import(self.analyse,self.user_id)

# print(formatted_analysis)
inspection_dict = inspection.new_inspection_with_label_info(
Expand All @@ -343,6 +349,7 @@ def test_missing_guaranteed_analysis(self):
)

self.assertIsNotNone(data["inspection_id"])
self.assertEqual(inspection_dict["inspector_id"],data["inspector_id"])
self.assertEqual(inspection_dict["inspection_id"], data["inspection_id"])
self.assertEqual(inspection_dict["verified"], data["verified"])

Expand Down Expand Up @@ -397,7 +404,7 @@ def test_missing_guaranteed_analysis(self):
# )

def test_null_in_middle_of_sub_label_en(self):
formatted_analysis = metadata.build_inspection_import(self.analyse)
formatted_analysis = metadata.build_inspection_import(self.analyse,self.user_id)

# Convert the JSON string to a dictionary
formatted_analysis_dict = json.loads(formatted_analysis)
Expand Down Expand Up @@ -442,7 +449,7 @@ def test_null_in_middle_of_sub_label_en(self):
)

def test_mismatched_sub_label_lengths_en_longer(self):
formatted_analysis = metadata.build_inspection_import(self.analyse)
formatted_analysis = metadata.build_inspection_import(self.analyse,self.user_id)

# Convert the JSON string to a dictionary
formatted_analysis_dict = json.loads(formatted_analysis)
Expand Down Expand Up @@ -483,7 +490,7 @@ def test_mismatched_sub_label_lengths_en_longer(self):
)

def test_mismatched_sub_label_lengths_fr_longer(self):
formatted_analysis = metadata.build_inspection_import(self.analyse)
formatted_analysis = metadata.build_inspection_import(self.analyse,self.user_id)

# Convert the JSON string to a dictionary
formatted_analysis_dict = json.loads(formatted_analysis)
Expand Down Expand Up @@ -536,7 +543,7 @@ def test_organization_not_located(self):
self.analyse["company_phone_number"] = None
self.analyse["company_website"] = test_str

formatted_analysis = metadata.build_inspection_import(self.analyse)
formatted_analysis = metadata.build_inspection_import(self.analyse,self.user_id)

# Create inspection and get label information
inspection_dict = inspection.new_inspection_with_label_info(
Expand Down Expand Up @@ -609,7 +616,7 @@ def tearDown(self):
db.end_query(self.con, self.cursor)

def test_perfect_inspection(self):
formatted_analysis = metadata.build_inspection_import(self.analyse)
formatted_analysis = metadata.build_inspection_import(self.analyse,self.user_id)

inspection_dict = inspection.new_inspection_with_label_info(
self.cursor, self.user_id, self.picture_set_id, formatted_analysis
Expand All @@ -624,7 +631,7 @@ def test_empty_sub_label(self):
self.analyse["cautions_en"] = []
self.analyse["cautions_fr"] = []

formatted_analysis = metadata.build_inspection_import(self.analyse)
formatted_analysis = metadata.build_inspection_import(self.analyse,self.user_id)

# Create inspection and get label information
inspection_dict = inspection.new_inspection_with_label_info(
Expand All @@ -651,27 +658,27 @@ def test_missing_keys(self):
# intentionally leaving out other required keys
}
with self.assertRaises(BuildInspectionImportError) as context:
metadata.build_inspection_import(analysis_form)
metadata.build_inspection_import(analysis_form,self.user_id)
self.assertIn("The analysis form is missing keys", str(context.exception))

def test_validation_error(self):
self.analyse["weight"] = [{"unit": "kg", "value": "invalid_value"}]
with self.assertRaises(BuildInspectionImportError) as context:
metadata.build_inspection_import(self.analyse)
metadata.build_inspection_import(self.analyse,self.user_id)
self.assertIn("Validation error", str(context.exception))

def test_npk_error(self):
self.analyse["npk"] = "invalid_npk"
with self.assertRaises(NPKError):
metadata.build_inspection_import(self.analyse)
metadata.build_inspection_import(self.analyse,self.user_id)

@patch("fertiscan.db.metadata.inspection.extract_npk")
def test_unexpected_error(self, mock_extract_npk):
# Mock extract_npk to raise an exception to simulate an unexpected error
mock_extract_npk.side_effect = Exception("Simulated unexpected error")

with self.assertRaises(BuildInspectionImportError) as context:
metadata.build_inspection_import(self.analyse)
metadata.build_inspection_import(self.analyse,self.user_id)

self.assertIn("Unexpected error", str(context.exception))
self.assertIn("Simulated unexpected error", str(context.exception))
5 changes: 3 additions & 2 deletions tests/fertiscan/test_datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def test_register_analysis_empty(self):
"guaranteed_analysis_fr": {"title": None, "nutrients": []},
}

formatted_analysis = metadata.build_inspection_import(empty_analysis)
formatted_analysis = metadata.build_inspection_import(empty_analysis,self.user.id)
picture_set_id = picture.new_picture_set(
self.cursor, json.dumps({}), self.user.id
)
Expand Down Expand Up @@ -321,7 +321,7 @@ def test_register_analysy_missing_key(self):
)

def test_get_full_inspection_json(self):
formatted_analysis = metadata.build_inspection_import(self.analysis_json)
formatted_analysis = metadata.build_inspection_import(self.analysis_json, self.user.id)
picture_set_id = picture.new_picture_set(
self.cursor, json.dumps({}), self.user.id
)
Expand All @@ -336,6 +336,7 @@ def test_get_full_inspection_json(self):
)
data = json.loads(data)
self.assertEqual(data["inspection_id"], str(inspection_id))
self.assertEqual(data["inspector_id"], str(self.user.id))

def test_delete_inspection(self):
# Create a new inspection to delete later
Expand Down
Loading