Skip to content

Commit

Permalink
Issue #212: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Francois-Werbrouck committed Nov 29, 2024
1 parent 7cdf3fb commit f63a8f5
Show file tree
Hide file tree
Showing 13 changed files with 199 additions and 362 deletions.
59 changes: 0 additions & 59 deletions fertiscan/db/bytebase/update_inspection_function.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,65 +22,6 @@ END;
$$ LANGUAGE plpgsql;


-- Function to upsert organization information
CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".upsert_organization_info(input_org_info jsonb)
RETURNS uuid AS $$
DECLARE
organization_info_id uuid;
location_id uuid;
address_str text;
BEGIN
-- Skip processing if the input JSON object is empty or null
IF jsonb_typeof(input_org_info) = 'null' OR NOT EXISTS (SELECT 1 FROM jsonb_object_keys(input_org_info)) or
COALESCE(input_org_info->>'name',
input_org_info->>'website',
input_org_info->>'phone_number',
input_org_info->>'address',
'') = '' THEN
RAISE WARNING 'Organization information is empty or null';
RETURN NULL;
ELSE

address_str := input_org_info->>'address';

-- CHECK IF ADRESS IS NULL
IF address_str IS NULL THEN
RAISE WARNING 'Address cannot be null';
ELSE
-- Check if organization location exists by address
SELECT id INTO location_id
FROM location
WHERE location.address ILIKE address_str
LIMIT 1;
-- Use upsert_location to insert or update the location
location_id := upsert_location(location_id, address_str);
END IF;

-- Extract the organization info ID from the input JSON or generate a new UUID if not provided
organization_info_id := COALESCE(NULLIF(input_org_info->>'id', '')::uuid, public.uuid_generate_v4());

-- Upsert organization information
INSERT INTO organization_information (id, name, website, phone_number, location_id)
VALUES (
organization_info_id,
input_org_info->>'name',
input_org_info->>'website',
input_org_info->>'phone_number',
location_id
)
ON CONFLICT (id) DO UPDATE
SET name = EXCLUDED.name,
website = EXCLUDED.website,
phone_number = EXCLUDED.phone_number,
location_id = EXCLUDED.location_id
RETURNING id INTO organization_info_id;

RETURN organization_info_id;
end if;
END;
$$ LANGUAGE plpgsql;


-- Function to update metrics: delete old and insert new
CREATE OR REPLACE FUNCTION "fertiscan_0.0.17".update_metrics(
p_label_id uuid,
Expand Down
11 changes: 2 additions & 9 deletions fertiscan/db/queries/label/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ def new_label_information(
title_en: str,
title_fr: str,
is_minimal: bool,
company_info_id,
manufacturer_info_id,
record_keeping,
):
"""
Expand All @@ -43,14 +41,13 @@ def new_label_information(
- title_en (str): The english title of the guaranteed analysis.
- title_fr (str): The french title of the guaranteed analysis.
- is_minimal (bool): if the tital is minimal for the guaranteed analysis.
- company_info_id (str): The UUID of the company.
- manufacturer_info_id (str): The UUID of the manufacturer.
- record_keeping (bool): if the label is a record keeping.
Returns:
- str: The UUID of the label_information
"""
query = """
SELECT new_label_information(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);
SELECT new_label_information(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s);
"""
cursor.execute(
query,
Expand All @@ -64,8 +61,6 @@ def new_label_information(
title_en,
title_fr,
is_minimal,
company_info_id,
manufacturer_info_id,
record_keeping,
),
)
Expand Down Expand Up @@ -110,8 +105,6 @@ def get_label_information(cursor: Cursor, label_information_id: str) -> dict:
guaranteed_title_en,
guaranteed_title_fr,
title_is_minimal,
company_info_id,
manufacturer_info_id,
record_keeping
FROM
label_information
Expand Down
63 changes: 57 additions & 6 deletions fertiscan/db/queries/organization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

from psycopg import Cursor
from uuid import UUID

from fertiscan.db.queries.errors import (
LocationCreationError,
Expand Down Expand Up @@ -70,7 +71,7 @@ def new_organization(cursor: Cursor, information_id, location_id=None):

@handle_query_errors(OrganizationInformationCreationError)
def new_organization_info_located(
cursor: Cursor, address: str, name: str, website: str, phone_number: str
cursor: Cursor, address: str, name: str, website: str, phone_number: str, label_id: UUID
):
"""
This function create a new organization information in the database.
Expand All @@ -94,6 +95,7 @@ def new_organization_info_located(
address,
website,
phone_number,
label_id,
),
)
if result := cursor.fetchone():
Expand All @@ -103,7 +105,7 @@ def new_organization_info_located(

@handle_query_errors(OrganizationInformationCreationError)
def new_organization_info(
cursor: Cursor, name, website, phone_number, location_id=None
cursor: Cursor, name, website, phone_number, location_id=None, label_id=None
):
"""
This function create a new organization information in the database.
Expand All @@ -123,16 +125,17 @@ def new_organization_info(
name,
website,
phone_number,
location_id
location_id,
label_id
)
VALUES
(%s, %s, %s, %s)
(%s, %s, %s, %s, %s)
RETURNING
id
"""
cursor.execute(
query,
(name, website, phone_number, location_id),
(name, website, phone_number, location_id, label_id),
)
if result := cursor.fetchone():
return result[0]
Expand All @@ -156,7 +159,8 @@ def get_organization_info(cursor: Cursor, information_id):
name,
website,
phone_number,
location_id
location_id,
label_id
FROM
organization_information
WHERE
Expand All @@ -169,6 +173,36 @@ def get_organization_info(cursor: Cursor, information_id):
"Organization information not found with information_id: " + information_id
)

def get_organizations_info_label(cursor: Cursor, label_id: UUID):
"""
This function get a organization information from the database.
Parameters:
- cursor (cursor): The cursor of the database.
- information_id (str): The UUID of the organization information.
Returns:
- tuple: The organization informations
"""
query = """
SELECT
name,
website,
phone_number,
location_id,
edited
FROM
organization_information
WHERE
label_id = %s
"""
cursor.execute(query, (label_id,))
if result := cursor.fetchone():
return result
raise OrganizationInformationNotFoundError(
"Organization information not found with label_id: " + label_id
)


@handle_query_errors(OrganizationInformationRetrievalError)
def get_organizations_info_json(cursor: Cursor, label_id) -> dict:
Expand Down Expand Up @@ -274,6 +308,23 @@ def update_organization_info(
)
return information_id

def upsert_organization_info(cursor: Cursor, organization_info, label_id: UUID):
"""
This function upserts an organization information in the database.
Parameters:
- cursor (cursor): The cursor of the database.
- organization_info (JSON: string): The organization information in a json.
Returns:
- str: The UUID of the organization information
"""
query = """
SELECT upsert_organization_info(%s, %s);
"""
cursor.execute(query, (organization_info, label_id,))
return cursor.fetchone()[0]


@handle_query_errors(OrganizationRetrievalError)
def get_organization(cursor: Cursor, organization_id):
Expand Down
2 changes: 0 additions & 2 deletions tests/fertiscan/db/test_guaranteed_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ def setUp(self):
self.title_fr,
self.is_minimal,
None,
None,
None,
)
self.language = "fr"

Expand Down
2 changes: 0 additions & 2 deletions tests/fertiscan/db/test_ingredient.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ def setUp(self):
None,
False,
None,
None,
None,
)
self.language = "fr"

Expand Down
24 changes: 8 additions & 16 deletions tests/fertiscan/db/test_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ def setUp(self):
self.guaranteed_analysis_title_en = "guaranteed_analysis"
self.guaranteed_analysis_title_fr = "analyse_garantie"
self.guaranteed_is_minimal = False
self.company_info_id = None
self.manufacturer_info_id = None

def tearDown(self):
self.con.rollback()
Expand All @@ -50,8 +48,6 @@ def test_new_label_information(self):
self.guaranteed_analysis_title_en,
self.guaranteed_analysis_title_fr,
self.guaranteed_is_minimal,
self.company_info_id,
self.manufacturer_info_id,
None,
)
self.assertTrue(validator.is_valid_uuid(label_information_id))
Expand All @@ -68,8 +64,6 @@ def test_get_label_information(self):
self.guaranteed_analysis_title_en,
self.guaranteed_analysis_title_fr,
self.guaranteed_is_minimal,
self.company_info_id,
self.manufacturer_info_id,
None,
)
label_data = label.get_label_information(self.cursor, label_information_id)
Expand All @@ -78,14 +72,14 @@ def test_get_label_information(self):
self.assertEqual(label_data[1], self.product_name)
self.assertEqual(label_data[2], self.lot_number)
self.assertEqual(label_data[3], self.npk)
self.assertEqual(label_data[4], self.n)
self.assertEqual(label_data[5], self.p)
self.assertEqual(label_data[6], self.k)
self.assertEqual(label_data[7], self.guaranteed_analysis_title_en)
self.assertEqual(label_data[8], self.guaranteed_analysis_title_fr)
self.assertEqual(label_data[9], self.guaranteed_is_minimal)
self.assertIsNone(label_data[10])
self.assertIsNone(label_data[11])
self.assertEqual(label_data[4], self.registration_number)
self.assertEqual(label_data[5], self.n)
self.assertEqual(label_data[6], self.p)
self.assertEqual(label_data[7], self.k)
self.assertEqual(label_data[8], self.guaranteed_analysis_title_en)
self.assertEqual(label_data[9], self.guaranteed_analysis_title_fr)
self.assertEqual(label_data[10], self.guaranteed_is_minimal)
self.assertIsNone(label_data[12])

def test_get_label_information_json(self):
label_information_id = label.new_label_information(
Expand All @@ -100,8 +94,6 @@ def test_get_label_information_json(self):
self.guaranteed_analysis_title_fr,
self.guaranteed_is_minimal,
None,
None,
None,
)
label_data = label.get_label_information_json(self.cursor, label_information_id)
self.assertEqual(label_data["label_id"], str(label_information_id))
Expand Down
2 changes: 0 additions & 2 deletions tests/fertiscan/db/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ def setUp(self):
None,
False,
None,
None,
None,
)
self.language = "fr"

Expand Down
Loading

0 comments on commit f63a8f5

Please sign in to comment.