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 #158: reorganize fertiscan tests #200

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
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
19 changes: 10 additions & 9 deletions fertiscan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import datastore.db.queries.user as user
import fertiscan.db.metadata.inspection as data_inspection
import fertiscan.db.queries.inspection as inspection
from fertiscan.db.models import DBInspection, Inspection

load_dotenv()

Expand Down Expand Up @@ -90,7 +91,7 @@ async def update_inspection(
cursor: Cursor,
inspection_id: str | UUID,
user_id: str | UUID,
updated_data: dict | data_inspection.Inspection,
updated_data: dict | Inspection,
):
"""
Update an existing inspection record in the database.
Expand All @@ -99,10 +100,10 @@ async def update_inspection(
- cursor (Cursor): Database cursor for executing queries.
- inspection_id (str | UUID): UUID of the inspection to update.
- user_id (str | UUID): UUID of the user performing the update.
- updated_data (dict | data_inspection.Inspection): Dictionary or Inspection model containing updated inspection data.
- updated_data (dict | Inspection): Dictionary or Inspection model containing updated inspection data.

Returns:
- data_inspection.Inspection: Updated inspection data from the database.
- Inspection: Updated inspection data from the database.

Raises:
- InspectionUpdateError: If an error occurs during the update.
Expand All @@ -114,8 +115,8 @@ async def update_inspection(
if not user.is_a_user_id(cursor, str(user_id)):
raise user.UserNotFoundError(f"User not found based on the given id: {user_id}")

if not isinstance(updated_data, data_inspection.Inspection):
updated_data = data_inspection.Inspection.model_validate(updated_data)
if not isinstance(updated_data, Inspection):
updated_data = Inspection.model_validate(updated_data)

# The inspection record must exist before updating it
if not inspection.is_a_inspection_id(cursor, str(inspection_id)):
Expand All @@ -126,7 +127,7 @@ async def update_inspection(
updated_result = inspection.update_inspection(
cursor, inspection_id, user_id, updated_data.model_dump()
)
return data_inspection.Inspection.model_validate(updated_result)
return Inspection.model_validate(updated_result)


async def get_full_inspection_json(
Expand Down Expand Up @@ -268,7 +269,7 @@ async def delete_inspection(
inspection_id: str | UUID,
user_id: str | UUID,
container_client: ContainerClient,
) -> data_inspection.DBInspection:
) -> DBInspection:
"""
Delete an existing inspection record and its associated picture set from the database.

Expand All @@ -278,7 +279,7 @@ async def delete_inspection(
- user_id (str | UUID): UUID of the user performing the deletion.

Returns:
- data_inspection.Inspection: The deleted inspection data from the database.
- DBInspection: The deleted inspection data from the database.

"""
if isinstance(inspection_id, str):
Expand All @@ -288,7 +289,7 @@ async def delete_inspection(

# Delete the inspection and get the returned data
deleted_inspection = inspection.delete_inspection(cursor, inspection_id, user_id)
deleted_inspection = data_inspection.DBInspection.model_validate(deleted_inspection)
deleted_inspection = DBInspection.model_validate(deleted_inspection)

await datastore.delete_picture_set_permanently(
cursor, str(user_id), str(deleted_inspection.picture_set_id), container_client
Expand Down
16 changes: 8 additions & 8 deletions fertiscan/db/bytebase/OLAP/guaranteed_triggers.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

CREATE OR REPLACE FUNCTION "fertiscan_0.0.15".olap_guaranteed_creation()
CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".olap_guaranteed_creation()
RETURNS TRIGGER AS $$
BEGIN
IF (TG_OP = 'INSERT') THEN
IF (NEW.id IS NOT NULL) AND (NEW.label_id IS NOT NULL) THEN
-- Update the label_dimension table with the new guaranteed_analysis_id
UPDATE "fertiscan_0.0.15"."label_dimension"
UPDATE "fertiscan_0.0.16"."label_dimension"
SET guaranteed_ids = array_append(guaranteed_ids, NEW.id)
WHERE label_dimension.label_id = NEW.label_id;
ELSE
Expand All @@ -17,19 +17,19 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS guaranteed_creation ON "fertiscan_0.0.15".guaranteed;
DROP TRIGGER IF EXISTS guaranteed_creation ON "fertiscan_0.0.16".guaranteed;
CREATE TRIGGER guaranteed_creation
AFTER INSERT ON "fertiscan_0.0.15".guaranteed
AFTER INSERT ON "fertiscan_0.0.16".guaranteed
FOR EACH ROW
EXECUTE FUNCTION olap_guaranteed_creation();

CREATE OR REPLACE FUNCTION "fertiscan_0.0.15".olap_guaranteed_deletion()
CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".olap_guaranteed_deletion()
RETURNS TRIGGER AS $$
BEGIN
IF (TG_OP = 'DELETE') THEN
IF (OLD.id IS NOT NULL) AND (OLD.label_id IS NOT NULL) THEN
-- Update the label_dimension table with the new guaranteed_analysis_id
UPDATE "fertiscan_0.0.15"."label_dimension"
UPDATE "fertiscan_0.0.16"."label_dimension"
SET guaranteed_ids = array_remove(guaranteed_ids, OLD.id)
WHERE label_dimension.label_id = OLD.label_id;
ELSE
Expand All @@ -41,8 +41,8 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS guaranteed_deletion ON "fertiscan_0.0.15".guaranteed;
DROP TRIGGER IF EXISTS guaranteed_deletion ON "fertiscan_0.0.16".guaranteed;
CREATE TRIGGER guaranteed_deletion
AFTER DELETE ON "fertiscan_0.0.15".guaranteed
AFTER DELETE ON "fertiscan_0.0.16".guaranteed
FOR EACH ROW
EXECUTE FUNCTION olap_guaranteed_deletion();
16 changes: 8 additions & 8 deletions fertiscan/db/bytebase/OLAP/ingredient_trigger.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

CREATE OR REPLACE FUNCTION "fertiscan_0.0.15".olap_ingredient_creation()
CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".olap_ingredient_creation()
RETURNS TRIGGER AS $$
BEGIN
IF (TG_OP = 'INSERT') THEN
IF (NEW.id IS NOT NULL) AND (NEW.label_id IS NOT NULL) THEN
-- Update the label_dimension table with the new ingredient_analysis_id
UPDATE "fertiscan_0.0.15"."label_dimension"
UPDATE "fertiscan_0.0.16"."label_dimension"
SET ingredient_ids = array_append(ingredient_ids, NEW.id)
WHERE label_dimension.label_id = NEW.label_id;
ELSE
Expand All @@ -17,19 +17,19 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS ingredient_creation ON "fertiscan_0.0.15".ingredient;
DROP TRIGGER IF EXISTS ingredient_creation ON "fertiscan_0.0.16".ingredient;
CREATE TRIGGER ingredient_creation
AFTER INSERT ON "fertiscan_0.0.15".ingredient
AFTER INSERT ON "fertiscan_0.0.16".ingredient
FOR EACH ROW
EXECUTE FUNCTION olap_ingredient_creation();

CREATE OR REPLACE FUNCTION "fertiscan_0.0.15".olap_ingredient_deletion()
CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".olap_ingredient_deletion()
RETURNS TRIGGER AS $$
BEGIN
IF (TG_OP = 'DELETE') THEN
IF (OLD.id IS NOT NULL) AND (OLD.label_id IS NOT NULL) THEN
-- Update the label_dimension table with the new ingredient_analysis_id
UPDATE "fertiscan_0.0.15"."label_dimension"
UPDATE "fertiscan_0.0.16"."label_dimension"
SET ingredient_ids = array_remove(ingredient_ids, OLD.id)
WHERE label_dimension.label_id = OLD.label_id;
ELSE
Expand All @@ -41,8 +41,8 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS ingredient_deletion ON "fertiscan_0.0.15".ingredient;
DROP TRIGGER IF EXISTS ingredient_deletion ON "fertiscan_0.0.16".ingredient;
CREATE TRIGGER ingredient_deletion
AFTER DELETE ON "fertiscan_0.0.15".ingredient
AFTER DELETE ON "fertiscan_0.0.16".ingredient
FOR EACH ROW
EXECUTE FUNCTION olap_ingredient_deletion();
26 changes: 13 additions & 13 deletions fertiscan/db/bytebase/OLAP/inspection_triggers.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

CREATE OR REPLACE FUNCTION "fertiscan_0.0.15".olap_inspection_creation()
CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".olap_inspection_creation()
RETURNS TRIGGER AS $$
DECLARE
time_id UUID;
BEGIN
IF (TG_OP = 'INSERT') THEN
IF (NEW.id IS NOT NULL) AND (NEW.label_info_id IS NOT NULL) THEN
-- Time Dimension
INSERT INTO "fertiscan_0.0.15".time_dimension (
INSERT INTO "fertiscan_0.0.16".time_dimension (
date_value, year,month,day)
VALUES (
CURRENT_DATE,
Expand All @@ -16,7 +16,7 @@ BEGIN
EXTRACT(DAY FROM CURRENT_DATE)
) RETURNING id INTO time_id;
-- Create the Inspection_factual entry
INSERT INTO "fertiscan_0.0.15".inspection_factual (
INSERT INTO "fertiscan_0.0.16".inspection_factual (
inspection_id, inspector_id, label_info_id, time_id, sample_id, company_id, manufacturer_id, picture_set_id, original_dataset
) VALUES (
NEW.id,
Expand All @@ -38,19 +38,19 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS inspection_creation ON "fertiscan_0.0.15".inspection;
DROP TRIGGER IF EXISTS inspection_creation ON "fertiscan_0.0.16".inspection;
CREATE TRIGGER inspection_creation
AFTER INSERT ON "fertiscan_0.0.15".inspection
AFTER INSERT ON "fertiscan_0.0.16".inspection
FOR EACH ROW
EXECUTE FUNCTION olap_inspection_creation();

CREATE OR REPLACE FUNCTION "fertiscan_0.0.15".olap_inspection_update()
CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".olap_inspection_update()
RETURNS TRIGGER AS $$
BEGIN
IF (TG_OP = 'UPDATE') THEN
IF (NEW.id IS NOT NULL) THEN
IF (NEW.label_info_id != OLD.label_info_id) OR (NEW.inspector_id != OLD.inspector_id) OR (NEW.picture_set_id != OLD.picture_set_id) THEN
UPDATE "fertiscan_0.0.15".inspection_factual
UPDATE "fertiscan_0.0.16".inspection_factual
SET inspector_id = NEW.inspector_id, label_info_id = NEW.label_info_id, picture_set_id = NEW.picture_set_id
WHERE inspection_id = NEW.id;
END IF;
Expand All @@ -63,18 +63,18 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS inspection_update ON "fertiscan_0.0.15".inspection;
DROP TRIGGER IF EXISTS inspection_update ON "fertiscan_0.0.16".inspection;
CREATE TRIGGER inspection_update
BEFORE UPDATE ON "fertiscan_0.0.15".inspection
BEFORE UPDATE ON "fertiscan_0.0.16".inspection
FOR EACH ROW
EXECUTE FUNCTION olap_inspection_update();

CREATE OR REPLACE FUNCTION "fertiscan_0.0.15".olap_inspection_deletion()
CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".olap_inspection_deletion()
RETURNS TRIGGER AS $$
BEGIN
IF (TG_OP = 'DELETE') THEN
IF (OLD.id IS NOT NULL) THEN
DELETE FROM "fertiscan_0.0.15".inspection_factual
DELETE FROM "fertiscan_0.0.16".inspection_factual
WHERE inspection_id = OLD.id;
ELSE
-- Raise a warning if the condition is not met
Expand All @@ -85,8 +85,8 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS inspection_deletion ON "fertiscan_0.0.15".inspection;
DROP TRIGGER IF EXISTS inspection_deletion ON "fertiscan_0.0.16".inspection;
CREATE TRIGGER inspection_deletion
AFTER DELETE ON "fertiscan_0.0.15".inspection
AFTER DELETE ON "fertiscan_0.0.16".inspection
FOR EACH ROW
EXECUTE FUNCTION olap_inspection_deletion();
24 changes: 12 additions & 12 deletions fertiscan/db/bytebase/OLAP/label_information_triggers.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

CREATE OR REPLACE FUNCTION "fertiscan_0.0.15".olap_label_information_creation()
CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".olap_label_information_creation()
RETURNS TRIGGER AS $$
BEGIN
IF (TG_OP = 'INSERT') THEN
IF (NEW.id IS NOT NULL) THEN
INSERT INTO "fertiscan_0.0.15"."label_dimension" (
INSERT INTO "fertiscan_0.0.16"."label_dimension" (
label_id, company_info_id, manufacturer_info_id
) VALUES (
NEW.id, NEW.company_info_id, NEW.manufacturer_info_id
Expand All @@ -18,19 +18,19 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS label_information_creation ON "fertiscan_0.0.15".label_information;
DROP TRIGGER IF EXISTS label_information_creation ON "fertiscan_0.0.16".label_information;
CREATE TRIGGER label_information_creation
AFTER INSERT ON "fertiscan_0.0.15".label_information
AFTER INSERT ON "fertiscan_0.0.16".label_information
FOR EACH ROW
EXECUTE FUNCTION olap_label_information_creation();

CREATE OR REPLACE FUNCTION "fertiscan_0.0.15".olap_label_information_update()
CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".olap_label_information_update()
RETURNS TRIGGER AS $$
BEGIN
IF (TG_OP = 'UPDATE') THEN
IF (NEW.id IS NOT NULL) THEN
IF (NEW.company_info_id !=OLD.company_info_id) OR (NEW.manufacturer_info_id != OLD.manufacturer_info_id) THEN
UPDATE "fertiscan_0.0.15"."label_dimension"
UPDATE "fertiscan_0.0.16"."label_dimension"
SET company_info_id = NEW.company_info_id, manufacturer_info_id = NEW.manufacturer_info_id
WHERE label_id = NEW.id;
END IF;
Expand All @@ -43,18 +43,18 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS label_information_update ON "fertiscan_0.0.15".label_information;
DROP TRIGGER IF EXISTS label_information_update ON "fertiscan_0.0.16".label_information;
CREATE TRIGGER label_information_update
BEFORE UPDATE ON "fertiscan_0.0.15".label_information
BEFORE UPDATE ON "fertiscan_0.0.16".label_information
FOR EACH ROW
EXECUTE FUNCTION olap_label_information_update();

CREATE OR REPLACE FUNCTION "fertiscan_0.0.15".olap_label_information_deletion()
CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".olap_label_information_deletion()
RETURNS TRIGGER AS $$
BEGIN
IF (TG_OP = 'DELETE') THEN
IF (OLD.id IS NOT NULL) THEN
DELETE FROM "fertiscan_0.0.15"."label_dimension"
DELETE FROM "fertiscan_0.0.16"."label_dimension"
WHERE label_id = OLD.id;
ELSE
-- Raise a warning if the condition is not met
Expand All @@ -65,8 +65,8 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS label_information_deletion ON "fertiscan_0.0.15".label_information;
DROP TRIGGER IF EXISTS label_information_deletion ON "fertiscan_0.0.16".label_information;
CREATE TRIGGER label_information_deletion
AFTER DELETE ON "fertiscan_0.0.15".label_information
AFTER DELETE ON "fertiscan_0.0.16".label_information
FOR EACH ROW
EXECUTE FUNCTION olap_label_information_deletion();
16 changes: 8 additions & 8 deletions fertiscan/db/bytebase/OLAP/metrics_triggers.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

CREATE OR REPLACE FUNCTION "fertiscan_0.0.15".olap_metrics_creation()
CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".olap_metrics_creation()
RETURNS TRIGGER AS $$
DECLARE
metric_type TEXT;
Expand All @@ -11,7 +11,7 @@ BEGIN
IF (metric_type ILIKE 'test%') THEN
RETURN NEW;
END IF;
EXECUTE format('UPDATE "fertiscan_0.0.15"."label_dimension"
EXECUTE format('UPDATE "fertiscan_0.0.16"."label_dimension"
SET %I = array_append(%I, %L)
WHERE label_dimension.label_id = %L',
metric_type, metric_type, NEW.id, NEW.label_id);
Expand All @@ -24,13 +24,13 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS metrics_creation ON "fertiscan_0.0.15".metric;
DROP TRIGGER IF EXISTS metrics_creation ON "fertiscan_0.0.16".metric;
CREATE TRIGGER metrics_creation
AFTER INSERT ON "fertiscan_0.0.15".metric
AFTER INSERT ON "fertiscan_0.0.16".metric
FOR EACH ROW
EXECUTE FUNCTION olap_metrics_creation();

CREATE OR REPLACE FUNCTION "fertiscan_0.0.15".olap_metrics_deletion()
CREATE OR REPLACE FUNCTION "fertiscan_0.0.16".olap_metrics_deletion()
RETURNS TRIGGER AS $$
DECLARE
metric_type TEXT;
Expand All @@ -42,7 +42,7 @@ BEGIN
IF (metric_type ILIKE 'test%') THEN
RETURN OLD;
END IF;
EXECUTE format('UPDATE "fertiscan_0.0.15"."label_dimension"
EXECUTE format('UPDATE "fertiscan_0.0.16"."label_dimension"
SET %I = array_remove(%I, %L)
WHERE label_dimension.label_id = %L',
metric_type, metric_type, OLD.id, OLD.label_id);
Expand All @@ -55,8 +55,8 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS metrics_deletion ON "fertiscan_0.0.15".metric;
DROP TRIGGER IF EXISTS metrics_deletion ON "fertiscan_0.0.16".metric;
CREATE TRIGGER metrics_deletion
AFTER DELETE ON "fertiscan_0.0.15".metric
AFTER DELETE ON "fertiscan_0.0.16".metric
FOR EACH ROW
EXECUTE FUNCTION olap_metrics_deletion();
Loading
Loading